-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathgkr.go
944 lines (792 loc) · 26.3 KB
/
gkr.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
// Copyright 2020 Consensys Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by consensys/gnark-crypto DO NOT EDIT
package gkr
import (
"fmt"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
"github.com/consensys/gnark-crypto/ecc/bn254/fr/polynomial"
"github.com/consensys/gnark-crypto/ecc/bn254/fr/sumcheck"
fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir"
"github.com/consensys/gnark-crypto/internal/parallel"
"github.com/consensys/gnark-crypto/utils"
"math/big"
"strconv"
"sync"
)
// The goal is to prove/verify evaluations of many instances of the same circuit
// Gate must be a low-degree polynomial
type Gate interface {
Evaluate(...fr.Element) fr.Element
Degree() int
}
type Wire struct {
Gate Gate
Inputs []*Wire // if there are no Inputs, the wire is assumed an input wire
nbUniqueOutputs int // number of other wires using it as input, not counting duplicates (i.e. providing two inputs to the same gate counts as one)
}
type Circuit []Wire
func (w Wire) IsInput() bool {
return len(w.Inputs) == 0
}
func (w Wire) IsOutput() bool {
return w.nbUniqueOutputs == 0
}
func (w Wire) NbClaims() int {
if w.IsOutput() {
return 1
}
return w.nbUniqueOutputs
}
func (w Wire) noProof() bool {
return w.IsInput() && w.NbClaims() == 1
}
func (c Circuit) maxGateDegree() int {
res := 1
for i := range c {
if !c[i].IsInput() {
res = utils.Max(res, c[i].Gate.Degree())
}
}
return res
}
// WireAssignment is assignment of values to the same wire across many instances of the circuit
type WireAssignment map[*Wire]polynomial.MultiLin
type Proof []sumcheck.Proof // for each layer, for each wire, a sumcheck (for each variable, a polynomial)
type eqTimesGateEvalSumcheckLazyClaims struct {
wire *Wire
evaluationPoints [][]fr.Element
claimedEvaluations []fr.Element
manager *claimsManager // WARNING: Circular references
}
func (e *eqTimesGateEvalSumcheckLazyClaims) ClaimsNum() int {
return len(e.evaluationPoints)
}
func (e *eqTimesGateEvalSumcheckLazyClaims) VarsNum() int {
return len(e.evaluationPoints[0])
}
func (e *eqTimesGateEvalSumcheckLazyClaims) CombinedSum(a fr.Element) fr.Element {
evalsAsPoly := polynomial.Polynomial(e.claimedEvaluations)
return evalsAsPoly.Eval(&a)
}
func (e *eqTimesGateEvalSumcheckLazyClaims) Degree(int) int {
return 1 + e.wire.Gate.Degree()
}
func (e *eqTimesGateEvalSumcheckLazyClaims) VerifyFinalEval(r []fr.Element, combinationCoeff fr.Element, purportedValue fr.Element, proof interface{}) error {
inputEvaluationsNoRedundancy := proof.([]fr.Element)
// the eq terms
numClaims := len(e.evaluationPoints)
evaluation := polynomial.EvalEq(e.evaluationPoints[numClaims-1], r)
for i := numClaims - 2; i >= 0; i-- {
evaluation.Mul(&evaluation, &combinationCoeff)
eq := polynomial.EvalEq(e.evaluationPoints[i], r)
evaluation.Add(&evaluation, &eq)
}
// the g(...) term
var gateEvaluation fr.Element
if e.wire.IsInput() {
gateEvaluation = e.manager.assignment[e.wire].Evaluate(r, e.manager.memPool)
} else {
inputEvaluations := make([]fr.Element, len(e.wire.Inputs))
indexesInProof := make(map[*Wire]int, len(inputEvaluationsNoRedundancy))
proofI := 0
for inI, in := range e.wire.Inputs {
indexInProof, found := indexesInProof[in]
if !found {
indexInProof = proofI
indexesInProof[in] = indexInProof
// defer verification, store new claim
e.manager.add(in, r, inputEvaluationsNoRedundancy[indexInProof])
proofI++
}
inputEvaluations[inI] = inputEvaluationsNoRedundancy[indexInProof]
}
if proofI != len(inputEvaluationsNoRedundancy) {
return fmt.Errorf("%d input wire evaluations given, %d expected", len(inputEvaluationsNoRedundancy), proofI)
}
gateEvaluation = e.wire.Gate.Evaluate(inputEvaluations...)
}
evaluation.Mul(&evaluation, &gateEvaluation)
if evaluation.Equal(&purportedValue) {
return nil
}
return fmt.Errorf("incompatible evaluations")
}
type eqTimesGateEvalSumcheckClaims struct {
wire *Wire
evaluationPoints [][]fr.Element // x in the paper
claimedEvaluations []fr.Element // y in the paper
manager *claimsManager
inputPreprocessors []polynomial.MultiLin // P_u in the paper, so that we don't need to pass along all the circuit's evaluations
eq polynomial.MultiLin // ∑_i τ_i eq(x_i, -)
}
func (c *eqTimesGateEvalSumcheckClaims) Combine(combinationCoeff fr.Element) polynomial.Polynomial {
varsNum := c.VarsNum()
eqLength := 1 << varsNum
claimsNum := c.ClaimsNum()
// initialize the eq tables
c.eq = c.manager.memPool.Make(eqLength)
c.eq[0].SetOne()
c.eq.Eq(c.evaluationPoints[0])
newEq := polynomial.MultiLin(c.manager.memPool.Make(eqLength))
aI := combinationCoeff
for k := 1; k < claimsNum; k++ { //TODO: parallelizable?
// define eq_k = aᵏ eq(x_k1, ..., x_kn, *, ..., *) where x_ki are the evaluation points
newEq[0].Set(&aI)
c.eqAcc(c.eq, newEq, c.evaluationPoints[k])
// newEq.Eq(c.evaluationPoints[k])
// eqAsPoly := polynomial.Polynomial(c.eq) //just semantics
// eqAsPoly.Add(eqAsPoly, polynomial.Polynomial(newEq))
if k+1 < claimsNum {
aI.Mul(&aI, &combinationCoeff)
}
}
c.manager.memPool.Dump(newEq)
// from this point on the claim is a rather simple one: g = E(h) × R_v (P_u0(h), ...) where E and the P_u are multilinear and R_v is of low-degree
return c.computeGJ()
}
// eqAcc sets m to an eq table at q and then adds it to e
func (c *eqTimesGateEvalSumcheckClaims) eqAcc(e, m polynomial.MultiLin, q []fr.Element) {
n := len(q)
//At the end of each iteration, m(h₁, ..., hₙ) = Eq(q₁, ..., qᵢ₊₁, h₁, ..., hᵢ₊₁)
for i := range q { // In the comments we use a 1-based index so q[i] = qᵢ₊₁
// go through all assignments of (b₁, ..., bᵢ) ∈ {0,1}ⁱ
const threshold = 1 << 6
k := 1 << i
if k < threshold {
for j := 0; j < k; j++ {
j0 := j << (n - i) // bᵢ₊₁ = 0
j1 := j0 + 1<<(n-1-i) // bᵢ₊₁ = 1
m[j1].Mul(&q[i], &m[j0]) // Eq(q₁, ..., qᵢ₊₁, b₁, ..., bᵢ, 1) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) Eq(qᵢ₊₁, 1) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) qᵢ₊₁
m[j0].Sub(&m[j0], &m[j1]) // Eq(q₁, ..., qᵢ₊₁, b₁, ..., bᵢ, 0) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) Eq(qᵢ₊₁, 0) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) (1-qᵢ₊₁)
}
} else {
c.manager.workers.Submit(k, func(start, end int) {
for j := start; j < end; j++ {
j0 := j << (n - i) // bᵢ₊₁ = 0
j1 := j0 + 1<<(n-1-i) // bᵢ₊₁ = 1
m[j1].Mul(&q[i], &m[j0]) // Eq(q₁, ..., qᵢ₊₁, b₁, ..., bᵢ, 1) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) Eq(qᵢ₊₁, 1) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) qᵢ₊₁
m[j0].Sub(&m[j0], &m[j1]) // Eq(q₁, ..., qᵢ₊₁, b₁, ..., bᵢ, 0) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) Eq(qᵢ₊₁, 0) = Eq(q₁, ..., qᵢ, b₁, ..., bᵢ) (1-qᵢ₊₁)
}
}, 1024).Wait()
}
}
c.manager.workers.Submit(len(e), func(start, end int) {
for i := start; i < end; i++ {
e[i].Add(&e[i], &m[i])
}
}, 512).Wait()
// e.Add(e, polynomial.Polynomial(m))
}
// computeGJ: gⱼ = ∑_{0≤i<2ⁿ⁻ʲ} g(r₁, r₂, ..., rⱼ₋₁, Xⱼ, i...) = ∑_{0≤i<2ⁿ⁻ʲ} E(r₁, ..., X_j, i...) R_v( P_u0(r₁, ..., X_j, i...), ... ) where E = ∑ eq_k
// the polynomial is represented by the evaluations g_j(1), g_j(2), ..., g_j(deg(g_j)).
// The value g_j(0) is inferred from the equation g_j(0) + g_j(1) = gⱼ₋₁(rⱼ₋₁). By convention, g₀ is a constant polynomial equal to the claimed sum.
func (c *eqTimesGateEvalSumcheckClaims) computeGJ() polynomial.Polynomial {
degGJ := 1 + c.wire.Gate.Degree() // guaranteed to be no smaller than the actual deg(g_j)
nbGateIn := len(c.inputPreprocessors)
// Let f ∈ { E(r₁, ..., X_j, d...) } ∪ {P_ul(r₁, ..., X_j, d...) }. It is linear in X_j, so f(m) = m×(f(1) - f(0)) + f(0), and f(0), f(1) are easily computed from the bookkeeping tables
s := make([]polynomial.MultiLin, nbGateIn+1)
s[0] = c.eq
copy(s[1:], c.inputPreprocessors)
// Perf-TODO: Collate once at claim "combination" time and not again. then, even folding can be done in one operation every time "next" is called
nbInner := len(s) // wrt output, which has high nbOuter and low nbInner
nbOuter := len(s[0]) / 2
gJ := make([]fr.Element, degGJ)
var mu sync.Mutex
computeAll := func(start, end int) {
var step fr.Element
res := make([]fr.Element, degGJ)
operands := make([]fr.Element, degGJ*nbInner)
for i := start; i < end; i++ {
block := nbOuter + i
for j := 0; j < nbInner; j++ {
step.Set(&s[j][i])
operands[j].Set(&s[j][block])
step.Sub(&operands[j], &step)
for d := 1; d < degGJ; d++ {
operands[d*nbInner+j].Add(&operands[(d-1)*nbInner+j], &step)
}
}
_s := 0
_e := nbInner
for d := 0; d < degGJ; d++ {
summand := c.wire.Gate.Evaluate(operands[_s+1 : _e]...)
summand.Mul(&summand, &operands[_s])
res[d].Add(&res[d], &summand)
_s, _e = _e, _e+nbInner
}
}
mu.Lock()
for i := 0; i < len(gJ); i++ {
gJ[i].Add(&gJ[i], &res[i])
}
mu.Unlock()
}
const minBlockSize = 64
if nbOuter < minBlockSize {
// no parallelization
computeAll(0, nbOuter)
} else {
c.manager.workers.Submit(nbOuter, computeAll, minBlockSize).Wait()
}
// Perf-TODO: Separate functions Gate.TotalDegree and Gate.Degree(i) so that we get to use possibly smaller values for degGJ. Won't help with MiMC though
return gJ
}
// Next first folds the "preprocessing" and "eq" polynomials then compute the new g_j
func (c *eqTimesGateEvalSumcheckClaims) Next(element fr.Element) polynomial.Polynomial {
const minBlockSize = 512
n := len(c.eq) / 2
if n < minBlockSize {
// no parallelization
for i := 0; i < len(c.inputPreprocessors); i++ {
c.inputPreprocessors[i].Fold(element)
}
c.eq.Fold(element)
} else {
wgs := make([]*sync.WaitGroup, len(c.inputPreprocessors))
for i := 0; i < len(c.inputPreprocessors); i++ {
wgs[i] = c.manager.workers.Submit(n, c.inputPreprocessors[i].FoldParallel(element), minBlockSize)
}
c.manager.workers.Submit(n, c.eq.FoldParallel(element), minBlockSize).Wait()
for _, wg := range wgs {
wg.Wait()
}
}
return c.computeGJ()
}
func (c *eqTimesGateEvalSumcheckClaims) VarsNum() int {
return len(c.evaluationPoints[0])
}
func (c *eqTimesGateEvalSumcheckClaims) ClaimsNum() int {
return len(c.claimedEvaluations)
}
func (c *eqTimesGateEvalSumcheckClaims) ProveFinalEval(r []fr.Element) interface{} {
//defer the proof, return list of claims
evaluations := make([]fr.Element, 0, len(c.wire.Inputs))
noMoreClaimsAllowed := make(map[*Wire]struct{}, len(c.inputPreprocessors))
noMoreClaimsAllowed[c.wire] = struct{}{}
for inI, in := range c.wire.Inputs {
puI := c.inputPreprocessors[inI]
if _, found := noMoreClaimsAllowed[in]; !found {
noMoreClaimsAllowed[in] = struct{}{}
puI.Fold(r[len(r)-1])
c.manager.add(in, r, puI[0])
evaluations = append(evaluations, puI[0])
}
c.manager.memPool.Dump(puI)
}
c.manager.memPool.Dump(c.claimedEvaluations, c.eq)
return evaluations
}
type claimsManager struct {
claimsMap map[*Wire]*eqTimesGateEvalSumcheckLazyClaims
assignment WireAssignment
memPool *polynomial.Pool
workers *utils.WorkerPool
}
func newClaimsManager(c Circuit, assignment WireAssignment, o settings) (claims claimsManager) {
claims.assignment = assignment
claims.claimsMap = make(map[*Wire]*eqTimesGateEvalSumcheckLazyClaims, len(c))
claims.memPool = o.pool
claims.workers = o.workers
for i := range c {
wire := &c[i]
claims.claimsMap[wire] = &eqTimesGateEvalSumcheckLazyClaims{
wire: wire,
evaluationPoints: make([][]fr.Element, 0, wire.NbClaims()),
claimedEvaluations: claims.memPool.Make(wire.NbClaims()),
manager: &claims,
}
}
return
}
func (m *claimsManager) add(wire *Wire, evaluationPoint []fr.Element, evaluation fr.Element) {
claim := m.claimsMap[wire]
i := len(claim.evaluationPoints)
claim.claimedEvaluations[i] = evaluation
claim.evaluationPoints = append(claim.evaluationPoints, evaluationPoint)
}
func (m *claimsManager) getLazyClaim(wire *Wire) *eqTimesGateEvalSumcheckLazyClaims {
return m.claimsMap[wire]
}
func (m *claimsManager) getClaim(wire *Wire) *eqTimesGateEvalSumcheckClaims {
lazy := m.claimsMap[wire]
res := &eqTimesGateEvalSumcheckClaims{
wire: wire,
evaluationPoints: lazy.evaluationPoints,
claimedEvaluations: lazy.claimedEvaluations,
manager: m,
}
if wire.IsInput() {
res.inputPreprocessors = []polynomial.MultiLin{m.memPool.Clone(m.assignment[wire])}
} else {
res.inputPreprocessors = make([]polynomial.MultiLin, len(wire.Inputs))
for inputI, inputW := range wire.Inputs {
res.inputPreprocessors[inputI] = m.memPool.Clone(m.assignment[inputW]) //will be edited later, so must be deep copied
}
}
return res
}
func (m *claimsManager) deleteClaim(wire *Wire) {
delete(m.claimsMap, wire)
}
type settings struct {
pool *polynomial.Pool
sorted []*Wire
transcript *fiatshamir.Transcript
transcriptPrefix string
nbVars int
workers *utils.WorkerPool
}
type Option func(*settings)
func WithPool(pool *polynomial.Pool) Option {
return func(options *settings) {
options.pool = pool
}
}
func WithSortedCircuit(sorted []*Wire) Option {
return func(options *settings) {
options.sorted = sorted
}
}
func WithWorkers(workers *utils.WorkerPool) Option {
return func(options *settings) {
options.workers = workers
}
}
// MemoryRequirements returns an increasing vector of memory allocation sizes required for proving a GKR statement
func (c Circuit) MemoryRequirements(nbInstances int) []int {
res := []int{256, nbInstances, nbInstances * (c.maxGateDegree() + 1)}
if res[0] > res[1] { // make sure it's sorted
res[0], res[1] = res[1], res[0]
if res[1] > res[2] {
res[1], res[2] = res[2], res[1]
}
}
return res
}
func setup(c Circuit, assignment WireAssignment, transcriptSettings fiatshamir.Settings, options ...Option) (settings, error) {
var o settings
var err error
for _, option := range options {
option(&o)
}
o.nbVars = assignment.NumVars()
nbInstances := assignment.NumInstances()
if 1<<o.nbVars != nbInstances {
return o, fmt.Errorf("number of instances must be power of 2")
}
if o.pool == nil {
pool := polynomial.NewPool(c.MemoryRequirements(nbInstances)...)
o.pool = &pool
}
if o.workers == nil {
o.workers = utils.NewWorkerPool()
}
if o.sorted == nil {
o.sorted = topologicalSort(c)
}
if transcriptSettings.Transcript == nil {
challengeNames := ChallengeNames(o.sorted, o.nbVars, transcriptSettings.Prefix)
o.transcript = fiatshamir.NewTranscript(transcriptSettings.Hash, challengeNames...)
for i := range transcriptSettings.BaseChallenges {
if err = o.transcript.Bind(challengeNames[0], transcriptSettings.BaseChallenges[i]); err != nil {
return o, err
}
}
} else {
o.transcript, o.transcriptPrefix = transcriptSettings.Transcript, transcriptSettings.Prefix
}
return o, err
}
// ProofSize computes how large the proof for a circuit would be. It needs nbUniqueOutputs to be set
func ProofSize(c Circuit, logNbInstances int) int {
nbUniqueInputs := 0
nbPartialEvalPolys := 0
for i := range c {
nbUniqueInputs += c[i].nbUniqueOutputs // each unique output is manifest in a finalEvalProof entry
if !c[i].noProof() {
nbPartialEvalPolys += c[i].Gate.Degree() + 1
}
}
return nbUniqueInputs + nbPartialEvalPolys*logNbInstances
}
func ChallengeNames(sorted []*Wire, logNbInstances int, prefix string) []string {
// Pre-compute the size TODO: Consider not doing this and just grow the list by appending
size := logNbInstances // first challenge
for _, w := range sorted {
if w.noProof() { // no proof, no challenge
continue
}
if w.NbClaims() > 1 { //combine the claims
size++
}
size += logNbInstances // full run of sumcheck on logNbInstances variables
}
nums := make([]string, utils.Max(len(sorted), logNbInstances))
for i := range nums {
nums[i] = strconv.Itoa(i)
}
challenges := make([]string, size)
// output wire claims
firstChallengePrefix := prefix + "fC."
for j := 0; j < logNbInstances; j++ {
challenges[j] = firstChallengePrefix + nums[j]
}
j := logNbInstances
for i := len(sorted) - 1; i >= 0; i-- {
if sorted[i].noProof() {
continue
}
wirePrefix := prefix + "w" + nums[i] + "."
if sorted[i].NbClaims() > 1 {
challenges[j] = wirePrefix + "comb"
j++
}
partialSumPrefix := wirePrefix + "pSP."
for k := 0; k < logNbInstances; k++ {
challenges[j] = partialSumPrefix + nums[k]
j++
}
}
return challenges
}
func getFirstChallengeNames(logNbInstances int, prefix string) []string {
res := make([]string, logNbInstances)
firstChallengePrefix := prefix + "fC."
for i := 0; i < logNbInstances; i++ {
res[i] = firstChallengePrefix + strconv.Itoa(i)
}
return res
}
func getChallenges(transcript *fiatshamir.Transcript, names []string) ([]fr.Element, error) {
res := make([]fr.Element, len(names))
for i, name := range names {
if bytes, err := transcript.ComputeChallenge(name); err == nil {
res[i].SetBytes(bytes)
} else {
return nil, err
}
}
return res, nil
}
// Prove consistency of the claimed assignment
func Prove(c Circuit, assignment WireAssignment, transcriptSettings fiatshamir.Settings, options ...Option) (Proof, error) {
o, err := setup(c, assignment, transcriptSettings, options...)
if err != nil {
return nil, err
}
defer o.workers.Stop()
claims := newClaimsManager(c, assignment, o)
proof := make(Proof, len(c))
// firstChallenge called rho in the paper
var firstChallenge []fr.Element
firstChallenge, err = getChallenges(o.transcript, getFirstChallengeNames(o.nbVars, o.transcriptPrefix))
if err != nil {
return nil, err
}
wirePrefix := o.transcriptPrefix + "w"
var baseChallenge [][]byte
for i := len(c) - 1; i >= 0; i-- {
wire := o.sorted[i]
if wire.IsOutput() {
claims.add(wire, firstChallenge, assignment[wire].Evaluate(firstChallenge, claims.memPool))
}
claim := claims.getClaim(wire)
if wire.noProof() { // input wires with one claim only
proof[i] = sumcheck.Proof{
PartialSumPolys: []polynomial.Polynomial{},
FinalEvalProof: []fr.Element{},
}
} else {
if proof[i], err = sumcheck.Prove(
claim, fiatshamir.WithTranscript(o.transcript, wirePrefix+strconv.Itoa(i)+".", baseChallenge...),
); err != nil {
return proof, err
}
finalEvalProof := proof[i].FinalEvalProof.([]fr.Element)
baseChallenge = make([][]byte, len(finalEvalProof))
for j := range finalEvalProof {
bytes := finalEvalProof[j].Bytes()
baseChallenge[j] = bytes[:]
}
}
// the verifier checks a single claim about input wires itself
claims.deleteClaim(wire)
}
return proof, nil
}
// Verify the consistency of the claimed output with the claimed input
// Unlike in Prove, the assignment argument need not be complete
func Verify(c Circuit, assignment WireAssignment, proof Proof, transcriptSettings fiatshamir.Settings, options ...Option) error {
o, err := setup(c, assignment, transcriptSettings, options...)
if err != nil {
return err
}
defer o.workers.Stop()
claims := newClaimsManager(c, assignment, o)
var firstChallenge []fr.Element
firstChallenge, err = getChallenges(o.transcript, getFirstChallengeNames(o.nbVars, o.transcriptPrefix))
if err != nil {
return err
}
wirePrefix := o.transcriptPrefix + "w"
var baseChallenge [][]byte
for i := len(c) - 1; i >= 0; i-- {
wire := o.sorted[i]
if wire.IsOutput() {
claims.add(wire, firstChallenge, assignment[wire].Evaluate(firstChallenge, claims.memPool))
}
proofW := proof[i]
finalEvalProof := proofW.FinalEvalProof.([]fr.Element)
claim := claims.getLazyClaim(wire)
if wire.noProof() { // input wires with one claim only
// make sure the proof is empty
if len(finalEvalProof) != 0 || len(proofW.PartialSumPolys) != 0 {
return fmt.Errorf("no proof allowed for input wire with a single claim")
}
if wire.NbClaims() == 1 { // input wire
// simply evaluate and see if it matches
evaluation := assignment[wire].Evaluate(claim.evaluationPoints[0], claims.memPool)
if !claim.claimedEvaluations[0].Equal(&evaluation) {
return fmt.Errorf("incorrect input wire claim")
}
}
} else if err = sumcheck.Verify(
claim, proof[i], fiatshamir.WithTranscript(o.transcript, wirePrefix+strconv.Itoa(i)+".", baseChallenge...),
); err == nil {
baseChallenge = make([][]byte, len(finalEvalProof))
for j := range finalEvalProof {
bytes := finalEvalProof[j].Bytes()
baseChallenge[j] = bytes[:]
}
} else {
return fmt.Errorf("sumcheck proof rejected: %v", err) //TODO: Any polynomials to dump?
}
claims.deleteClaim(wire)
}
return nil
}
// outputsList also sets the nbUniqueOutputs fields. It also sets the wire metadata.
func outputsList(c Circuit, indexes map[*Wire]int) [][]int {
res := make([][]int, len(c))
for i := range c {
res[i] = make([]int, 0)
c[i].nbUniqueOutputs = 0
if c[i].IsInput() {
c[i].Gate = IdentityGate{}
}
}
ins := make(map[int]struct{}, len(c))
for i := range c {
for k := range ins { // clear map
delete(ins, k)
}
for _, in := range c[i].Inputs {
inI := indexes[in]
res[inI] = append(res[inI], i)
if _, ok := ins[inI]; !ok {
in.nbUniqueOutputs++
ins[inI] = struct{}{}
}
}
}
return res
}
type topSortData struct {
outputs [][]int
status []int // status > 0 indicates number of inputs left to be ready. status = 0 means ready. status = -1 means done
index map[*Wire]int
leastReady int
}
func (d *topSortData) markDone(i int) {
d.status[i] = -1
for _, outI := range d.outputs[i] {
d.status[outI]--
if d.status[outI] == 0 && outI < d.leastReady {
d.leastReady = outI
}
}
for d.leastReady < len(d.status) && d.status[d.leastReady] != 0 {
d.leastReady++
}
}
func indexMap(c Circuit) map[*Wire]int {
res := make(map[*Wire]int, len(c))
for i := range c {
res[&c[i]] = i
}
return res
}
func statusList(c Circuit) []int {
res := make([]int, len(c))
for i := range c {
res[i] = len(c[i].Inputs)
}
return res
}
// topologicalSort sorts the wires in order of dependence. Such that for any wire, any one it depends on
// occurs before it. It tries to stick to the input order as much as possible. An already sorted list will remain unchanged.
// It also sets the nbOutput flags, and a dummy IdentityGate for input wires.
// Worst-case inefficient O(n^2), but that probably won't matter since the circuits are small.
// Furthermore, it is efficient with already-close-to-sorted lists, which are the expected input
func topologicalSort(c Circuit) []*Wire {
var data topSortData
data.index = indexMap(c)
data.outputs = outputsList(c, data.index)
data.status = statusList(c)
sorted := make([]*Wire, len(c))
for data.leastReady = 0; data.status[data.leastReady] != 0; data.leastReady++ {
}
for i := range c {
sorted[i] = &c[data.leastReady]
data.markDone(data.leastReady)
}
return sorted
}
// Complete the circuit evaluation from input values
func (a WireAssignment) Complete(c Circuit) WireAssignment {
sortedWires := topologicalSort(c)
nbInstances := a.NumInstances()
maxNbIns := 0
for _, w := range sortedWires {
maxNbIns = utils.Max(maxNbIns, len(w.Inputs))
if a[w] == nil {
a[w] = make([]fr.Element, nbInstances)
}
}
parallel.Execute(nbInstances, func(start, end int) {
ins := make([]fr.Element, maxNbIns)
for i := start; i < end; i++ {
for _, w := range sortedWires {
if !w.IsInput() {
for inI, in := range w.Inputs {
ins[inI] = a[in][i]
}
a[w][i] = w.Gate.Evaluate(ins[:len(w.Inputs)]...)
}
}
}
})
return a
}
func (a WireAssignment) NumInstances() int {
for _, aW := range a {
return len(aW)
}
panic("empty assignment")
}
func (a WireAssignment) NumVars() int {
for _, aW := range a {
return aW.NumVars()
}
panic("empty assignment")
}
// SerializeToBigInts flattens a proof object into the given slice of big.Ints
// useful in gnark hints. TODO: Change propagation: Once this is merged, it will duplicate some code in std/gkr/bn254Prover.go. Remove that in favor of this
func (p Proof) SerializeToBigInts(outs []*big.Int) {
offset := 0
for i := range p {
for _, poly := range p[i].PartialSumPolys {
frToBigInts(outs[offset:], poly)
offset += len(poly)
}
if p[i].FinalEvalProof != nil {
finalEvalProof := p[i].FinalEvalProof.([]fr.Element)
frToBigInts(outs[offset:], finalEvalProof)
offset += len(finalEvalProof)
}
}
}
func frToBigInts(dst []*big.Int, src []fr.Element) {
for i := range src {
src[i].BigInt(dst[i])
}
}
// Gates defined by name
var Gates = map[string]Gate{
"identity": IdentityGate{},
"add": AddGate{},
"sub": SubGate{},
"neg": NegGate{},
"mul": MulGate(2),
}
type IdentityGate struct{}
type AddGate struct{}
type MulGate int
type SubGate struct{}
type NegGate struct{}
func (IdentityGate) Evaluate(input ...fr.Element) fr.Element {
return input[0]
}
func (IdentityGate) Degree() int {
return 1
}
func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) {
switch len(x) {
case 0:
// set zero
case 1:
res.Set(&x[0])
case 2:
res.Add(&x[0], &x[1])
for i := 2; i < len(x); i++ {
res.Add(&res, &x[2])
}
}
return
}
func (g AddGate) Degree() int {
return 1
}
func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) {
if len(x) != int(g) {
panic("wrong input count")
}
switch len(x) {
case 0:
res.SetOne()
case 1:
res.Set(&x[0])
default:
res.Mul(&x[0], &x[1])
for i := 2; i < len(x); i++ {
res.Mul(&res, &x[2])
}
}
return
}
func (g MulGate) Degree() int {
return int(g)
}
func (g SubGate) Evaluate(element ...fr.Element) (diff fr.Element) {
if len(element) > 2 {
panic("not implemented") //TODO
}
diff.Sub(&element[0], &element[1])
return
}
func (g SubGate) Degree() int {
return 1
}
func (g NegGate) Evaluate(element ...fr.Element) (neg fr.Element) {
if len(element) != 1 {
panic("univariate gate")
}
neg.Neg(&element[0])
return
}
func (g NegGate) Degree() int {
return 1
}