@@ -16,8 +16,6 @@ import (
16
16
bw6761 "github.com/consensys/gnark/constraint/bw6-761"
17
17
"github.com/consensys/gnark/test"
18
18
19
- "github.com/consensys/gnark-crypto/kzg"
20
- "github.com/consensys/gnark/backend/plonk"
21
19
bn254 "github.com/consensys/gnark/constraint/bn254"
22
20
"github.com/stretchr/testify/require"
23
21
@@ -26,15 +24,12 @@ import (
26
24
"github.com/consensys/gnark-crypto/ecc/bn254/fr/gkr"
27
25
bn254MiMC "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
28
26
"github.com/consensys/gnark/backend/groth16"
29
- "github.com/consensys/gnark/backend/witness"
30
27
"github.com/consensys/gnark/constraint"
31
28
"github.com/consensys/gnark/frontend"
32
29
"github.com/consensys/gnark/frontend/cs/r1cs"
33
- "github.com/consensys/gnark/frontend/cs/scs"
34
30
stdHash "github.com/consensys/gnark/std/hash"
35
31
"github.com/consensys/gnark/std/hash/mimc"
36
32
test_vector_utils "github.com/consensys/gnark/std/internal/test_vectors_utils"
37
- "github.com/consensys/gnark/test/unsafekzg"
38
33
)
39
34
40
35
// compressThreshold --> if linear expressions are larger than this, the frontend will introduce
@@ -69,6 +64,7 @@ func (c *doubleNoDependencyCircuit) Define(api frontend.API) error {
69
64
}
70
65
71
66
func TestDoubleNoDependencyCircuit (t * testing.T ) {
67
+ assert := test .NewAssert (t )
72
68
73
69
xValuess := [][]frontend.Variable {
74
70
{1 , 1 },
@@ -77,12 +73,13 @@ func TestDoubleNoDependencyCircuit(t *testing.T) {
77
73
78
74
hashes := []string {"-1" , "-20" }
79
75
80
- for _ , xValues := range xValuess {
76
+ for i , xValues := range xValuess {
81
77
for _ , hashName := range hashes {
82
78
assignment := doubleNoDependencyCircuit {X : xValues }
83
79
circuit := doubleNoDependencyCircuit {X : make ([]frontend.Variable , len (xValues )), hashName : hashName }
84
-
85
- test .NewAssert (t ).CheckCircuit (& circuit , test .WithValidAssignment (& assignment ))
80
+ assert .Run (func (assert * test.Assert ) {
81
+ assert .CheckCircuit (& circuit , test .WithValidAssignment (& assignment ), test .WithCurves (ecc .BN254 ))
82
+ }, fmt .Sprintf ("xValue=%d/hash=%s" , i , hashName ))
86
83
87
84
}
88
85
}
@@ -115,6 +112,7 @@ func (c *sqNoDependencyCircuit) Define(api frontend.API) error {
115
112
}
116
113
117
114
func TestSqNoDependencyCircuit (t * testing.T ) {
115
+ assert := test .NewAssert (t )
118
116
119
117
xValuess := [][]frontend.Variable {
120
118
{1 , 1 },
@@ -123,12 +121,13 @@ func TestSqNoDependencyCircuit(t *testing.T) {
123
121
124
122
hashes := []string {"-1" , "-20" }
125
123
126
- for _ , xValues := range xValuess {
124
+ for i , xValues := range xValuess {
127
125
for _ , hashName := range hashes {
128
126
assignment := sqNoDependencyCircuit {X : xValues }
129
127
circuit := sqNoDependencyCircuit {X : make ([]frontend.Variable , len (xValues )), hashName : hashName }
130
- testGroth16 (t , & circuit , & assignment )
131
- testPlonk (t , & circuit , & assignment )
128
+ assert .Run (func (assert * test.Assert ) {
129
+ assert .CheckCircuit (& circuit , test .WithValidAssignment (& assignment ), test .WithCurves (ecc .BN254 ))
130
+ }, fmt .Sprintf ("xValues=%d/hash=%s" , i , hashName ))
132
131
}
133
132
}
134
133
}
@@ -168,6 +167,7 @@ func (c *mulNoDependencyCircuit) Define(api frontend.API) error {
168
167
}
169
168
170
169
func TestMulNoDependency (t * testing.T ) {
170
+ assert := test .NewAssert (t )
171
171
xValuess := [][]frontend.Variable {
172
172
{1 , 2 },
173
173
}
@@ -189,9 +189,9 @@ func TestMulNoDependency(t *testing.T) {
189
189
Y : make ([]frontend.Variable , len (yValuess [i ])),
190
190
hashName : hashName ,
191
191
}
192
-
193
- testGroth16 ( t , & circuit , & assignment )
194
- testPlonk ( t , & circuit , & assignment )
192
+ assert . Run ( func ( assert * test. Assert ) {
193
+ assert . CheckCircuit ( & circuit , test . WithValidAssignment ( & assignment ), test . WithCurves ( ecc . BN254 ) )
194
+ }, fmt . Sprintf ( "xValues=%d/hash=%s" , i , hashName ) )
195
195
}
196
196
}
197
197
}
@@ -240,14 +240,13 @@ func (c *mulWithDependencyCircuit) Define(api frontend.API) error {
240
240
}
241
241
242
242
func TestSolveMulWithDependency (t * testing.T ) {
243
+ assert := test .NewAssert (t )
243
244
assignment := mulWithDependencyCircuit {
244
245
XLast : 1 ,
245
246
Y : []frontend.Variable {3 , 2 },
246
247
}
247
248
circuit := mulWithDependencyCircuit {Y : make ([]frontend.Variable , len (assignment .Y )), hashName : "-20" }
248
-
249
- testGroth16 (t , & circuit , & assignment )
250
- testPlonk (t , & circuit , & assignment )
249
+ assert .CheckCircuit (& circuit , test .WithValidAssignment (& assignment ), test .WithCurves (ecc .BN254 ))
251
250
}
252
251
253
252
func TestApiMul (t * testing.T ) {
@@ -386,53 +385,6 @@ func (c *benchMiMCMerkleTreeCircuit) Define(api frontend.API) error {
386
385
return solution .Verify ("-20" , challenge )
387
386
}
388
387
389
- func testGroth16 (t * testing.T , circuit , assignment frontend.Circuit ) {
390
- cs , err := frontend .Compile (ecc .BN254 .ScalarField (), r1cs .NewBuilder , circuit , frontend .WithCompressThreshold (compressThreshold ))
391
- require .NoError (t , err )
392
- var (
393
- fullWitness witness.Witness
394
- publicWitness witness.Witness
395
- pk groth16.ProvingKey
396
- vk groth16.VerifyingKey
397
- proof groth16.Proof
398
- )
399
- fullWitness , err = frontend .NewWitness (assignment , ecc .BN254 .ScalarField ())
400
- require .NoError (t , err )
401
- publicWitness , err = fullWitness .Public ()
402
- require .NoError (t , err )
403
- pk , vk , err = groth16 .Setup (cs )
404
- require .NoError (t , err )
405
- proof , err = groth16 .Prove (cs , pk , fullWitness )
406
- require .NoError (t , err )
407
- err = groth16 .Verify (proof , vk , publicWitness )
408
- require .NoError (t , err )
409
- }
410
-
411
- func testPlonk (t * testing.T , circuit , assignment frontend.Circuit ) {
412
- cs , err := frontend .Compile (ecc .BN254 .ScalarField (), scs .NewBuilder , circuit , frontend .WithCompressThreshold (compressThreshold ))
413
- require .NoError (t , err )
414
- var (
415
- fullWitness witness.Witness
416
- publicWitness witness.Witness
417
- pk plonk.ProvingKey
418
- vk plonk.VerifyingKey
419
- proof plonk.Proof
420
- kzgSrs kzg.SRS
421
- )
422
- fullWitness , err = frontend .NewWitness (assignment , ecc .BN254 .ScalarField ())
423
- require .NoError (t , err )
424
- publicWitness , err = fullWitness .Public ()
425
- require .NoError (t , err )
426
- kzgSrs , srsLagrange , err := unsafekzg .NewSRS (cs )
427
- require .NoError (t , err )
428
- pk , vk , err = plonk .Setup (cs , kzgSrs , srsLagrange )
429
- require .NoError (t , err )
430
- proof , err = plonk .Prove (cs , pk , fullWitness )
431
- require .NoError (t , err )
432
- err = plonk .Verify (proof , vk , publicWitness )
433
- require .NoError (t , err )
434
- }
435
-
436
388
func registerMiMC () {
437
389
bn254 .RegisterHashBuilder ("mimc" , func () hash.Hash {
438
390
return bn254MiMC .NewMiMC ()
@@ -644,19 +596,21 @@ func BenchmarkMiMCNoGkrFullDepthSolve(b *testing.B) {
644
596
}
645
597
646
598
func TestMiMCFullDepthNoDepSolve (t * testing.T ) {
599
+ assert := test .NewAssert (t )
647
600
registerMiMC ()
648
601
for i := 0 ; i < 100 ; i ++ {
649
602
circuit , assignment := mimcNoDepCircuits (5 , 1 << 2 , "-20" )
650
- testGroth16 (t , circuit , assignment )
651
- testPlonk (t , circuit , assignment )
603
+ assert .Run (func (assert * test.Assert ) {
604
+ assert .CheckCircuit (circuit , test .WithValidAssignment (assignment ), test .WithCurves (ecc .BN254 ))
605
+ }, fmt .Sprintf ("i=%d" , i ))
652
606
}
653
607
}
654
608
655
609
func TestMiMCFullDepthNoDepSolveWithMiMCHash (t * testing.T ) {
610
+ assert := test .NewAssert (t )
656
611
registerMiMC ()
657
612
circuit , assignment := mimcNoDepCircuits (5 , 1 << 2 , "mimc" )
658
- testGroth16 (t , circuit , assignment )
659
- testPlonk (t , circuit , assignment )
613
+ assert .CheckCircuit (circuit , test .WithValidAssignment (assignment ), test .WithCurves (ecc .BN254 ))
660
614
}
661
615
662
616
func mimcNoGkrCircuits (mimcDepth , nbInstances int ) (circuit , assignment frontend.Circuit ) {
@@ -677,3 +631,55 @@ func mimcNoGkrCircuits(mimcDepth, nbInstances int) (circuit, assignment frontend
677
631
}
678
632
return
679
633
}
634
+
635
+ func TestSolveInTestEngine (t * testing.T ) {
636
+ assignment := testSolveInTestEngineCircuit {
637
+ X : []frontend.Variable {2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 },
638
+ }
639
+ circuit := testSolveInTestEngineCircuit {
640
+ X : make ([]frontend.Variable , len (assignment .X )),
641
+ }
642
+
643
+ require .NoError (t , test .IsSolved (& circuit , & assignment , ecc .BN254 .ScalarField ()))
644
+ require .NoError (t , test .IsSolved (& circuit , & assignment , ecc .BLS24_315 .ScalarField ()))
645
+ require .NoError (t , test .IsSolved (& circuit , & assignment , ecc .BLS12_381 .ScalarField ()))
646
+ require .NoError (t , test .IsSolved (& circuit , & assignment , ecc .BLS24_317 .ScalarField ()))
647
+ require .NoError (t , test .IsSolved (& circuit , & assignment , ecc .BW6_633 .ScalarField ()))
648
+ require .NoError (t , test .IsSolved (& circuit , & assignment , ecc .BW6_761 .ScalarField ()))
649
+ require .NoError (t , test .IsSolved (& circuit , & assignment , ecc .BLS12_377 .ScalarField ()))
650
+ }
651
+
652
+ type testSolveInTestEngineCircuit struct {
653
+ X []frontend.Variable
654
+ }
655
+
656
+ func (c * testSolveInTestEngineCircuit ) Define (api frontend.API ) error {
657
+ gkr := NewApi ()
658
+ x , err := gkr .Import (c .X )
659
+ if err != nil {
660
+ return err
661
+ }
662
+ Y := make ([]frontend.Variable , len (c .X ))
663
+ Y [0 ] = 1
664
+ y , err := gkr .Import (Y )
665
+ if err != nil {
666
+ return err
667
+ }
668
+
669
+ z := gkr .Mul (x , y )
670
+
671
+ for i := range len (c .X ) - 1 {
672
+ gkr .Series (y , z , i + 1 , i )
673
+ }
674
+
675
+ assignments := gkr .SolveInTestEngine (api )
676
+
677
+ product := frontend .Variable (1 )
678
+ for i := range c .X {
679
+ api .AssertIsEqual (assignments [y ][i ], product )
680
+ product = api .Mul (product , c .X [i ])
681
+ api .AssertIsEqual (assignments [z ][i ], product )
682
+ }
683
+
684
+ return nil
685
+ }
0 commit comments