forked from bnb-chain/zkbnb-setup
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
315 lines (269 loc) · 8.6 KB
/
utils.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package phase1
import (
"bufio"
"bytes"
"errors"
"io"
"math"
"math/big"
"os"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
"github.com/worldcoin/semaphore-mtb-setup/common"
)
const batchSize = 1048576 // 2^20
// Returns powers of b starting from a as [a, ba, ..., abⁿ⁻¹ ]
func powers(a, b *fr.Element, n int) []fr.Element {
result := make([]fr.Element, n)
result[0].Set(a)
for i := 1; i < n; i++ {
result[i].Mul(&result[i-1], b)
}
return result
}
// Multiply each element by b
func batchMul(a []fr.Element, b *fr.Element) {
common.Parallelize(len(a), func(start, end int) {
for i := start; i < end; i++ {
a[i].Mul(&a[i], b)
}
})
}
func scaleG1(dec *bn254.Decoder, enc *bn254.Encoder, N int, tau, multiplicand *fr.Element) (*bn254.G1Affine, error) {
// Allocate batch with smallest of (N, batchSize)
var initialSize = int(math.Min(float64(N), float64(batchSize)))
buff := make([]bn254.G1Affine, initialSize)
var firstPoint bn254.G1Affine
var startPower fr.Element
var scalars []fr.Element
startPower.SetOne()
remaining := N
for remaining > 0 {
// Read batch
readCount := int(math.Min(float64(remaining), float64(batchSize)))
for i := 0; i < readCount; i++ {
if err := dec.Decode(&buff[i]); err != nil {
return nil, err
}
}
// Compute powers for the current batch
scalars = powers(&startPower, tau, readCount)
// Update startPower for next batch
startPower.Mul(&scalars[readCount-1], tau)
// If there is α or β, then mul it with powers of τ
if multiplicand != nil {
batchMul(scalars, multiplicand)
}
// Process the batch
common.Parallelize(readCount, func(start, end int) {
for i := start; i < end; i++ {
var tmpBi big.Int
scalars[i].BigInt(&tmpBi)
buff[i].ScalarMultiplication(&buff[i], &tmpBi)
}
})
// Write the batch
for i := 0; i < readCount; i++ {
if err := enc.Encode(&buff[i]); err != nil {
return nil, err
}
}
// Should be initialized in first batch only
if firstPoint.X.IsZero() {
if multiplicand == nil {
// Set firstPoint to the second point = [τ]
firstPoint.Set(&buff[1])
} else {
// Set firstPoint to the first point = [α] or [β]
firstPoint.Set(&buff[0])
}
}
// Update remaining
remaining -= readCount
}
return &firstPoint, nil
}
func scaleG2(dec *bn254.Decoder, enc *bn254.Encoder, N int, tau *fr.Element) (*bn254.G2Affine, error) {
// Allocate batch with smallest of (N, batchSize)
var initialSize = int(math.Min(float64(N), float64(batchSize)))
buff := make([]bn254.G2Affine, initialSize)
var firstPoint bn254.G2Affine
var startPower fr.Element
var scalars []fr.Element
startPower.SetOne()
remaining := N
for remaining > 0 {
// Read batch
readCount := int(math.Min(float64(remaining), float64(batchSize)))
for i := 0; i < readCount; i++ {
if err := dec.Decode(&buff[i]); err != nil {
return nil, err
}
}
// Compute powers for the current batch
scalars = powers(&startPower, tau, readCount)
// Update startPower for next batch
startPower.Mul(&scalars[readCount-1], tau)
// Process the batch
common.Parallelize(readCount, func(start, end int) {
for i := start; i < end; i++ {
var tmpBi big.Int
scalars[i].BigInt(&tmpBi)
buff[i].ScalarMultiplication(&buff[i], &tmpBi)
}
})
// Write the batch
for i := 0; i < readCount; i++ {
if err := enc.Encode(&buff[i]); err != nil {
return nil, err
}
}
// Should be initialized in first batch only
if firstPoint.X.IsZero() {
firstPoint.Set(&buff[1])
}
// Update remaining
remaining -= readCount
}
return &firstPoint, nil
}
func randomize(r []fr.Element) {
common.Parallelize(len(r), func(start, end int) {
for i := start; i < end; i++ {
r[i].SetRandom()
}
})
}
func linearCombinationG1(dec *bn254.Decoder, N int) (bn254.G1Affine, bn254.G1Affine, error) {
// Allocate batch with smallest of (N, batchSize)
var initialSize = int(math.Min(float64(N), float64(batchSize)))
buff := make([]bn254.G1Affine, initialSize)
r := make([]fr.Element, initialSize)
var L1, L2, tmpL1, tmpL2 bn254.G1Affine
remaining := N
for remaining > 0 {
// Read batch
readCount := int(math.Min(float64(remaining), float64(batchSize)))
for i := 0; i < readCount; i++ {
if err := dec.Decode(&buff[i]); err != nil {
return L1, L2, err
}
}
// Generate randomness
randomize(r)
// Process the batch
tmpL1.MultiExp(buff[:readCount-1], r, ecc.MultiExpConfig{})
tmpL2.MultiExp(buff[1:readCount], r, ecc.MultiExpConfig{})
L1.Add(&L1, &tmpL1)
L2.Add(&L2, &tmpL2)
// Update remaining
remaining -= readCount
}
return L1, L2, nil
}
func linearCombinationG2(dec *bn254.Decoder, N int) (bn254.G2Affine, bn254.G2Affine, error) {
// Allocate batch with smallest of (N, batchSize)
var initialSize = int(math.Min(float64(N), float64(batchSize)))
buff := make([]bn254.G2Affine, initialSize)
r := make([]fr.Element, initialSize)
var L1, L2, tmpL1, tmpL2 bn254.G2Affine
remaining := N
for remaining > 0 {
// Read batch
readCount := int(math.Min(float64(remaining), float64(batchSize)))
for i := 0; i < readCount; i++ {
if err := dec.Decode(&buff[i]); err != nil {
return L1, L2, err
}
}
// Generate randomness
randomize(r)
// Process the batch
tmpL1.MultiExp(buff[:readCount-1], r, ecc.MultiExpConfig{})
tmpL2.MultiExp(buff[1:readCount], r, ecc.MultiExpConfig{})
L1.Add(&L1, &tmpL1)
L2.Add(&L2, &tmpL2)
// Update remaining
remaining -= readCount
}
return L1, L2, nil
}
func verifyContribution(current, prev Contribution) error {
// Compute SP for τ, α, β
tauSP := common.GenSP(current.PublicKeys.Tau.S, current.PublicKeys.Tau.SX, prev.Hash[:], 1)
alphaSP := common.GenSP(current.PublicKeys.Alpha.S, current.PublicKeys.Alpha.SX, prev.Hash[:], 2)
betaSP := common.GenSP(current.PublicKeys.Beta.S, current.PublicKeys.Beta.SX, prev.Hash[:], 3)
// Check for knowledge of toxic parameters
if !common.SameRatio(current.PublicKeys.Tau.S, current.PublicKeys.Tau.SX, current.PublicKeys.Tau.SPX, tauSP) {
return errors.New("couldn't verify knowledge of Tau")
}
if !common.SameRatio(current.PublicKeys.Alpha.S, current.PublicKeys.Alpha.SX, current.PublicKeys.Alpha.SPX, alphaSP) {
return errors.New("couldn't verify knowledge of Alpha")
}
if !common.SameRatio(current.PublicKeys.Beta.S, current.PublicKeys.Beta.SX, current.PublicKeys.Beta.SPX, betaSP) {
return errors.New("couldn't verify knowledge of Beta")
}
// Check for valid updates using previous parameters
if !common.SameRatio(current.G1.Tau, prev.G1.Tau, tauSP, current.PublicKeys.Tau.SPX) {
return errors.New("couldn't verify that TauG1 is based on previous contribution")
}
if !common.SameRatio(current.G1.Alpha, prev.G1.Alpha, alphaSP, current.PublicKeys.Alpha.SPX) {
return errors.New("couldn't verify that AlphaTauG1 is based on previous contribution")
}
if !common.SameRatio(current.G1.Beta, prev.G1.Beta, betaSP, current.PublicKeys.Beta.SPX) {
return errors.New("couldn't verify that BetaTauG1 is based on previous contribution")
}
if !common.SameRatio(current.PublicKeys.Tau.S, current.PublicKeys.Tau.SX, current.G2.Tau, prev.G2.Tau) {
return errors.New("couldn't verify that TauG2 is based on previous contribution")
}
if !common.SameRatio(current.PublicKeys.Beta.S, current.PublicKeys.Beta.SX, current.G2.Beta, prev.G2.Beta) {
return errors.New("couldn't verify that BetaG2 is based on previous contribution")
}
// Check hash of the contribution
h := computeHash(¤t)
if !bytes.Equal(current.Hash, h) {
return errors.New("couldn't verify hash of contribution")
}
return nil
}
func transformG1(inputFile, outputFile *os.File, position int64, size int) error {
var g1 bn254.G1Affine
if _, err := inputFile.Seek(position, io.SeekStart); err != nil {
return err
}
reader := bufio.NewReader(inputFile)
writer := bufio.NewWriter(outputFile)
defer writer.Flush()
dec := bn254.NewDecoder(reader)
enc := bn254.NewEncoder(writer)
for i := 0; i < size; i++ {
if err := dec.Decode(&g1); err != nil {
return err
}
if err := enc.Encode(&g1); err != nil {
return err
}
}
return nil
}
func transformG2(inputFile, outputFile *os.File, position int64, size int) error {
var g2 bn254.G2Affine
if _, err := inputFile.Seek(position, io.SeekStart); err != nil {
return err
}
reader := bufio.NewReader(inputFile)
writer := bufio.NewWriter(outputFile)
defer writer.Flush()
dec := bn254.NewDecoder(reader)
enc := bn254.NewEncoder(writer)
for i := 0; i < size; i++ {
if err := dec.Decode(&g2); err != nil {
return err
}
if err := enc.Encode(&g2); err != nil {
return err
}
}
return nil
}