@@ -3,8 +3,10 @@ package bls12381
33import (
44 "fmt"
55
6- // "github.com/consensys/gnark-crypto/ecc/bn254/fr"
6+ "github.com/consensys/gnark-crypto/ecc"
7+ "github.com/consensys/gnark/constraint"
78 "github.com/consensys/gnark/frontend"
9+ "github.com/consensys/gnark/frontend/cs/r1cs"
810 "github.com/consensys/gnark/std/algebra/emulated/sw_bn254"
911
1012 "github.com/consensys/gnark/std/algebra/emulated/sw_emulated"
@@ -13,15 +15,30 @@ import (
1315)
1416
1517type Circuit struct {
16- Proof groth16.Proof [sw_bn254.G1Affine , sw_bn254.G2Affine ]
17- VerifyingKey groth16.VerifyingKey [sw_bn254.G1Affine , sw_bn254.G2Affine , sw_bn254.GTEl ]
18- InnerWitness groth16.Witness [sw_bn254.ScalarField ]
19- CommitmentHash frontend.Variable `gnark:",public"`
20- CommitmentX frontend.Variable `gnark:",public"`
21- CommitmentY frontend.Variable `gnark:",public"`
22- InnerInputsHash frontend.Variable `gnark:",public"`
23- // VkHash frontend.Variable `gnark:",public"`
24- // OptimizedInnerWitness frontend.Variable `gnark:",public"`
18+ Proof groth16.Proof [sw_bn254.G1Affine , sw_bn254.G2Affine ]
19+ InnerWitness groth16.Witness [sw_bn254.ScalarField ] `gnark:",public"`
20+ // we are using an embedded constant verifying key since it's easier and doesn't require a vkhash
21+ verifyingKey groth16.VerifyingKey [sw_bn254.G1Affine , sw_bn254.G2Affine , sw_bn254.GTEl ] `gnark:"-"`
22+ CommitmentHash frontend.Variable `gnark:",public"`
23+ CommitmentX frontend.Variable `gnark:",public"`
24+ CommitmentY frontend.Variable `gnark:",public"`
25+ InnerInputsHash frontend.Variable `gnark:",public"`
26+ }
27+
28+ func Compile (
29+ proof groth16.Proof [sw_bn254.G1Affine , sw_bn254.G2Affine ],
30+ innerWitness groth16.Witness [sw_bn254.ScalarField ],
31+ verifyingKey groth16.VerifyingKey [sw_bn254.G1Affine , sw_bn254.G2Affine , sw_bn254.GTEl ],
32+ ) (constraint.ConstraintSystem , error ) {
33+
34+ r1csInstance , err := frontend .Compile (ecc .BLS12_381 .ScalarField (), r1cs .NewBuilder , & Circuit {
35+ Proof : proof ,
36+ verifyingKey : verifyingKey ,
37+ InnerWitness : innerWitness ,
38+ }, frontend .WithCompressThreshold (300 ))
39+
40+ return r1csInstance , err
41+
2542}
2643
2744func (c * Circuit ) Define (api frontend.API ) error {
@@ -30,8 +47,6 @@ func (c *Circuit) Define(api frontend.API) error {
3047 return fmt .Errorf ("new verifier: %w" , err )
3148 }
3249
33- // AssertEq(mimcHash(verifyikgkey.G1.X.Limbs..., verifyingKey.G1.Y.Limbs...), VkHash)
34-
3550 xLimbs := Unpack (api , c .CommitmentX , 256 , 64 )
3651 yLimbs := Unpack (api , c .CommitmentY , 256 , 64 )
3752
@@ -48,7 +63,7 @@ func (c *Circuit) Define(api frontend.API) error {
4863 innerInputsHash := scalarApi .FromBits (api .ToBinary (c .InnerInputsHash )... )
4964 scalarApi .AssertIsEqual (& c .InnerWitness .Public [0 ], innerInputsHash )
5065
51- return verifier .AssertProof (c .VerifyingKey , c .Proof , c .InnerWitness , groth16 .WithCommitmentHash (c .CommitmentHash ))
66+ return verifier .AssertProof (c .verifyingKey , c .Proof , c .InnerWitness , groth16 .WithCommitmentHash (c .CommitmentHash ))
5267}
5368
5469func Unpack (api frontend.API , packed frontend.Variable , sizeOfInput int , sizeOfElem int ) []frontend.Variable {
0 commit comments