Skip to content

Commit

Permalink
v0.7.2: dedup same bytecode row usage in ccc; upgrade to latest zkevm…
Browse files Browse the repository at this point in the history
…-circuits (#240)

* adjust row limit

* towards v0.7.1

* done

* minor

* minor

* fix

* fix

* Update to `v0.7.2`.

* Update

* Update prover/src/zkevm/capacity_checker.rs

Co-authored-by: Haichen Shen <shenhaichen@gmail.com>

* Delete useless import.

---------

Co-authored-by: Steven Gu <asongala@163.com>
Co-authored-by: Haichen Shen <shenhaichen@gmail.com>
  • Loading branch information
3 people authored Aug 22, 2023
1 parent f8b9de7 commit a29cbaa
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 118 deletions.
30 changes: 15 additions & 15 deletions Cargo.lock

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

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ maingate = { git = "https://github.com/scroll-tech/halo2wrong", branch = "halo2-
[patch."https://github.com/privacy-scaling-explorations/halo2curves.git"]
halo2curves = { git = "https://github.com/scroll-tech/halo2curves.git", branch = "0.3.1-derive-serde" }

[patch."https://github.com/scroll-tech/snark-verifier"]
snark-verifier = { git = "https://github.com/scroll-tech//snark-verifier", tag = "v0.1.1" }
snark-verifier-sdk = { git = "https://github.com/scroll-tech//snark-verifier", tag = "v0.1.1" }

[profile.test]
opt-level = 3
debug-assertions = true
Expand Down
2 changes: 1 addition & 1 deletion bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bin"
version = "0.6.4"
version = "0.7.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
14 changes: 7 additions & 7 deletions prover/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
name = "prover"
version = "0.6.4"
version = "0.7.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[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.6.5" }
bus-mapping = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.6.5" }
eth-types = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.6.5" }
zkevm-circuits = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.6.5", default-features = false, features = ["test","scroll","scroll-trace","shanghai"] }
mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.6.5" }
mock = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.6.5" }
aggregator = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.2" }
bus-mapping = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.2" }
eth-types = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.2" }
zkevm-circuits = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.2", default-features = false, features = ["test","scroll","scroll-trace","shanghai"] }
mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.2" }
mock = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.7.2" }

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
79 changes: 42 additions & 37 deletions prover/src/zkevm/capacity_checker.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
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,
};
use eth_types::H256;
use itertools::Itertools;
use mpt_zktrie::state::ZktrieState;
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -38,41 +46,21 @@ impl RowUsage {
}
// We treat 1M as 100%
pub fn normalize(&self) -> Self {
/*
const MAX_TXS: usize = 100;
const MAX_INNER_BLOCKS: usize = 100;
const MAX_EXP_STEPS: usize = 10_000;
const MAX_CALLDATA: usize = 400_000;
const MAX_BYTECODE: usize = 400_000;
const MAX_MPT_ROWS: usize = 400_000;
const MAX_KECCAK_ROWS: usize = 524_000;
const MAX_RWS: usize = 1_000_000;
const MAX_PRECOMPILE_EC_ADD: usize = 50;
const MAX_PRECOMPILE_EC_MUL: usize = 50;
const MAX_PRECOMPILE_EC_PAIRING: usize = 2;
*/
use super::circuit::{
MAX_BYTECODE, MAX_CALLDATA, MAX_EXP_STEPS, MAX_KECCAK_ROWS, MAX_MPT_ROWS, MAX_RWS,
};
// 14 in total
// "evm", "state", "bytecode", "copy",
// "keccak", "tx", "rlp", "exp", "modexp", "pi",
// "poseidon", "sig", "ecc", "mpt",
let real_available_rows = [
MAX_RWS,
MAX_RWS,
MAX_BYTECODE,
MAX_RWS,
MAX_KECCAK_ROWS,
MAX_CALLDATA,
MAX_CALLDATA,
MAX_RWS, // evm
MAX_RWS, // state
MAX_BYTECODE, // bytecode
MAX_RWS, // copy
MAX_KECCAK_ROWS, // keccak
MAX_CALLDATA, // tx
MAX_CALLDATA, // rlp
7 * MAX_EXP_STEPS, // exp
MAX_KECCAK_ROWS,
MAX_RWS,
MAX_MPT_ROWS, // poseidon
(1 << 20) - 256, // sig
(1 << 20) - 256, // FIXME: pairing may be limit to 1, fix later
MAX_MPT_ROWS,
MAX_KECCAK_ROWS, // modexp
MAX_RWS, // pi
MAX_POSEIDON_ROWS, // poseidon
MAX_VERTICLE_ROWS, // sig
MAX_VERTICLE_ROWS, // ecc
MAX_MPT_ROWS, // mpt
]
.map(|x| (x as f32 * 0.95) as usize);
let details = self
Expand Down Expand Up @@ -131,6 +119,8 @@ pub struct CircuitCapacityChecker {
pub acc_row_usage: RowUsage,
pub row_usages: Vec<RowUsage>,
pub state: Option<ZktrieState>,
// poseidon codehash to code len
pub codelen: HashMap<H256, usize>,
}

// Currently TxTrace is same as BlockTrace, with "transactions" and "executionResults" should be of
Expand All @@ -151,27 +141,42 @@ impl CircuitCapacityChecker {
row_usages: Vec::new(),
state: None,
light_mode: true,
codelen: HashMap::new(),
}
}
pub fn reset(&mut self) {
self.state = 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, RowUsage), anyhow::Error> {
) -> 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 =
let (witness_block, codedb) =
block_traces_to_witness_block_with_updated_state(traces, state, self.light_mode)?;
let rows = calculate_row_usage_of_witness_block(&witness_block)?;
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 row_usage_details: Vec<SubCircuitRowUsage> = rows
.into_iter()
.map(|x| SubCircuitRowUsage {
Expand All @@ -182,6 +187,6 @@ 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);
Ok((self.acc_row_usage.normalize(), tx_row_usage.normalize()))
Ok(self.acc_row_usage.normalize())
}
}
21 changes: 5 additions & 16 deletions prover/src/zkevm/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,12 @@ pub use self::builder::{
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,
SUB_CIRCUIT_NAMES,
};

// TODO: more smart row capacity checking rather than max_of(row_usage_details) > 1<<20 - 256
// Need to compare with real row nums like MAX_MPT_ROWS/MAX_KECCAK_ROWS etc.
////// params for degree = 20 ////////////
pub const MAX_TXS: usize = 100;
pub const MAX_INNER_BLOCKS: usize = 100;
pub const MAX_EXP_STEPS: usize = 10_000;
pub const MAX_CALLDATA: usize = 400_000;
pub const MAX_BYTECODE: usize = 400_000;
pub const MAX_MPT_ROWS: usize = 400_000;
pub const MAX_KECCAK_ROWS: usize = 524_000;
pub const MAX_RWS: usize = 1_000_000;
pub const MAX_PRECOMPILE_EC_ADD: usize = 50;
pub const MAX_PRECOMPILE_EC_MUL: usize = 50;
pub const MAX_PRECOMPILE_EC_PAIRING: usize = 2;
pub use builder::{
MAX_BYTECODE, MAX_CALLDATA, MAX_EXP_STEPS, MAX_INNER_BLOCKS, MAX_KECCAK_ROWS, MAX_MPT_ROWS,
MAX_POSEIDON_ROWS, MAX_PRECOMPILE_EC_ADD, MAX_PRECOMPILE_EC_MUL, MAX_PRECOMPILE_EC_PAIRING,
MAX_RWS, MAX_TXS, MAX_VERTICLE_ROWS,
};

static CHAIN_ID: Lazy<u64> = Lazy::new(|| read_env_var("CHAIN_ID", 53077));
static AUTO_TRUNCATE: Lazy<bool> = Lazy::new(|| read_env_var("AUTO_TRUNCATE", false));
Expand Down
Loading

0 comments on commit a29cbaa

Please sign in to comment.