-
Notifications
You must be signed in to change notification settings - Fork 0
/
bls.go
58 lines (48 loc) · 1.47 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
package bls
import (
"bytes"
"github.com/SickleZhang/bls/bn256"
)
func Sign(msg []byte, pvtk *PrivateKey) *Signature {
hpt := new(HashPoint).Sum(msg)
return new(Signature).Sign(hpt, pvtk)
}
var g2 = new(bn256.G2).Base()
func Verify(sig *Signature, msg []byte, pubk *PublicKey) bool {
hpt := new(HashPoint).Sum(msg)
e1 := bn256.Pair(&hpt.val, &pubk.val)
e2 := bn256.Pair(&sig.val, g2)
return bytes.Equal(e1.Marshal(), e2.Marshal())
}
func VerifyNFor1(sig *Signature, msg []byte, pubks ...*PublicKey) bool {
hpt := new(HashPoint).Sum(msg)
apubk := new(PublicKey).Aggregate(pubks...)
e1 := bn256.Pair(&hpt.val, &apubk.val)
e2 := bn256.Pair(&sig.val, g2)
return bytes.Equal(e1.Marshal(), e2.Marshal())
}
func Verify1ForN(sig *Signature, pubk *PublicKey, msgs ...[]byte) bool {
ahpt := new(HashPoint).SumAggregate(msgs...)
e1 := bn256.Pair(&ahpt.val, &pubk.val)
e2 := bn256.Pair(&sig.val, g2)
return bytes.Equal(e1.Marshal(), e2.Marshal())
}
type VerifiableMessage struct {
msghash HashPoint
member PublicKey
}
func NewVerifiableMessage(msg []byte, pubk *PublicKey) *VerifiableMessage {
ret := new(VerifiableMessage)
ret.msghash.Sum(msg)
ret.member.Unmarshal(pubk.Marshal())
return ret
}
func VerifyGroupMessage(sig *Signature, msgs ...*VerifiableMessage) bool {
e1 := new(bn256.GT).Unit()
for _, msg := range msgs {
e := bn256.Pair(&msg.msghash.val, &msg.member.val)
e1.Add(e1, e)
}
e2 := bn256.Pair(&sig.val, g2)
return bytes.Equal(e1.Marshal(), e2.Marshal())
}