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
mod accum;
pub mod adapter;
pub mod executor;
mod fri;
mod merkle;
pub mod poly_group;
pub mod write_iop;
use alloc::{vec, vec::Vec};
use log::debug;
use self::adapter::ProveAdapter;
use crate::{
adapter::{CircuitDef, CircuitStepHandler},
core::{
poly::{poly_divide, poly_interpolate},
sha::Sha,
},
field::{Elem, ExtElem, Field, RootsOfUnity},
hal::{Buffer, EvalCheck, Hal},
prove::{fri::fri_prove, poly_group::PolyGroup, write_iop::WriteIOP},
taps::RegisterGroup,
INV_RATE, MAX_CYCLES_PO2,
};
pub fn prove_without_seal<'a, F, S, C, CS>(sha: &S, circuit: &mut ProveAdapter<'a, F, C, CS>)
where
F: Field,
S: Sha,
C: CircuitDef<F>,
CS: CircuitStepHandler<F::Elem>,
{
let mut iop = WriteIOP::new(sha);
circuit.execute(&mut iop);
}
#[tracing::instrument(skip_all)]
pub fn prove<'a, F, H, S, E, C, CS>(
hal: &H,
sha: &S,
circuit: &mut ProveAdapter<'a, F, C, CS>,
eval: &E,
) -> Vec<u32>
where
F: Field,
H: Hal<Elem = F::Elem, ExtElem = F::ExtElem>,
S: Sha,
E: EvalCheck<H>,
C: CircuitDef<F>,
CS: CircuitStepHandler<F::Elem>,
{
let taps = circuit.get_taps();
let code_size = taps.group_size(RegisterGroup::Code);
let data_size = taps.group_size(RegisterGroup::Data);
let accum_size = taps.group_size(RegisterGroup::Accum);
debug!(
"code: {code_size}/{}, data: {data_size}/{}, accum: {accum_size}/{}",
circuit.get_code().len(),
circuit.get_data().len(),
circuit.get_accum().len()
);
let mut iop = WriteIOP::new(sha);
circuit.execute(&mut iop);
let po2 = circuit.po2();
assert!(po2 as usize <= MAX_CYCLES_PO2);
let size = 1 << po2;
let code_coeffs = make_coeffs(hal, circuit.get_code(), code_size);
let code_group = PolyGroup::new(hal, &code_coeffs, code_size, size, "code");
code_group.merkle.commit(&mut iop);
debug!("codeGroup: {}", code_group.merkle.root());
let data_coeffs = make_coeffs(hal, circuit.get_data(), data_size);
let data_group = PolyGroup::new(hal, &data_coeffs, data_size, size, "data");
data_group.merkle.commit(&mut iop);
debug!("dataGroup: {}", data_group.merkle.root());
circuit.accumulate(&mut iop);
debug!("size = {size}, accumSize = {accum_size}");
debug!("getAccum.size() = {}", circuit.get_accum().len());
let accum_coeffs = make_coeffs(hal, circuit.get_accum(), accum_size);
let accum_group = PolyGroup::new(hal, &accum_coeffs, accum_size, size, "accum");
accum_group.merkle.commit(&mut iop);
debug!("accumGroup: {}", accum_group.merkle.root());
let poly_mix = H::ExtElem::random(&mut iop.rng);
let domain = size * INV_RATE;
let check_poly = hal.alloc_elem("check_poly", H::ExtElem::EXT_SIZE * domain);
let mix = hal.copy_from_elem("mix", circuit.get_mix());
let out = hal.copy_from_elem("out", circuit.get_io());
eval.eval_check(
&check_poly,
&code_group.evaluated,
&data_group.evaluated,
&accum_group.evaluated,
&mix,
&out,
poly_mix,
po2 as usize,
circuit.get_steps(),
);
#[cfg(feature = "circuit_debug")]
check_poly.view(|check_out| {
for i in (0..domain).step_by(4) {
if check_out[i] != H::Elem::ZERO {
debug!("check[{}] = {:?}", i, check_out[i]);
}
}
});
hal.batch_interpolate_ntt(&check_poly, H::ExtElem::EXT_SIZE);
let check_group = PolyGroup::new(hal, &check_poly, H::CHECK_SIZE, size, "check");
check_group.merkle.commit(&mut iop);
debug!("checkGroup: {}", check_group.merkle.root());
let z = H::ExtElem::random(&mut iop.rng);
let back_one = H::ExtElem::from_subfield(&H::Elem::ROU_REV[po2 as usize]);
let mut all_xs = Vec::new();
let mut eval_u: Vec<H::ExtElem> = Vec::new();
let mut eval_group = |id: RegisterGroup, pg: &PolyGroup<H>| {
let mut which = Vec::new();
let mut xs = Vec::new();
for tap in taps.group_taps(id) {
which.push(tap.offset() as u32);
let x = back_one.pow(tap.back()) * z;
xs.push(x);
all_xs.push(x);
}
let which = hal.copy_from_u32("which", which.as_slice());
let xs = hal.copy_from_extelem("xs", xs.as_slice());
let out = hal.alloc_extelem("out", which.size());
hal.batch_evaluate_any(&pg.coeffs, pg.count, &which, &xs, &out);
out.view(|view| {
eval_u.extend(view);
});
};
eval_group(RegisterGroup::Accum, &accum_group);
eval_group(RegisterGroup::Code, &code_group);
eval_group(RegisterGroup::Data, &data_group);
let mut pos = 0;
let mut coeff_u = vec![H::ExtElem::ZERO; eval_u.len()];
for reg in taps.regs() {
poly_interpolate(
&mut coeff_u[pos..],
&all_xs[pos..],
&eval_u[pos..],
reg.size(),
);
pos += reg.size();
}
let z_pow = z.pow(H::ExtElem::EXT_SIZE);
let which = Vec::from_iter(0u32..H::CHECK_SIZE as u32);
let xs = vec![z_pow; H::CHECK_SIZE];
let out = hal.alloc_extelem("out", H::CHECK_SIZE);
let which = hal.copy_from_u32("which", which.as_slice());
let xs = hal.copy_from_extelem("xs", xs.as_slice());
hal.batch_evaluate_any(&check_group.coeffs, H::CHECK_SIZE, &which, &xs, &out);
out.view(|view| {
coeff_u.extend(view);
});
debug!("Size of U = {}", coeff_u.len());
iop.write_field_elem_slice(&coeff_u);
let hash_u = sha.hash_raw_pod_slice(coeff_u.as_slice());
iop.commit(&hash_u);
let mix = H::ExtElem::random(&mut iop.rng);
debug!("Mix = {mix:?}");
let combo_count = taps.combos_size();
let combos = vec![H::ExtElem::ZERO; size * (combo_count + 1)];
let combos = hal.copy_from_extelem("combos", combos.as_slice());
let mut cur_mix = H::ExtElem::ONE;
let mut mix_group = |id: RegisterGroup, pg: &PolyGroup<H>| {
let mut which = Vec::new();
for reg in taps.group_regs(id) {
which.push(reg.combo_id() as u32);
}
let which_buf = hal.copy_from_u32("which", which.as_slice());
let group_size = taps.group_size(id);
hal.mix_poly_coeffs(
&combos, &cur_mix, &mix, &pg.coeffs, &which_buf, group_size, size,
);
cur_mix *= mix.pow(group_size);
};
mix_group(RegisterGroup::Accum, &accum_group);
mix_group(RegisterGroup::Code, &code_group);
mix_group(RegisterGroup::Data, &data_group);
let which = vec![combo_count as u32; H::CHECK_SIZE];
let which_buf = hal.copy_from_u32("which", which.as_slice());
hal.mix_poly_coeffs(
&combos,
&cur_mix,
&mix,
&check_group.coeffs,
&which_buf,
H::CHECK_SIZE,
size,
);
combos.view_mut(|combos| {
let mut cur_pos = 0;
let mut cur = H::ExtElem::ONE;
for reg in taps.regs() {
for i in 0..reg.size() {
combos[size * reg.combo_id() + i] -= cur * coeff_u[cur_pos + i];
}
cur *= mix;
cur_pos += reg.size();
}
for _ in 0..H::CHECK_SIZE {
combos[size * combo_count] -= cur * coeff_u[cur_pos];
cur_pos += 1;
cur *= mix;
}
for combo in 0..combo_count {
for back in taps.get_combo(combo).slice() {
assert_eq!(
poly_divide(
&mut combos[combo * size..combo * size + size],
z * back_one.pow((*back).into())
),
H::ExtElem::ZERO
);
}
}
assert_eq!(
poly_divide(
&mut combos[combo_count * size..combo_count * size + size],
z_pow
),
H::ExtElem::ZERO
);
});
let final_poly_coeffs = hal.alloc_elem("final_poly_coeffs", size * H::ExtElem::EXT_SIZE);
hal.eltwise_sum_extelem(&final_poly_coeffs, &combos);
hal.batch_bit_reverse(&final_poly_coeffs, H::ExtElem::EXT_SIZE);
debug!(
"FRI-proof, size = {}",
final_poly_coeffs.size() / H::ExtElem::EXT_SIZE
);
fri_prove(hal, &mut iop, &final_poly_coeffs, |iop, idx| {
accum_group.merkle.prove(iop, idx);
code_group.merkle.prove(iop, idx);
data_group.merkle.prove(iop, idx);
check_group.merkle.prove(iop, idx);
});
let proof = iop.proof;
debug!("Proof size = {}", proof.len());
proof
}
fn make_coeffs<H: Hal>(hal: &H, input: &[H::Elem], count: usize) -> H::BufferElem {
let buf = hal.copy_from_elem("input", input);
hal.batch_interpolate_ntt(&buf, count);
#[cfg(not(feature = "circuit_debug"))]
hal.zk_shift(&buf, count);
buf
}