forked from herumi/bls-go-binary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagg-verify.go
125 lines (114 loc) · 2.38 KB
/
agg-verify.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
package main
import (
"fmt"
"github.com/mxmar/bls-go-binary/bls"
)
var g_Qcoeff []uint64
func initQcoeff() {
bls.Init(bls.BLS12_381)
n := bls.GetUint64NumToPrecompute()
g_Qcoeff = make([]uint64, n)
var Q bls.PublicKey
bls.BlsGetGeneratorOfPublicKey(&Q)
bls.PrecomputeG2(g_Qcoeff, bls.CastFromPublicKey(&Q))
}
func toG(h []byte) (P bls.G1) {
var t bls.Fp
t.SetLittleEndian(h)
bls.MapToG1(&P, &t)
return P
}
func splitVAH_init(agg *bls.Sign) (e bls.GT) {
P := *bls.CastFromSign(agg)
bls.G1Neg(&P, &P)
bls.PrecomputedMillerLoop(&e, &P, g_Qcoeff)
return e
}
// return the processed number
func splitVAH_exec(e *bls.GT, pubs []bls.PublicKey, hs [][]byte) (m int) {
N := 16
g1s := make([]bls.G1, N)
g2s := make([]bls.G2, N)
m = len(pubs)
if m > N {
m = N
}
for i := 0; i < m; i++ {
g1s[i] = toG(hs[i])
g2s[i] = *bls.CastFromPublicKey(&pubs[i])
}
var e1 bls.GT
bls.MillerLoopVec(&e1, g1s[0:m], g2s[0:m])
bls.GTMul(e, e, &e1)
return m
}
func splitVAH_final(e *bls.GT) bool {
bls.FinalExp(e, e)
return e.IsOne()
}
func splitVerifyAggregateHashes(agg *bls.Sign, pubs []bls.PublicKey, hs [][]byte) bool {
n := len(pubs)
if n != len(hs) {
return false
}
e := splitVAH_init(agg)
for {
m := splitVAH_exec(&e, pubs, hs)
pubs = pubs[m:]
hs = hs[m:]
n = n - m
if n == 0 {
break
}
}
/*
N := 16
g1work := make([]bls.G1, N)
g2work := make([]bls.G2, N)
for {
m := N
if n < m {
m = n
}
for i := 0; i < m; i++ {
g1work[i] = toG(hs[i])
g2work[i] = *bls.CastFromPublicKey(&pubs[i])
}
var e1 bls.GT
bls.MillerLoopVec(&e1, g1work[0:m], g2work[0:m])
bls.GTMul(&e, &e, &e1)
pubs = pubs[m:]
hs = hs[m:]
n = n - m
if n == 0 {
break
}
}
*/
return splitVAH_final(&e)
}
func testVerifyAggregateHashes() {
n := 100
secs := make([]bls.SecretKey, n)
pubs := make([]bls.PublicKey, n)
hs := make([][]byte, n)
sigs := make([]bls.Sign, n)
for i := 0; i < n; i++ {
hs[i] = make([]byte, 32)
hs[i][0] = byte(i + 1)
secs[i].SetByCSPRNG()
pubs[i] = *secs[i].GetPublicKey()
sigs[i] = *secs[i].SignHash(hs[i])
if !sigs[i].VerifyHash(&pubs[i], hs[i]) {
fmt.Printf("err %v\n", i)
}
}
var agg bls.Sign
agg.Aggregate(sigs)
fmt.Printf("verify=%v\n", agg.VerifyAggregateHashes(pubs, hs))
fmt.Printf("verify split=%v\n", splitVerifyAggregateHashes(&agg, pubs, hs))
}
func main() {
initQcoeff()
testVerifyAggregateHashes()
}