22
22
//
23
23
// # References
24
24
//
25
- // [1] https://datatracker.ietf.org/doc/draft-irtf-cfrg-bls-signature/
25
+ // [1] https://datatracker.ietf.org/doc/html/ draft-irtf-cfrg-bls-signature-05
26
26
//
27
27
// [2] https://github.com/zkcrypto/bls12_381/blob/0.7.0/src/notes/serialization.rs
28
28
package bls
@@ -88,12 +88,12 @@ func (k *PrivateKey[K]) Public() crypto.PublicKey { return k.PublicKey() }
88
88
func (k * PrivateKey [K ]) PublicKey () * PublicKey [K ] {
89
89
if k .pub == nil {
90
90
k .pub = new (PublicKey [K ])
91
- switch ( interface {}) (k ).(type ) {
91
+ switch any (k ).(type ) {
92
92
case * PrivateKey [G1 ]:
93
- kk := ( interface {}) (& k .pub .key ).(* G1 )
93
+ kk := any (& k .pub .key ).(* G1 )
94
94
kk .g .ScalarMult (& k .key , GG .G1Generator ())
95
95
case * PrivateKey [G2 ]:
96
- kk := ( interface {}) (& k .pub .key ).(* G2 )
96
+ kk := any (& k .pub .key ).(* G2 )
97
97
kk .g .ScalarMult (& k .key , GG .G2Generator ())
98
98
default :
99
99
panic (ErrInvalid )
@@ -109,7 +109,7 @@ func (k *PrivateKey[K]) Equal(x crypto.PrivateKey) bool {
109
109
return false
110
110
}
111
111
112
- switch ( interface {}) (k ).(type ) {
112
+ switch any (k ).(type ) {
113
113
case * PrivateKey [G1 ], * PrivateKey [G2 ]:
114
114
return k .key .IsEqual (& xx .key ) == 1
115
115
default :
@@ -119,7 +119,7 @@ func (k *PrivateKey[K]) Equal(x crypto.PrivateKey) bool {
119
119
120
120
// Validate explicitly determines if a private key is valid.
121
121
func (k * PrivateKey [K ]) Validate () bool {
122
- switch ( interface {}) (k ).(type ) {
122
+ switch any (k ).(type ) {
123
123
case * PrivateKey [G1 ], * PrivateKey [G2 ]:
124
124
return k .key .IsZero () == 0
125
125
default :
@@ -130,7 +130,7 @@ func (k *PrivateKey[K]) Validate() bool {
130
130
// MarshalBinary returns a slice with the representation of
131
131
// the underlying PrivateKey scalar (in big-endian order).
132
132
func (k * PrivateKey [K ]) MarshalBinary () ([]byte , error ) {
133
- switch ( interface {}) (k ).(type ) {
133
+ switch any (k ).(type ) {
134
134
case * PrivateKey [G1 ], * PrivateKey [G2 ]:
135
135
return k .key .MarshalBinary ()
136
136
default :
@@ -139,7 +139,7 @@ func (k *PrivateKey[K]) MarshalBinary() ([]byte, error) {
139
139
}
140
140
141
141
func (k * PrivateKey [K ]) UnmarshalBinary (data []byte ) error {
142
- switch ( interface {}) (k ).(type ) {
142
+ switch any (k ).(type ) {
143
143
case * PrivateKey [G1 ], * PrivateKey [G2 ]:
144
144
if err := k .key .UnmarshalBinary (data ); err != nil {
145
145
return err
@@ -156,12 +156,12 @@ func (k *PrivateKey[K]) UnmarshalBinary(data []byte) error {
156
156
157
157
// Validate explicitly determines if a public key is valid.
158
158
func (k * PublicKey [K ]) Validate () bool {
159
- switch ( interface {}) (k ).(type ) {
159
+ switch any (k ).(type ) {
160
160
case * PublicKey [G1 ]:
161
- kk := ( interface {}) (k .key ).(G1 )
161
+ kk := any (k .key ).(G1 )
162
162
return ! kk .g .IsIdentity () && kk .g .IsOnG1 ()
163
163
case * PublicKey [G2 ]:
164
- kk := ( interface {}) (k .key ).(G2 )
164
+ kk := any (k .key ).(G2 )
165
165
return ! kk .g .IsIdentity () && kk .g .IsOnG2 ()
166
166
default :
167
167
panic (ErrInvalid )
@@ -174,14 +174,14 @@ func (k *PublicKey[K]) Equal(x crypto.PublicKey) bool {
174
174
return false
175
175
}
176
176
177
- switch ( interface {}) (k ).(type ) {
177
+ switch any (k ).(type ) {
178
178
case * PublicKey [G1 ]:
179
- xxx := ( interface {}) (xx .key ).(G1 )
180
- kk := ( interface {}) (k .key ).(G1 )
179
+ xxx := any (xx .key ).(G1 )
180
+ kk := any (k .key ).(G1 )
181
181
return kk .g .IsEqual (& xxx .g )
182
182
case * PublicKey [G2 ]:
183
- xxx := ( interface {}) (xx .key ).(G2 )
184
- kk := ( interface {}) (k .key ).(G2 )
183
+ xxx := any (xx .key ).(G2 )
184
+ kk := any (k .key ).(G2 )
185
185
return kk .g .IsEqual (& xxx .g )
186
186
default :
187
187
panic (ErrInvalid )
@@ -191,25 +191,25 @@ func (k *PublicKey[K]) Equal(x crypto.PublicKey) bool {
191
191
// MarshalBinary returns a slice with the compressed
192
192
// representation of the underlying element in G1 or G2.
193
193
func (k * PublicKey [K ]) MarshalBinary () ([]byte , error ) {
194
- switch ( interface {}) (k ).(type ) {
194
+ switch any (k ).(type ) {
195
195
case * PublicKey [G1 ]:
196
- kk := ( interface {}) (k .key ).(G1 )
196
+ kk := any (k .key ).(G1 )
197
197
return kk .g .BytesCompressed (), nil
198
198
case * PublicKey [G2 ]:
199
- kk := ( interface {}) (k .key ).(G2 )
199
+ kk := any (k .key ).(G2 )
200
200
return kk .g .BytesCompressed (), nil
201
201
default :
202
202
panic (ErrInvalid )
203
203
}
204
204
}
205
205
206
206
func (k * PublicKey [K ]) UnmarshalBinary (data []byte ) error {
207
- switch ( interface {}) (k ).(type ) {
207
+ switch any (k ).(type ) {
208
208
case * PublicKey [G1 ]:
209
- kk := ( interface {}) (& k .key ).(* G1 )
209
+ kk := any (& k .key ).(* G1 )
210
210
return kk .setBytes (data )
211
211
case * PublicKey [G2 ]:
212
- kk := ( interface {}) (& k .key ).(* G2 )
212
+ kk := any (& k .key ).(* G2 )
213
213
return kk .setBytes (data )
214
214
default :
215
215
panic (ErrInvalid )
@@ -263,7 +263,7 @@ func Sign[K KeyGroup](k *PrivateKey[K], msg []byte) Signature {
263
263
panic (ErrInvalidKey )
264
264
}
265
265
266
- switch ( interface {}) (k ).(type ) {
266
+ switch any (k ).(type ) {
267
267
case * PrivateKey [G1 ]:
268
268
var Q GG.G2
269
269
Q .Hash (msg , []byte (dstG2 ))
@@ -291,17 +291,17 @@ func Verify[K KeyGroup](pub *PublicKey[K], msg []byte, sig Signature) bool {
291
291
listG2 [2 ]* GG.G2
292
292
)
293
293
294
- switch ( interface {}) (pub ).(type ) {
294
+ switch any (pub ).(type ) {
295
295
case * PublicKey [G1 ]:
296
296
aa , bb := new (G2 ), new (G2 )
297
297
a , b = aa , bb
298
- k := ( interface {}) (pub .key ).(G1 )
298
+ k := any (pub .key ).(G1 )
299
299
listG1 [0 ], listG1 [1 ] = & k .g , GG .G1Generator ()
300
300
listG2 [0 ], listG2 [1 ] = & aa .g , & bb .g
301
301
case * PublicKey [G2 ]:
302
302
aa , bb := new (G1 ), new (G1 )
303
303
a , b = aa , bb
304
- k := ( interface {}) (pub .key ).(G2 )
304
+ k := any (pub .key ).(G2 )
305
305
listG2 [0 ], listG2 [1 ] = & k .g , GG .G2Generator ()
306
306
listG1 [0 ], listG1 [1 ] = & aa .g , & bb .g
307
307
default :
@@ -329,7 +329,7 @@ func Aggregate[K KeyGroup](k K, sigs []Signature) (Signature, error) {
329
329
return nil , ErrAggregate
330
330
}
331
331
332
- switch ( interface {}) (k ).(type ) {
332
+ switch any (k ).(type ) {
333
333
case G1 :
334
334
var P , Q GG.G2
335
335
P .SetIdentity ()
@@ -361,28 +361,34 @@ func Aggregate[K KeyGroup](k K, sigs []Signature) (Signature, error) {
361
361
// the list of messages and public keys provided. The slices must have
362
362
// equal size and have at least one element.
363
363
func VerifyAggregate [K KeyGroup ](pubs []* PublicKey [K ], msgs [][]byte , aggSig Signature ) bool {
364
- if len (pubs ) != len (msgs ) || len (pubs ) == 0 || len ( msgs ) == 0 {
364
+ if len (pubs ) != len (msgs ) || len (pubs ) == 0 {
365
365
return false
366
366
}
367
367
368
+ for _ , p := range pubs {
369
+ if ! p .Validate () {
370
+ return false
371
+ }
372
+ }
373
+
368
374
n := len (pubs )
369
375
listG1 := make ([]* GG.G1 , n + 1 )
370
376
listG2 := make ([]* GG.G2 , n + 1 )
371
- listExp := make ([]int , n + 1 )
377
+ listSigns := make ([]int , n + 1 )
372
378
373
379
listG1 [n ] = GG .G1Generator ()
374
380
listG2 [n ] = GG .G2Generator ()
375
- listExp [n ] = - 1
381
+ listSigns [n ] = - 1
376
382
377
- switch ( interface {}) (pubs ).(type ) {
383
+ switch any (pubs ).(type ) {
378
384
case []* PublicKey [G1 ]:
379
385
for i := range msgs {
380
386
listG2 [i ] = new (GG.G2 )
381
387
listG2 [i ].Hash (msgs [i ], []byte (dstG2 ))
382
388
383
- xP := ( interface {}) (pubs [i ].key ).(G1 )
389
+ xP := any (pubs [i ].key ).(G1 )
384
390
listG1 [i ] = & xP .g
385
- listExp [i ] = 1
391
+ listSigns [i ] = 1
386
392
}
387
393
388
394
err := listG2 [n ].SetBytes (aggSig )
@@ -395,9 +401,9 @@ func VerifyAggregate[K KeyGroup](pubs []*PublicKey[K], msgs [][]byte, aggSig Sig
395
401
listG1 [i ] = new (GG.G1 )
396
402
listG1 [i ].Hash (msgs [i ], []byte (dstG1 ))
397
403
398
- xP := ( interface {}) (pubs [i ].key ).(G2 )
404
+ xP := any (pubs [i ].key ).(G2 )
399
405
listG2 [i ] = & xP .g
400
- listExp [i ] = 1
406
+ listSigns [i ] = 1
401
407
}
402
408
403
409
err := listG1 [n ].SetBytes (aggSig )
@@ -409,6 +415,6 @@ func VerifyAggregate[K KeyGroup](pubs []*PublicKey[K], msgs [][]byte, aggSig Sig
409
415
panic (ErrInvalid )
410
416
}
411
417
412
- C := GG .ProdPairFrac (listG1 , listG2 , listExp )
418
+ C := GG .ProdPairFrac (listG1 , listG2 , listSigns )
413
419
return C .IsIdentity ()
414
420
}
0 commit comments