@@ -16,8 +16,6 @@ import (
1616 bw6761 "github.com/consensys/gnark/constraint/bw6-761"
1717 "github.com/consensys/gnark/test"
1818
19- "github.com/consensys/gnark-crypto/kzg"
20- "github.com/consensys/gnark/backend/plonk"
2119 bn254 "github.com/consensys/gnark/constraint/bn254"
2220 "github.com/stretchr/testify/require"
2321
@@ -26,15 +24,12 @@ import (
2624 "github.com/consensys/gnark-crypto/ecc/bn254/fr/gkr"
2725 bn254MiMC "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
2826 "github.com/consensys/gnark/backend/groth16"
29- "github.com/consensys/gnark/backend/witness"
3027 "github.com/consensys/gnark/constraint"
3128 "github.com/consensys/gnark/frontend"
3229 "github.com/consensys/gnark/frontend/cs/r1cs"
33- "github.com/consensys/gnark/frontend/cs/scs"
3430 stdHash "github.com/consensys/gnark/std/hash"
3531 "github.com/consensys/gnark/std/hash/mimc"
3632 test_vector_utils "github.com/consensys/gnark/std/internal/test_vectors_utils"
37- "github.com/consensys/gnark/test/unsafekzg"
3833)
3934
4035// compressThreshold --> if linear expressions are larger than this, the frontend will introduce
@@ -69,6 +64,7 @@ func (c *doubleNoDependencyCircuit) Define(api frontend.API) error {
6964}
7065
7166func TestDoubleNoDependencyCircuit (t * testing.T ) {
67+ assert := test .NewAssert (t )
7268
7369 xValuess := [][]frontend.Variable {
7470 {1 , 1 },
@@ -77,12 +73,13 @@ func TestDoubleNoDependencyCircuit(t *testing.T) {
7773
7874 hashes := []string {"-1" , "-20" }
7975
80- for _ , xValues := range xValuess {
76+ for i , xValues := range xValuess {
8177 for _ , hashName := range hashes {
8278 assignment := doubleNoDependencyCircuit {X : xValues }
8379 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 ))
8683
8784 }
8885 }
@@ -115,6 +112,7 @@ func (c *sqNoDependencyCircuit) Define(api frontend.API) error {
115112}
116113
117114func TestSqNoDependencyCircuit (t * testing.T ) {
115+ assert := test .NewAssert (t )
118116
119117 xValuess := [][]frontend.Variable {
120118 {1 , 1 },
@@ -123,12 +121,13 @@ func TestSqNoDependencyCircuit(t *testing.T) {
123121
124122 hashes := []string {"-1" , "-20" }
125123
126- for _ , xValues := range xValuess {
124+ for i , xValues := range xValuess {
127125 for _ , hashName := range hashes {
128126 assignment := sqNoDependencyCircuit {X : xValues }
129127 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 ))
132131 }
133132 }
134133}
@@ -168,6 +167,7 @@ func (c *mulNoDependencyCircuit) Define(api frontend.API) error {
168167}
169168
170169func TestMulNoDependency (t * testing.T ) {
170+ assert := test .NewAssert (t )
171171 xValuess := [][]frontend.Variable {
172172 {1 , 2 },
173173 }
@@ -189,9 +189,9 @@ func TestMulNoDependency(t *testing.T) {
189189 Y : make ([]frontend.Variable , len (yValuess [i ])),
190190 hashName : hashName ,
191191 }
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 ) )
195195 }
196196 }
197197}
@@ -240,14 +240,13 @@ func (c *mulWithDependencyCircuit) Define(api frontend.API) error {
240240}
241241
242242func TestSolveMulWithDependency (t * testing.T ) {
243+ assert := test .NewAssert (t )
243244 assignment := mulWithDependencyCircuit {
244245 XLast : 1 ,
245246 Y : []frontend.Variable {3 , 2 },
246247 }
247248 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 ))
251250}
252251
253252func TestApiMul (t * testing.T ) {
@@ -386,53 +385,6 @@ func (c *benchMiMCMerkleTreeCircuit) Define(api frontend.API) error {
386385 return solution .Verify ("-20" , challenge )
387386}
388387
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-
436388func registerMiMC () {
437389 bn254 .RegisterHashBuilder ("mimc" , func () hash.Hash {
438390 return bn254MiMC .NewMiMC ()
@@ -644,19 +596,21 @@ func BenchmarkMiMCNoGkrFullDepthSolve(b *testing.B) {
644596}
645597
646598func TestMiMCFullDepthNoDepSolve (t * testing.T ) {
599+ assert := test .NewAssert (t )
647600 registerMiMC ()
648601 for i := 0 ; i < 100 ; i ++ {
649602 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 ))
652606 }
653607}
654608
655609func TestMiMCFullDepthNoDepSolveWithMiMCHash (t * testing.T ) {
610+ assert := test .NewAssert (t )
656611 registerMiMC ()
657612 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 ))
660614}
661615
662616func mimcNoGkrCircuits (mimcDepth , nbInstances int ) (circuit , assignment frontend.Circuit ) {
@@ -677,3 +631,55 @@ func mimcNoGkrCircuits(mimcDepth, nbInstances int) (circuit, assignment frontend
677631 }
678632 return
679633}
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