-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
269 lines (239 loc) · 8.68 KB
/
main.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
package main
import (
"bytes"
"context"
"fmt"
"math/rand"
"os"
"runtime"
"sync"
"time"
multiproof "github.com/crate-crypto/go-ipa"
"github.com/crate-crypto/go-ipa/bandersnatch/fr"
"github.com/crate-crypto/go-ipa/banderwagon"
"github.com/crate-crypto/go-ipa/common"
"github.com/crate-crypto/go-ipa/ipa"
"github.com/gballet/go-verkle"
"golang.org/x/sync/errgroup"
)
const (
vectorSize = 256
numRounds = 10
)
func main() {
conf := genOrLoadConfig("precomp")
fmt.Printf("##### VKT proof benchmark #####\n")
benchVKTProof(conf)
fmt.Printf("\n##### Raw polynomials multiproof benchmark #####\n")
benchProvingAndVerification(conf)
}
func benchProvingAndVerification(conf *ipa.IPAConfig) {
provingStepNames := []string{
"Generate challenge r and powers ",
"Calculate t, g(x) and D ",
"Calculate h(x) and E ",
"Calculate (h-g)(x) and E-D ",
"IPA for (h-g)(x) and E-D on t ",
}
provingNumSteps := len(provingStepNames)
verificationStepNames := []string{
"Generate challenge r and powers ",
"Calculating helper_scalars r^i/(t-z_i) ",
"g_2(t) = SUM y_i*(r^i/(t-z_i))=SUM y_i*helper_scalars ",
"Compute E ",
"Compute E-D and verify IPA ",
}
verificationNumSteps := len(verificationStepNames)
for _, numPolynomials := range []int{1, 1000, 2000, 4000, 8000, 16000, 128000} {
var proofAggrSerialization time.Duration
var provingAggrTotalTime time.Duration
provingAggrStepDuration := make([]time.Duration, provingNumSteps)
var proofAggrDeserialization time.Duration
var verificationAggrTotalTime time.Duration
verificationAggrStepDuration := make([]time.Duration, verificationNumSteps)
for i := 0; i < numRounds; i++ {
runtime.GC()
// Setup.
cs, fs, zs := generateNRandomPolysEvals(conf, numPolynomials)
// Proving.
transcriptProving := common.NewTranscript("bench")
proof := multiproof.CreateMultiProof(transcriptProving, conf, cs, fs, zs)
timestamps := transcriptProving.GetTimestamps()
if len(timestamps) != provingNumSteps+1 {
panic("wrong number of timestamps")
}
for k := 1; k < len(timestamps); k++ {
provingAggrStepDuration[k-1] += timestamps[k].Sub(timestamps[k-1])
}
provingAggrTotalTime += timestamps[len(timestamps)-1].Sub(timestamps[0])
var buf bytes.Buffer
now := time.Now()
proof.Write(&buf)
proofAggrSerialization += time.Since(now)
now = time.Now()
var deserializedProof multiproof.MultiProof
deserializedProof.Read(&buf)
proofAggrDeserialization += time.Since(now)
// Normalize Cs since the verifier will receive them in affine form. The verifier won't have
// Projective->Affine overhead.
for i := range cs {
cs[i].Normalise()
}
// Verification.
ys := make([]*fr.Element, len(zs))
for i, z := range zs {
ys[i] = &fs[i][z]
}
transcriptVerification := common.NewTranscript("bench")
if ok := multiproof.CheckMultiProof(transcriptVerification, conf, &deserializedProof, cs, ys, zs); !ok {
panic("verification failed")
}
timestamps = transcriptVerification.GetTimestamps()
if len(timestamps) != verificationNumSteps+1 {
panic("wrong number of timestamps")
}
for k := 1; k < len(timestamps); k++ {
verificationAggrStepDuration[k-1] += timestamps[k].Sub(timestamps[k-1])
}
verificationAggrTotalTime += timestamps[len(timestamps)-1].Sub(timestamps[0])
}
fmt.Printf("For %d polynomials:\n", numPolynomials)
fmt.Printf("\tProving:\n")
fmt.Printf("\t\tProof serialization duration: %.02fms\n", float64((proofAggrSerialization/time.Duration(numRounds)).Microseconds())/1000)
fmt.Printf("\t\tProof creation duration: %dms\n", (provingAggrTotalTime / time.Duration(numRounds)).Milliseconds())
fmt.Printf("\t\tDuration per step:\n")
for i := 0; i < provingNumSteps; i++ {
fmt.Printf("\t\t\t%s: %.02fms\n", provingStepNames[i], float64((provingAggrStepDuration[i]/time.Duration(numRounds)).Microseconds())/1000)
}
fmt.Printf("\tVerification:\n")
fmt.Printf("\t\tProof deserialization duration: %.02fms\n", float64((proofAggrSerialization/time.Duration(numRounds)).Microseconds())/1000)
fmt.Printf("\t\tTotal duration: %dms\n", (verificationAggrTotalTime / time.Duration(numRounds)).Milliseconds())
fmt.Printf("\t\tDuration per step:\n")
for i := 0; i < verificationNumSteps; i++ {
fmt.Printf("\t\t\t%s: %.02fms\n", verificationStepNames[i], float64((verificationAggrStepDuration[i]/time.Duration(numRounds)).Microseconds())/1000)
}
fmt.Println()
}
}
func benchVKTProof(conf *ipa.IPAConfig) {
const numRandKeyValues = 1_000_000
fmt.Printf("Setup: tree with %d random key-values...\n\n", numRandKeyValues)
keyValues, tree := genRandomTree(rand.New(rand.NewSource(42)), numRandKeyValues)
tree.Commit()
for _, numKeyValues := range []int{1, 1000, 2000, 4000, 8000, 16000} {
runtime.GC()
var aggrProofGenTime time.Duration
var aggrSerializationTime time.Duration
var aggrDeserializationTime time.Duration
verkle.BenchCollectPolynomialsDuration = 0
verkle.BenchNumPolynomials = 0
for i := 0; i < numRounds; i++ {
keyvals := map[string][]byte{}
keys := make([][]byte, 0, numKeyValues)
for i, kv := range keyValues {
if i == numKeyValues {
break
}
keys = append(keys, kv.key)
keyvals[string(kv.key)] = kv.value
}
start := time.Now()
proof, _, _, _, err := verkle.MakeVerkleMultiProof(tree, keys, keyvals)
if err != nil {
panic("failed to generate proof")
}
aggrProofGenTime += time.Since(start)
start = time.Now()
serProof, serStateDiff, err := verkle.SerializeProof(proof)
if err != nil {
panic("failed to serialize proof")
}
aggrSerializationTime += time.Since(start)
start = time.Now()
if _, err := verkle.DeserializeProof(serProof, serStateDiff); err != nil {
panic("failed to deserialize proof")
}
aggrDeserializationTime += time.Since(start)
}
fmt.Printf("For %d random-key values of the tree:\n", numKeyValues)
fmt.Printf("\tGenerating proof took %dms:\n", (aggrProofGenTime / time.Duration(numRounds)).Milliseconds())
fmt.Printf("\t\tCollected %d polynomials evaluations to prove\n", verkle.BenchNumPolynomials/numRounds)
fmt.Printf("\t\tCollecting those polys evals (comm, evals, etc) took %dms\n", (verkle.BenchCollectPolynomialsDuration / time.Duration(numRounds)).Milliseconds())
fmt.Printf("\t\tThe rest (Multiproof + nits) took %dms\n", ((aggrProofGenTime - verkle.BenchCollectPolynomialsDuration) / time.Duration(numRounds)).Milliseconds())
fmt.Printf("\tSerializing proof took %dms\n", (aggrSerializationTime / time.Duration(numRounds)).Milliseconds())
fmt.Printf("\tDeserializing proof took %dms\n", (aggrDeserializationTime / time.Duration(numRounds)).Milliseconds())
fmt.Println()
}
}
func genRandomTree(rand *rand.Rand, keyValueCount int) ([]keyValue, verkle.VerkleNode) {
tree := verkle.New()
keyValues := make([]keyValue, 0, keyValueCount)
for _, kv := range genRandomKeyValues(rand, keyValueCount) {
if err := tree.Insert(kv.key, kv.value, nil); err != nil {
panic(fmt.Sprintf("failed to insert key: %v", err))
}
keyValues = append(keyValues, kv)
}
return keyValues, tree
}
type keyValue struct {
key []byte
value []byte
}
func genRandomKeyValues(rand *rand.Rand, count int) []keyValue {
ret := make([]keyValue, count)
for i := 0; i < count; i++ {
keyval := make([]byte, 64)
rand.Read(keyval)
ret[i].key = keyval[:32]
ret[i].value = keyval[32:]
}
return ret
}
func generateNRandomPolysEvals(conf *ipa.IPAConfig, n int) ([]*banderwagon.Element, [][]fr.Element, []uint8) {
var lock sync.Mutex
retCs := make([]*banderwagon.Element, 0, n)
retEvals := make([][]fr.Element, 0, n)
retZs := make([]uint8, 0, n)
batchSize := n/runtime.NumCPU() + 1
g, _ := errgroup.WithContext(context.Background())
for i := 0; i < n; i += batchSize {
num := batchSize
if i+batchSize > n {
num = n - i
}
g.Go(func() error {
for i := 0; i < num; i++ {
frs := make([]fr.Element, vectorSize)
for j := 0; j < vectorSize; j++ {
frs[j].SetRandom()
}
c := conf.Commit(frs)
lock.Lock()
retEvals = append(retEvals, frs)
retCs = append(retCs, &c)
retZs = append(retZs, uint8(rand.Uint32()%vectorSize))
lock.Unlock()
}
return nil
})
}
g.Wait()
return retCs, retEvals, retZs
}
func genOrLoadConfig(fileName string) *ipa.IPAConfig {
serialized, err := os.ReadFile(fileName)
if err == nil {
srs, err := ipa.DeserializeSRSPrecomp(serialized)
if err != nil {
panic(err)
}
return ipa.NewIPASettingsWithSRSPrecomp(srs)
}
conf := ipa.NewIPASettings()
bts, err := conf.SRSPrecompPoints.SerializeSRSPrecomp()
if err == nil {
_ = os.WriteFile(fileName, bts, 0644)
}
return conf
}