forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
509 lines (437 loc) · 16.4 KB
/
builder.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
Copyright © 2020 ConsenSys
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 r1cs
import (
"errors"
"math/big"
"reflect"
"sort"
"github.com/aakash4dev/gnark2/constraint"
"github.com/aakash4dev/gnark2/debug"
"github.com/aakash4dev/gnark2/frontend"
"github.com/aakash4dev/gnark2/frontend/internal/expr"
"github.com/aakash4dev/gnark2/frontend/schema"
"github.com/aakash4dev/gnark2/internal/circuitdefer"
"github.com/aakash4dev/gnark2/internal/frontendtype"
"github.com/aakash4dev/gnark2/internal/kvstore"
"github.com/aakash4dev/gnark2/internal/tinyfield"
"github.com/aakash4dev/gnark2/internal/utils"
"github.com/aakash4dev/gnark2/logger"
"github.com/consensys/gnark-crypto/ecc"
bls12377r1cs "github.com/aakash4dev/gnark2/constraint/bls12-377"
bls12381r1cs "github.com/aakash4dev/gnark2/constraint/bls12-381"
bls24315r1cs "github.com/aakash4dev/gnark2/constraint/bls24-315"
bls24317r1cs "github.com/aakash4dev/gnark2/constraint/bls24-317"
bn254r1cs "github.com/aakash4dev/gnark2/constraint/bn254"
bw6633r1cs "github.com/aakash4dev/gnark2/constraint/bw6-633"
bw6761r1cs "github.com/aakash4dev/gnark2/constraint/bw6-761"
"github.com/aakash4dev/gnark2/constraint/solver"
tinyfieldr1cs "github.com/aakash4dev/gnark2/constraint/tinyfield"
)
// NewBuilder returns a new R1CS builder which implements frontend.API.
// Additionally, this builder also implements [frontend.Committer].
func NewBuilder(field *big.Int, config frontend.CompileConfig) (frontend.Builder, error) {
return newBuilder(field, config), nil
}
type builder struct {
cs constraint.R1CS
config frontend.CompileConfig
kvstore.Store
// map for recording boolean constrained variables (to not constrain them twice)
mtBooleans map[uint64][]expr.LinearExpression
tOne constraint.Element
eZero, eOne expr.LinearExpression
cZero, cOne constraint.LinearExpression
// helps merge k sorted linear expressions
heap minHeap
// buffers used to do in place api.MAC
mbuf1 expr.LinearExpression
mbuf2 expr.LinearExpression
genericGate constraint.BlueprintID
}
// initialCapacity has quite some impact on frontend performance, especially on large circuits size
// we may want to add build tags to tune that
func newBuilder(field *big.Int, config frontend.CompileConfig) *builder {
macCapacity := 100
if config.CompressThreshold != 0 {
macCapacity = config.CompressThreshold
}
builder := builder{
mtBooleans: make(map[uint64][]expr.LinearExpression, config.Capacity/10),
config: config,
heap: make(minHeap, 0, 100),
mbuf1: make(expr.LinearExpression, 0, macCapacity),
mbuf2: make(expr.LinearExpression, 0, macCapacity),
Store: kvstore.New(),
}
// by default the circuit is given a public wire equal to 1
curve := utils.FieldToCurve(field)
switch curve {
case ecc.BLS12_377:
builder.cs = bls12377r1cs.NewR1CS(config.Capacity)
case ecc.BLS12_381:
builder.cs = bls12381r1cs.NewR1CS(config.Capacity)
case ecc.BN254:
builder.cs = bn254r1cs.NewR1CS(config.Capacity)
case ecc.BW6_761:
builder.cs = bw6761r1cs.NewR1CS(config.Capacity)
case ecc.BW6_633:
builder.cs = bw6633r1cs.NewR1CS(config.Capacity)
case ecc.BLS24_315:
builder.cs = bls24315r1cs.NewR1CS(config.Capacity)
case ecc.BLS24_317:
builder.cs = bls24317r1cs.NewR1CS(config.Capacity)
default:
if field.Cmp(tinyfield.Modulus()) == 0 {
builder.cs = tinyfieldr1cs.NewR1CS(config.Capacity)
break
}
panic("not implemented")
}
builder.tOne = builder.cs.One()
builder.cs.AddPublicVariable("1")
builder.genericGate = builder.cs.AddBlueprint(&constraint.BlueprintGenericR1C{})
builder.eZero = expr.NewLinearExpression(0, constraint.Element{})
builder.eOne = expr.NewLinearExpression(0, builder.tOne)
builder.cOne = constraint.LinearExpression{constraint.Term{VID: 0, CID: constraint.CoeffIdOne}}
builder.cZero = constraint.LinearExpression{constraint.Term{VID: 0, CID: constraint.CoeffIdZero}}
return &builder
}
// newInternalVariable creates a new wire, appends it on the list of wires of the circuit, sets
// the wire's id to the number of wires, and returns it
func (builder *builder) newInternalVariable() expr.LinearExpression {
idx := builder.cs.AddInternalVariable()
return expr.NewLinearExpression(idx, builder.tOne)
}
// PublicVariable creates a new public Variable
func (builder *builder) PublicVariable(f schema.LeafInfo) frontend.Variable {
idx := builder.cs.AddPublicVariable(f.FullName())
return expr.NewLinearExpression(idx, builder.tOne)
}
// SecretVariable creates a new secret Variable
func (builder *builder) SecretVariable(f schema.LeafInfo) frontend.Variable {
idx := builder.cs.AddSecretVariable(f.FullName())
return expr.NewLinearExpression(idx, builder.tOne)
}
// cstOne return the one constant
func (builder *builder) cstOne() expr.LinearExpression {
return builder.eOne
}
// cstZero return the zero constant
func (builder *builder) cstZero() expr.LinearExpression {
return builder.eZero
}
func (builder *builder) isCstOne(c constraint.Element) bool {
return builder.cs.IsOne(c)
}
func (builder *builder) Field() *big.Int {
return builder.cs.Field()
}
func (builder *builder) FieldBitLen() int {
return builder.cs.FieldBitLen()
}
// newR1C clones the linear expression associated with the Variables (to avoid offsetting the ID multiple time)
// and return a R1C
func (builder *builder) newR1C(l, r, o frontend.Variable) constraint.R1C {
L := builder.getLinearExpression(l)
R := builder.getLinearExpression(r)
O := builder.getLinearExpression(o)
// interestingly, this is key to groth16 performance.
// l * r == r * l == o
// but the "l" linear expression is going to end up in the A matrix
// the "r" linear expression is going to end up in the B matrix
// the less Variable we have appearing in the B matrix, the more likely groth16.Setup
// is going to produce infinity points in pk.G1.B and pk.G2.B, which will speed up proving time
if len(L) > len(R) {
// TODO @gbotrel shouldn't we do the opposite? Code doesn't match comment.
L, R = R, L
}
return constraint.R1C{L: L, R: R, O: O}
}
func (builder *builder) getLinearExpression(_l interface{}) constraint.LinearExpression {
var L constraint.LinearExpression
switch tl := _l.(type) {
case expr.LinearExpression:
if len(tl) == 1 && tl[0].VID == 0 {
if tl[0].Coeff.IsZero() {
return builder.cZero
} else if tl[0].Coeff == builder.tOne {
return builder.cOne
}
}
L = make(constraint.LinearExpression, 0, len(tl))
for _, t := range tl {
L = append(L, builder.cs.MakeTerm(t.Coeff, t.VID))
}
case constraint.LinearExpression:
L = tl
default:
if debug.Debug {
panic("invalid input for getLinearExpression") // sanity check
}
}
return L
}
// MarkBoolean sets (but do not **constraint**!) v to be boolean
// This is useful in scenarios where a variable is known to be boolean through a constraint
// that is not api.AssertIsBoolean. If v is a constant, this is a no-op.
func (builder *builder) MarkBoolean(v frontend.Variable) {
if b, ok := builder.constantValue(v); ok {
if !(b.IsZero() || builder.isCstOne(b)) {
panic("MarkBoolean called a non-boolean constant")
}
return
}
// v is a linear expression
l := v.(expr.LinearExpression)
sort.Sort(l)
key := l.HashCode()
list := builder.mtBooleans[key]
list = append(list, l)
builder.mtBooleans[key] = list
}
// IsBoolean returns true if given variable was marked as boolean in the compiler (see MarkBoolean)
// Use with care; variable may not have been **constrained** to be boolean
// This returns true if the v is a constant and v == 0 || v == 1.
func (builder *builder) IsBoolean(v frontend.Variable) bool {
if b, ok := builder.constantValue(v); ok {
return (b.IsZero() || builder.isCstOne(b))
}
// v is a linear expression
l := v.(expr.LinearExpression)
sort.Sort(l)
key := l.HashCode()
list, ok := builder.mtBooleans[key]
if !ok {
return false
}
for _, v := range list {
if v.Equal(l) {
return true
}
}
return false
}
var tVariable reflect.Type
func init() {
tVariable = reflect.ValueOf(struct{ A frontend.Variable }{}).FieldByName("A").Type()
}
// Compile constructs a rank-1 constraint sytem
func (builder *builder) Compile() (constraint.ConstraintSystem, error) {
// TODO if already compiled, return builder.cs object
log := logger.Logger()
log.Info().
Int("nbConstraints", builder.cs.GetNbConstraints()).
Msg("building constraint builder")
// ensure all inputs and hints are constrained
if err := builder.cs.CheckUnconstrainedWires(); err != nil {
log.Warn().Msg("circuit has unconstrained inputs")
if !builder.config.IgnoreUnconstrainedInputs {
return nil, err
}
}
return builder.cs, nil
}
// ConstantValue returns the big.Int value of v.
// Will panic if v.IsConstant() == false
func (builder *builder) ConstantValue(v frontend.Variable) (*big.Int, bool) {
coeff, ok := builder.constantValue(v)
if !ok {
return nil, false
}
return builder.cs.ToBigInt(coeff), true
}
func (builder *builder) constantValue(v frontend.Variable) (constraint.Element, bool) {
if _v, ok := v.(expr.LinearExpression); ok {
assertIsSet(_v)
if len(_v) != 1 {
// TODO @gbotrel this assumes linear expressions of coeff are not possible
// and are always reduced to one element. may not always be true?
return constraint.Element{}, false
}
if !(_v[0].WireID() == 0) { // public ONE WIRE
return constraint.Element{}, false
}
return _v[0].Coeff, true
}
return builder.cs.FromInterface(v), true
}
// toVariable will return (and allocate if necessary) a linearExpression from given value
//
// if input is already a linearExpression, does nothing
// else, attempts to convert input to a big.Int (see utils.FromInterface) and returns a toVariable linearExpression
func (builder *builder) toVariable(input interface{}) expr.LinearExpression {
switch t := input.(type) {
case expr.LinearExpression:
// this is already a "kwown" variable
assertIsSet(t)
return t
case *expr.LinearExpression:
assertIsSet(*t)
return *t
case constraint.Element:
return expr.NewLinearExpression(0, t)
case *constraint.Element:
return expr.NewLinearExpression(0, *t)
default:
// try to make it into a constant
c := builder.cs.FromInterface(t)
return expr.NewLinearExpression(0, c)
}
}
// toVariables return frontend.Variable corresponding to inputs and the total size of the linear expressions
func (builder *builder) toVariables(in ...frontend.Variable) ([]expr.LinearExpression, int) {
r := make([]expr.LinearExpression, 0, len(in))
s := 0
e := func(i frontend.Variable) {
v := builder.toVariable(i)
r = append(r, v)
s += len(v)
}
// e(i1)
// e(i2)
for i := 0; i < len(in); i++ {
e(in[i])
}
return r, s
}
// NewHint initializes internal variables whose value will be evaluated using
// the provided hint function at run time from the inputs. Inputs must be either
// variables or convertible to *big.Int. The function returns an error if the
// number of inputs is not compatible with f.
//
// The hint function is provided at the proof creation time and is not embedded
// into the circuit. From the backend point of view, the variable returned by
// the hint function is equivalent to the user-supplied witness, but its actual
// value is assigned by the solver, not the caller.
//
// No new constraints are added to the newly created wire and must be added
// manually in the circuit. Failing to do so leads to solver failure.
func (builder *builder) NewHint(f solver.Hint, nbOutputs int, inputs ...frontend.Variable) ([]frontend.Variable, error) {
return builder.newHint(f, solver.GetHintID(f), nbOutputs, inputs)
}
func (builder *builder) NewHintForId(id solver.HintID, nbOutputs int, inputs ...frontend.Variable) ([]frontend.Variable, error) {
return builder.newHint(nil, id, nbOutputs, inputs)
}
func (builder *builder) newHint(f solver.Hint, id solver.HintID, nbOutputs int, inputs []frontend.Variable) ([]frontend.Variable, error) {
hintInputs := make([]constraint.LinearExpression, len(inputs))
// TODO @gbotrel hint input pass
// ensure inputs are set and pack them in a []uint64
for i, in := range inputs {
if t, ok := in.(expr.LinearExpression); ok {
assertIsSet(t)
hintInputs[i] = builder.getLinearExpression(t)
} else {
c := builder.cs.FromInterface(in)
term := builder.cs.MakeTerm(c, 0)
term.MarkConstant()
hintInputs[i] = constraint.LinearExpression{term}
}
}
internalVariables, err := builder.cs.AddSolverHint(f, id, hintInputs, nbOutputs)
if err != nil {
return nil, err
}
// make the variables
res := make([]frontend.Variable, len(internalVariables))
for i, idx := range internalVariables {
res[i] = expr.NewLinearExpression(idx, builder.tOne)
}
return res, nil
}
// assertIsSet panics if the variable is unset
// this may happen if inside a Define we have
// var a variable
// cs.Mul(a, 1)
// since a was not in the circuit struct it is not a secret variable
func assertIsSet(l expr.LinearExpression) {
if len(l) == 0 {
// errNoValue triggered when trying to access a variable that was not allocated
errNoValue := errors.New("can't determine API input value")
panic(errNoValue)
}
if debug.Debug {
// frontend/ package must build linear expressions that are sorted.
if !sort.IsSorted(l) {
panic("unsorted linear expression")
}
}
}
// newDebugInfo this is temporary to restore debug logs
// something more like builder.sprintf("my message %le %lv", l0, l1)
// to build logs for both debug and println
// and append some program location.. (see other todo in debug_info.go)
func (builder *builder) newDebugInfo(errName string, in ...interface{}) constraint.DebugInfo {
for i := 0; i < len(in); i++ {
// for inputs that are LinearExpressions or Term, we need to "Make" them in the backend.
// TODO @gbotrel this is a duplicate effort with adding a constraint and should be taken care off
switch t := in[i].(type) {
case *expr.LinearExpression:
in[i] = builder.getLinearExpression(*t)
case expr.LinearExpression:
in[i] = builder.getLinearExpression(t)
case expr.Term:
in[i] = builder.getLinearExpression(expr.LinearExpression{t})
case *expr.Term:
in[i] = builder.getLinearExpression(expr.LinearExpression{*t})
case constraint.Element:
in[i] = builder.cs.String(t)
case *constraint.Element:
in[i] = builder.cs.String(*t)
}
}
return builder.cs.NewDebugInfo(errName, in...)
}
// compress checks the length of the linear expression le and if it is larger or
// equal than CompressThreshold in the configuration, replaces it with a linear
// expression of one term. In that case it adds an equality constraint enforcing
// the correctness of the returned linear expression.
func (builder *builder) compress(le expr.LinearExpression) expr.LinearExpression {
if builder.config.CompressThreshold <= 0 || len(le) < builder.config.CompressThreshold {
return le
}
one := builder.cstOne()
t := builder.newInternalVariable()
builder.cs.AddR1C(builder.newR1C(le, one, t), builder.genericGate)
return t
}
func (builder *builder) Defer(cb func(frontend.API) error) {
circuitdefer.Put(builder, cb)
}
func (*builder) FrontendType() frontendtype.Type {
return frontendtype.R1CS
}
// AddInstruction is used to add custom instructions to the constraint system.
func (builder *builder) AddInstruction(bID constraint.BlueprintID, calldata []uint32) []uint32 {
return builder.cs.AddInstruction(bID, calldata)
}
// AddBlueprint adds a custom blueprint to the constraint system.
func (builder *builder) AddBlueprint(b constraint.Blueprint) constraint.BlueprintID {
return builder.cs.AddBlueprint(b)
}
func (builder *builder) InternalVariable(wireID uint32) frontend.Variable {
return expr.NewLinearExpression(int(wireID), builder.tOne)
}
// ToCanonicalVariable converts a frontend.Variable to a constraint system specific Variable
// ! Experimental: use in conjunction with constraint.CustomizableSystem
func (builder *builder) ToCanonicalVariable(in frontend.Variable) frontend.CanonicalVariable {
if t, ok := in.(expr.LinearExpression); ok {
assertIsSet(t)
return builder.getLinearExpression(t)
} else {
c := builder.cs.FromInterface(in)
term := builder.cs.MakeTerm(c, 0)
term.MarkConstant()
return constraint.LinearExpression{term}
}
}