-
Notifications
You must be signed in to change notification settings - Fork 0
/
prover.go
336 lines (263 loc) · 8.85 KB
/
prover.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
package worker
import (
"fmt"
"os"
"path/filepath"
"github.com/cf/gnark-plonky2-verifier/types"
"github.com/cf/gnark-plonky2-verifier/variables"
"github.com/cf/gnark-plonky2-verifier/verifier"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/backend/witness"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
)
func NewGroth16Prover(Path string, curveId ecc.ID) (*Groth16Prover, error) {
pk := groth16.NewProvingKey(curveId)
vk := groth16.NewVerifyingKey(curveId)
proof := groth16.NewProof(curveId)
witness, err := witness.New(curveId.ScalarField())
if err != nil {
return nil, fmt.Errorf("error creating public witness: %w", err)
}
r1cs := groth16.NewCS(curveId)
// PublicWitness: publicWitness
return &Groth16Prover{Path: Path, pk: pk, vk: vk, Proof: proof, Witness: witness, curveId: curveId, r1cs: r1cs}, nil
}
func (w *Groth16Prover) CheckPath() error {
files := []string{
"common_circuit_data.json",
"proof_with_public_inputs.json",
"verifier_only_circuit_data.json",
}
for _, file := range files {
path := filepath.Join(w.Path, file)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("path %s does not exist", path)
} else {
return fmt.Errorf("error checking path %s: %s", path, err)
}
}
}
return nil
}
func (w *Groth16Prover) Setup() error {
if err := w.CheckPath(); err != nil {
return fmt.Errorf("failed to check path: %w", err)
}
commonCircuitData := types.ReadCommonCircuitData(filepath.Join(w.Path, "common_circuit_data.json"))
proofWithPis := variables.DeserializeProofWithPublicInputs(types.ReadProofWithPublicInputs(filepath.Join(w.Path, "proof_with_public_inputs.json")))
verifierOnlyCircuitData := variables.DeserializeVerifierOnlyCircuitData(types.ReadVerifierOnlyCircuitData(filepath.Join(w.Path, "verifier_only_circuit_data.json")))
circuit := verifier.ExampleVerifierCircuit{
Proof: proofWithPis.Proof,
PublicInputs: proofWithPis.PublicInputs,
VerifierOnlyCircuitData: verifierOnlyCircuitData,
CommonCircuitData: commonCircuitData,
}
builder := r1cs.NewBuilder
r1cs, err := frontend.Compile(w.curveId.ScalarField(), builder, &circuit)
if err != nil {
return fmt.Errorf("failed to compile circuit: %w", err)
}
w.r1cs = r1cs
var errSetup error
w.pk, w.vk, errSetup = groth16.Setup(r1cs)
if errSetup != nil {
return fmt.Errorf("error in setting up circuit: %w", errSetup)
}
return nil
}
func (w *Groth16Prover) GenerateWitness() error {
// https://github.com/Consensys/gnark/issues/1038
// error in generating witness: can't set fr.Element from type expr.LinearExpression
proofWithPis := variables.DeserializeProofWithPublicInputs(types.ReadProofWithPublicInputs(filepath.Join(w.Path, "proof_with_public_inputs.json")))
verifierOnlyCircuitData := variables.DeserializeVerifierOnlyCircuitData(types.ReadVerifierOnlyCircuitData(filepath.Join(w.Path, "verifier_only_circuit_data.json")))
assignment := verifier.ExampleVerifierCircuit{
Proof: proofWithPis.Proof,
PublicInputs: proofWithPis.PublicInputs,
VerifierOnlyCircuitData: verifierOnlyCircuitData,
}
witness, err := frontend.NewWitness(&assignment, w.curveId.ScalarField())
if err != nil {
return fmt.Errorf("error in generating witness: %s", err)
}
w.Witness = witness
return nil
}
func (w *Groth16Prover) GenerateR1CS() error {
commonCircuitData := types.ReadCommonCircuitData(filepath.Join(w.Path, "common_circuit_data.json"))
proofWithPis := variables.DeserializeProofWithPublicInputs(types.ReadProofWithPublicInputs(filepath.Join(w.Path, "proof_with_public_inputs.json")))
verifierOnlyCircuitData := variables.DeserializeVerifierOnlyCircuitData(types.ReadVerifierOnlyCircuitData(filepath.Join(w.Path, "verifier_only_circuit_data.json")))
circuit := verifier.ExampleVerifierCircuit{
Proof: proofWithPis.Proof,
PublicInputs: proofWithPis.PublicInputs,
VerifierOnlyCircuitData: verifierOnlyCircuitData,
CommonCircuitData: commonCircuitData,
}
builder := r1cs.NewBuilder
r1cs, err := frontend.Compile(ecc.BLS12_381.ScalarField(), builder, &circuit)
if err != nil {
return fmt.Errorf("failed to compile circuit: %w", err)
}
w.r1cs = r1cs
return nil
}
func (w *Groth16Prover) Prove() error {
proof, err := groth16.Prove(w.r1cs, w.pk, w.Witness)
if err != nil {
return fmt.Errorf("error in creating proof: %s", err)
}
w.Proof = proof
return nil
}
func (w *Groth16Prover) ReadProvingKey(keyPath string) error {
if w.pk == nil {
return fmt.Errorf("proving key is not initialized")
}
provingKeyFile, err := os.Open(keyPath)
if err != nil {
return fmt.Errorf("failed to open proving key file: %w", err)
}
defer provingKeyFile.Close()
_, err = w.pk.ReadFrom(provingKeyFile)
if err != nil {
return fmt.Errorf("failed to read proving key: %w", err)
}
return nil
}
func (w *Groth16Prover) WriteProvingKey(keyPath string) error {
if w.pk == nil {
return fmt.Errorf("proving key is not initialized")
}
fPK, err := os.Create(keyPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer fPK.Close()
if _, err := w.pk.WriteRawTo(fPK); err != nil {
return fmt.Errorf("failed to write verifying key to file: %w", err)
}
return nil
}
func (w *Groth16Prover) ReadProof(keyPath string) error {
if w.Proof == nil {
return fmt.Errorf("proof is not initialized")
}
proofFile, err := os.Open(keyPath)
if err != nil {
return fmt.Errorf("failed to open proof file: %w", err)
}
defer proofFile.Close()
_, err = w.Proof.ReadFrom(proofFile)
if err != nil {
return fmt.Errorf("failed to read proof: %w", err)
}
return nil
}
func (w *Groth16Prover) WriteProof(keyPath string) error {
if w.Proof == nil {
return fmt.Errorf("verifier or proof is not initialized")
}
proofFile, err := os.Create(keyPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer proofFile.Close()
if _, err := w.Proof.WriteTo(proofFile); err != nil {
return fmt.Errorf("failed to write proof to file: %w", err)
}
return nil
}
// func (w *Groth16Prover) ReadPublicInputs(inputPath string) error {
// if w.Witness == nil {
// return fmt.Errorf("public inputs is not initialized")
// }
// publicinputsFile, err := os.Open(inputPath)
// if err != nil {
// return fmt.Errorf("failed to open public inputs file: %w", err)
// }
// defer publicinputsFile.Close()
// publicwitness, err := w.Witness.Public()
// _, err = publicwitness.ReadFrom(publicinputsFile)
// if err != nil {
// return fmt.Errorf("failed to read public inputs: %w", err)
// }
// return nil
// }
func (w *Groth16Prover) WritePublicInputs(inputPath string) error {
if w.Witness == nil {
return fmt.Errorf("public inputs is not initialized")
}
publicinputsFile, err := os.Create(inputPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer publicinputsFile.Close()
publicWitness, err := w.Witness.Public()
if err != nil {
return fmt.Errorf("failed to create publicWitness: %w", err)
}
if _, err := publicWitness.WriteTo(publicinputsFile); err != nil {
return fmt.Errorf("failed to write proof to file: %w", err)
}
return nil
}
func (w *Groth16Prover) ReadVerifyingKey(keyPath string) error {
if w.vk == nil {
return fmt.Errorf("verifying key is not initialized")
}
verifyingKeyFile, err := os.Open(keyPath)
if err != nil {
return fmt.Errorf("failed to open verifying key file: %w", err)
}
defer verifyingKeyFile.Close()
_, err = w.vk.ReadFrom(verifyingKeyFile)
if err != nil {
return fmt.Errorf("failed to read verifying key: %w", err)
}
return nil
}
func (w *Groth16Prover) WriteVerifyingKey(keyPath string) error {
if w.vk == nil {
return fmt.Errorf("verifying key is not initialized")
}
fVK, err := os.Create(keyPath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer fVK.Close()
if _, err := w.vk.WriteTo(fVK); err != nil {
return fmt.Errorf("failed to write verifying key to file: %w", err)
}
return nil
}
func (w *Groth16Prover) ReadCircuit(keyPath string) error {
if w.r1cs == nil {
return fmt.Errorf("r1cs is not initialized")
}
circuitFile, err := os.Open(keyPath)
if err != nil {
return fmt.Errorf("failed to open circuit file: %w", err)
}
defer circuitFile.Close()
_, err = w.r1cs.ReadFrom(circuitFile)
if err != nil {
return fmt.Errorf("failed to read circuit: %w", err)
}
return nil
}
func (w *Groth16Prover) WriteCircuit(keyPath string) error {
if w.r1cs == nil {
return fmt.Errorf("r1cs is not initialized")
}
circuitFile, err := os.Create(keyPath)
if err != nil {
return fmt.Errorf("failed to circuit file: %w", err)
}
defer circuitFile.Close()
if _, err := w.r1cs.WriteTo(circuitFile); err != nil {
return fmt.Errorf("failed to write circuit to file: %w", err)
}
return nil
}