-
Notifications
You must be signed in to change notification settings - Fork 365
/
groth16.go
441 lines (409 loc) · 13.7 KB
/
groth16.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2020 ConsenSys AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package groth16 implements Groth16 Zero Knowledge Proof system (aka zkSNARK).
//
// # See also
//
// https://eprint.iacr.org/2016/260.pdf
package groth16
import (
"io"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/witness"
"github.com/consensys/gnark/constraint"
cs_bls12377 "github.com/consensys/gnark/constraint/bls12-377"
cs_bls12381 "github.com/consensys/gnark/constraint/bls12-381"
cs_bls24315 "github.com/consensys/gnark/constraint/bls24-315"
cs_bls24317 "github.com/consensys/gnark/constraint/bls24-317"
cs_bn254 "github.com/consensys/gnark/constraint/bn254"
cs_bw6633 "github.com/consensys/gnark/constraint/bw6-633"
cs_bw6761 "github.com/consensys/gnark/constraint/bw6-761"
fr_bls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
fr_bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
fr_bls24315 "github.com/consensys/gnark-crypto/ecc/bls24-315/fr"
fr_bls24317 "github.com/consensys/gnark-crypto/ecc/bls24-317/fr"
fr_bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr"
fr_bw6633 "github.com/consensys/gnark-crypto/ecc/bw6-633/fr"
fr_bw6761 "github.com/consensys/gnark-crypto/ecc/bw6-761/fr"
gnarkio "github.com/consensys/gnark/io"
groth16_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/groth16"
groth16_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/groth16"
groth16_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/groth16"
groth16_bls24317 "github.com/consensys/gnark/internal/backend/bls24-317/groth16"
groth16_bn254 "github.com/consensys/gnark/internal/backend/bn254/groth16"
groth16_bw6633 "github.com/consensys/gnark/internal/backend/bw6-633/groth16"
groth16_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/groth16"
)
type groth16Object interface {
gnarkio.WriterRawTo
io.WriterTo
io.ReaderFrom
CurveID() ecc.ID
}
// Proof represents a Groth16 proof generated by groth16.Prove
//
// it's underlying implementation is curve specific (see gnark/internal/backend)
type Proof interface {
groth16Object
}
// ProvingKey represents a Groth16 ProvingKey
//
// it's underlying implementation is strongly typed with the curve (see gnark/internal/backend)
type ProvingKey interface {
groth16Object
gnarkio.UnsafeReaderFrom
// NbG1 returns the number of G1 elements in the ProvingKey
NbG1() int
// NbG2 returns the number of G2 elements in the ProvingKey
NbG2() int
IsDifferent(interface{}) bool
}
// VerifyingKey represents a Groth16 VerifyingKey
//
// it's underlying implementation is strongly typed with the curve (see gnark/internal/backend)
//
// ExportSolidity is implemented for BN254 and will return an error with other curves
type VerifyingKey interface {
groth16Object
gnarkio.UnsafeReaderFrom
// NbPublicWitness returns number of elements expected in the public witness
NbPublicWitness() int
// NbG1 returns the number of G1 elements in the VerifyingKey
NbG1() int
// NbG2 returns the number of G2 elements in the VerifyingKey
NbG2() int
// ExportSolidity writes a solidity Verifier contract from the VerifyingKey
// this will return an error if not supported on the CurveID()
ExportSolidity(w io.Writer) error
IsDifferent(interface{}) bool
}
// Verify runs the groth16.Verify algorithm on provided proof with given witness
func Verify(proof Proof, vk VerifyingKey, publicWitness witness.Witness) error {
switch _proof := proof.(type) {
case *groth16_bls12377.Proof:
w, ok := publicWitness.Vector().(fr_bls12377.Vector)
if !ok {
return witness.ErrInvalidWitness
}
return groth16_bls12377.Verify(_proof, vk.(*groth16_bls12377.VerifyingKey), w)
case *groth16_bls12381.Proof:
w, ok := publicWitness.Vector().(fr_bls12381.Vector)
if !ok {
return witness.ErrInvalidWitness
}
return groth16_bls12381.Verify(_proof, vk.(*groth16_bls12381.VerifyingKey), w)
case *groth16_bn254.Proof:
w, ok := publicWitness.Vector().(fr_bn254.Vector)
if !ok {
return witness.ErrInvalidWitness
}
return groth16_bn254.Verify(_proof, vk.(*groth16_bn254.VerifyingKey), w)
case *groth16_bw6761.Proof:
w, ok := publicWitness.Vector().(fr_bw6761.Vector)
if !ok {
return witness.ErrInvalidWitness
}
return groth16_bw6761.Verify(_proof, vk.(*groth16_bw6761.VerifyingKey), w)
case *groth16_bls24317.Proof:
w, ok := publicWitness.Vector().(fr_bls24317.Vector)
if !ok {
return witness.ErrInvalidWitness
}
return groth16_bls24317.Verify(_proof, vk.(*groth16_bls24317.VerifyingKey), w)
case *groth16_bls24315.Proof:
w, ok := publicWitness.Vector().(fr_bls24315.Vector)
if !ok {
return witness.ErrInvalidWitness
}
return groth16_bls24315.Verify(_proof, vk.(*groth16_bls24315.VerifyingKey), w)
case *groth16_bw6633.Proof:
w, ok := publicWitness.Vector().(fr_bw6633.Vector)
if !ok {
return witness.ErrInvalidWitness
}
return groth16_bw6633.Verify(_proof, vk.(*groth16_bw6633.VerifyingKey), w)
default:
panic("unrecognized R1CS curve type")
}
}
// Prove runs the groth16.Prove algorithm.
//
// if the force flag is set:
//
// will execute all the prover computations, even if the witness is invalid
// will produce an invalid proof
// internally, the solution vector to the R1CS will be filled with random values which may impact benchmarking
func Prove(r1cs constraint.ConstraintSystem, pk ProvingKey, fullWitness witness.Witness, opts ...backend.ProverOption) (Proof, error) {
// apply options
opt, err := backend.NewProverConfig(opts...)
if err != nil {
return nil, err
}
switch _r1cs := r1cs.(type) {
case *cs_bls12377.R1CS:
w, ok := fullWitness.Vector().(fr_bls12377.Vector)
if !ok {
return nil, witness.ErrInvalidWitness
}
return groth16_bls12377.Prove(_r1cs, pk.(*groth16_bls12377.ProvingKey), w, opt)
case *cs_bls12381.R1CS:
w, ok := fullWitness.Vector().(fr_bls12381.Vector)
if !ok {
return nil, witness.ErrInvalidWitness
}
return groth16_bls12381.Prove(_r1cs, pk.(*groth16_bls12381.ProvingKey), w, opt)
case *cs_bn254.R1CS:
w, ok := fullWitness.Vector().(fr_bn254.Vector)
if !ok {
return nil, witness.ErrInvalidWitness
}
return groth16_bn254.Prove(_r1cs, pk.(*groth16_bn254.ProvingKey), w, opt)
case *cs_bw6761.R1CS:
w, ok := fullWitness.Vector().(fr_bw6761.Vector)
if !ok {
return nil, witness.ErrInvalidWitness
}
return groth16_bw6761.Prove(_r1cs, pk.(*groth16_bw6761.ProvingKey), w, opt)
case *cs_bls24317.R1CS:
w, ok := fullWitness.Vector().(fr_bls24317.Vector)
if !ok {
return nil, witness.ErrInvalidWitness
}
return groth16_bls24317.Prove(_r1cs, pk.(*groth16_bls24317.ProvingKey), w, opt)
case *cs_bls24315.R1CS:
w, ok := fullWitness.Vector().(fr_bls24315.Vector)
if !ok {
return nil, witness.ErrInvalidWitness
}
return groth16_bls24315.Prove(_r1cs, pk.(*groth16_bls24315.ProvingKey), w, opt)
case *cs_bw6633.R1CS:
w, ok := fullWitness.Vector().(fr_bw6633.Vector)
if !ok {
return nil, witness.ErrInvalidWitness
}
return groth16_bw6633.Prove(_r1cs, pk.(*groth16_bw6633.ProvingKey), w, opt)
default:
panic("unrecognized R1CS curve type")
}
}
// Setup runs groth16.Setup with provided R1CS and outputs a key pair associated with the circuit.
//
// Note that careful consideration must be given to this step in production environment.
// groth16.Setup uses some randomness to precompute the Proving and Verifying keys. If the process
// or machine leaks this randomness, an attacker could break the ZKP protocol.
//
// Two main solutions to this deployment issues are: running the Setup through a MPC (multi party computation)
// or using a ZKP backend like PLONK where the per-circuit Setup is deterministic.
func Setup(r1cs constraint.ConstraintSystem) (ProvingKey, VerifyingKey, error) {
switch _r1cs := r1cs.(type) {
case *cs_bls12377.R1CS:
var pk groth16_bls12377.ProvingKey
var vk groth16_bls12377.VerifyingKey
if err := groth16_bls12377.Setup(_r1cs, &pk, &vk); err != nil {
return nil, nil, err
}
return &pk, &vk, nil
case *cs_bls12381.R1CS:
var pk groth16_bls12381.ProvingKey
var vk groth16_bls12381.VerifyingKey
if err := groth16_bls12381.Setup(_r1cs, &pk, &vk); err != nil {
return nil, nil, err
}
return &pk, &vk, nil
case *cs_bn254.R1CS:
var pk groth16_bn254.ProvingKey
var vk groth16_bn254.VerifyingKey
if err := groth16_bn254.Setup(_r1cs, &pk, &vk); err != nil {
return nil, nil, err
}
return &pk, &vk, nil
case *cs_bw6761.R1CS:
var pk groth16_bw6761.ProvingKey
var vk groth16_bw6761.VerifyingKey
if err := groth16_bw6761.Setup(_r1cs, &pk, &vk); err != nil {
return nil, nil, err
}
return &pk, &vk, nil
case *cs_bls24317.R1CS:
var pk groth16_bls24317.ProvingKey
var vk groth16_bls24317.VerifyingKey
if err := groth16_bls24317.Setup(_r1cs, &pk, &vk); err != nil {
return nil, nil, err
}
return &pk, &vk, nil
case *cs_bls24315.R1CS:
var pk groth16_bls24315.ProvingKey
var vk groth16_bls24315.VerifyingKey
if err := groth16_bls24315.Setup(_r1cs, &pk, &vk); err != nil {
return nil, nil, err
}
return &pk, &vk, nil
case *cs_bw6633.R1CS:
var pk groth16_bw6633.ProvingKey
var vk groth16_bw6633.VerifyingKey
if err := groth16_bw6633.Setup(_r1cs, &pk, &vk); err != nil {
return nil, nil, err
}
return &pk, &vk, nil
default:
panic("unrecognized R1CS curve type")
}
}
// DummySetup create a random ProvingKey with provided R1CS
// it doesn't return a VerifyingKey and is use for benchmarking or test purposes only.
func DummySetup(r1cs constraint.ConstraintSystem) (ProvingKey, error) {
switch _r1cs := r1cs.(type) {
case *cs_bls12377.R1CS:
var pk groth16_bls12377.ProvingKey
if err := groth16_bls12377.DummySetup(_r1cs, &pk); err != nil {
return nil, err
}
return &pk, nil
case *cs_bls12381.R1CS:
var pk groth16_bls12381.ProvingKey
if err := groth16_bls12381.DummySetup(_r1cs, &pk); err != nil {
return nil, err
}
return &pk, nil
case *cs_bn254.R1CS:
var pk groth16_bn254.ProvingKey
if err := groth16_bn254.DummySetup(_r1cs, &pk); err != nil {
return nil, err
}
return &pk, nil
case *cs_bw6761.R1CS:
var pk groth16_bw6761.ProvingKey
if err := groth16_bw6761.DummySetup(_r1cs, &pk); err != nil {
return nil, err
}
return &pk, nil
case *cs_bls24317.R1CS:
var pk groth16_bls24317.ProvingKey
if err := groth16_bls24317.DummySetup(_r1cs, &pk); err != nil {
return nil, err
}
return &pk, nil
case *cs_bls24315.R1CS:
var pk groth16_bls24315.ProvingKey
if err := groth16_bls24315.DummySetup(_r1cs, &pk); err != nil {
return nil, err
}
return &pk, nil
case *cs_bw6633.R1CS:
var pk groth16_bw6633.ProvingKey
if err := groth16_bw6633.DummySetup(_r1cs, &pk); err != nil {
return nil, err
}
return &pk, nil
default:
panic("unrecognized R1CS curve type")
}
}
// NewProvingKey instantiates a curve-typed ProvingKey and returns an interface object
// This function exists for serialization purposes
func NewProvingKey(curveID ecc.ID) ProvingKey {
var pk ProvingKey
switch curveID {
case ecc.BN254:
pk = &groth16_bn254.ProvingKey{}
case ecc.BLS12_377:
pk = &groth16_bls12377.ProvingKey{}
case ecc.BLS12_381:
pk = &groth16_bls12381.ProvingKey{}
case ecc.BW6_761:
pk = &groth16_bw6761.ProvingKey{}
case ecc.BLS24_317:
pk = &groth16_bls24317.ProvingKey{}
case ecc.BLS24_315:
pk = &groth16_bls24315.ProvingKey{}
case ecc.BW6_633:
pk = &groth16_bw6633.ProvingKey{}
default:
panic("not implemented")
}
return pk
}
// NewVerifyingKey instantiates a curve-typed VerifyingKey and returns an interface
// This function exists for serialization purposes
func NewVerifyingKey(curveID ecc.ID) VerifyingKey {
var vk VerifyingKey
switch curveID {
case ecc.BN254:
vk = &groth16_bn254.VerifyingKey{}
case ecc.BLS12_377:
vk = &groth16_bls12377.VerifyingKey{}
case ecc.BLS12_381:
vk = &groth16_bls12381.VerifyingKey{}
case ecc.BW6_761:
vk = &groth16_bw6761.VerifyingKey{}
case ecc.BLS24_317:
vk = &groth16_bls24317.VerifyingKey{}
case ecc.BLS24_315:
vk = &groth16_bls24315.VerifyingKey{}
case ecc.BW6_633:
vk = &groth16_bw6633.VerifyingKey{}
default:
panic("not implemented")
}
return vk
}
// NewProof instantiates a curve-typed Proof and returns an interface
// This function exists for serialization purposes
func NewProof(curveID ecc.ID) Proof {
var proof Proof
switch curveID {
case ecc.BN254:
proof = &groth16_bn254.Proof{}
case ecc.BLS12_377:
proof = &groth16_bls12377.Proof{}
case ecc.BLS12_381:
proof = &groth16_bls12381.Proof{}
case ecc.BW6_761:
proof = &groth16_bw6761.Proof{}
case ecc.BLS24_317:
proof = &groth16_bls24317.Proof{}
case ecc.BLS24_315:
proof = &groth16_bls24315.Proof{}
case ecc.BW6_633:
proof = &groth16_bw6633.Proof{}
default:
panic("not implemented")
}
return proof
}
// NewCS instantiate a concrete curved-typed R1CS and return a R1CS interface
// This method exists for (de)serialization purposes
func NewCS(curveID ecc.ID) constraint.ConstraintSystem {
var r1cs constraint.ConstraintSystem
switch curveID {
case ecc.BN254:
r1cs = &cs_bn254.R1CS{}
case ecc.BLS12_377:
r1cs = &cs_bls12377.R1CS{}
case ecc.BLS12_381:
r1cs = &cs_bls12381.R1CS{}
case ecc.BW6_761:
r1cs = &cs_bw6761.R1CS{}
case ecc.BLS24_317:
r1cs = &cs_bls24317.R1CS{}
case ecc.BLS24_315:
r1cs = &cs_bls24315.R1CS{}
case ecc.BW6_633:
r1cs = &cs_bw6633.R1CS{}
default:
panic("not implemented")
}
return r1cs
}