-
Notifications
You must be signed in to change notification settings - Fork 0
/
musig.go
137 lines (127 loc) · 3.35 KB
/
musig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package musig
import (
"crypto/rand"
"crypto/sha256"
"math/big"
"github.com/btcsuite/btcd/btcec"
)
// KeyGen returns a private/public key pair.
func KeyGen() (*btcec.PrivateKey, *btcec.PublicKey) {
x, _ := rand.Int(rand.Reader, btcec.S256().N)
return btcec.PrivKeyFromBytes(btcec.S256(), x.Bytes())
}
// Sign returns a signature.
// L = {X1, ... , Xn} is the multiset of all public keys.
// R is sum all random points. R = R1 + ... + Rn
// m is message.
// x is private key. Xi = xG, X is public key.
// r is random value. Ri = rG.
func Sign(L []*btcec.PublicKey, R *btcec.PublicKey, m []byte, x, r *btcec.PrivateKey) *big.Int {
var a *big.Int
Xt := new(btcec.PublicKey)
// X~ = a_0*X_0 + ... + a_n*X_n
for i, Xi := range L {
// a_i = H_agg(L,X_i)
ai := Hagg(L, Xi)
if i == 0 {
Xt.X, Xt.Y = btcec.S256().ScalarMult(Xi.X, Xi.Y, ai.Bytes())
} else {
Xix, Xiy := btcec.S256().ScalarMult(Xi.X, Xi.Y, ai.Bytes())
Xt.X, Xt.Y = btcec.S256().Add(Xt.X, Xt.Y, Xix, Xiy)
}
if a == nil && Xi.IsEqual(x.PubKey()) {
a = ai
}
}
// c = H_sig(H~,R,m)
c := Hsig(Xt, R, m)
// s_i = r_i + c*a_i*x_i mod p
s := new(big.Int).Mod(new(big.Int).Add(r.D, new(big.Int).Mul(new(big.Int).Mul(c, a), x.D)), btcec.S256().N)
return s
}
// Ver returns 1 if the signature is valid and 0 otherwise.
// L = {X1, ... , Xn} is the multiset of all public keys.
// m is message.
// σ = (R,s)
// R is sum all random points. R = R1 + ... + Rn
// s is signature.
func Ver(L []*btcec.PublicKey, m []byte, R *btcec.PublicKey, s *big.Int) int {
Xt := new(btcec.PublicKey)
// X~ = a_0*X_0 + ... + a_n*X_n
for i, Xi := range L {
// a_i = H_agg(L,X_i)
ai := Hagg(L, Xi)
if i == 0 {
Xt.X, Xt.Y = btcec.S256().ScalarMult(Xi.X, Xi.Y, ai.Bytes())
} else {
Xix, Xiy := btcec.S256().ScalarMult(Xi.X, Xi.Y, ai.Bytes())
Xt.X, Xt.Y = btcec.S256().Add(Xt.X, Xt.Y, Xix, Xiy)
}
}
// c = H_sig(H~,R,m)
c := Hsig(Xt, R, m)
cXt := new(btcec.PublicKey)
// cX~ = c * X~
cXt.X, cXt.Y = btcec.S256().ScalarMult(Xt.X, Xt.Y, c.Bytes())
RXc := new(btcec.PublicKey)
// R + cX~
RXc.X, RXc.Y = btcec.S256().Add(R.X, R.Y, cXt.X, cXt.Y)
// sG = s*G
sG := new(btcec.PublicKey)
sG.X, sG.Y = btcec.S256().ScalarBaseMult(s.Bytes())
// sG = R + cX~
if sG.IsEqual(RXc) {
return 1
}
return 0
}
// Hagg returns hash value.
func Hagg(L []*btcec.PublicKey, R *btcec.PublicKey) *big.Int {
s := sha256.New()
for _, Xi := range L {
s.Write(Xi.SerializeCompressed())
}
s.Write(R.SerializeCompressed())
hash := s.Sum(nil)
h := big.NewInt(0)
h.SetBytes(hash)
return h
}
// Hsig returns hash value.
func Hsig(X, R *btcec.PublicKey, m []byte) *big.Int {
s := sha256.New()
s.Write(X.SerializeCompressed())
s.Write(R.SerializeCompressed())
s.Write(m)
hash := s.Sum(nil)
h := big.NewInt(0)
h.SetBytes(hash)
return h
}
// Hcom returns hash value.
func Hcom(R *btcec.PublicKey) []byte {
s := sha256.New()
s.Write(R.SerializeCompressed())
hash := s.Sum(nil)
return hash
}
// AddPubs returns sum public key.
func AddPubs(pubs ...*btcec.PublicKey) *btcec.PublicKey {
P := new(btcec.PublicKey)
for i, pub := range pubs {
if i == 0 {
P.X, P.Y = pub.X, pub.Y
} else {
P.X, P.Y = btcec.S256().Add(P.X, P.Y, pub.X, pub.Y)
}
}
return P
}
// AddSigs returns sum signature.
func AddSigs(sigs ...*big.Int) *big.Int {
S := big.NewInt(0)
for _, sig := range sigs {
S = new(big.Int).Add(S, sig)
}
return S
}