Skip to content

Commit eb2439a

Browse files
authored
feat: add row usages to chunk proof, and refactor prover codes (#1314)
* change keccak degree for agg circuit * fix agg keccak rows * fix config * disable some agg tests * avoid to regen WitnessBlock when chunk proof can be loaded * add type ChunkProvingTask and BatchProvingTask * fix * clean deps * try * Revert "try" This reverts commit c671eef. * Revert "clean deps" This reverts commit 7993665. * get_step_reported_error return err instead of panic * add row usage to chunk proof * use BatchProvingTask * done * rename ChunkHash to ChunkInfo as golang side * more on renaming ChunkHash to ChunkInfo as golang side * fmt * fmt * update comments * lint * make ChunkProvingTask.chunk_info optional * upgrade workspace Cargo.toml version to v0.11.0 * add ChunkProvingTask::from(Vec<BlockTrace>) * simplify codes about proof and add some comments * super circuit prover can return snark
1 parent 53ff881 commit eb2439a

30 files changed

+359
-282
lines changed

.rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
wrap_comments = false

Cargo.lock

+13-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ members = [
1717
resolver = "2"
1818

1919
[workspace.package]
20-
version = "0.1.0"
20+
version = "0.11.0"
2121
edition = "2021"
2222
license = "MIT OR Apache-2.0"
2323

aggregator/src/batch.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ethers_core::utils::keccak256;
66

77
use crate::{
88
blob::{BatchData, PointEvaluationAssignments},
9-
chunk::ChunkHash,
9+
chunk::ChunkInfo,
1010
};
1111

1212
#[derive(Default, Debug, Clone)]
@@ -23,7 +23,7 @@ pub struct BatchHash<const N_SNARKS: usize> {
2323
/// chunks with padding.
2424
/// - the first [0..number_of_valid_chunks) are real ones
2525
/// - the last [number_of_valid_chunks, N_SNARKS) are padding
26-
pub(crate) chunks_with_padding: Vec<ChunkHash>,
26+
pub(crate) chunks_with_padding: Vec<ChunkInfo>,
2727
/// The batch data hash:
2828
/// - keccak256([chunk.hash for chunk in batch])
2929
pub(crate) data_hash: H256,
@@ -41,7 +41,7 @@ pub struct BatchHash<const N_SNARKS: usize> {
4141

4242
impl<const N_SNARKS: usize> BatchHash<N_SNARKS> {
4343
/// Build Batch hash from an ordered list of #N_SNARKS of chunks.
44-
pub fn construct(chunks_with_padding: &[ChunkHash]) -> Self {
44+
pub fn construct(chunks_with_padding: &[ChunkInfo]) -> Self {
4545
assert_eq!(
4646
chunks_with_padding.len(),
4747
N_SNARKS,
@@ -111,7 +111,7 @@ impl<const N_SNARKS: usize> BatchHash<N_SNARKS> {
111111
let preimage = chunks_with_padding
112112
.iter()
113113
.take(number_of_valid_chunks)
114-
.flat_map(|chunk_hash| chunk_hash.data_hash.0.iter())
114+
.flat_map(|chunk_info| chunk_info.data_hash.0.iter())
115115
.cloned()
116116
.collect::<Vec<_>>();
117117
let batch_data_hash = keccak256(preimage);

aggregator/src/blob.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
aggregation::{interpolate, witgen::init_zstd_encoder, BLS_MODULUS},
3-
BatchHash, ChunkHash,
3+
BatchHash, ChunkInfo,
44
};
55

66
use eth_types::{ToBigEndian, H256, U256};
@@ -173,7 +173,7 @@ impl<const N_SNARKS: usize> BatchData<N_SNARKS> {
173173
N_BATCH_BYTES + Self::n_rows_digest()
174174
}
175175

176-
pub(crate) fn new(num_valid_chunks: usize, chunks_with_padding: &[ChunkHash]) -> Self {
176+
pub(crate) fn new(num_valid_chunks: usize, chunks_with_padding: &[ChunkInfo]) -> Self {
177177
assert!(num_valid_chunks > 0);
178178
assert!(num_valid_chunks <= N_SNARKS);
179179

aggregator/src/chunk.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ use zkevm_circuits::witness::Block;
88

99
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1010
/// A chunk is a set of continuous blocks.
11-
/// A ChunkHash consists of 5 hashes, representing the changes incurred by this chunk of blocks:
11+
/// ChunkInfo is metadata of chunk, with following fields:
1212
/// - state root before this chunk
1313
/// - state root after this chunk
1414
/// - the withdraw root after this chunk
1515
/// - the data hash of this chunk
1616
/// - the tx data hash of this chunk
17+
/// - flattened L2 tx bytes
1718
/// - if the chunk is padded (en empty but valid chunk that is padded for aggregation)
18-
pub struct ChunkHash {
19+
pub struct ChunkInfo {
1920
/// Chain identifier
2021
pub chain_id: u64,
2122
/// state root before this chunk
@@ -33,7 +34,7 @@ pub struct ChunkHash {
3334
pub is_padding: bool,
3435
}
3536

36-
impl ChunkHash {
37+
impl ChunkInfo {
3738
/// Construct by a witness block.
3839
pub fn from_witness_block(block: &Block, is_padding: bool) -> Self {
3940
// <https://github.com/scroll-tech/zkevm-circuits/blob/25dd32aa316ec842ffe79bb8efe9f05f86edc33e/bus-mapping/src/circuit_input_builder.rs#L690>
@@ -121,9 +122,9 @@ impl ChunkHash {
121122
H256(keccak256(&self.tx_bytes))
122123
}
123124

124-
/// Sample a chunk hash from random (for testing)
125+
/// Sample a chunk info from random (for testing)
125126
#[cfg(test)]
126-
pub(crate) fn mock_random_chunk_hash_for_testing<R: rand::RngCore>(r: &mut R) -> Self {
127+
pub(crate) fn mock_random_chunk_info_for_testing<R: rand::RngCore>(r: &mut R) -> Self {
127128
use eth_types::Address;
128129
use ethers_core::types::TransactionRequest;
129130
use rand::{
@@ -194,7 +195,7 @@ impl ChunkHash {
194195

195196
/// Build a padded chunk from previous one
196197
#[cfg(test)]
197-
pub(crate) fn mock_padded_chunk_hash_for_testing(previous_chunk: &Self) -> Self {
198+
pub(crate) fn mock_padded_chunk_info_for_testing(previous_chunk: &Self) -> Self {
198199
assert!(
199200
!previous_chunk.is_padding,
200201
"previous chunk is padded already"

aggregator/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod tests;
2727
pub use self::core::extract_proof_and_instances_with_pairing_check;
2828
pub use aggregation::*;
2929
pub use batch::BatchHash;
30-
pub use chunk::ChunkHash;
30+
pub use chunk::ChunkInfo;
3131
pub use compression::*;
3232
pub use constants::MAX_AGG_SNARKS;
3333
pub(crate) use constants::*;

aggregator/src/tests/aggregation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use snark_verifier_sdk::{gen_pk, gen_snark_shplonk, verify_snark_shplonk, Circui
88

99
use crate::{
1010
aggregation::AggregationCircuit, batch::BatchHash, constants::MAX_AGG_SNARKS, layer_0,
11-
tests::mock_chunk::MockChunkCircuit, ChunkHash,
11+
tests::mock_chunk::MockChunkCircuit, ChunkInfo,
1212
};
1313

1414
// See https://github.com/scroll-tech/zkevm-circuits/pull/1311#issuecomment-2139559866
@@ -141,13 +141,13 @@ fn build_new_aggregation_circuit<const N_SNARKS: usize>(
141141
let params = gen_srs(k0);
142142

143143
let mut chunks_without_padding = (0..num_real_chunks)
144-
.map(|_| ChunkHash::mock_random_chunk_hash_for_testing(&mut rng))
144+
.map(|_| ChunkInfo::mock_random_chunk_info_for_testing(&mut rng))
145145
.collect_vec();
146146
for i in 0..num_real_chunks - 1 {
147147
chunks_without_padding[i + 1].prev_state_root = chunks_without_padding[i].post_state_root;
148148
}
149149
let padded_chunk =
150-
ChunkHash::mock_padded_chunk_hash_for_testing(&chunks_without_padding[num_real_chunks - 1]);
150+
ChunkInfo::mock_padded_chunk_info_for_testing(&chunks_without_padding[num_real_chunks - 1]);
151151
let chunks_with_padding = [
152152
chunks_without_padding,
153153
vec![padded_chunk; N_SNARKS - num_real_chunks],

aggregator/src/tests/mock_chunk.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use zkevm_circuits::{table::KeccakTable, util::Challenges};
1313

1414
use crate::{
1515
constants::{ACC_LEN, DIGEST_LEN},
16-
ChunkHash, RlcConfig, LOG_DEGREE,
16+
ChunkInfo, RlcConfig, LOG_DEGREE,
1717
};
1818

1919
/// This config is used to compute RLCs for bytes.
@@ -36,11 +36,11 @@ pub struct MockConfig {
3636
pub(crate) struct MockChunkCircuit {
3737
// This circuit has an accumulator if it has already gone through compression
3838
pub(crate) has_accumulator: bool,
39-
pub(crate) chunk: ChunkHash,
39+
pub(crate) chunk: ChunkInfo,
4040
}
4141

4242
impl MockChunkCircuit {
43-
pub(crate) fn new(has_accumulator: bool, chunk: ChunkHash) -> Self {
43+
pub(crate) fn new(has_accumulator: bool, chunk: ChunkInfo) -> Self {
4444
MockChunkCircuit {
4545
has_accumulator,
4646
chunk,
@@ -54,11 +54,11 @@ impl MockChunkCircuit {
5454
has_accumulator: bool,
5555
is_padding: bool,
5656
) -> Self {
57-
let chunk = ChunkHash::mock_random_chunk_hash_for_testing(r);
57+
let chunk = ChunkInfo::mock_random_chunk_info_for_testing(r);
5858
Self {
5959
has_accumulator,
6060
chunk: if is_padding {
61-
ChunkHash::mock_padded_chunk_hash_for_testing(&chunk)
61+
ChunkInfo::mock_padded_chunk_info_for_testing(&chunk)
6262
} else {
6363
chunk
6464
},

bus-mapping/src/circuit_input_builder/input_state_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ impl<'a> CircuitInputStateRef<'a> {
16541654
}
16551655

16561656
if let Some(error) = step.error {
1657-
return Ok(Some(get_step_reported_error(&step.op, error)));
1657+
return Ok(Some(get_step_reported_error(&step.op, error)?));
16581658
}
16591659

16601660
let call = self.call()?;

bus-mapping/src/error.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,11 @@ pub enum ExecError {
180180
}
181181

182182
// TODO: Move to impl block.
183-
pub(crate) fn get_step_reported_error(op: &OpcodeId, error: GethExecError) -> ExecError {
184-
match error {
183+
pub(crate) fn get_step_reported_error(
184+
op: &OpcodeId,
185+
error: GethExecError,
186+
) -> Result<ExecError, Error> {
187+
Ok(match error {
185188
GethExecError::OutOfGas | GethExecError::GasUintOverflow => {
186189
// NOTE: We report a GasUintOverflow error as an OutOfGas error
187190
let oog_err = match op {
@@ -218,6 +221,12 @@ pub(crate) fn get_step_reported_error(op: &OpcodeId, error: GethExecError) -> Ex
218221
GethExecError::StackOverflow { .. } => ExecError::StackOverflow,
219222
GethExecError::StackUnderflow { .. } => ExecError::StackUnderflow,
220223
GethExecError::WriteProtection => ExecError::WriteProtection,
221-
_ => panic!("Unknown GethExecStep.error: {error}"),
222-
}
224+
_ => {
225+
log::error!("Unknown GethExecStep.error: {error}");
226+
let err_msg = format!("Unknown GethExecStep.error: {op:?} {error}");
227+
return Err(Error::InvalidGethExecTrace(Box::leak(
228+
err_msg.into_boxed_str(),
229+
)));
230+
}
231+
})
223232
}

prover/src/aggregator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod prover;
22
mod verifier;
33

4-
pub use self::prover::Prover;
4+
pub use self::prover::{check_chunk_hashes, Prover};
55
pub use aggregator::{BatchHash, MAX_AGG_SNARKS};
66
pub use verifier::Verifier;

0 commit comments

Comments
 (0)