-
Notifications
You must be signed in to change notification settings - Fork 0
/
bls_demo.go
132 lines (107 loc) · 3.9 KB
/
bls_demo.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
package main
import (
"fmt"
"github.com/prysmaticlabs/prysm/crypto/bls/blst"
"github.com/prysmaticlabs/prysm/crypto/bls/common"
"github.com/prysmaticlabs/prysm/crypto/bls/herumi"
)
var (
prvKey1 common.SecretKey
pubKey1 common.PublicKey
prvKey2 common.SecretKey
pubKey2 common.PublicKey
prvKey3 common.SecretKey
pubKey3 common.PublicKey
msg = [32]byte{101, 131, 24, 49, 81, 116, 222, 176, 189, 214, 82, 243, 72, 211, 168, 55, 209, 120, 224, 169, 149, 225, 192, 152, 168, 139, 121, 136, 18, 111, 159, 71}
)
func init() {
herumi.HerumiInit()
}
func GenerateKey() {
var err error
prvKey1, err = blst.RandKey()
if err != nil {
fmt.Errorf("Can't generate bls private key\n")
}
pubKey1 = prvKey1.PublicKey()
fmt.Printf("[Account1]\nprivate key: %v\npublic key: %v\n\n", prvKey1.Marshal(), pubKey1.Marshal())
prvKey2, err = blst.RandKey()
if err != nil {
fmt.Errorf("Can't generate bls private key\n")
}
pubKey2 = prvKey2.PublicKey()
fmt.Printf("[Account2]\nprivate key: %v\npublic key: %v\n\n", prvKey2.Marshal(), pubKey2.Marshal())
prvKey3, err = blst.RandKey()
if err != nil {
fmt.Errorf("Can't generate bls private key\n")
}
pubKey3 = prvKey3.PublicKey()
fmt.Printf("[Account3]\nprivate key: %v\npublic key: %v\n\n", prvKey3.Marshal(), pubKey3.Marshal())
}
func SingleSignAndVerify() {
fmt.Printf("============================================\n")
fmt.Printf("=========Single sign and verify ============\n")
sig1 := prvKey1.Sign(msg[:])
fmt.Printf("Account1 signature: %v\n", sig1.Marshal())
sig2 := prvKey2.Sign(msg[:])
fmt.Printf("Account2 signature: %v\n", sig2.Marshal())
sig3 := prvKey3.Sign(msg[:])
fmt.Printf("Account3 signature: %v\n", sig3.Marshal())
if !sig1.Verify(pubKey1, msg[:]) {
fmt.Printf("Verify account1 signature failed\n")
}
if !sig2.Verify(pubKey2, msg[:]) {
fmt.Printf("Verify account2 signature failed\n")
}
if !sig3.Verify(pubKey3, msg[:]) {
fmt.Printf("Verify account3 signature failed\n")
}
if sig1.Verify(pubKey2, msg[:]) {
fmt.Printf("Verify account1 signature by account2 success, rediculous\n")
}
fmt.Printf("============================================\n")
}
func AggregateSignAndVerify() {
fmt.Printf("============================================\n")
fmt.Printf("=======Aggregate sign and verify ===========\n")
sig1 := prvKey1.Sign(msg[:])
fmt.Printf("Account1 signature: %v\n", sig1.Marshal())
sig2 := prvKey2.Sign(msg[:])
fmt.Printf("Account2 signature: %v\n", sig2.Marshal())
sig3 := prvKey3.Sign(msg[:])
fmt.Printf("Account3 signature: %v\n", sig3.Marshal())
var sigs = [3]common.Signature{sig1, sig2, sig3}
var pubKeys = [3]common.PublicKey{pubKey1, pubKey2, pubKey3}
sig := blst.AggregateSignatures(sigs[:])
fmt.Printf("Aggregated signature: %v\n", sig.Marshal())
if !sig.FastAggregateVerify(pubKeys[:], msg) {
fmt.Printf("Fast aggregate verify signature failed\n")
}
fmt.Printf("============================================\n")
}
func AggregatePubkeyVerifySignature() {
fmt.Printf("============================================\n")
fmt.Printf("====Aggregate pubkey and verify signature =======\n")
sig1 := prvKey1.Sign(msg[:])
fmt.Printf("Account1 signature: %v\n", sig1.Marshal())
sig2 := prvKey2.Sign(msg[:])
fmt.Printf("Account2 signature: %v\n", sig2.Marshal())
sig3 := prvKey3.Sign(msg[:])
fmt.Printf("Account3 signature: %v\n", sig3.Marshal())
var sigs = [3]common.Signature{sig1, sig2, sig3}
sig := blst.AggregateSignatures(sigs[:])
fmt.Printf("Aggregated signature: %v\n", sig.Marshal())
pubkey := pubKey1.Aggregate(pubKey2).Aggregate(pubKey3)
fmt.Printf("Aggregated pubkey: %v\n", pubkey.Marshal())
if !sig.Verify(pubkey, msg[:]) {
//if !sig.Verify(pubkey, []byte("hello world")) {
fmt.Printf("Aggregated pubkey verify aggregate signature failed\n")
}
fmt.Printf("============================================\n")
}
func main() {
GenerateKey()
SingleSignAndVerify()
AggregateSignAndVerify()
AggregatePubkeyVerifySignature()
}