-
Notifications
You must be signed in to change notification settings - Fork 368
/
setup.go
407 lines (343 loc) · 13.8 KB
/
setup.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
// Copyright 2020 ConsenSys Software Inc.
//
// 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.
// Code generated by gnark DO NOT EDIT
package plonk
import (
"errors"
"fmt"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/ecc/bls24-315/fr"
"github.com/consensys/gnark-crypto/ecc/bls24-315/fr/fft"
"github.com/consensys/gnark-crypto/ecc/bls24-315/fr/iop"
"github.com/consensys/gnark-crypto/ecc/bls24-315/kzg"
"github.com/consensys/gnark/backend/plonk/internal"
"github.com/consensys/gnark/constraint"
cs "github.com/consensys/gnark/constraint/bls24-315"
)
// VerifyingKey stores the data needed to verify a proof:
// * The commitment scheme
// * Commitments of ql prepended with as many ones as there are public inputs
// * Commitments of qr, qm, qo, qk prepended with as many zeroes as there are public inputs
// * Commitments to S1, S2, S3
type VerifyingKey struct {
// Size circuit
Size uint64
SizeInv fr.Element
Generator fr.Element
NbPublicVariables uint64
// Commitment scheme that is used for an instantiation of PLONK
Kzg kzg.VerifyingKey
// cosetShift generator of the coset on the small domain
CosetShift fr.Element
// S commitments to S1, S2, S3
S [3]kzg.Digest
// Commitments to ql, qr, qm, qo, qcp prepended with as many zeroes (ones for l) as there are public inputs.
// In particular Qk is not complete.
Ql, Qr, Qm, Qo, Qk kzg.Digest
Qcp []kzg.Digest
CommitmentConstraintIndexes []uint64
}
// Trace stores a plonk trace as columns
type Trace struct {
// Constants describing a plonk circuit. The first entries
// of LQk (whose index correspond to the public inputs) are set to 0, and are to be
// completed by the prover. At those indices i (so from 0 to nb_public_variables), LQl[i]=-1
// so the first nb_public_variables constraints look like this:
// -1*Wire[i] + 0* + 0 . It is zero when the constant coefficient is replaced by Wire[i].
Ql, Qr, Qm, Qo, Qk *iop.Polynomial
Qcp []*iop.Polynomial
// Polynomials representing the splitted permutation. The full permutation's support is 3*N where N=nb wires.
// The set of interpolation is <g> of size N, so to represent the permutation S we let S acts on the
// set A=(<g>, u*<g>, u^{2}*<g>) of size 3*N, where u is outside <g> (its use is to shift the set <g>).
// We obtain a permutation of A, A'. We split A' in 3 (A'_{1}, A'_{2}, A'_{3}), and S1, S2, S3 are
// respectively the interpolation of A'_{1}, A'_{2}, A'_{3} on <g>.
S1, S2, S3 *iop.Polynomial
// S full permutation, i -> S[i]
S []int64
}
// ProvingKey stores the data needed to generate a proof:
// * the commitment scheme
// * ql, prepended with as many ones as they are public inputs
// * qr, qm, qo prepended with as many zeroes as there are public inputs.
// * qk, prepended with as many zeroes as public inputs, to be completed by the prover
// with the list of public inputs.
// * sigma_1, sigma_2, sigma_3 in both basis
// * the copy constraint permutation
type ProvingKey struct {
// stores ql, qr, qm, qo, qk (-> to be completed by the prover)
// and s1, s2, s3. They are set in canonical basis before generating the proof, they will be used
// for computing the opening proofs (hence the canonical form). The canonical version
// of qk incomplete is used in the linearisation polynomial.
// The polynomials in trace are in canonical basis.
trace Trace
Kzg, KzgLagrange kzg.ProvingKey
// Verifying Key is embedded into the proving key (needed by Prove)
Vk *VerifyingKey
// Domains used for the FFTs.
// Domain[0] = small Domain
// Domain[1] = big Domain
Domain [2]fft.Domain
}
// TODO modify the signature to receive the SRS in Lagrange form (optional argument ?)
func Setup(spr *cs.SparseR1CS, kzgSrs kzg.SRS) (*ProvingKey, *VerifyingKey, error) {
var pk ProvingKey
var vk VerifyingKey
pk.Vk = &vk
vk.CommitmentConstraintIndexes = internal.IntSliceToUint64Slice(spr.CommitmentInfo.CommitmentIndexes())
// step 0: set the fft domains
pk.initDomains(spr)
if pk.Domain[0].Cardinality < 2 {
return nil, nil, fmt.Errorf("circuit has only %d constraints; unsupported by the current implementation", spr.GetNbConstraints())
}
// step 1: set the verifying key
pk.Vk.CosetShift.Set(&pk.Domain[0].FrMultiplicativeGen)
vk.Size = pk.Domain[0].Cardinality
vk.SizeInv.SetUint64(vk.Size).Inverse(&vk.SizeInv)
vk.Generator.Set(&pk.Domain[0].Generator)
vk.NbPublicVariables = uint64(len(spr.Public))
if len(kzgSrs.Pk.G1) < int(vk.Size)+3 { // + 3 for the kzg.Open of blinded poly
return nil, nil, errors.New("kzg srs is too small")
}
pk.Kzg.G1 = kzgSrs.Pk.G1[:int(vk.Size)+3]
var err error
pk.KzgLagrange.G1, err = kzg.ToLagrangeG1(kzgSrs.Pk.G1[:int(vk.Size)])
if err != nil {
return nil, nil, err
}
vk.Kzg = kzgSrs.Vk
// step 2: ql, qr, qm, qo, qk, qcp in Lagrange Basis
BuildTrace(spr, &pk.trace)
// step 3: build the permutation and build the polynomials S1, S2, S3 to encode the permutation.
// Note: at this stage, the permutation takes in account the placeholders
nbVariables := spr.NbInternalVariables + len(spr.Public) + len(spr.Secret)
buildPermutation(spr, &pk.trace, nbVariables)
s := computePermutationPolynomials(&pk.trace, &pk.Domain[0])
pk.trace.S1 = s[0]
pk.trace.S2 = s[1]
pk.trace.S3 = s[2]
// step 4: commit to s1, s2, s3, ql, qr, qm, qo, and (the incomplete version of) qk.
// All the above polynomials are expressed in canonical basis afterwards. This is why
// we save lqk before, because the prover needs to complete it in Lagrange form, and
// then express it on the Lagrange coset basis.
if err = commitTrace(&pk.trace, &pk); err != nil {
return nil, nil, err
}
return &pk, &vk, nil
}
// NbPublicWitness returns the expected public witness size (number of field elements)
func (vk *VerifyingKey) NbPublicWitness() int {
return int(vk.NbPublicVariables)
}
// VerifyingKey returns pk.Vk
func (pk *ProvingKey) VerifyingKey() interface{} {
return pk.Vk
}
// BuildTrace fills the constant columns ql, qr, qm, qo, qk from the sparser1cs.
// Size is the size of the system that is nb_constraints+nb_public_variables
func BuildTrace(spr *cs.SparseR1CS, pt *Trace) {
nbConstraints := spr.GetNbConstraints()
sizeSystem := uint64(nbConstraints + len(spr.Public))
size := ecc.NextPowerOfTwo(sizeSystem)
commitmentInfo := spr.CommitmentInfo.(constraint.PlonkCommitments)
ql := make([]fr.Element, size)
qr := make([]fr.Element, size)
qm := make([]fr.Element, size)
qo := make([]fr.Element, size)
qk := make([]fr.Element, size)
qcp := make([][]fr.Element, len(commitmentInfo))
for i := 0; i < len(spr.Public); i++ { // placeholders (-PUB_INPUT_i + qk_i = 0) TODO should return error if size is inconsistent
ql[i].SetOne().Neg(&ql[i])
qr[i].SetZero()
qm[i].SetZero()
qo[i].SetZero()
qk[i].SetZero() // → to be completed by the prover
}
offset := len(spr.Public)
j := 0
it := spr.GetSparseR1CIterator()
for c := it.Next(); c != nil; c = it.Next() {
ql[offset+j].Set(&spr.Coefficients[c.QL])
qr[offset+j].Set(&spr.Coefficients[c.QR])
qm[offset+j].Set(&spr.Coefficients[c.QM])
qo[offset+j].Set(&spr.Coefficients[c.QO])
qk[offset+j].Set(&spr.Coefficients[c.QC])
j++
}
lagReg := iop.Form{Basis: iop.Lagrange, Layout: iop.Regular}
pt.Ql = iop.NewPolynomial(&ql, lagReg)
pt.Qr = iop.NewPolynomial(&qr, lagReg)
pt.Qm = iop.NewPolynomial(&qm, lagReg)
pt.Qo = iop.NewPolynomial(&qo, lagReg)
pt.Qk = iop.NewPolynomial(&qk, lagReg)
pt.Qcp = make([]*iop.Polynomial, len(qcp))
for i := range commitmentInfo {
qcp[i] = make([]fr.Element, size)
for _, committed := range commitmentInfo[i].Committed {
qcp[i][offset+committed].SetOne()
}
pt.Qcp[i] = iop.NewPolynomial(&qcp[i], lagReg)
}
}
// commitTrace commits to every polynomial in the trace, and put
// the commitments int the verifying key.
func commitTrace(trace *Trace, pk *ProvingKey) error {
trace.Ql.ToCanonical(&pk.Domain[0]).ToRegular()
trace.Qr.ToCanonical(&pk.Domain[0]).ToRegular()
trace.Qm.ToCanonical(&pk.Domain[0]).ToRegular()
trace.Qo.ToCanonical(&pk.Domain[0]).ToRegular()
trace.Qk.ToCanonical(&pk.Domain[0]).ToRegular() // -> qk is not complete
trace.S1.ToCanonical(&pk.Domain[0]).ToRegular()
trace.S2.ToCanonical(&pk.Domain[0]).ToRegular()
trace.S3.ToCanonical(&pk.Domain[0]).ToRegular()
var err error
pk.Vk.Qcp = make([]kzg.Digest, len(trace.Qcp))
for i := range trace.Qcp {
trace.Qcp[i].ToCanonical(&pk.Domain[0]).ToRegular()
if pk.Vk.Qcp[i], err = kzg.Commit(pk.trace.Qcp[i].Coefficients(), pk.Kzg); err != nil {
return err
}
}
if pk.Vk.Ql, err = kzg.Commit(pk.trace.Ql.Coefficients(), pk.Kzg); err != nil {
return err
}
if pk.Vk.Qr, err = kzg.Commit(pk.trace.Qr.Coefficients(), pk.Kzg); err != nil {
return err
}
if pk.Vk.Qm, err = kzg.Commit(pk.trace.Qm.Coefficients(), pk.Kzg); err != nil {
return err
}
if pk.Vk.Qo, err = kzg.Commit(pk.trace.Qo.Coefficients(), pk.Kzg); err != nil {
return err
}
if pk.Vk.Qk, err = kzg.Commit(pk.trace.Qk.Coefficients(), pk.Kzg); err != nil {
return err
}
if pk.Vk.S[0], err = kzg.Commit(pk.trace.S1.Coefficients(), pk.Kzg); err != nil {
return err
}
if pk.Vk.S[1], err = kzg.Commit(pk.trace.S2.Coefficients(), pk.Kzg); err != nil {
return err
}
if pk.Vk.S[2], err = kzg.Commit(pk.trace.S3.Coefficients(), pk.Kzg); err != nil {
return err
}
return nil
}
func (pk *ProvingKey) initDomains(spr *cs.SparseR1CS) {
nbConstraints := spr.GetNbConstraints()
sizeSystem := uint64(nbConstraints + len(spr.Public)) // len(spr.Public) is for the placeholder constraints
pk.Domain[0] = *fft.NewDomain(sizeSystem)
// h, the quotient polynomial is of degree 3(n+1)+2, so it's in a 3(n+2) dim vector space,
// the domain is the next power of 2 superior to 3(n+2). 4*domainNum is enough in all cases
// except when n<6.
if sizeSystem < 6 {
pk.Domain[1] = *fft.NewDomain(8 * sizeSystem)
} else {
pk.Domain[1] = *fft.NewDomain(4 * sizeSystem)
}
}
// buildPermutation builds the Permutation associated with a circuit.
//
// The permutation s is composed of cycles of maximum length such that
//
// s. (l∥r∥o) = (l∥r∥o)
//
// , where l∥r∥o is the concatenation of the indices of l, r, o in
// ql.l+qr.r+qm.l.r+qo.O+k = 0.
//
// The permutation is encoded as a slice s of size 3*size(l), where the
// i-th entry of l∥r∥o is sent to the s[i]-th entry, so it acts on a tab
// like this: for i in tab: tab[i] = tab[permutation[i]]
func buildPermutation(spr *cs.SparseR1CS, pt *Trace, nbVariables int) {
// nbVariables := spr.NbInternalVariables + len(spr.Public) + len(spr.Secret)
sizeSolution := len(pt.Ql.Coefficients())
sizePermutation := 3 * sizeSolution
// init permutation
permutation := make([]int64, sizePermutation)
for i := 0; i < len(permutation); i++ {
permutation[i] = -1
}
// init LRO position -> variable_ID
lro := make([]int, sizePermutation) // position -> variable_ID
for i := 0; i < len(spr.Public); i++ {
lro[i] = i // IDs of LRO associated to placeholders (only L needs to be taken care of)
}
offset := len(spr.Public)
j := 0
it := spr.GetSparseR1CIterator()
for c := it.Next(); c != nil; c = it.Next() {
lro[offset+j] = int(c.XA)
lro[sizeSolution+offset+j] = int(c.XB)
lro[2*sizeSolution+offset+j] = int(c.XC)
j++
}
// init cycle:
// map ID -> last position the ID was seen
cycle := make([]int64, nbVariables)
for i := 0; i < len(cycle); i++ {
cycle[i] = -1
}
for i := 0; i < len(lro); i++ {
if cycle[lro[i]] != -1 {
// if != -1, it means we already encountered this value
// so we need to set the corresponding permutation index.
permutation[i] = cycle[lro[i]]
}
cycle[lro[i]] = int64(i)
}
// complete the Permutation by filling the first IDs encountered
for i := 0; i < sizePermutation; i++ {
if permutation[i] == -1 {
permutation[i] = cycle[lro[i]]
}
}
pt.S = permutation
}
// computePermutationPolynomials computes the LDE (Lagrange basis) of the permutation.
// We let the permutation act on <g> || u<g> || u^{2}<g>, split the result in 3 parts,
// and interpolate each of the 3 parts on <g>.
func computePermutationPolynomials(pt *Trace, domain *fft.Domain) [3]*iop.Polynomial {
nbElmts := int(domain.Cardinality)
var res [3]*iop.Polynomial
// Lagrange form of ID
evaluationIDSmallDomain := getSupportPermutation(domain)
// Lagrange form of S1, S2, S3
s1Canonical := make([]fr.Element, nbElmts)
s2Canonical := make([]fr.Element, nbElmts)
s3Canonical := make([]fr.Element, nbElmts)
for i := 0; i < nbElmts; i++ {
s1Canonical[i].Set(&evaluationIDSmallDomain[pt.S[i]])
s2Canonical[i].Set(&evaluationIDSmallDomain[pt.S[nbElmts+i]])
s3Canonical[i].Set(&evaluationIDSmallDomain[pt.S[2*nbElmts+i]])
}
lagReg := iop.Form{Basis: iop.Lagrange, Layout: iop.Regular}
res[0] = iop.NewPolynomial(&s1Canonical, lagReg)
res[1] = iop.NewPolynomial(&s2Canonical, lagReg)
res[2] = iop.NewPolynomial(&s3Canonical, lagReg)
return res
}
// getSupportPermutation returns the support on which the permutation acts, it is
// <g> || u<g> || u^{2}<g>
func getSupportPermutation(domain *fft.Domain) []fr.Element {
res := make([]fr.Element, 3*domain.Cardinality)
res[0].SetOne()
res[domain.Cardinality].Set(&domain.FrMultiplicativeGen)
res[2*domain.Cardinality].Square(&domain.FrMultiplicativeGen)
for i := uint64(1); i < domain.Cardinality; i++ {
res[i].Mul(&res[i-1], &domain.Generator)
res[domain.Cardinality+i].Mul(&res[domain.Cardinality+i-1], &domain.Generator)
res[2*domain.Cardinality+i].Mul(&res[2*domain.Cardinality+i-1], &domain.Generator)
}
return res
}