forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prove.go
396 lines (335 loc) · 11.3 KB
/
prove.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
// 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 groth16
import (
"fmt"
"github.com/consensys/gnark-crypto/ecc"
curve "github.com/consensys/gnark-crypto/ecc/bw6-633"
"github.com/consensys/gnark-crypto/ecc/bw6-633/fr"
"github.com/consensys/gnark-crypto/ecc/bw6-633/fr/fft"
"github.com/consensys/gnark-crypto/ecc/bw6-633/fr/hash_to_field"
"github.com/consensys/gnark-crypto/ecc/bw6-633/fr/pedersen"
"github.com/aakash4dev/gnark2/backend"
"github.com/aakash4dev/gnark2/backend/groth16/internal"
"github.com/aakash4dev/gnark2/backend/witness"
"github.com/aakash4dev/gnark2/constraint"
cs "github.com/aakash4dev/gnark2/constraint/bw6-633"
"github.com/aakash4dev/gnark2/constraint/solver"
"github.com/aakash4dev/gnark2/internal/utils"
"github.com/aakash4dev/gnark2/logger"
"math/big"
"runtime"
"time"
fcs "github.com/aakash4dev/gnark2/frontend/cs"
)
// Proof represents a Groth16 proof that was encoded with a ProvingKey and can be verified
// with a valid statement and a VerifyingKey
// Notation follows Figure 4. in DIZK paper https://eprint.iacr.org/2018/691.pdf
type Proof struct {
Ar, Krs curve.G1Affine
Bs curve.G2Affine
Commitments []curve.G1Affine // Pedersen commitments a la https://eprint.iacr.org/2022/1072
CommitmentPok curve.G1Affine // Batched proof of knowledge of the above commitments
}
// isValid ensures proof elements are in the correct subgroup
func (proof *Proof) isValid() bool {
return proof.Ar.IsInSubGroup() && proof.Krs.IsInSubGroup() && proof.Bs.IsInSubGroup()
}
// CurveID returns the curveID
func (proof *Proof) CurveID() ecc.ID {
return curve.ID
}
// Prove generates the proof of knowledge of a r1cs with full witness (secret + public part).
func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...backend.ProverOption) (*Proof, error) {
opt, err := backend.NewProverConfig(opts...)
if err != nil {
return nil, fmt.Errorf("new prover config: %w", err)
}
if opt.HashToFieldFn == nil {
opt.HashToFieldFn = hash_to_field.New([]byte(constraint.CommitmentDst))
}
log := logger.Logger().With().Str("curve", r1cs.CurveID().String()).Str("acceleration", "none").Int("nbConstraints", r1cs.GetNbConstraints()).Str("backend", "groth16").Logger()
commitmentInfo := r1cs.CommitmentInfo.(constraint.Groth16Commitments)
proof := &Proof{Commitments: make([]curve.G1Affine, len(commitmentInfo))}
solverOpts := opt.SolverOpts[:len(opt.SolverOpts):len(opt.SolverOpts)]
privateCommittedValues := make([][]fr.Element, len(commitmentInfo))
// override hints
bsb22ID := solver.GetHintID(fcs.Bsb22CommitmentComputePlaceholder)
solverOpts = append(solverOpts, solver.OverrideHint(bsb22ID, func(_ *big.Int, in []*big.Int, out []*big.Int) error {
i := int(in[0].Int64())
in = in[1:]
privateCommittedValues[i] = make([]fr.Element, len(commitmentInfo[i].PrivateCommitted))
hashed := in[:len(commitmentInfo[i].PublicAndCommitmentCommitted)]
committed := in[+len(hashed):]
for j, inJ := range committed {
privateCommittedValues[i][j].SetBigInt(inJ)
}
var err error
if proof.Commitments[i], err = pk.CommitmentKeys[i].Commit(privateCommittedValues[i]); err != nil {
return err
}
opt.HashToFieldFn.Write(constraint.SerializeCommitment(proof.Commitments[i].Marshal(), hashed, (fr.Bits-1)/8+1))
hashBts := opt.HashToFieldFn.Sum(nil)
opt.HashToFieldFn.Reset()
nbBuf := fr.Bytes
if opt.HashToFieldFn.Size() < fr.Bytes {
nbBuf = opt.HashToFieldFn.Size()
}
var res fr.Element
res.SetBytes(hashBts[:nbBuf])
res.BigInt(out[0])
return nil
}))
if r1cs.GkrInfo.Is() {
var gkrData cs.GkrSolvingData
solverOpts = append(solverOpts,
solver.OverrideHint(r1cs.GkrInfo.SolveHintID, cs.GkrSolveHint(r1cs.GkrInfo, &gkrData)),
solver.OverrideHint(r1cs.GkrInfo.ProveHintID, cs.GkrProveHint(r1cs.GkrInfo.HashName, &gkrData)))
}
_solution, err := r1cs.Solve(fullWitness, solverOpts...)
if err != nil {
return nil, err
}
solution := _solution.(*cs.R1CSSolution)
wireValues := []fr.Element(solution.W)
start := time.Now()
commitmentsSerialized := make([]byte, fr.Bytes*len(commitmentInfo))
for i := range commitmentInfo {
copy(commitmentsSerialized[fr.Bytes*i:], wireValues[commitmentInfo[i].CommitmentIndex].Marshal())
}
if proof.CommitmentPok, err = pedersen.BatchProve(pk.CommitmentKeys, privateCommittedValues, commitmentsSerialized); err != nil {
return nil, err
}
// H (witness reduction / FFT part)
var h []fr.Element
chHDone := make(chan struct{}, 1)
go func() {
h = computeH(solution.A, solution.B, solution.C, &pk.Domain)
solution.A = nil
solution.B = nil
solution.C = nil
chHDone <- struct{}{}
}()
// we need to copy and filter the wireValues for each multi exp
// as pk.G1.A, pk.G1.B and pk.G2.B may have (a significant) number of point at infinity
var wireValuesA, wireValuesB []fr.Element
chWireValuesA, chWireValuesB := make(chan struct{}, 1), make(chan struct{}, 1)
go func() {
wireValuesA = make([]fr.Element, len(wireValues)-int(pk.NbInfinityA))
for i, j := 0, 0; j < len(wireValuesA); i++ {
if pk.InfinityA[i] {
continue
}
wireValuesA[j] = wireValues[i]
j++
}
close(chWireValuesA)
}()
go func() {
wireValuesB = make([]fr.Element, len(wireValues)-int(pk.NbInfinityB))
for i, j := 0, 0; j < len(wireValuesB); i++ {
if pk.InfinityB[i] {
continue
}
wireValuesB[j] = wireValues[i]
j++
}
close(chWireValuesB)
}()
// sample random r and s
var r, s big.Int
var _r, _s, _kr fr.Element
if _, err := _r.SetRandom(); err != nil {
return nil, err
}
if _, err := _s.SetRandom(); err != nil {
return nil, err
}
_kr.Mul(&_r, &_s).Neg(&_kr)
_r.BigInt(&r)
_s.BigInt(&s)
// computes r[δ], s[δ], kr[δ]
deltas := curve.BatchScalarMultiplicationG1(&pk.G1.Delta, []fr.Element{_r, _s, _kr})
var bs1, ar curve.G1Jac
n := runtime.NumCPU()
chBs1Done := make(chan error, 1)
computeBS1 := func() {
<-chWireValuesB
if _, err := bs1.MultiExp(pk.G1.B, wireValuesB, ecc.MultiExpConfig{NbTasks: n / 2}); err != nil {
chBs1Done <- err
close(chBs1Done)
return
}
bs1.AddMixed(&pk.G1.Beta)
bs1.AddMixed(&deltas[1])
chBs1Done <- nil
}
chArDone := make(chan error, 1)
computeAR1 := func() {
<-chWireValuesA
if _, err := ar.MultiExp(pk.G1.A, wireValuesA, ecc.MultiExpConfig{NbTasks: n / 2}); err != nil {
chArDone <- err
close(chArDone)
return
}
ar.AddMixed(&pk.G1.Alpha)
ar.AddMixed(&deltas[0])
proof.Ar.FromJacobian(&ar)
chArDone <- nil
}
chKrsDone := make(chan error, 1)
computeKRS := func() {
// we could NOT split the Krs multiExp in 2, and just append pk.G1.K and pk.G1.Z
// however, having similar lengths for our tasks helps with parallelism
var krs, krs2, p1 curve.G1Jac
chKrs2Done := make(chan error, 1)
sizeH := int(pk.Domain.Cardinality - 1) // comes from the fact the deg(H)=(n-1)+(n-1)-n=n-2
go func() {
_, err := krs2.MultiExp(pk.G1.Z, h[:sizeH], ecc.MultiExpConfig{NbTasks: n / 2})
chKrs2Done <- err
}()
// filter the wire values if needed
// TODO Perf @Tabaie worst memory allocation offender
toRemove := commitmentInfo.GetPrivateCommitted()
toRemove = append(toRemove, commitmentInfo.CommitmentIndexes())
_wireValues := filterHeap(wireValues[r1cs.GetNbPublicVariables():], r1cs.GetNbPublicVariables(), internal.ConcatAll(toRemove...))
if _, err := krs.MultiExp(pk.G1.K, _wireValues, ecc.MultiExpConfig{NbTasks: n / 2}); err != nil {
chKrsDone <- err
return
}
krs.AddMixed(&deltas[2])
n := 3
for n != 0 {
select {
case err := <-chKrs2Done:
if err != nil {
chKrsDone <- err
return
}
krs.AddAssign(&krs2)
case err := <-chArDone:
if err != nil {
chKrsDone <- err
return
}
p1.ScalarMultiplication(&ar, &s)
krs.AddAssign(&p1)
case err := <-chBs1Done:
if err != nil {
chKrsDone <- err
return
}
p1.ScalarMultiplication(&bs1, &r)
krs.AddAssign(&p1)
}
n--
}
proof.Krs.FromJacobian(&krs)
chKrsDone <- nil
}
computeBS2 := func() error {
// Bs2 (1 multi exp G2 - size = len(wires))
var Bs, deltaS curve.G2Jac
nbTasks := n
if nbTasks <= 16 {
// if we don't have a lot of CPUs, this may artificially split the MSM
nbTasks *= 2
}
<-chWireValuesB
if _, err := Bs.MultiExp(pk.G2.B, wireValuesB, ecc.MultiExpConfig{NbTasks: nbTasks}); err != nil {
return err
}
deltaS.FromAffine(&pk.G2.Delta)
deltaS.ScalarMultiplication(&deltaS, &s)
Bs.AddAssign(&deltaS)
Bs.AddMixed(&pk.G2.Beta)
proof.Bs.FromJacobian(&Bs)
return nil
}
// wait for FFT to end, as it uses all our CPUs
<-chHDone
// schedule our proof part computations
go computeKRS()
go computeAR1()
go computeBS1()
if err := computeBS2(); err != nil {
return nil, err
}
// wait for all parts of the proof to be computed.
if err := <-chKrsDone; err != nil {
return nil, err
}
log.Debug().Dur("took", time.Since(start)).Msg("prover done")
return proof, nil
}
// if len(toRemove) == 0, returns slice
// else, returns a new slice without the indexes in toRemove. The first value in the slice is taken as indexes as sliceFirstIndex
// this assumes len(slice) > len(toRemove)
// filterHeap modifies toRemove
func filterHeap(slice []fr.Element, sliceFirstIndex int, toRemove []int) (r []fr.Element) {
if len(toRemove) == 0 {
return slice
}
heap := utils.IntHeap(toRemove)
heap.Heapify()
r = make([]fr.Element, 0, len(slice))
// note: we can optimize that for the likely case where len(slice) >>> len(toRemove)
for i := 0; i < len(slice); i++ {
if len(heap) > 0 && i+sliceFirstIndex == heap[0] {
for len(heap) > 0 && i+sliceFirstIndex == heap[0] {
heap.Pop()
}
continue
}
r = append(r, slice[i])
}
return
}
func computeH(a, b, c []fr.Element, domain *fft.Domain) []fr.Element {
// H part of Krs
// Compute H (hz=ab-c, where z=-2 on ker X^n+1 (z(x)=x^n-1))
// 1 - _a = ifft(a), _b = ifft(b), _c = ifft(c)
// 2 - ca = fft_coset(_a), ba = fft_coset(_b), cc = fft_coset(_c)
// 3 - h = ifft_coset(ca o cb - cc)
n := len(a)
// add padding to ensure input length is domain cardinality
padding := make([]fr.Element, int(domain.Cardinality)-n)
a = append(a, padding...)
b = append(b, padding...)
c = append(c, padding...)
n = len(a)
domain.FFTInverse(a, fft.DIF)
domain.FFTInverse(b, fft.DIF)
domain.FFTInverse(c, fft.DIF)
domain.FFT(a, fft.DIT, fft.OnCoset())
domain.FFT(b, fft.DIT, fft.OnCoset())
domain.FFT(c, fft.DIT, fft.OnCoset())
var den, one fr.Element
one.SetOne()
den.Exp(domain.FrMultiplicativeGen, big.NewInt(int64(domain.Cardinality)))
den.Sub(&den, &one).Inverse(&den)
// h = ifft_coset(ca o cb - cc)
// reusing a to avoid unnecessary memory allocation
utils.Parallelize(n, func(start, end int) {
for i := start; i < end; i++ {
a[i].Mul(&a[i], &b[i]).
Sub(&a[i], &c[i]).
Mul(&a[i], &den)
}
})
// ifft_coset
domain.FFTInverse(a, fft.DIF, fft.OnCoset())
return a
}