-
Notifications
You must be signed in to change notification settings - Fork 9
/
hints.go
317 lines (263 loc) · 8.42 KB
/
hints.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
package gadget
import (
"fmt"
"math/big"
"github.com/AlexandreBelling/gnark/backend/hint"
"github.com/AlexandreBelling/gnark/frontend"
"github.com/consensys/gkr-mimc/common"
gkrNative "github.com/consensys/gkr-mimc/gkr"
"github.com/consensys/gkr-mimc/hash"
"github.com/consensys/gkr-mimc/poly"
"github.com/consensys/gkr-mimc/snark/gkr"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
"golang.org/x/crypto/sha3"
)
const debug bool = false
type HashHint struct {
g *GkrGadget
}
type InitialRandomnessHint struct {
g *GkrGadget
}
type GkrProverHint struct {
g *GkrGadget
}
// Accessor for the hint from the GkrGadget
func (g *GkrGadget) HashHint() *HashHint {
return &HashHint{g: g}
}
// Accessor for the hint from the InitialRandomnessHint
func (g *GkrGadget) InitialRandomnessHint() *InitialRandomnessHint {
return &InitialRandomnessHint{g: g}
}
// Accessor for the hint from the GkrProver
func (g *GkrGadget) GkrProverHint() *GkrProverHint {
return &GkrProverHint{g: g}
}
// UUID of the hash hint
func (h *HashHint) UUID() hint.ID {
return 156461454
}
// UUID of the initial randomness hint hint
func (h *InitialRandomnessHint) UUID() hint.ID {
return 46842135
}
// UUID of the gkr prover hint hint
func (h *GkrProverHint) UUID() hint.ID {
return 13135755
}
// NbOutputs of the hash hint
func (h *HashHint) NbOutputs(_ ecc.ID, nbInput int) int {
return 1
}
// NbOutputs of the initial randomness hint
func (h *InitialRandomnessHint) NbOutputs(_ ecc.ID, nbInput int) int {
return 1
}
// NbOutputs of the gkr prover hint
// Returns the number of field elements to represent the
func (h *GkrProverHint) NbOutputs(_ ecc.ID, nbInput int) int {
// Find the circuit
circuit := h.g.Circuit
// Iteratively finds the bN of the circuit from the input size
// Can't guarantee that g.ioStore.index contains the right values
bN := 0
inputArity := circuit.InputArity()
for {
inputSize := (1<<bN)*(inputArity+1) + bN
if inputSize == nbInput {
break
}
// sanity check in case something must be wrong with the formula
if inputSize > nbInput {
panic(fmt.Sprintf("Took over the size: %v > %v. Something wrong with the formula for the size", inputSize, nbInput))
}
// try with a larger bn
bN += 1
}
sumcheckSize := 0
claimsSize := 0
qPrimeSize := 0
for _, layer := range circuit {
// If not an input gate, adds the sumchecks
if layer.Gate != nil {
// The degree is deg + 1 (because of the multiplication by `eq(h, q)`)
// So the number of coefficients is deg + 2
nbCoeffs := layer.Gate.Degree() + 2
sumcheckSize += bN * nbCoeffs // size of the sumcheck for the layer
}
claimsSize += len(layer.Out)
qPrimeSize += bN * len(layer.Out)
}
qPrimeSize += bN // For the output layer
return sumcheckSize + claimsSize + qPrimeSize
}
// String of the hash hint
func (h *HashHint) String() string {
return "HashHint"
}
// String of the initial randomness hint hint
func (h *InitialRandomnessHint) String() string {
return "InitialRandomnessHint"
}
// String of the gkr prover hint hint
func (h *GkrProverHint) String() string {
return "GkrProverHint"
}
// Returns the Hint functions that can help gnark's solver figure out that
// the output of the GKR should be a hash
func (h *HashHint) Call(curve ecc.ID, inps []*big.Int, outputs []*big.Int) error {
var state, block fr.Element
state.SetBigInt(inps[0])
block.SetBigInt(inps[1])
// Properly computes the hash
hashed := hash.MimcKeyedPermutation(block, state)
hashed.ToBigIntRegular(outputs[0])
h.g.ioStore.index++
return nil
}
// Derives the initial randomness from an elliptic curve point
func DeriveRandomnessFromPoint(g1 bn254.G1Affine) fr.Element {
// Hash the uncompressed point, then get a field element out of it
bytesG1 := g1.RawBytes()
keccak := sha3.NewLegacyKeccak256()
keccak.Write(bytesG1[:])
hashed := keccak.Sum(nil)
// Derive the initial randomness from the hash
var randomness fr.Element
randomness.SetBytes(hashed)
return randomness
}
// Hint for generating the initial randomness
func (h *InitialRandomnessHint) Call(_ ecc.ID, inpss []*big.Int, oups []*big.Int) error {
t := common.NewTimer("initial randomness hint")
defer t.Close()
// Takes a subslice and convert to fr.Element
subSlice := func(array []*big.Int, indices []int, offset int) []fr.Element {
res := make([]fr.Element, len(indices))
for i, idx := range indices {
res[i].SetBigInt(array[idx+offset])
// Switch to regular
res[i].FromMont()
}
return res
}
// Separate the scalars for the public/private parts
scalarsPub := subSlice(inpss, h.g.r1cs.pubGkrIo, 0)
scalarsPriv := subSlice(inpss, h.g.r1cs.privGkrIo, 0)
// Compute the K associated to the gkr public/private inputs
var KrsGkr, KrsGkrPriv bn254.G1Affine
KrsGkr.MultiExp(h.g.r1cs.provingKey.pubKGkr, scalarsPub, ecc.MultiExpConfig{})
KrsGkrPriv.MultiExp(h.g.r1cs.provingKey.privKGkrSigma, scalarsPriv, ecc.MultiExpConfig{})
KrsGkr.Add(&KrsGkr, &KrsGkrPriv)
h.g.proof = &Proof{KrsGkrPriv: KrsGkrPriv}
initialRandomness := DeriveRandomnessFromPoint(KrsGkr)
initialRandomness.ToBigIntRegular(oups[0])
return nil
}
// Returns the Hint functions that can help gnark's solver figure out that
// we need to compute the GkrProof and verify
// In order to return the fields one after the other, the function is built as a stateful iterator
func (h *GkrProverHint) Call(_ ecc.ID, inputsBI []*big.Int, oups []*big.Int) error {
bN := common.Log2Ceil(h.g.ioStore.Index())
paddedIndex := 1 << bN
drain := make([]fr.Element, len(inputsBI))
for i := range drain {
drain[i].SetBigInt(inputsBI[i])
}
// Passes the outputs
inputs := make([]poly.MultiLin, h.g.Circuit.InputArity())
qPrime, drain := drain[:bN], drain[bN:]
for i := range inputs {
inputs[i], drain = drain[:paddedIndex], drain[paddedIndex:]
}
// The output: here is passed to force the solver to wait for all the output
outputs, drain := drain[:paddedIndex], drain[paddedIndex:]
// Sanity check
common.Assert(len(drain) == 0, "The drain was expected to emptied but there remains %v elements", len(drain))
// Runs the actual prover
assignment := h.g.Circuit.Assign(inputs...)
t := common.NewTimer("gkr prover hint")
gkrProof := gkrNative.Prove(h.g.Circuit, assignment, qPrime)
t.Close()
if debug {
// For debug : only -> Check that the proof verifies
valid := gkrNative.Verify(h.g.Circuit, gkrProof, inputs, outputs, qPrime)
common.Assert(valid == nil, "GKR proof was wrong - Bug in proof generation - %v", valid)
}
GkrProofToVec(gkrProof, oups)
return nil
}
// Writes the proof in a res buffer. We assume the res buffer to be allocated a priori
func GkrProofToVec(proof gkrNative.Proof, resBuff []*big.Int) {
cursor := 0
// Writes the sumcheck proofs
for _, layer := range proof.SumcheckProofs {
for _, sumcheckRound := range layer {
for _, val := range sumcheckRound {
val.ToBigIntRegular(resBuff[cursor])
cursor += 1
}
}
}
// Writes the claimLeft
for _, layer := range proof.Claims {
for _, claim := range layer {
claim.ToBigIntRegular(resBuff[cursor])
cursor += 1
}
}
// Writes the qPrimes
for _, layer := range proof.QPrimes {
for _, qs := range layer {
for _, q := range qs {
q.ToBigIntRegular(resBuff[cursor])
cursor += 1
}
}
}
// sanity check : expect to have written entirely the vector
if cursor < len(resBuff) {
panic("expected to have written the entire buffer")
}
}
// Reads the proof to obtain the variable equivalent, the gadget is here
// to provide the dimensions
func (g *GkrGadget) GkrProofFromVec(vec []frontend.Variable) gkr.Proof {
bN := common.Log2Ceil(g.ioStore.index)
// At this point, all the dimension of the proof are available
proof := gkr.AllocateProof(bN, g.Circuit)
cursor := 0
// Writes the sumcheck proofs
for i, layer := range proof.SumcheckProofs {
for j, sumcheckRound := range layer {
for k := range sumcheckRound {
proof.SumcheckProofs[i][j][k] = vec[cursor]
cursor += 1
}
}
}
// Writes the claimLeft
for i, layer := range proof.Claims {
for j := range layer {
proof.Claims[i][j] = vec[cursor]
cursor += 1
}
}
// Writes the qPrimes
for i, layer := range proof.QPrimes {
for j, qs := range layer {
for k := range qs {
proof.QPrimes[i][j][k] = vec[cursor]
cursor += 1
}
}
}
// sanity check, we expect to have read the entire vector by now
if cursor < len(vec) {
panic("the vector was not completely read to complete the proof")
}
return proof
}