-
Notifications
You must be signed in to change notification settings - Fork 144
/
bls.go
420 lines (368 loc) · 9.91 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Package bls provides BLS signatures using the BLS12-381 pairing curve.
//
// This packages implements the IETF/CFRG draft for BLS signatures [1].
// Currently only the BASIC mode (one of the three modes specified
// in the draft) is supported. The pairing function is instantiated
// with the BLS12-381 curve.
//
// # Groups
//
// The BLS signature scheme can be instantiated with keys in one of the
// two groups: G1 or G2, which correspond to the input domain of a pairing
// function e(G1,G2) -> Gt.
// Thus, choosing keys in G1 implies that signature values are internally
// represented in G2; or viceversa. Use the types KeyG1SigG2 or KeyG2SigG1
// to express this preference.
//
// # Serialization
//
// The serialization of elements in G1 and G2 follows the recommendation
// given in [2], in order to be compatible with other implementations of
// BLS12-381 curve.
//
// # References
//
// [1] https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-05
//
// [2] https://github.com/zkcrypto/bls12_381/blob/0.7.0/src/notes/serialization.rs
package bls
import (
"crypto"
"crypto/sha256"
"encoding/binary"
"errors"
"io"
GG "github.com/cloudflare/circl/ecc/bls12381"
"golang.org/x/crypto/hkdf"
)
var (
ErrInvalid = errors.New("bls: invalid BLS instance")
ErrInvalidKey = errors.New("bls: invalid key")
ErrKeyGen = errors.New("bls: too many unsuccessful key generation tries")
ErrShortIKM = errors.New("bls: IKM material shorter than 32 bytes")
ErrAggregate = errors.New("bls: error while aggregating signatures")
)
const (
dstG1 = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_"
dstG2 = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_"
)
type Signature = []byte
type (
// G1 group used for keys defined in pairing group G1.
G1 struct{ g GG.G1 }
// G2 group used for keys defined in pairing group G2.
G2 struct{ g GG.G2 }
// KeyG1SigG2 sets the keys to G1 and signatures to G2.
KeyG1SigG2 = G1
// KeyG2SigG1 sets the keys to G2 and signatures to G1.
KeyG2SigG1 = G2
)
func (f *G1) setBytes(b []byte) error { return f.g.SetBytes(b) }
func (f *G2) setBytes(b []byte) error { return f.g.SetBytes(b) }
func (f *G1) hash(msg []byte) { f.g.Hash(msg, []byte(dstG1)) }
func (f *G2) hash(msg []byte) { f.g.Hash(msg, []byte(dstG2)) }
// KeyGroup determines the group used for keys, while the other
// group is used for signatures.
type KeyGroup interface{ G1 | G2 }
type PrivateKey[K KeyGroup] struct {
key GG.Scalar
pub *PublicKey[K]
}
type PublicKey[K KeyGroup] struct{ key K }
func (k *PrivateKey[K]) Public() crypto.PublicKey { return k.PublicKey() }
// PublicKey computes the corresponding public key. The key is cached
// for further invocations to this function.
func (k *PrivateKey[K]) PublicKey() *PublicKey[K] {
if k.pub == nil {
k.pub = new(PublicKey[K])
switch any(k).(type) {
case *PrivateKey[G1]:
kk := any(&k.pub.key).(*G1)
kk.g.ScalarMult(&k.key, GG.G1Generator())
case *PrivateKey[G2]:
kk := any(&k.pub.key).(*G2)
kk.g.ScalarMult(&k.key, GG.G2Generator())
default:
panic(ErrInvalid)
}
}
return k.pub
}
func (k *PrivateKey[K]) Equal(x crypto.PrivateKey) bool {
xx, ok := x.(*PrivateKey[K])
if !ok {
return false
}
switch any(k).(type) {
case *PrivateKey[G1], *PrivateKey[G2]:
return k.key.IsEqual(&xx.key) == 1
default:
panic(ErrInvalid)
}
}
// Validate explicitly determines if a private key is valid.
func (k *PrivateKey[K]) Validate() bool {
switch any(k).(type) {
case *PrivateKey[G1], *PrivateKey[G2]:
return k.key.IsZero() == 0
default:
panic(ErrInvalid)
}
}
// MarshalBinary returns a slice with the representation of
// the underlying PrivateKey scalar (in big-endian order).
func (k *PrivateKey[K]) MarshalBinary() ([]byte, error) {
switch any(k).(type) {
case *PrivateKey[G1], *PrivateKey[G2]:
return k.key.MarshalBinary()
default:
panic(ErrInvalid)
}
}
func (k *PrivateKey[K]) UnmarshalBinary(data []byte) error {
switch any(k).(type) {
case *PrivateKey[G1], *PrivateKey[G2]:
if err := k.key.UnmarshalBinary(data); err != nil {
return err
}
if !k.Validate() {
return ErrInvalidKey
}
k.pub = nil
return nil
default:
panic(ErrInvalid)
}
}
// Validate explicitly determines if a public key is valid.
func (k *PublicKey[K]) Validate() bool {
switch any(k).(type) {
case *PublicKey[G1]:
kk := any(k.key).(G1)
return !kk.g.IsIdentity() && kk.g.IsOnG1()
case *PublicKey[G2]:
kk := any(k.key).(G2)
return !kk.g.IsIdentity() && kk.g.IsOnG2()
default:
panic(ErrInvalid)
}
}
func (k *PublicKey[K]) Equal(x crypto.PublicKey) bool {
xx, ok := x.(*PublicKey[K])
if !ok {
return false
}
switch any(k).(type) {
case *PublicKey[G1]:
xxx := any(xx.key).(G1)
kk := any(k.key).(G1)
return kk.g.IsEqual(&xxx.g)
case *PublicKey[G2]:
xxx := any(xx.key).(G2)
kk := any(k.key).(G2)
return kk.g.IsEqual(&xxx.g)
default:
panic(ErrInvalid)
}
}
// MarshalBinary returns a slice with the compressed
// representation of the underlying element in G1 or G2.
func (k *PublicKey[K]) MarshalBinary() ([]byte, error) {
switch any(k).(type) {
case *PublicKey[G1]:
kk := any(k.key).(G1)
return kk.g.BytesCompressed(), nil
case *PublicKey[G2]:
kk := any(k.key).(G2)
return kk.g.BytesCompressed(), nil
default:
panic(ErrInvalid)
}
}
func (k *PublicKey[K]) UnmarshalBinary(data []byte) error {
switch any(k).(type) {
case *PublicKey[G1]:
kk := any(&k.key).(*G1)
return kk.setBytes(data)
case *PublicKey[G2]:
kk := any(&k.key).(*G2)
return kk.setBytes(data)
default:
panic(ErrInvalid)
}
}
// KeyGen derives a private key for the specified group (G1 or G2).
// The length of ikm material should be at least 32 bytes length.
// The salt value should be either empty or a uniformly random
// bytes whose length equals the output length of SHA-256.
func KeyGen[K KeyGroup](ikm, salt, keyInfo []byte) (*PrivateKey[K], error) {
// Implements recommended method at:
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-05#name-keygen
if len(ikm) < 32 {
return nil, ErrShortIKM
}
ikmZero := make([]byte, len(ikm)+1)
keyInfoTwo := make([]byte, len(keyInfo)+2)
copy(ikmZero, ikm)
copy(keyInfoTwo, keyInfo)
const L = uint16(48)
binary.BigEndian.PutUint16(keyInfoTwo[len(keyInfo):], L)
OKM := make([]byte, L)
var ss GG.Scalar
for tries := 8; tries > 0; tries-- {
rd := hkdf.New(sha256.New, ikmZero, salt, keyInfoTwo)
n, err := io.ReadFull(rd, OKM)
if n != len(OKM) || err != nil {
return nil, err
}
ss.SetBytes(OKM)
if ss.IsZero() == 1 {
digest := sha256.Sum256(salt)
salt = digest[:]
} else {
return &PrivateKey[K]{key: ss, pub: nil}, nil
}
}
return nil, ErrKeyGen
}
// Sign computes a signature of a message using a key (defined in
// G1 or G1).
func Sign[K KeyGroup](k *PrivateKey[K], msg []byte) Signature {
if !k.Validate() {
panic(ErrInvalidKey)
}
switch any(k).(type) {
case *PrivateKey[G1]:
var Q GG.G2
Q.Hash(msg, []byte(dstG2))
Q.ScalarMult(&k.key, &Q)
return Q.BytesCompressed()
case *PrivateKey[G2]:
var Q GG.G1
Q.Hash(msg, []byte(dstG1))
Q.ScalarMult(&k.key, &Q)
return Q.BytesCompressed()
default:
panic(ErrInvalid)
}
}
// Verify returns true if the signature of a message is valid for the
// corresponding public key.
func Verify[K KeyGroup](pub *PublicKey[K], msg []byte, sig Signature) bool {
var (
a, b interface {
setBytes([]byte) error
hash([]byte)
}
listG1 [2]*GG.G1
listG2 [2]*GG.G2
)
switch any(pub).(type) {
case *PublicKey[G1]:
aa, bb := new(G2), new(G2)
a, b = aa, bb
k := any(pub.key).(G1)
listG1[0], listG1[1] = &k.g, GG.G1Generator()
listG2[0], listG2[1] = &aa.g, &bb.g
case *PublicKey[G2]:
aa, bb := new(G1), new(G1)
a, b = aa, bb
k := any(pub.key).(G2)
listG2[0], listG2[1] = &k.g, GG.G2Generator()
listG1[0], listG1[1] = &aa.g, &bb.g
default:
panic(ErrInvalid)
}
err := b.setBytes(sig)
if err != nil {
return false
}
if !pub.Validate() {
return false
}
a.hash(msg)
res := GG.ProdPairFrac(listG1[:], listG2[:], []int{1, -1})
return res.IsIdentity()
}
// Aggregate produces a unified signature given a list of signatures.
// To specify the group of keys pass either G1{} or G2{} as the first
// parameter.
func Aggregate[K KeyGroup](k K, sigs []Signature) (Signature, error) {
if len(sigs) == 0 {
return nil, ErrAggregate
}
switch any(k).(type) {
case G1:
var P, Q GG.G2
P.SetIdentity()
for _, sig := range sigs {
if err := Q.SetBytes(sig); err != nil {
return nil, err
}
P.Add(&P, &Q)
}
return P.BytesCompressed(), nil
case G2:
var P, Q GG.G1
P.SetIdentity()
for _, sig := range sigs {
if err := Q.SetBytes(sig); err != nil {
return nil, err
}
P.Add(&P, &Q)
}
return P.BytesCompressed(), nil
default:
panic(ErrInvalid)
}
}
// VerifyAggregate returns true if the aggregated signature is valid for
// the list of messages and public keys provided. The slices must have
// equal size and have at least one element.
func VerifyAggregate[K KeyGroup](pubs []*PublicKey[K], msgs [][]byte, aggSig Signature) bool {
if len(pubs) != len(msgs) || len(pubs) == 0 {
return false
}
for _, p := range pubs {
if !p.Validate() {
return false
}
}
n := len(pubs)
listG1 := make([]*GG.G1, n+1)
listG2 := make([]*GG.G2, n+1)
listSigns := make([]int, n+1)
listG1[n] = GG.G1Generator()
listG2[n] = GG.G2Generator()
listSigns[n] = -1
switch any(pubs).(type) {
case []*PublicKey[G1]:
for i := range msgs {
listG2[i] = new(GG.G2)
listG2[i].Hash(msgs[i], []byte(dstG2))
xP := any(pubs[i].key).(G1)
listG1[i] = &xP.g
listSigns[i] = 1
}
err := listG2[n].SetBytes(aggSig)
if err != nil {
return false
}
case []*PublicKey[G2]:
for i := range msgs {
listG1[i] = new(GG.G1)
listG1[i].Hash(msgs[i], []byte(dstG1))
xP := any(pubs[i].key).(G2)
listG2[i] = &xP.g
listSigns[i] = 1
}
err := listG1[n].SetBytes(aggSig)
if err != nil {
return false
}
default:
panic(ErrInvalid)
}
C := GG.ProdPairFrac(listG1, listG2, listSigns)
return C.IsIdentity()
}