Skip to content

Commit

Permalink
refactor codes to be compatible with "use scroll l2 trace for super c…
Browse files Browse the repository at this point in the history
…ircuit testing in zkevm-circuits" feature (#249)

* wip: refactor according to new CircuitBuilder

* dump zkevm-circuits' version and pass compile

* wip: refactor builder

* update lock

* Update mock.rs

* bump version of zkevm-circuits

* refactor circuit builder and ccc

* post merging fixes

* bump zkevm-circuits
temporary disable missed circuit params

* bump circuit version
refine ccc with new circuibuilder constructor

* clippy and fmt

* bump zkevm

* develop branch uses zkevm-circuits/decelop (#248)

* run

* upgrade zkevm-circuits

* fix tx circuit ccc

* Merge remote-tracking branch 'origin/main' into develop_pre

* resume missed changes

---------

Co-authored-by: Zhang Zhuo <[email protected]>
  • Loading branch information
noel2004 and lispc authored Aug 29, 2023
1 parent 3d33c63 commit bcfef14
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 644 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ edition = "2021"
[dependencies]
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2023_02_02" }

aggregator = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.5" }
bus-mapping = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.5" }
eth-types = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.5" }
zkevm-circuits = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.5", default-features = false, features = ["test","scroll","scroll-trace","shanghai"] }
mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.5" }
mock = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.5" }
aggregator = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop" }
bus-mapping = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop" }
eth-types = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop" }
zkevm-circuits = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop", default-features = false, features = ["test","scroll","scroll-trace","shanghai"] }
mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop" }
mock = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "develop" }

snark-verifier = { git = "https://github.com/scroll-tech/snark-verifier", tag = "v0.1.2" }
snark-verifier-sdk = { git = "https://github.com/scroll-tech/snark-verifier", tag = "v0.1.2" }
Expand Down
2 changes: 1 addition & 1 deletion prover/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{
str::FromStr,
sync::Once,
};
use types::eth::{BlockTrace, BlockTraceJsonRpcResult};
use types::{eth::BlockTrace, BlockTraceJsonRpcResult};
use zkevm_circuits::evm_circuit::witness::Block;

pub static LOGGER: Once = Once::new();
Expand Down
80 changes: 46 additions & 34 deletions prover/src/zkevm/capacity_checker.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::collections::HashMap;

use super::circuit::{
MAX_BYTECODE, MAX_CALLDATA, MAX_EXP_STEPS, MAX_KECCAK_ROWS, MAX_MPT_ROWS, MAX_POSEIDON_ROWS,
MAX_RWS, MAX_VERTICLE_ROWS,
};

use super::circuit::{
block_traces_to_witness_block_with_updated_state, calculate_row_usage_of_witness_block,
fill_zktrie_state_from_proofs,
get_super_circuit_params,
};
use bus_mapping::{
circuit_input_builder::{self, CircuitInputBuilder},
state_db::{CodeDB, StateDB},
};
use eth_types::H256;
use eth_types::{ToWord, H256};
use itertools::Itertools;
use mpt_zktrie::state::ZktrieState;
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -52,7 +54,7 @@ impl RowUsage {
MAX_BYTECODE, // bytecode
MAX_RWS, // copy
MAX_KECCAK_ROWS, // keccak
MAX_CALLDATA, // tx
MAX_VERTICLE_ROWS, // tx
MAX_CALLDATA, // rlp
7 * MAX_EXP_STEPS, // exp
MAX_KECCAK_ROWS, // modexp
Expand Down Expand Up @@ -118,9 +120,7 @@ pub struct CircuitCapacityChecker {
pub light_mode: bool,
pub acc_row_usage: RowUsage,
pub row_usages: Vec<RowUsage>,
pub state: Option<ZktrieState>,
// poseidon codehash to code len
pub codelen: HashMap<H256, usize>,
pub builder_ctx: Option<(CodeDB, StateDB, ZktrieState)>,
}

// Currently TxTrace is same as BlockTrace, with "transactions" and "executionResults" should be of
Expand All @@ -139,44 +139,51 @@ impl CircuitCapacityChecker {
Self {
acc_row_usage: RowUsage::new(),
row_usages: Vec::new(),
state: None,
light_mode: true,
codelen: HashMap::new(),
builder_ctx: None,
}
}
pub fn reset(&mut self) {
self.state = None;
self.builder_ctx = None;
self.acc_row_usage = RowUsage::new();
self.row_usages = Vec::new();
self.codelen = HashMap::new();
}
pub fn estimate_circuit_capacity(
&mut self,
txs: &[TxTrace],
) -> Result<RowUsage, anyhow::Error> {
assert!(!txs.is_empty());
if self.state.is_none() {
self.state = Some(ZktrieState::construct(txs[0].storage_trace.root_before));
}
let traces = txs;
let state = self.state.as_mut().unwrap();
fill_zktrie_state_from_proofs(state, traces, self.light_mode)?;
let (witness_block, codedb) =
block_traces_to_witness_block_with_updated_state(traces, state, self.light_mode)?;
let mut rows = calculate_row_usage_of_witness_block(&witness_block)?;

// Dedup bytecode row usage for bytecode circuit / poseidon circuit
for (hash, bytes) in &codedb.0 {
if self.codelen.contains_key(hash) {
assert_eq!(rows[2].name, "bytecode");
rows[2].row_num_real -= bytes.len() + 1;
assert_eq!(rows[10].name, "poseidon");
rows[10].row_num_real -= bytes.len() / (31 * 2) * 9;
} else {
self.codelen.insert(*hash, bytes.len());
}
}

let mut estimate_builder = if let Some((code_db, sdb, mpt_state)) = self.builder_ctx.take()
{
// here we create a new builder for another (sealed) witness block
// this builder inherit the current execution state (sdb/cdb) of
// the previous one and do not use zktrie state,
// notice the prev_root in current builder may be not invalid (since the state has
// changed but we may not update it in light mode)
let mut builder_block =
circuit_input_builder::Block::from_headers(&[], get_super_circuit_params());
builder_block.chain_id = txs[0].chain_id;
builder_block.start_l1_queue_index = txs[0].start_l1_queue_index;
builder_block.prev_state_root = H256(*mpt_state.root()).to_word();
let mut builder =
CircuitInputBuilder::new_with_trie_state(sdb, code_db, mpt_state, &builder_block);
builder.add_more_l2_trace(&txs[0], txs.len() > 1, true)?;
builder
} else {
CircuitInputBuilder::new_from_l2_trace(
get_super_circuit_params(),
&txs[0],
txs.len() > 1,
true,
)?
};
let traces = &txs[1..];
let witness_block = block_traces_to_witness_block_with_updated_state(
traces,
&mut estimate_builder,
self.light_mode,
)?;
let rows = calculate_row_usage_of_witness_block(&witness_block)?;
let row_usage_details: Vec<SubCircuitRowUsage> = rows
.into_iter()
.map(|x| SubCircuitRowUsage {
Expand All @@ -187,6 +194,11 @@ impl CircuitCapacityChecker {
let tx_row_usage = RowUsage::from_row_usage_details(row_usage_details);
self.row_usages.push(tx_row_usage.clone());
self.acc_row_usage.add(&tx_row_usage);
self.builder_ctx.replace((
estimate_builder.code_db,
estimate_builder.sdb,
estimate_builder.mpt_init_state,
));
Ok(self.acc_row_usage.normalize())
}
}
4 changes: 2 additions & 2 deletions prover/src/zkevm/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::utils::read_env_var;
pub use self::builder::{
block_traces_to_padding_witness_block, block_traces_to_witness_block,
block_traces_to_witness_block_with_updated_state, calculate_row_usage_of_trace,
calculate_row_usage_of_witness_block, check_batch_capacity, fill_zktrie_state_from_proofs,
normalize_withdraw_proof, storage_trace_to_padding_witness_block, WitnessBlock,
calculate_row_usage_of_witness_block, check_batch_capacity, get_super_circuit_params,
normalize_withdraw_proof, WitnessBlock,
};
pub use builder::{
MAX_BYTECODE, MAX_CALLDATA, MAX_EXP_STEPS, MAX_INNER_BLOCKS, MAX_KECCAK_ROWS, MAX_MPT_ROWS,
Expand Down
Loading

0 comments on commit bcfef14

Please sign in to comment.