forked from phoreproject/bls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bls.go
311 lines (258 loc) · 7.71 KB
/
bls.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package g1pubs
import (
"bytes"
"io"
"log"
"sort"
"github.com/phoreproject/bls"
)
// Signature is a message signature.
type Signature struct {
s *bls.G2Projective
}
// Serialize serializes a signature in compressed form.
func (s *Signature) Serialize() [96]byte {
return bls.CompressG2(s.s.ToAffine())
}
func (s *Signature) String() string {
return s.s.String()
}
// GetPoint gets the G2 point associated with the signature.
func (s *Signature) GetPoint() *bls.G2Projective {
return s.s.Copy()
}
// NewSignatureFromG2 creates a new signature from a G2
// element.
func NewSignatureFromG2(g2 *bls.G2Affine) *Signature {
return &Signature{g2.ToProjective()}
}
// DeserializeSignature deserializes a signature from bytes.
func DeserializeSignature(b [96]byte) (*Signature, error) {
a, err := bls.DecompressG2(b)
if err != nil {
return nil, err
}
return &Signature{s: a.ToProjective()}, nil
}
// Copy returns a copy of the signature.
func (s *Signature) Copy() *Signature {
return &Signature{s.s.Copy()}
}
// PublicKey is a public key.
type PublicKey struct {
p *bls.G1Projective
}
func (p PublicKey) String() string {
return p.p.String()
}
// GetPoint gets the G1 point associated with the public key.
func (p *PublicKey) GetPoint() *bls.G1Projective {
return p.p.Copy()
}
// Serialize serializes a public key to bytes.
func (p PublicKey) Serialize() [48]byte {
return bls.CompressG1(p.p.ToAffine())
}
// NewPublicKeyFromG1 creates a new public key from a G1 element.
func NewPublicKeyFromG1(g1 *bls.G1Affine) *PublicKey {
return &PublicKey{g1.ToProjective()}
}
func concatAppend(slices [][]byte) []byte {
var tmp []byte
for _, s := range slices {
tmp = append(tmp, s...)
}
return tmp
}
// Equals checks if two public keys are equal
func (p PublicKey) Equals(other PublicKey) bool {
return p.p.Equal(other.p)
}
// DeserializePublicKey deserializes a public key from
// bytes.
func DeserializePublicKey(b [48]byte) (*PublicKey, error) {
a, err := bls.DecompressG1(b)
if err != nil {
return nil, err
}
return &PublicKey{p: a.ToProjective()}, nil
}
// SecretKey represents a BLS private key.
type SecretKey struct {
f *bls.FR
}
// GetFRElement gets the underlying FR element.
func (s SecretKey) GetFRElement() *bls.FR {
return s.f
}
func (s SecretKey) String() string {
return s.f.String()
}
// Serialize serializes a secret key to bytes.
func (s SecretKey) Serialize() [32]byte {
return s.f.Bytes()
}
// DeserializeSecretKey deserializes a secret key from
// bytes.
func DeserializeSecretKey(b [32]byte) *SecretKey {
return &SecretKey{bls.FRReprToFR(bls.FRReprFromBytes(b))}
}
// DeriveSecretKey derives a secret key from
// bytes.
func DeriveSecretKey(b [32]byte) *SecretKey {
return &SecretKey{bls.HashSecretKey(b)}
}
// Sign signs a message with a secret key.
func Sign(message []byte, key *SecretKey) *Signature {
h := bls.HashG2(message).MulFR(key.f.ToRepr())
return &Signature{s: h}
}
// SignWithDomain signs a message with a secret key and its domain.
func SignWithDomain(message [32]byte, key *SecretKey, domain [8]byte) *Signature {
h := bls.HashG2WithDomain(message, domain).MulFR(key.f.ToRepr())
return &Signature{s: h}
}
// PrivToPub converts the private key into a public key.
func PrivToPub(k *SecretKey) *PublicKey {
return &PublicKey{p: bls.G1AffineOne.MulFR(k.f.ToRepr())}
}
// RandKey generates a random secret key.
func RandKey(r io.Reader) (*SecretKey, error) {
k, err := bls.RandFR(r)
if err != nil {
return nil, err
}
s := &SecretKey{f: k}
return s, nil
}
// KeyFromFQRepr returns a new key based on a FQRepr in
// FR.
func KeyFromFQRepr(i *bls.FRRepr) *SecretKey {
return &SecretKey{f: bls.FRReprToFR(i)}
}
// Verify verifies a signature against a message and a public key.
func Verify(m []byte, pub *PublicKey, sig *Signature) bool {
h := bls.HashG2(m)
return bls.CompareTwoPairings(bls.G1ProjectiveOne, sig.s, pub.p, h.ToProjective())
}
// VerifyWithDomain verifies a signature against a message and a public key and a domain
func VerifyWithDomain(m [32]byte, pub *PublicKey, sig *Signature, domain [8]byte) bool {
h := bls.HashG2WithDomain(m, domain)
return bls.CompareTwoPairings(bls.G1ProjectiveOne, sig.s, pub.p, h.ToAffine().ToProjective())
}
// AggregateSignatures adds up all of the signatures.
func AggregateSignatures(s []*Signature) *Signature {
newSig := &Signature{s: bls.G2ProjectiveZero.Copy()}
for _, sig := range s {
newSig.Aggregate(sig)
}
return newSig
}
// Aggregate adds one signature to another
func (s *Signature) Aggregate(other *Signature) {
newS := s.s.Add(other.s)
s.s = newS
}
// AggregatePublicKeys adds public keys together.
func AggregatePublicKeys(p []*PublicKey) *PublicKey {
newPub := &PublicKey{p: bls.G1ProjectiveZero.Copy()}
for _, pub := range p {
newPub.Aggregate(pub)
}
return newPub
}
// Aggregate adds two public keys together.
func (p *PublicKey) Aggregate(other *PublicKey) {
newP := p.p.Add(other.p)
p.p = newP
}
// Copy copies the public key and returns it.
func (p *PublicKey) Copy() *PublicKey {
return &PublicKey{p: p.p.Copy()}
}
// NewAggregateSignature creates a blank aggregate signature.
func NewAggregateSignature() *Signature {
return &Signature{s: bls.G2ProjectiveZero.Copy()}
}
// NewAggregatePubkey creates a blank public key.
func NewAggregatePubkey() *PublicKey {
return &PublicKey{p: bls.G1ProjectiveZero.Copy()}
}
// implement `Interface` in sort package.
type sortableByteArray [][]byte
func (b sortableByteArray) Len() int {
return len(b)
}
func (b sortableByteArray) Less(i, j int) bool {
// bytes package already implements Comparable for []byte.
switch bytes.Compare(b[i], b[j]) {
case -1:
return true
case 0, 1:
return false
default:
log.Panic("not fail-able with `bytes.Comparable` bounded [-1, 1].")
return false
}
}
func (b sortableByteArray) Swap(i, j int) {
b[j], b[i] = b[i], b[j]
}
func sortByteArrays(src [][]byte) [][]byte {
sorted := sortableByteArray(src)
sort.Sort(sorted)
return sorted
}
// VerifyAggregate verifies each public key against each message.
func (s *Signature) VerifyAggregate(pubKeys []*PublicKey, msgs [][]byte) bool {
if len(pubKeys) != len(msgs) {
return false
}
msgsCopy := make([][]byte, len(msgs))
for i, m := range msgs {
msgsCopy[i] = make([]byte, len(m))
copy(msgsCopy[i], m)
}
msgsSorted := sortByteArrays(msgsCopy)
lastMsg := []byte(nil)
// check for duplicates
for _, m := range msgsSorted {
if bytes.Equal(m, lastMsg) {
return false
}
lastMsg = m
}
lhs := bls.Pairing(bls.G1ProjectiveOne, s.s)
rhs := bls.FQ12One.Copy()
for i := range pubKeys {
h := bls.HashG2(msgs[i])
rhs.MulAssign(bls.Pairing(pubKeys[i].p, h.ToProjective()))
}
return lhs.Equals(rhs)
}
// VerifyAggregateCommon verifies each public key against a message.
// This is vulnerable to rogue public-key attack. Each user must
// provide a proof-of-knowledge of the public key.
func (s *Signature) VerifyAggregateCommon(pubKeys []*PublicKey, msg []byte) bool {
aggPub := AggregatePublicKeys(pubKeys)
return Verify(msg, aggPub, s)
}
// VerifyAggregateCommonWithDomain verifies each public key against a message and
// its domain.
func (s *Signature) VerifyAggregateCommonWithDomain(pubKeys []*PublicKey, msg [32]byte, domain [8]byte) bool {
aggPub := AggregatePublicKeys(pubKeys)
return VerifyWithDomain(msg, aggPub, s, domain)
}
// VerifyAggregateWithDomain verifies each public key against each message and its domain.
func (s *Signature) VerifyAggregateWithDomain(pubKeys []*PublicKey, msgs [][32]byte, domain [8]byte) bool {
if len(pubKeys) != len(msgs) {
return false
}
lhs := bls.Pairing(bls.G1ProjectiveOne, s.s)
rhs := bls.FQ12One.Copy()
for i := range pubKeys {
h := bls.HashG2WithDomain(msgs[i], domain)
rhs.MulAssign(bls.Pairing(pubKeys[i].p, h.ToAffine().ToProjective()))
}
return lhs.Equals(rhs)
}