forked from privacy-scaling-explorations/zkevm-circuits
-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathcircuit.rs
422 lines (374 loc) · 15.7 KB
/
circuit.rs
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
use ark_std::{end_timer, start_timer};
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
halo2curves::bn256::{Bn256, Fq, Fr, G1Affine},
plonk::{Circuit, ConstraintSystem, Error, Selector},
poly::{commitment::ParamsProver, kzg::commitment::ParamsKZG},
};
use itertools::Itertools;
use rand::Rng;
use std::{env, fs::File};
#[cfg(not(feature = "disable_proof_aggregation"))]
use snark_verifier::loader::halo2::halo2_ecc::halo2_base;
#[cfg(not(feature = "disable_proof_aggregation"))]
use snark_verifier::{
loader::halo2::{
halo2_ecc::halo2_base::{AssignedValue, Context, ContextParams},
Halo2Loader,
},
pcs::kzg::{Bdfg21, Kzg},
};
use snark_verifier::{
loader::native::NativeLoader,
pcs::kzg::{KzgAccumulator, KzgSuccinctVerifyingKey},
util::arithmetic::fe_to_limbs,
};
#[cfg(not(feature = "disable_proof_aggregation"))]
use snark_verifier_sdk::{aggregate, flatten_accumulator};
use snark_verifier_sdk::{CircuitExt, Snark, SnarkWitness};
use zkevm_circuits::util::Challenges;
use crate::{
batch::BatchHash,
constants::{ACC_LEN, BITS, DIGEST_LEN, LIMBS, MAX_AGG_SNARKS},
core::{assign_batch_hashes, extract_accumulators_and_proof},
util::parse_hash_digest_cells,
ConfigParams,
};
use super::AggregationConfig;
/// Aggregation circuit that does not re-expose any public inputs from aggregated snarks
#[derive(Clone)]
pub struct AggregationCircuit {
pub svk: KzgSuccinctVerifyingKey<G1Affine>,
// the input snarks for the aggregation circuit
// it is padded already so it will have a fixed length of MAX_AGG_SNARKS
pub snarks_with_padding: Vec<SnarkWitness>,
// the public instance for this circuit consists of
// - an accumulator (12 elements)
// - the batch's public_input_hash (32 elements)
pub flattened_instances: Vec<Fr>,
// accumulation scheme proof, private input
pub as_proof: Value<Vec<u8>>,
// batch hash circuit for which the snarks are generated
// the chunks in this batch are also padded already
pub batch_hash: BatchHash,
}
impl AggregationCircuit {
pub fn new(
params: &ParamsKZG<Bn256>,
snarks_with_padding: &[Snark],
rng: impl Rng + Send,
batch_hash: BatchHash,
) -> Result<Self, snark_verifier::Error> {
let timer = start_timer!(|| "generate aggregation circuit");
// sanity check: snarks's public input matches chunk_hashes
for (chunk, snark) in batch_hash
.chunks_with_padding
.iter()
.zip(snarks_with_padding.iter())
{
let chunk_hash_bytes = chunk.public_input_hash();
let snark_hash_bytes = &snark.instances[0];
assert_eq!(snark_hash_bytes.len(), ACC_LEN + DIGEST_LEN);
for i in 0..DIGEST_LEN {
// for each snark,
// first 12 elements are accumulator
// next 32 elements are public_input_hash
// accumulator + public_input_hash = snark public input
assert_eq!(
Fr::from(chunk_hash_bytes.as_bytes()[i] as u64),
snark_hash_bytes[i + ACC_LEN]
);
}
}
// extract the accumulators and proofs
let svk = params.get_g()[0].into();
// this aggregates MULTIPLE snarks
// (instead of ONE as in proof compression)
let (accumulator, as_proof) =
extract_accumulators_and_proof(params, snarks_with_padding, rng)?;
let KzgAccumulator::<G1Affine, NativeLoader> { lhs, rhs } = accumulator;
let acc_instances = [lhs.x, lhs.y, rhs.x, rhs.y]
.map(fe_to_limbs::<Fq, Fr, LIMBS, BITS>)
.concat();
// extract batch's public input hash
let public_input_hash = &batch_hash.instances_exclude_acc()[0];
// the public instance for this circuit consists of
// - an accumulator (12 elements)
// - the batch's public_input_hash (32 elements)
let flattened_instances: Vec<Fr> =
[acc_instances.as_slice(), public_input_hash.as_slice()].concat();
end_timer!(timer);
Ok(Self {
svk,
snarks_with_padding: snarks_with_padding.iter().cloned().map_into().collect(),
flattened_instances,
as_proof: Value::known(as_proof),
batch_hash,
})
}
pub fn as_proof(&self) -> Value<&[u8]> {
self.as_proof.as_ref().map(Vec::as_slice)
}
}
impl Circuit<Fr> for AggregationCircuit {
type Config = (AggregationConfig, Challenges);
type FloorPlanner = SimpleFloorPlanner;
fn without_witnesses(&self) -> Self {
unimplemented!()
}
fn configure(meta: &mut ConstraintSystem<Fr>) -> Self::Config {
let params = env::var("AGGREGATION_CONFIG").map_or_else(
|_| ConfigParams::aggregation_param(),
|path| {
serde_json::from_reader(
File::open(path.as_str()).unwrap_or_else(|_| panic!("{path:?} does not exist")),
)
.unwrap()
},
);
let challenges = Challenges::construct(meta);
let config = AggregationConfig::configure(meta, ¶ms, challenges);
log::info!(
"aggregation circuit configured with k = {} and {:?} advice columns",
params.degree,
params.num_advice
);
(config, challenges)
}
#[allow(clippy::type_complexity)]
fn synthesize(
&self,
config: Self::Config,
mut layouter: impl Layouter<Fr>,
) -> Result<(), Error> {
let (config, challenge) = config;
let witness_time = start_timer!(|| "synthesize | Aggregation Circuit");
let timer = start_timer!(|| "aggregation");
// ==============================================
// Step 1: snark aggregation circuit
// ==============================================
#[cfg(not(feature = "disable_proof_aggregation"))]
let (accumulator_instances, snark_inputs) = {
config
.range()
.load_lookup_table(&mut layouter)
.expect("load range lookup table");
let mut first_pass = halo2_base::SKIP_FIRST_PASS;
let (accumulator_instances, snark_inputs) = layouter.assign_region(
|| "aggregation",
|region| -> Result<(Vec<AssignedValue<Fr>>, Vec<AssignedValue<Fr>>), Error> {
if first_pass {
first_pass = false;
return Ok((vec![], vec![]));
}
// stores accumulators for all snarks, including the padded ones
let mut accumulator_instances: Vec<AssignedValue<Fr>> = vec![];
// stores public inputs for all snarks, including the padded ones
let mut snark_inputs: Vec<AssignedValue<Fr>> = vec![];
let ctx = Context::new(
region,
ContextParams {
max_rows: config.flex_gate().max_rows,
num_context_ids: 1,
fixed_columns: config.flex_gate().constants.clone(),
},
);
let ecc_chip = config.ecc_chip();
let loader = Halo2Loader::new(ecc_chip, ctx);
//
// extract the assigned values for
// - instances which are the public inputs of each chunk (prefixed with 12
// instances from previous accumulators)
// - new accumulator to be verified on chain
//
let (assigned_aggregation_instances, acc) = aggregate::<Kzg<Bn256, Bdfg21>>(
&self.svk,
&loader,
&self.snarks_with_padding,
self.as_proof(),
);
log::trace!("aggregation circuit during assigning");
for (i, e) in assigned_aggregation_instances[0].iter().enumerate() {
log::trace!("{}-th instance: {:?}", i, e.value)
}
// extract the following cells for later constraints
// - the accumulators
// - the public inputs from each snark
accumulator_instances.extend(flatten_accumulator(acc).iter().copied());
// the snark is not a fresh one, assigned_instances already contains an
// accumulator so we want to skip the first 12 elements from the public input
snark_inputs.extend(
assigned_aggregation_instances
.iter()
.flat_map(|instance_column| instance_column.iter().skip(ACC_LEN)),
);
config.range().finalize(&mut loader.ctx_mut());
loader.ctx_mut().print_stats(&["Range"]);
Ok((accumulator_instances, snark_inputs))
},
)?;
assert_eq!(snark_inputs.len(), MAX_AGG_SNARKS * DIGEST_LEN);
(accumulator_instances, snark_inputs)
};
end_timer!(timer);
// ==============================================
// step 2: public input aggregation circuit
// ==============================================
// extract all the hashes and load them to the hash table
let challenges = challenge.values(&layouter);
let timer = start_timer!(|| "load aux table");
let hash_digest_cells = {
config
.keccak_circuit_config
.load_aux_tables(&mut layouter)?;
end_timer!(timer);
let timer = start_timer!(|| "extract hash");
// orders:
// - batch_public_input_hash
// - chunk\[i\].piHash for i in \[0, MAX_AGG_SNARKS)
// - batch_data_hash_preimage
let preimages = self.batch_hash.extract_hash_preimages();
assert_eq!(
preimages.len(),
MAX_AGG_SNARKS + 2,
"error extracting preimages"
);
end_timer!(timer);
let timer = start_timer!(|| ("assign hash cells").to_string());
let chunks_are_valid = self
.batch_hash
.chunks_with_padding
.iter()
.map(|chunk| !chunk.is_padding)
.collect::<Vec<_>>();
let hash_digest_cells = assign_batch_hashes(
&config,
&mut layouter,
challenges,
&chunks_are_valid,
&preimages,
)
.map_err(|_e| Error::ConstraintSystemFailure)?;
end_timer!(timer);
hash_digest_cells
};
// digests
let (batch_pi_hash_digest, chunk_pi_hash_digests, _potential_batch_data_hash_digest) =
parse_hash_digest_cells(&hash_digest_cells);
// ==============================================
// step 3: assert public inputs to the snarks are correct
// ==============================================
for (i, chunk) in chunk_pi_hash_digests.iter().enumerate() {
let hash = self.batch_hash.chunks_with_padding[i].public_input_hash();
for j in 0..4 {
for k in 0..8 {
log::trace!(
"pi {:02x} {:?}",
hash[j * 8 + k],
chunk[8 * (3 - j) + k].value()
);
}
}
}
#[cfg(not(feature = "disable_proof_aggregation"))]
let mut first_pass = halo2_base::SKIP_FIRST_PASS;
#[cfg(not(feature = "disable_proof_aggregation"))]
layouter.assign_region(
|| "pi checks",
|mut region| -> Result<(), Error> {
if first_pass {
// this region only use copy constraints and do not affect the shape of the
// layouter
first_pass = false;
return Ok(());
}
for i in 0..MAX_AGG_SNARKS {
for j in 0..4 {
for k in 0..8 {
let mut t1 = Fr::default();
let mut t2 = Fr::default();
chunk_pi_hash_digests[i][j * 8 + k].value().map(|x| t1 = *x);
snark_inputs[i * DIGEST_LEN + (3 - j) * 8 + k]
.value()
.map(|x| t2 = *x);
log::trace!(
"{}-th snark: {:?} {:?}",
i,
chunk_pi_hash_digests[i][j * 8 + k].value(),
snark_inputs[i * DIGEST_LEN + (3 - j) * 8 + k].value()
);
region.constrain_equal(
// in the keccak table, the input and output data have different
// endianess
chunk_pi_hash_digests[i][j * 8 + k].cell(),
snark_inputs[i * DIGEST_LEN + (3 - j) * 8 + k].cell(),
)?;
}
}
}
Ok(())
},
)?;
// ==============================================
// step 4: assert public inputs to the aggregator circuit are correct
// ==============================================
// accumulator
#[cfg(not(feature = "disable_proof_aggregation"))]
{
assert!(accumulator_instances.len() == ACC_LEN);
for (i, v) in accumulator_instances.iter().enumerate() {
layouter.constrain_instance(v.cell(), config.instance, i)?;
}
}
// public input hash
for i in 0..4 {
for j in 0..8 {
log::trace!(
"pi (circuit vs real): {:?} {:?}",
batch_pi_hash_digest[i * 8 + j].value(),
self.instances()[0][(3 - i) * 8 + j + ACC_LEN]
);
layouter.constrain_instance(
batch_pi_hash_digest[i * 8 + j].cell(),
config.instance,
(3 - i) * 8 + j + ACC_LEN,
)?;
}
}
end_timer!(witness_time);
Ok(())
}
}
impl CircuitExt<Fr> for AggregationCircuit {
fn num_instance(&self) -> Vec<usize> {
// 12 elements from accumulator
// 32 elements from batch's public_input_hash
vec![ACC_LEN + DIGEST_LEN]
}
// 12 elements from accumulator
// 32 elements from batch's public_input_hash
fn instances(&self) -> Vec<Vec<Fr>> {
vec![self.flattened_instances.clone()]
}
fn accumulator_indices() -> Option<Vec<(usize, usize)>> {
// the accumulator are the first 12 cells in the instance
Some((0..ACC_LEN).map(|idx| (0, idx)).collect())
}
fn selectors(config: &Self::Config) -> Vec<Selector> {
// - advice columns from flex gate
// - selector from RLC gate
config.0.flex_gate().basic_gates[0]
.iter()
.map(|gate| gate.q_enable)
.into_iter()
.chain(
[
config.0.rlc_config.selector,
config.0.rlc_config.enable_challenge,
]
.iter()
.cloned(),
)
.collect()
}
}