Skip to content

Commit 90deb27

Browse files
committed
Applying changes after Bas' review.
1 parent c8d23eb commit 90deb27

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

sign/bls/bls.go

+42-36
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//
2323
// # References
2424
//
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
2626
//
2727
// [2] https://github.com/zkcrypto/bls12_381/blob/0.7.0/src/notes/serialization.rs
2828
package bls
@@ -88,12 +88,12 @@ func (k *PrivateKey[K]) Public() crypto.PublicKey { return k.PublicKey() }
8888
func (k *PrivateKey[K]) PublicKey() *PublicKey[K] {
8989
if k.pub == nil {
9090
k.pub = new(PublicKey[K])
91-
switch (interface{})(k).(type) {
91+
switch any(k).(type) {
9292
case *PrivateKey[G1]:
93-
kk := (interface{})(&k.pub.key).(*G1)
93+
kk := any(&k.pub.key).(*G1)
9494
kk.g.ScalarMult(&k.key, GG.G1Generator())
9595
case *PrivateKey[G2]:
96-
kk := (interface{})(&k.pub.key).(*G2)
96+
kk := any(&k.pub.key).(*G2)
9797
kk.g.ScalarMult(&k.key, GG.G2Generator())
9898
default:
9999
panic(ErrInvalid)
@@ -109,7 +109,7 @@ func (k *PrivateKey[K]) Equal(x crypto.PrivateKey) bool {
109109
return false
110110
}
111111

112-
switch (interface{})(k).(type) {
112+
switch any(k).(type) {
113113
case *PrivateKey[G1], *PrivateKey[G2]:
114114
return k.key.IsEqual(&xx.key) == 1
115115
default:
@@ -119,7 +119,7 @@ func (k *PrivateKey[K]) Equal(x crypto.PrivateKey) bool {
119119

120120
// Validate explicitly determines if a private key is valid.
121121
func (k *PrivateKey[K]) Validate() bool {
122-
switch (interface{})(k).(type) {
122+
switch any(k).(type) {
123123
case *PrivateKey[G1], *PrivateKey[G2]:
124124
return k.key.IsZero() == 0
125125
default:
@@ -130,7 +130,7 @@ func (k *PrivateKey[K]) Validate() bool {
130130
// MarshalBinary returns a slice with the representation of
131131
// the underlying PrivateKey scalar (in big-endian order).
132132
func (k *PrivateKey[K]) MarshalBinary() ([]byte, error) {
133-
switch (interface{})(k).(type) {
133+
switch any(k).(type) {
134134
case *PrivateKey[G1], *PrivateKey[G2]:
135135
return k.key.MarshalBinary()
136136
default:
@@ -139,7 +139,7 @@ func (k *PrivateKey[K]) MarshalBinary() ([]byte, error) {
139139
}
140140

141141
func (k *PrivateKey[K]) UnmarshalBinary(data []byte) error {
142-
switch (interface{})(k).(type) {
142+
switch any(k).(type) {
143143
case *PrivateKey[G1], *PrivateKey[G2]:
144144
if err := k.key.UnmarshalBinary(data); err != nil {
145145
return err
@@ -156,12 +156,12 @@ func (k *PrivateKey[K]) UnmarshalBinary(data []byte) error {
156156

157157
// Validate explicitly determines if a public key is valid.
158158
func (k *PublicKey[K]) Validate() bool {
159-
switch (interface{})(k).(type) {
159+
switch any(k).(type) {
160160
case *PublicKey[G1]:
161-
kk := (interface{})(k.key).(G1)
161+
kk := any(k.key).(G1)
162162
return !kk.g.IsIdentity() && kk.g.IsOnG1()
163163
case *PublicKey[G2]:
164-
kk := (interface{})(k.key).(G2)
164+
kk := any(k.key).(G2)
165165
return !kk.g.IsIdentity() && kk.g.IsOnG2()
166166
default:
167167
panic(ErrInvalid)
@@ -174,14 +174,14 @@ func (k *PublicKey[K]) Equal(x crypto.PublicKey) bool {
174174
return false
175175
}
176176

177-
switch (interface{})(k).(type) {
177+
switch any(k).(type) {
178178
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)
181181
return kk.g.IsEqual(&xxx.g)
182182
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)
185185
return kk.g.IsEqual(&xxx.g)
186186
default:
187187
panic(ErrInvalid)
@@ -191,25 +191,25 @@ func (k *PublicKey[K]) Equal(x crypto.PublicKey) bool {
191191
// MarshalBinary returns a slice with the compressed
192192
// representation of the underlying element in G1 or G2.
193193
func (k *PublicKey[K]) MarshalBinary() ([]byte, error) {
194-
switch (interface{})(k).(type) {
194+
switch any(k).(type) {
195195
case *PublicKey[G1]:
196-
kk := (interface{})(k.key).(G1)
196+
kk := any(k.key).(G1)
197197
return kk.g.BytesCompressed(), nil
198198
case *PublicKey[G2]:
199-
kk := (interface{})(k.key).(G2)
199+
kk := any(k.key).(G2)
200200
return kk.g.BytesCompressed(), nil
201201
default:
202202
panic(ErrInvalid)
203203
}
204204
}
205205

206206
func (k *PublicKey[K]) UnmarshalBinary(data []byte) error {
207-
switch (interface{})(k).(type) {
207+
switch any(k).(type) {
208208
case *PublicKey[G1]:
209-
kk := (interface{})(&k.key).(*G1)
209+
kk := any(&k.key).(*G1)
210210
return kk.setBytes(data)
211211
case *PublicKey[G2]:
212-
kk := (interface{})(&k.key).(*G2)
212+
kk := any(&k.key).(*G2)
213213
return kk.setBytes(data)
214214
default:
215215
panic(ErrInvalid)
@@ -263,7 +263,7 @@ func Sign[K KeyGroup](k *PrivateKey[K], msg []byte) Signature {
263263
panic(ErrInvalidKey)
264264
}
265265

266-
switch (interface{})(k).(type) {
266+
switch any(k).(type) {
267267
case *PrivateKey[G1]:
268268
var Q GG.G2
269269
Q.Hash(msg, []byte(dstG2))
@@ -291,17 +291,17 @@ func Verify[K KeyGroup](pub *PublicKey[K], msg []byte, sig Signature) bool {
291291
listG2 [2]*GG.G2
292292
)
293293

294-
switch (interface{})(pub).(type) {
294+
switch any(pub).(type) {
295295
case *PublicKey[G1]:
296296
aa, bb := new(G2), new(G2)
297297
a, b = aa, bb
298-
k := (interface{})(pub.key).(G1)
298+
k := any(pub.key).(G1)
299299
listG1[0], listG1[1] = &k.g, GG.G1Generator()
300300
listG2[0], listG2[1] = &aa.g, &bb.g
301301
case *PublicKey[G2]:
302302
aa, bb := new(G1), new(G1)
303303
a, b = aa, bb
304-
k := (interface{})(pub.key).(G2)
304+
k := any(pub.key).(G2)
305305
listG2[0], listG2[1] = &k.g, GG.G2Generator()
306306
listG1[0], listG1[1] = &aa.g, &bb.g
307307
default:
@@ -329,7 +329,7 @@ func Aggregate[K KeyGroup](k K, sigs []Signature) (Signature, error) {
329329
return nil, ErrAggregate
330330
}
331331

332-
switch (interface{})(k).(type) {
332+
switch any(k).(type) {
333333
case G1:
334334
var P, Q GG.G2
335335
P.SetIdentity()
@@ -361,28 +361,34 @@ func Aggregate[K KeyGroup](k K, sigs []Signature) (Signature, error) {
361361
// the list of messages and public keys provided. The slices must have
362362
// equal size and have at least one element.
363363
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 {
365365
return false
366366
}
367367

368+
for _, p := range pubs {
369+
if !p.Validate() {
370+
return false
371+
}
372+
}
373+
368374
n := len(pubs)
369375
listG1 := make([]*GG.G1, n+1)
370376
listG2 := make([]*GG.G2, n+1)
371-
listExp := make([]int, n+1)
377+
listSigns := make([]int, n+1)
372378

373379
listG1[n] = GG.G1Generator()
374380
listG2[n] = GG.G2Generator()
375-
listExp[n] = -1
381+
listSigns[n] = -1
376382

377-
switch (interface{})(pubs).(type) {
383+
switch any(pubs).(type) {
378384
case []*PublicKey[G1]:
379385
for i := range msgs {
380386
listG2[i] = new(GG.G2)
381387
listG2[i].Hash(msgs[i], []byte(dstG2))
382388

383-
xP := (interface{})(pubs[i].key).(G1)
389+
xP := any(pubs[i].key).(G1)
384390
listG1[i] = &xP.g
385-
listExp[i] = 1
391+
listSigns[i] = 1
386392
}
387393

388394
err := listG2[n].SetBytes(aggSig)
@@ -395,9 +401,9 @@ func VerifyAggregate[K KeyGroup](pubs []*PublicKey[K], msgs [][]byte, aggSig Sig
395401
listG1[i] = new(GG.G1)
396402
listG1[i].Hash(msgs[i], []byte(dstG1))
397403

398-
xP := (interface{})(pubs[i].key).(G2)
404+
xP := any(pubs[i].key).(G2)
399405
listG2[i] = &xP.g
400-
listExp[i] = 1
406+
listSigns[i] = 1
401407
}
402408

403409
err := listG1[n].SetBytes(aggSig)
@@ -409,6 +415,6 @@ func VerifyAggregate[K KeyGroup](pubs []*PublicKey[K], msgs [][]byte, aggSig Sig
409415
panic(ErrInvalid)
410416
}
411417

412-
C := GG.ProdPairFrac(listG1, listG2, listExp)
418+
C := GG.ProdPairFrac(listG1, listG2, listSigns)
413419
return C.IsIdentity()
414420
}

0 commit comments

Comments
 (0)