Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 990a2d1

Browse files
committed
[fix] chain id u32 -> u64
1 parent c3e8207 commit 990a2d1

File tree

11 files changed

+60
-44
lines changed

11 files changed

+60
-44
lines changed

aggregator/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ It also performs public input aggregation, i.e., reducing the `64k` public eleme
4949
- last_chunk_post_state_root: 32 Field elements
5050
- last_chunk_withdraw_root: 32 Field elements
5151
- batch_public_input_hash: 32 Field elements
52-
- chain_id: 4 Field elements
52+
- chain_id: 8 Field elements
5353

5454
In addition, it attests that, for chunks indexed from `0` to `k-1`,
5555
- batch_data_hash := keccak(chunk_0.data_hash || ... || chunk_k-1.data_hash) where chunk_i.data_hash is a public input to the i-th batch snark circuit

aggregator/src/chunk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ethers_core::utils::keccak256;
1212
/// - the data hash of this chunk
1313
pub struct ChunkHash {
1414
/// Chain identifier
15-
pub(crate) chain_id: u32,
15+
pub(crate) chain_id: u64,
1616
/// state root before this chunk
1717
pub(crate) prev_state_root: H256,
1818
/// state root after this chunk

aggregator/src/core.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use zkevm_circuits::{
2727

2828
use crate::{
2929
util::{assert_equal, capacity, get_indices},
30-
LOG_DEGREE,
30+
CHAIN_ID_LEN, LOG_DEGREE,
3131
};
3232

3333
/// Input the hash input bytes,
@@ -127,12 +127,12 @@ pub(crate) fn assign_batch_hashes<F: Field>(
127127
for j in 0..8 {
128128
// sanity check
129129
assert_equal(
130-
&hash_input_cells[0][i * 8 + j + 100],
130+
&hash_input_cells[0][i * 8 + j + 96 + CHAIN_ID_LEN],
131131
&hash_output_cells[1][(3 - i) * 8 + j],
132132
);
133133
region.constrain_equal(
134134
// preimage and digest has different endianness
135-
hash_input_cells[0][i * 8 + j + 100].cell(),
135+
hash_input_cells[0][i * 8 + j + 96 + CHAIN_ID_LEN].cell(),
136136
hash_output_cells[1][(3 - i) * 8 + j].cell(),
137137
)?;
138138
}
@@ -158,29 +158,32 @@ pub(crate) fn assign_batch_hashes<F: Field>(
158158
for i in 0..32 {
159159
// 2.2.1 chunk[0].prev_state_root
160160
// sanity check
161-
assert_equal(&hash_input_cells[0][i + 4], &hash_input_cells[2][i + 4]);
161+
assert_equal(
162+
&hash_input_cells[0][i + CHAIN_ID_LEN],
163+
&hash_input_cells[2][i + CHAIN_ID_LEN],
164+
);
162165
region.constrain_equal(
163-
hash_input_cells[0][i + 4].cell(),
164-
hash_input_cells[2][i + 4].cell(),
166+
hash_input_cells[0][i + CHAIN_ID_LEN].cell(),
167+
hash_input_cells[2][i + CHAIN_ID_LEN].cell(),
165168
)?;
166169
// 2.2.2 chunk[k-1].post_state_root
167170
// sanity check
168171
assert_equal(
169-
&hash_input_cells[0][i + 36],
170-
&hash_input_cells[hash_num - 1][i + 36],
172+
&hash_input_cells[0][i + CHAIN_ID_LEN + 32],
173+
&hash_input_cells[hash_num - 1][i + CHAIN_ID_LEN + 32],
171174
);
172175
region.constrain_equal(
173-
hash_input_cells[0][i + 36].cell(),
174-
hash_input_cells[hash_num - 1][i + 36].cell(),
176+
hash_input_cells[0][i + CHAIN_ID_LEN + 32].cell(),
177+
hash_input_cells[hash_num - 1][i + CHAIN_ID_LEN + 32].cell(),
175178
)?;
176179
// 2.2.3 chunk[k-1].withdraw_root
177180
assert_equal(
178-
&hash_input_cells[0][i + 68],
179-
&hash_input_cells[hash_num - 1][i + 68],
181+
&hash_input_cells[0][i + CHAIN_ID_LEN + 64],
182+
&hash_input_cells[hash_num - 1][i + CHAIN_ID_LEN + 64],
180183
);
181184
region.constrain_equal(
182-
hash_input_cells[0][i + 68].cell(),
183-
hash_input_cells[hash_num - 1][i + 68].cell(),
185+
hash_input_cells[0][i + CHAIN_ID_LEN + 64].cell(),
186+
hash_input_cells[hash_num - 1][i + CHAIN_ID_LEN + 64].cell(),
184187
)?;
185188
}
186189

@@ -198,8 +201,11 @@ pub(crate) fn assign_batch_hashes<F: Field>(
198201
for (i, chunk) in hash_input_cells[1].chunks(32).enumerate().take(num_chunks) {
199202
for (j, cell) in chunk.iter().enumerate() {
200203
// sanity check
201-
assert_equal(cell, &hash_input_cells[2 + i][j + 100]);
202-
region.constrain_equal(cell.cell(), hash_input_cells[2 + i][j + 100].cell())?;
204+
assert_equal(cell, &hash_input_cells[2 + i][j + CHAIN_ID_LEN + 96]);
205+
region.constrain_equal(
206+
cell.cell(),
207+
hash_input_cells[2 + i][j + CHAIN_ID_LEN + 96].cell(),
208+
)?;
203209
}
204210
}
205211

@@ -208,21 +214,21 @@ pub(crate) fn assign_batch_hashes<F: Field>(
208214
for j in 0..32 {
209215
// sanity check
210216
assert_equal(
211-
&hash_input_cells[i + 3][4 + j],
212-
&hash_input_cells[i + 2][36 + j],
217+
&hash_input_cells[i + 3][CHAIN_ID_LEN + j],
218+
&hash_input_cells[i + 2][CHAIN_ID_LEN + 32 + j],
213219
);
214220
region.constrain_equal(
215221
// chunk[i+1].prevStateRoot
216-
hash_input_cells[i + 3][4 + j].cell(),
222+
hash_input_cells[i + 3][CHAIN_ID_LEN + j].cell(),
217223
// chunk[i].postStateRoot
218-
hash_input_cells[i + 2][36 + j].cell(),
224+
hash_input_cells[i + 2][CHAIN_ID_LEN + 32 + j].cell(),
219225
)?;
220226
}
221227
}
222228

223229
// 2.5 assert hashes use a same chain id
224230
for i in 0..num_chunks {
225-
for j in 0..4 {
231+
for j in 0..CHAIN_ID_LEN {
226232
// sanity check
227233
assert_equal(&hash_input_cells[0][j], &hash_input_cells[i + 2][j]);
228234
region.constrain_equal(

aggregator/src/proof_aggregation/circuit.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
core::{assign_batch_hashes, extract_accumulators_and_proof},
2828
param::{ConfigParams, BITS, LIMBS},
2929
proof_aggregation::config::AggregationConfig,
30-
BatchHashCircuit, ChunkHash,
30+
BatchHashCircuit, ChunkHash, CHAIN_ID_LEN,
3131
};
3232

3333
/// Aggregation circuit that does not re-expose any public inputs from aggregated snarks
@@ -329,19 +329,19 @@ impl Circuit<Fr> for AggregationCircuit {
329329
for i in 0..32 {
330330
// first_chunk_prev_state_root
331331
layouter.constrain_instance(
332-
hash_input_cells[2][4 + i].cell(),
332+
hash_input_cells[2][CHAIN_ID_LEN + i].cell(),
333333
config.instance,
334334
i + acc_len,
335335
)?;
336336
// last_chunk_post_state_root
337337
layouter.constrain_instance(
338-
hash_input_cells.last().unwrap()[36 + i].cell(),
338+
hash_input_cells.last().unwrap()[CHAIN_ID_LEN + 32 + i].cell(),
339339
config.instance,
340340
i + 32 + acc_len,
341341
)?;
342342
// last_chunk_withdraw_root
343343
layouter.constrain_instance(
344-
hash_input_cells.last().unwrap()[68 + i].cell(),
344+
hash_input_cells.last().unwrap()[CHAIN_ID_LEN + 64 + i].cell(),
345345
config.instance,
346346
i + 64 + acc_len,
347347
)?;
@@ -357,8 +357,8 @@ impl Circuit<Fr> for AggregationCircuit {
357357
)?;
358358
}
359359
}
360-
// last 4 inputs are the chain id
361-
for i in 0..4 {
360+
// last 8 inputs are the chain id
361+
for i in 0..CHAIN_ID_LEN {
362362
layouter.constrain_instance(
363363
hash_input_cells[0][i].cell(),
364364
config.instance,

aggregator/src/proof_aggregation/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct AggregationConfig {
2929
pub keccak_circuit_config: KeccakCircuitConfig<Fr>,
3030
/// Instance for public input; stores
3131
/// - accumulator from aggregation (12 elements)
32-
/// - aggregated public inputs (132 elements):
32+
/// - aggregated public inputs (136 elements):
3333
/// chain_id ||
3434
/// chunk\[0\].prev_state_root ||
3535
/// chunk\[k-1\].post_state_root ||

aggregator/src/proof_aggregation/public_input_aggregation.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
//! - last_chunk_post_state_root: 32 Field elements
3535
//! - last_chunk_withdraw_root: 32 Field elements
3636
//! - batch_public_input_hash: 32 Field elements
37-
//! - chain_id: 4 Field elements
37+
//! - chain_id: 8 Field elements
3838
//!
3939
//! ## Constraints
4040
//! The circuit attests the following statements:
@@ -66,6 +66,9 @@ pub use config::{BatchCircuitConfig, BatchCircuitConfigArgs};
6666
// TODO(ZZ): update to the right degree
6767
pub(crate) const LOG_DEGREE: u32 = 19;
6868

69+
// A chain_id is u64 and uses 8 bytes
70+
pub(crate) const CHAIN_ID_LEN: usize = 8;
71+
6972
// Each round requires (NUM_ROUNDS+1) * DEFAULT_KECCAK_ROWS = 300 rows.
7073
// This library is hard coded for this parameter.
7174
// Modifying the following parameters may result into bugs.

aggregator/src/proof_aggregation/public_input_aggregation/circuit.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use halo2_proofs::{
99

1010
use zkevm_circuits::util::{Challenges, SubCircuitConfig};
1111

12-
use crate::{core::assign_batch_hashes, BatchHash, ChunkHash};
12+
use crate::{core::assign_batch_hashes, BatchHash, ChunkHash, CHAIN_ID_LEN};
1313

1414
use super::config::{BatchCircuitConfig, BatchCircuitConfigArgs};
1515

@@ -19,7 +19,7 @@ use super::config::{BatchCircuitConfig, BatchCircuitConfigArgs};
1919
/// generate the circuit.
2020
#[derive(Clone, Debug, Default)]
2121
pub struct BatchHashCircuit<F: Field> {
22-
pub(crate) chain_id: u32,
22+
pub(crate) chain_id: u64,
2323
pub(crate) chunks: Vec<ChunkHash>,
2424
pub(crate) batch: BatchHash,
2525
_phantom: PhantomData<F>,
@@ -28,7 +28,7 @@ pub struct BatchHashCircuit<F: Field> {
2828
/// Public input to a batch circuit.
2929
/// In raw format. I.e., before converting to field elements.
3030
pub struct BatchHashCircuitPublicInput {
31-
pub(crate) chain_id: u32,
31+
pub(crate) chain_id: u64,
3232
pub(crate) first_chunk_prev_state_root: H256,
3333
pub(crate) last_chunk_post_state_root: H256,
3434
pub(crate) last_chunk_withdraw_root: H256,
@@ -184,19 +184,19 @@ impl<F: Field> Circuit<F> for BatchHashCircuit<F> {
184184
for i in 0..32 {
185185
// first_chunk_prev_state_root
186186
layouter.constrain_instance(
187-
hash_input_cells[2][4 + i].cell(),
187+
hash_input_cells[2][CHAIN_ID_LEN + i].cell(),
188188
config.hash_digest_column,
189189
i,
190190
)?;
191191
// last_chunk_post_state_root
192192
layouter.constrain_instance(
193-
hash_input_cells.last().unwrap()[36 + i].cell(),
193+
hash_input_cells.last().unwrap()[CHAIN_ID_LEN + 32 + i].cell(),
194194
config.hash_digest_column,
195195
i + 32,
196196
)?;
197197
// last_chunk_withdraw_root
198198
layouter.constrain_instance(
199-
hash_input_cells.last().unwrap()[68 + i].cell(),
199+
hash_input_cells.last().unwrap()[CHAIN_ID_LEN + 64 + i].cell(),
200200
config.hash_digest_column,
201201
i + 64,
202202
)?;
@@ -212,8 +212,8 @@ impl<F: Field> Circuit<F> for BatchHashCircuit<F> {
212212
)?;
213213
}
214214
}
215-
// last 4 inputs are the chain id
216-
for i in 0..4 {
215+
// last 8 inputs are the chain id
216+
for i in 0..CHAIN_ID_LEN {
217217
layouter.constrain_instance(
218218
hash_input_cells[0][i].cell(),
219219
config.hash_digest_column,

aggregator/src/proof_aggregation/public_input_aggregation/circuit_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::BatchHashCircuit;
55

66
impl<F: Field> CircuitExt<F> for BatchHashCircuit<F> {
77
fn num_instance(&self) -> Vec<usize> {
8-
vec![132]
8+
vec![136]
99
}
1010

1111
/// Compute the public inputs for this circuit.

aggregator/src/tests/mock_chunk/circuit_ext.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
use halo2_proofs::halo2curves::bn256::Fr;
22
use snark_verifier_sdk::CircuitExt;
33

4+
use crate::CHAIN_ID_LEN;
5+
46
use super::MockChunkCircuit;
57

68
impl CircuitExt<Fr> for MockChunkCircuit {
79
/// 64 elements from digest
810
fn num_instance(&self) -> Vec<usize> {
9-
vec![64]
11+
vec![64 + CHAIN_ID_LEN]
1012
}
1113

1214
/// return vec![data hash | public input hash]
1315
fn instances(&self) -> Vec<Vec<Fr>> {
14-
vec![self
16+
vec![
17+
self.chain_id.to_le_bytes().iter().zip(
18+
19+
self
1520
.chunk
1621
.data_hash
1722
.as_bytes()
1823
.iter()
19-
.chain(self.chunk.public_input_hash().as_bytes().iter())
24+
.chain(self.chunk.public_input_hash().as_bytes().iter()))
2025
.map(|&x| Fr::from(x as u64))
2126
.collect()]
2227
}

aggregator/src/util.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ pub(crate) fn get_indices(preimages: &[Vec<u8>]) -> (Vec<usize>, Vec<usize>) {
3131

3232
for preimage in preimages.iter() {
3333
let num_rounds = 1 + preimage.len() / 136;
34-
for (i, round) in preimage.chunks(136).enumerate() {
34+
let mut preimage_padded = preimage.clone();
35+
preimage_padded.resize(136 * num_rounds, 0);
36+
for (i, round) in preimage_padded.chunks(136).enumerate() {
3537
// indices for preimages
3638
for (j, _chunk) in round.chunks(8).into_iter().enumerate() {
3739
for k in 0..8 {

0 commit comments

Comments
 (0)