gnark-plonky2-verifier/benchmark.go
benchmark.goRaw
1package main
2
3import (
4 "bytes"
5 "flag"
6 "fmt"
7 "math/big"
8 "os"
9 "time"
10
11 gl "github.com/succinctlabs/gnark-plonky2-verifier/goldilocks"
12 "github.com/succinctlabs/gnark-plonky2-verifier/types"
13 "github.com/succinctlabs/gnark-plonky2-verifier/verifier"
14
15 "github.com/consensys/gnark-crypto/ecc"
16 "github.com/consensys/gnark/backend/groth16"
17 "github.com/consensys/gnark/constraint"
18 "github.com/consensys/gnark/frontend"
19 "github.com/consensys/gnark/frontend/cs/r1cs"
20 "github.com/consensys/gnark/profile"
21)
22
23type BenchmarkPlonky2VerifierCircuit struct {
24 Proof types.Proof
25 PublicInputs []gl.GoldilocksVariable `gnark:",public"`
26
27 verifierChip *verifier.VerifierChip `gnark:"-"`
28 plonky2CircuitName string `gnark:"-"`
29}
30
31func (circuit *BenchmarkPlonky2VerifierCircuit) Define(api frontend.API) error {
32 circuitDirname := "./verifier/data/" + circuit.plonky2CircuitName + "/"
33 commonCircuitData := verifier.DeserializeCommonCircuitData(circuitDirname + "common_circuit_data.json")
34 verifierOnlyCircuitData := verifier.DeserializeVerifierOnlyCircuitData(circuitDirname + "verifier_only_circuit_data.json")
35
36 circuit.verifierChip = verifier.NewVerifierChip(api, commonCircuitData)
37
38 circuit.verifierChip.Verify(circuit.Proof, circuit.PublicInputs, verifierOnlyCircuitData, commonCircuitData)
39
40 return nil
41}
42
43func compileCircuit(plonky2Circuit string, profileCircuit bool, serialize bool, outputSolidity bool) (constraint.ConstraintSystem, groth16.ProvingKey, groth16.VerifyingKey) {
44 circuit := BenchmarkPlonky2VerifierCircuit{
45 plonky2CircuitName: plonky2Circuit,
46 }
47 proofWithPis := verifier.DeserializeProofWithPublicInputs("./verifier/data/" + plonky2Circuit + "/proof_with_public_inputs.json")
48 circuit.Proof = proofWithPis.Proof
49 circuit.PublicInputs = proofWithPis.PublicInputs
50
51 var p *profile.Profile
52 if profileCircuit {
53 p = profile.Start()
54 }
55 r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
56 if err != nil {
57 fmt.Println("error in building circuit", err)
58 os.Exit(1)
59 }
60
61 if profileCircuit {
62 p.Stop()
63 p.Top()
64 println("r1cs.GetNbCoefficients(): ", r1cs.GetNbCoefficients())
65 println("r1cs.GetNbConstraints(): ", r1cs.GetNbConstraints())
66 println("r1cs.GetNbSecretVariables(): ", r1cs.GetNbSecretVariables())
67 println("r1cs.GetNbPublicVariables(): ", r1cs.GetNbPublicVariables())
68 println("r1cs.GetNbInternalVariables(): ", r1cs.GetNbInternalVariables())
69 }
70
71 // Don't serialize the circuit for now, since it takes up too much memory
72 /*
73 if serialize {
74 fR1CS, _ := os.Create("circuit")
75 r1cs.WriteTo(fR1CS)
76 fR1CS.Close()
77 }
78 */
79
80 fmt.Println("Running circuit setup", time.Now())
81 pk, vk, err := groth16.Setup(r1cs)
82 if err != nil {
83 fmt.Println(err)
84 os.Exit(1)
85 }
86
87 if serialize {
88 fPK, _ := os.Create("proving.key")
89 pk.WriteTo(fPK)
90 fPK.Close()
91
92 fVK, _ := os.Create("verifying.key")
93 vk.WriteTo(fVK)
94 fVK.Close()
95 }
96
97 if outputSolidity {
98 fSolidity, _ := os.Create("proof.sol")
99 err = vk.ExportSolidity(fSolidity)
100 }
101
102 return r1cs, pk, vk
103}
104
105func createProof(plonky2Circuit string, r1cs constraint.ConstraintSystem, pk groth16.ProvingKey, vk groth16.VerifyingKey, serialize bool) groth16.Proof {
106 proofWithPis := verifier.DeserializeProofWithPublicInputs("./verifier/data/" + plonky2Circuit + "/proof_with_public_inputs.json")
107
108 // Witness
109 assignment := &BenchmarkPlonky2VerifierCircuit{
110 Proof: proofWithPis.Proof,
111 PublicInputs: proofWithPis.PublicInputs,
112 }
113
114 fmt.Println("Generating witness", time.Now())
115 witness, _ := frontend.NewWitness(assignment, ecc.BN254.ScalarField())
116 publicWitness, _ := witness.Public()
117 if serialize {
118 fWitness, _ := os.Create("witness")
119 witness.WriteTo(fWitness)
120 fWitness.Close()
121 }
122
123 fmt.Println("Creating proof", time.Now())
124 proof, err := groth16.Prove(r1cs, pk, witness)
125 if err != nil {
126 fmt.Println(err)
127 os.Exit(1)
128 }
129 if serialize {
130 fProof, _ := os.Create("proof.proof")
131 proof.WriteTo(fProof)
132 fProof.Close()
133 }
134
135 fmt.Println("Verifying proof", time.Now())
136 err = groth16.Verify(proof, vk, publicWitness)
137 if err != nil {
138 fmt.Println(err)
139 os.Exit(1)
140 }
141
142 const fpSize = 4 * 8
143 var buf bytes.Buffer
144 proof.WriteRawTo(&buf)
145 proofBytes := buf.Bytes()
146
147 var (
148 a [2]*big.Int
149 b [2][2]*big.Int
150 c [2]*big.Int
151 )
152
153 // proof.Ar, proof.Bs, proof.Krs
154 a[0] = new(big.Int).SetBytes(proofBytes[fpSize*0 : fpSize*1])
155 a[1] = new(big.Int).SetBytes(proofBytes[fpSize*1 : fpSize*2])
156 b[0][0] = new(big.Int).SetBytes(proofBytes[fpSize*2 : fpSize*3])
157 b[0][1] = new(big.Int).SetBytes(proofBytes[fpSize*3 : fpSize*4])
158 b[1][0] = new(big.Int).SetBytes(proofBytes[fpSize*4 : fpSize*5])
159 b[1][1] = new(big.Int).SetBytes(proofBytes[fpSize*5 : fpSize*6])
160 c[0] = new(big.Int).SetBytes(proofBytes[fpSize*6 : fpSize*7])
161 c[1] = new(big.Int).SetBytes(proofBytes[fpSize*7 : fpSize*8])
162
163 println("a[0] is ", a[0].String())
164 println("a[1] is ", a[1].String())
165
166 println("b[0][0] is ", b[0][0].String())
167 println("b[0][1] is ", b[0][1].String())
168 println("b[1][0] is ", b[1][0].String())
169 println("b[1][1] is ", b[1][1].String())
170
171 println("c[0] is ", c[0].String())
172 println("c[1] is ", c[1].String())
173
174 return proof
175}
176
177func main() {
178 plonky2Circuit := flag.String("plonky2-circuit", "", "plonky2 circuit to benchmark")
179 profileCircuit := flag.Bool("profile", false, "profile the circuit")
180 serialize := flag.Bool("serialize", false, "serialize the circuit")
181 outputSolidity := flag.Bool("solidity", false, "output solidity code for the circuit")
182
183 flag.Parse()
184
185 if plonky2Circuit == nil || *plonky2Circuit == "" {
186 fmt.Println("Please provide a plonky2 circuit to benchmark")
187 os.Exit(1)
188 }
189
190 r1cs, pk, vk := compileCircuit(*plonky2Circuit, *profileCircuit, *serialize, *outputSolidity)
191 createProof(*plonky2Circuit, r1cs, pk, vk, *serialize)
192}