Skip to content

Commit f3cc71f

Browse files
committed
partiall address review comments of #670
1 parent 5ae54f4 commit f3cc71f

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

aggregator/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ snark-verifier-sdk = { git = "https://github.com/scroll-tech/snark-verifier", br
2525

2626

2727
[features]
28-
default = [ ]
28+
default = [ "disable_proof_aggregation" ]
2929
print-trace = [ "ark-std/print-trace" ]
3030
# This feature is useful for unit tests where we check the SAT of pi aggregation circuit
3131
disable_proof_aggregation = []

aggregator/src/aggregation/circuit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ impl Circuit<Fr> for AggregationCircuit {
337337
);
338338

339339
region.constrain_equal(
340+
// in the keccak table, the input and output date have different
341+
// endianess
340342
chunk_pi_hash_digests[i][j * 8 + k].cell(),
341343
snark_inputs[i * DIGEST_LEN + (3 - j) * 8 + k].cell(),
342344
)?;

aggregator/src/aggregation/rlc/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl RlcConfig {
6464
let q2 = meta.query_selector(enable_challenge);
6565
let cs2 = q2 * (a - challenge_expr.keccak_input());
6666

67-
vec![cs1 + cs2]
67+
vec![cs1, cs2]
6868
});
6969
Self {
7070
#[cfg(test)]

aggregator/src/aggregation/rlc/gates.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ impl RlcConfig {
1616
region.assign_fixed(|| "const one", self.fixed, 1, || Value::known(Fr::one()))?;
1717
region.assign_fixed(|| "const two", self.fixed, 2, || Value::known(Fr::from(2)))?;
1818
region.assign_fixed(|| "const four", self.fixed, 3, || Value::known(Fr::from(4)))?;
19-
region.assign_fixed(
20-
|| "const eight",
21-
self.fixed,
22-
4,
23-
|| Value::known(Fr::from(8)),
24-
)?;
19+
region.assign_fixed(|| "const nine", self.fixed, 4, || Value::known(Fr::from(9)))?;
2520
region.assign_fixed(
2621
|| "const thirty two",
2722
self.fixed,
@@ -68,7 +63,7 @@ impl RlcConfig {
6863
}
6964

7065
#[inline]
71-
pub(crate) fn eight_cell(&self, region_index: RegionIndex) -> Cell {
66+
pub(crate) fn nine_cell(&self, region_index: RegionIndex) -> Cell {
7267
Cell {
7368
region_index,
7469
row_offset: 4,
@@ -288,7 +283,7 @@ impl RlcConfig {
288283
self.mul_add(region, b, &cond_not, &tmp, offset)
289284
}
290285

291-
// Returns inputs[0] + challenge * inputs[1] + ... + challenge^k * inputs[k]
286+
// Returns challenge^k * inputs[0] + ... + challenge * inputs[k-1] + inputs[k]
292287
#[allow(dead_code)]
293288
pub(crate) fn rlc(
294289
&self,
@@ -304,7 +299,8 @@ impl RlcConfig {
304299
Ok(acc)
305300
}
306301

307-
// Returns inputs[0] + challenge * inputs[1] + ... + challenge^k * inputs[k]
302+
// Returns challenge^k * inputs[0] * flag[0] + ... + challenge * inputs[k-1] * flag[k-1]] +
303+
// inputs[k]* flag[k]
308304
pub(crate) fn rlc_with_flag(
309305
&self,
310306
region: &mut Region<Fr>,

aggregator/src/core.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub(crate) fn extract_accumulators_and_proof(
100100
// 4. chunks are continuous: they are linked via the state roots
101101
// 5. batch and all its chunks use a same chain id
102102
// 6. chunk[i]'s prev_state_root == post_state_root when chunk[i] is padded
103-
// 7. chunk[i]'s data_hash == "" when chunk[i] is padded
103+
// 7. chunk[i]'s data_hash == keccak("") when chunk[i] is padded
104104
#[allow(clippy::type_complexity)]
105105
pub(crate) fn assign_batch_hashes(
106106
config: &AggregationConfig,
@@ -127,7 +127,7 @@ pub(crate) fn assign_batch_hashes(
127127
// 3. batch_data_hash and chunk[i].pi_hash use a same chunk[i].data_hash when chunk[i] is not
128128
// padded
129129
// 6. chunk[i]'s prev_state_root == post_state_root when chunk[i] is padded
130-
// 7. chunk[i]'s data_hash == "" when chunk[i] is padded
130+
// 7. chunk[i]'s data_hash == keccak("") when chunk[i] is padded
131131
let num_valid_snarks = conditional_constraints(
132132
&config.rlc_config,
133133
// config.flex_gate(),
@@ -405,7 +405,7 @@ fn copy_constraints(
405405
// 1. batch_data_hash digest is reused for public input hash
406406
// 3. batch_data_hash and chunk[i].pi_hash use a same chunk[i].data_hash when chunk[i] is not padded
407407
// 6. chunk[i]'s prev_state_root == post_state_root when chunk[i] is padded
408-
// 7. chunk[i]'s data_hash == "" when chunk[i] is padded
408+
// 7. chunk[i]'s data_hash == keccak("") when chunk[i] is padded
409409
#[allow(clippy::too_many_arguments)]
410410
pub(crate) fn conditional_constraints(
411411
rlc_config: &RlcConfig,
@@ -462,11 +462,11 @@ pub(crate) fn conditional_constraints(
462462
region.constrain_equal(four_cell, four.cell())?;
463463
four
464464
};
465-
let eight = {
466-
let eight = rlc_config.load_private(&mut region, &Fr::from(8), &mut offset)?;
467-
let eight_cell = rlc_config.eight_cell(eight.cell().region_index);
468-
region.constrain_equal(eight_cell, eight.cell())?;
469-
eight
465+
let nine = {
466+
let nine = rlc_config.load_private(&mut region, &Fr::from(9), &mut offset)?;
467+
let nine_cell = rlc_config.nine_cell(nine.cell().region_index);
468+
region.constrain_equal(nine_cell, nine.cell())?;
469+
nine
470470
};
471471
let flag1 = rlc_config.is_smaller_than(
472472
&mut region,
@@ -478,7 +478,7 @@ pub(crate) fn conditional_constraints(
478478
let not_flag3 = rlc_config.is_smaller_than(
479479
&mut region,
480480
&num_of_valid_snarks_cell[0],
481-
&eight,
481+
&nine,
482482
&mut offset,
483483
)?;
484484
let flag3 = rlc_config.not(&mut region, &not_flag3, &mut offset)?;
@@ -511,6 +511,11 @@ pub(crate) fn conditional_constraints(
511511
//
512512
// 1 batch_data_hash digest is reused for public input hash
513513
//
514+
// the following part of the code is hard coded for the case where
515+
// MAX_AGG_SNARKS <= 10
516+
// in theory it may support up to 12 SNARKS (not tested)
517+
// more SNARKs beyond 12 will require a revamp of the circuit
518+
//
514519
// public input hash is build as
515520
// keccak(
516521
// chain_id ||
@@ -519,6 +524,8 @@ pub(crate) fn conditional_constraints(
519524
// chunk[k-1].withdraw_root ||
520525
// batch_data_hash )
521526
//
527+
// batchDataHash = keccak(chunk[0].dataHash || ... || chunk[k-1].dataHash)
528+
//
522529
// #valid snarks | offset of data hash | flags
523530
// 1,2,3,4 | 0 | 1, 0, 0
524531
// 5,6,7,8 | 32 | 0, 1, 0
@@ -658,7 +665,7 @@ pub(crate) fn conditional_constraints(
658665
}
659666
}
660667

661-
// 7. chunk[i]'s data_hash == "" when chunk[i] is padded
668+
// 7. chunk[i]'s data_hash == keccak("") when chunk[i] is padded
662669
// that means the data_hash length is 32 * number_of_valid_snarks
663670
let const32 = rlc_config.load_private(&mut region, &Fr::from(32), &mut offset)?;
664671
let const32_cell = rlc_config.thirty_two_cell(const32.cell().region_index);

aggregator/src/util.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ pub(crate) fn get_indices(preimages: &[Vec<u8>]) -> (Vec<usize>, Vec<usize>) {
4343
let mut round_ctr = 0;
4444

4545
for preimage in preimages.iter().take(MAX_AGG_SNARKS + 1) {
46-
// 136 = 17 * 8 is the size in bits of each
46+
// 136 = 17 * 8 is the size in bytes of each
4747
// input chunk that can be processed by Keccak circuit using absorb
4848
// each chunk of size 136 needs 300 Keccak circuit rows to prove
4949
// which consists of 12 Keccak rows for each of 24 + 1 Keccak circuit rounds
5050
// digest only happens at the end of the last input chunk with
51-
// 4 Keccak circuit rounds, so 48 Keccak rows, and 300 - 48 = 256
51+
// 4 Keccak circuit rounds, so 48 Keccak rows, and 300 - 48 = 252
5252
let num_rounds = 1 + preimage.len() / INPUT_LEN_PER_ROUND;
5353
let mut preimage_padded = preimage.clone();
5454
preimage_padded.resize(INPUT_LEN_PER_ROUND * num_rounds, 0);
@@ -192,6 +192,9 @@ pub(crate) fn parse_hash_preimage_cells(
192192
Vec<&[AssignedCell<Fr, Fr>]>,
193193
&[AssignedCell<Fr, Fr>],
194194
) {
195+
// each pi hash has INPUT_LEN_PER_ROUND bytes as input
196+
// keccak will pad the input with another INPUT_LEN_PER_ROUND bytes
197+
// we extract all those bytes
195198
let batch_pi_hash_preimage = &hash_input_cells[0..INPUT_LEN_PER_ROUND * 2];
196199
let mut chunk_pi_hash_preimages = vec![];
197200
for i in 0..MAX_AGG_SNARKS {

0 commit comments

Comments
 (0)