diff --git a/Cargo.lock b/Cargo.lock index 8c80f9102a..e0d8e8fceb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -496,12 +496,6 @@ dependencies = [ "serde", ] -[[package]] -name = "cast" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" - [[package]] name = "cc" version = "1.0.79" @@ -557,17 +551,6 @@ dependencies = [ "zkevm-circuits", ] -[[package]] -name = "clap" -version = "2.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" -dependencies = [ - "bitflags", - "textwrap 0.11.0", - "unicode-width", -] - [[package]] name = "clap" version = "3.2.23" @@ -582,7 +565,7 @@ dependencies = [ "once_cell", "strsim 0.10.0", "termcolor", - "textwrap 0.16.0", + "textwrap", ] [[package]] @@ -824,42 +807,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "criterion" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f" -dependencies = [ - "atty", - "cast", - "clap 2.34.0", - "criterion-plot", - "csv", - "itertools", - "lazy_static", - "num-traits", - "oorandom", - "plotters", - "rayon", - "regex", - "serde", - "serde_cbor", - "serde_derive", - "serde_json", - "tinytemplate", - "walkdir", -] - -[[package]] -name = "criterion-plot" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876" -dependencies = [ - "cast", - "itertools", -] - [[package]] name = "crossbeam-channel" version = "0.5.6" @@ -2150,12 +2097,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "half" -version = "1.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" - [[package]] name = "halo2_proofs" version = "0.1.0" @@ -3014,12 +2955,6 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" -[[package]] -name = "oorandom" -version = "11.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" - [[package]] name = "opaque-debug" version = "0.2.3" @@ -4166,16 +4101,6 @@ dependencies = [ "serde_json", ] -[[package]] -name = "serde_cbor" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" -dependencies = [ - "half", - "serde", -] - [[package]] name = "serde_derive" version = "1.0.152" @@ -4537,7 +4462,7 @@ version = "0.1.0" dependencies = [ "anyhow", "bus-mapping", - "clap 3.2.23", + "clap", "env_logger", "eth-types", "ethers-core", @@ -4567,15 +4492,6 @@ dependencies = [ "zkevm-circuits", ] -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - [[package]] name = "textwrap" version = "0.16.0" @@ -4622,16 +4538,6 @@ dependencies = [ "crunchy", ] -[[package]] -name = "tinytemplate" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" -dependencies = [ - "serde", - "serde_json", -] - [[package]] name = "tinyvec" version = "1.6.0" @@ -5273,7 +5179,6 @@ dependencies = [ "array-init", "bus-mapping", "cli-table", - "criterion", "ctor", "ecc", "ecdsa 0.1.0", @@ -5304,5 +5209,4 @@ dependencies = [ "snark-verifier", "strum", "strum_macros", - "subtle", ] diff --git a/Makefile b/Makefile index f8dabed664..58656a3448 100644 --- a/Makefile +++ b/Makefile @@ -58,15 +58,15 @@ exp_bench: ## Run Exp Circuit benchmarks circuit_benches: evm_bench state_bench ## Run All Circuit benchmarks stats_state_circuit: # Print a table with State Circuit stats by ExecState/opcode - @cargo test -p zkevm-circuits --features=test,warn-unimplemented get_state_states_stats -- --nocapture --ignored + @cargo run --bin stats --features stats -- state stats_evm_circuit: # Print a table with EVM Circuit stats by ExecState/opcode - @cargo test -p zkevm-circuits --features=test,warn-unimplemented get_evm_states_stats -- --nocapture --ignored + @cargo run --bin stats --features stats -- evm stats_copy_circuit: # Print a table with Copy Circuit stats by ExecState/opcode - @cargo test -p zkevm-circuits --features=test,warn-unimplemented get_copy_states_stats -- --nocapture --ignored + @cargo run --bin stats --features stats -- copy evm_exec_steps_occupancy: # Print a table for each EVM-CellManager CellType with the top 10 occupancy ExecutionSteps associated - @cargo test -p zkevm-circuits --release get_exec_steps_occupancy --features=test,warn-unimplemented -- --nocapture --ignored + @cargo run --bin stats --features stats -- exec .PHONY: clippy doc fmt test test_benches test-all evm_bench state_bench circuit_benches evm_exec_steps_occupancy stats_state_circuit stats_evm_circuit stats_copy_circuit help diff --git a/zkevm-circuits/Cargo.toml b/zkevm-circuits/Cargo.toml index 433f1a99b5..3f0bd6641f 100644 --- a/zkevm-circuits/Cargo.toml +++ b/zkevm-circuits/Cargo.toml @@ -33,20 +33,18 @@ maingate = { git = "https://github.com/privacy-scaling-explorations/halo2wrong" integer = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2023_04_20" } libsecp256k1 = "0.7" num-bigint = { version = "0.4" } -subtle = "2.4" rand_chacha = "0.3" snark-verifier = { git = "https://github.com/privacy-scaling-explorations/snark-verifier", tag = "v2023_04_20", default-features = false, features = ["loader_halo2", "system_halo2"] } +cli-table = { version = "0.4", optional = true } [dev-dependencies] bus-mapping = { path = "../bus-mapping", features = ["test"] } -criterion = "0.3" ctor = "0.1.22" ethers-signers = "0.17.0" hex = "0.4.3" itertools = "0.10.1" mock = { path = "../mock" } pretty_assertions = "1.0.0" -cli-table = "0.4" serde = { version = "1.0.130", features = ["derive"] } serde_json = "1.0.78" @@ -55,3 +53,8 @@ default = [] test = ["ethers-signers", "mock", "bus-mapping/test"] test-circuits = [] warn-unimplemented = ["eth-types/warn-unimplemented"] +stats = ["warn-unimplemented", "dep:cli-table"] + +[[bin]] +name = "stats" +required-features = ["stats"] diff --git a/zkevm-circuits/src/stats.rs b/zkevm-circuits/src/bin/stats/helpers.rs similarity index 79% rename from zkevm-circuits/src/stats.rs rename to zkevm-circuits/src/bin/stats/helpers.rs index ed43527497..01117a48c0 100644 --- a/zkevm-circuits/src/stats.rs +++ b/zkevm-circuits/src/bin/stats/helpers.rs @@ -1,61 +1,17 @@ use std::cmp::Ordering; -use crate::evm_circuit::step::ExecutionState; use bus_mapping::{ circuit_input_builder::{self, CircuitsParams, ExecState}, mock::BlockData, }; +use cli_table::{ + format::{Justify, Separator}, + print_stdout, Table, WithTitle, +}; use eth_types::{bytecode, evm_types::OpcodeId, geth_types::GethData, Address, Bytecode, ToWord}; use mock::{eth, test_ctx::TestContext, MOCK_ACCOUNTS}; use strum::IntoEnumIterator; - -/// Helper type to print formatted tables in MarkDown -pub(crate) struct DisplayTable { - header: [String; N], - rows: Vec<[String; N]>, -} - -impl DisplayTable { - pub(crate) fn new(header: [String; N]) -> Self { - Self { - header, - rows: Vec::new(), - } - } - fn push_row(&mut self, row: [String; N]) { - self.rows.push(row) - } - fn print_row(row: &[String; N], rows_width: &[usize; N]) { - for (i, h) in row.iter().enumerate() { - if i == 0 { - print!("|"); - } - print!(" {:width$} |", h, width = rows_width[i]); - } - println!(); - } - pub(crate) fn print(&self) { - let mut rows_width = [0; N]; - for row in std::iter::once(&self.header).chain(self.rows.iter()) { - for (i, s) in row.iter().enumerate() { - if s.len() > rows_width[i] { - rows_width[i] = s.len(); - } - } - } - Self::print_row(&self.header, &rows_width); - for (i, width) in rows_width.iter().enumerate() { - if i == 0 { - print!("|"); - } - print!(" {:- Bytecode { @@ -106,12 +62,34 @@ pub(crate) fn bytecode_prefix_op_big_rws(opcode: OpcodeId) -> Bytecode { } } +/// Wrap f64 for both sorting and pretty formatting +#[derive(PartialEq, PartialOrd)] +struct PrettyF64(f64); + +impl std::fmt::Display for PrettyF64 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:1.3}", self.0) + } +} + +impl From for PrettyF64 { + fn from(value: f64) -> Self { + Self(value) + } +} + +#[derive(Table)] struct Row { + #[table(title = "Execution State")] state: ExecutionState, + #[table(title = "Opcode")] opcode: OpcodeId, + #[table(title = "Height", justify = "Justify::Right")] height: usize, + #[table(title = "Gas Cost", justify = "Justify::Right")] gas_cost: u64, - height_per_gas: f64, + #[table(title = "Height per Gas", justify = "Justify::Right")] + height_per_gas: PrettyF64, } /// This function prints to stdout a table with all the implemented states @@ -161,7 +139,6 @@ pub(crate) fn print_circuit_stats_by_states( STOP }; - let mut table = DisplayTable::new(["state", "opcode", "h", "g", "h/g"].map(|s| s.into())); let mut rows = vec![]; for state in implemented_states { if !fn_filter(state) { @@ -251,7 +228,7 @@ pub(crate) fn print_circuit_stats_by_states( opcode, height, gas_cost, - height_per_gas: height as f64 / gas_cost as f64, + height_per_gas: (height as f64 / gas_cost as f64).into(), }); } } @@ -261,16 +238,6 @@ pub(crate) fn print_circuit_stats_by_states( .unwrap_or(Ordering::Greater) }); - for row in rows.iter() { - let row = [ - format!("{:?}", row.state), - format!("{:?}", row.opcode), - format!("{}", row.height), - format!("{}", row.gas_cost), - format!("{:1.3}", row.height_per_gas), - ]; - table.push_row(row); - } - - table.print(); + print_stdout(rows.with_title().separator(Separator::builder().build())) + .expect("the table renders"); } diff --git a/zkevm-circuits/src/bin/stats/main.rs b/zkevm-circuits/src/bin/stats/main.rs new file mode 100644 index 0000000000..30c8fc9a27 --- /dev/null +++ b/zkevm-circuits/src/bin/stats/main.rs @@ -0,0 +1,185 @@ +use cli_table::{print_stdout, Cell, Style, Table}; +use eth_types::{bytecode, evm_types::OpcodeId, ToWord}; +use halo2_proofs::{ + halo2curves::bn256::Fr, + plonk::{Circuit, ConstraintSystem}, +}; +mod helpers; +use helpers::{bytecode_prefix_op_big_rws, print_circuit_stats_by_states}; +use itertools::Itertools; +use mock::MOCK_ACCOUNTS; +use std::env; +use zkevm_circuits::evm_circuit::{ + param::{LOOKUP_CONFIG, N_BYTE_LOOKUPS, N_COPY_COLUMNS, N_PHASE1_COLUMNS, N_PHASE2_COLUMNS}, + step::ExecutionState, + EvmCircuit, +}; +fn main() { + let args: Vec = env::args().collect(); + + match &args[1][..] { + "evm" => evm_states_stats(), + "state" => state_states_stats(), + "copy" => copy_states_stats(), + "exec" => get_exec_steps_occupancy(), + &_ => unreachable!("Unsupported arg"), + } +} + +/// Prints the stats of EVM circuit per execution state. +fn evm_states_stats() { + print_circuit_stats_by_states( + |state| { + // TODO: Enable CREATE/CREATE2 once they are supported + !matches!( + state, + ExecutionState::ErrorInvalidOpcode + | ExecutionState::CREATE + | ExecutionState::CREATE2 + | ExecutionState::SELFDESTRUCT + ) + }, + |opcode| match opcode { + OpcodeId::RETURNDATACOPY => { + bytecode! { + PUSH1(0x00) // retLength + PUSH1(0x00) // retOffset + PUSH1(0x00) // argsLength + PUSH1(0x00) // argsOffset + PUSH1(0x00) // value + PUSH32(MOCK_ACCOUNTS[3].to_word()) + PUSH32(0x1_0000) // gas + CALL + PUSH2(0x01) // size + PUSH2(0x00) // offset + PUSH2(0x00) // destOffset + } + } + _ => bytecode! { + PUSH2(0x40) + PUSH2(0x50) + }, + }, + |_, state, _| state.get_step_height_option().unwrap(), + ); +} + +/// Prints the stats of State circuit per execution state. +fn state_states_stats() { + print_circuit_stats_by_states( + |state| { + // TODO: Enable CREATE/CREATE2 once they are supported + !matches!( + state, + ExecutionState::ErrorInvalidOpcode + | ExecutionState::CREATE + | ExecutionState::CREATE2 + | ExecutionState::SELFDESTRUCT + ) + }, + bytecode_prefix_op_big_rws, + |block, _, step_index| { + let step = &block.txs[0].steps()[step_index]; + let step_next = &block.txs[0].steps()[step_index + 1]; + step_next.rwc.0 - step.rwc.0 + }, + ); +} + +/// Prints the stats of Copy circuit per execution state. +fn copy_states_stats() { + print_circuit_stats_by_states( + |state| { + // TODO: Enable CREATE/CREATE2 once they are supported + matches!( + state, + ExecutionState::RETURNDATACOPY + | ExecutionState::CODECOPY + | ExecutionState::LOG + | ExecutionState::CALLDATACOPY + | ExecutionState::EXTCODECOPY + | ExecutionState::RETURN_REVERT + ) + }, + bytecode_prefix_op_big_rws, + |block, _, _| { + assert!(block.copy_events.len() <= 1); + block + .copy_events + .iter() + .map(|c| c.bytes.len() * 2) + .sum::() + }, + ); +} + +/// This function prints to stdout a table with the top X ExecutionState +/// cell consumers of each EVM Cell type. +fn get_exec_steps_occupancy() { + let mut meta = ConstraintSystem::::default(); + let circuit = EvmCircuit::configure(&mut meta); + + let report = circuit.0.execution.instrument().clone().analyze(); + macro_rules! gen_report { + ($report:expr, $($id:ident, $cols:expr), +) => { + $( + let row_report = report + .iter() + .sorted_by(|a, b| a.$id.utilization.partial_cmp(&b.$id.utilization).unwrap()) + .rev() + .take(10) + .map(|exec| { + vec![ + format!("{:?}", exec.state), + format!("{:?}", exec.$id.available_cells), + format!("{:?}", exec.$id.unused_cells), + format!("{:?}", exec.$id.used_cells), + format!("{:?}", exec.$id.top_height), + format!("{:?}", exec.$id.used_columns), + format!("{:?}", exec.$id.utilization), + ] + }) + .collect::>>(); + + let table = row_report.table().title(vec![ + format!("{:?}", stringify!($id)).cell().bold(true), + format!("total_available_cells").cell().bold(true), + format!("unused_cells").cell().bold(true), + format!("cells").cell().bold(true), + format!("top_height").cell().bold(true), + format!("used columns (Max: {:?})", $cols).cell().bold(true), + format!("Utilization").cell().bold(true), + ]); + print_stdout(table).unwrap(); + )* + }; + } + + gen_report!( + report, + storage_1, + N_PHASE1_COLUMNS, + storage_2, + N_PHASE2_COLUMNS, + storage_perm, + N_COPY_COLUMNS, + byte_lookup, + N_BYTE_LOOKUPS, + fixed_table, + LOOKUP_CONFIG[0].1, + tx_table, + LOOKUP_CONFIG[1].1, + rw_table, + LOOKUP_CONFIG[2].1, + bytecode_table, + LOOKUP_CONFIG[3].1, + block_table, + LOOKUP_CONFIG[4].1, + copy_table, + LOOKUP_CONFIG[5].1, + keccak_table, + LOOKUP_CONFIG[6].1, + exp_table, + LOOKUP_CONFIG[7].1 + ); +} diff --git a/zkevm-circuits/src/copy_circuit.rs b/zkevm-circuits/src/copy_circuit.rs index 292c8a2094..342b12a6c4 100644 --- a/zkevm-circuits/src/copy_circuit.rs +++ b/zkevm-circuits/src/copy_circuit.rs @@ -777,45 +777,3 @@ impl SubCircuit for CopyCircuit { config.assign_copy_events(layouter, &self.copy_events, self.max_copy_rows, *challenges) } } - -#[cfg(test)] -mod copy_circuit_stats { - use crate::{ - evm_circuit::step::ExecutionState, - stats::{bytecode_prefix_op_big_rws, print_circuit_stats_by_states}, - }; - - /// Prints the stats of Copy circuit per execution state. See - /// `print_circuit_stats_by_states` for more details. - /// - /// Run with: - /// `cargo test -p zkevm-circuits --release --all-features - /// get_evm_states_stats -- --nocapture --ignored` - #[ignore] - #[test] - fn get_copy_states_stats() { - print_circuit_stats_by_states( - |state| { - // TODO: Enable CREATE/CREATE2 once they are supported - matches!( - state, - ExecutionState::RETURNDATACOPY - | ExecutionState::CODECOPY - | ExecutionState::LOG - | ExecutionState::CALLDATACOPY - | ExecutionState::EXTCODECOPY - | ExecutionState::RETURN_REVERT - ) - }, - bytecode_prefix_op_big_rws, - |block, _, _| { - assert!(block.copy_events.len() <= 1); - block - .copy_events - .iter() - .map(|c| c.bytes.len() * 2) - .sum::() - }, - ); - } -} diff --git a/zkevm-circuits/src/evm_circuit.rs b/zkevm-circuits/src/evm_circuit.rs index 7cf334e891..86d1aad98a 100644 --- a/zkevm-circuits/src/evm_circuit.rs +++ b/zkevm-circuits/src/evm_circuit.rs @@ -8,7 +8,7 @@ use halo2_proofs::{ mod execution; pub mod param; -pub(crate) mod step; +pub mod step; pub mod table; pub(crate) mod util; @@ -39,7 +39,7 @@ use witness::Block; pub struct EvmCircuitConfig { fixed_table: [Column; 4], byte_table: [Column; 1], - pub(crate) execution: Box>, + pub execution: Box>, // External tables tx_table: TxTable, rw_table: RwTable, @@ -74,7 +74,6 @@ impl SubCircuitConfig for EvmCircuitConfig { type ConfigArgs = EvmCircuitConfigArgs; /// Configure EvmCircuitConfig - #[allow(clippy::too_many_arguments)] fn new( meta: &mut ConstraintSystem, Self::ConfigArgs { @@ -440,33 +439,19 @@ impl Circuit for EvmCircuit { #[cfg(test)] mod evm_circuit_stats { use crate::{ - evm_circuit::{ - param::{ - LOOKUP_CONFIG, N_BYTE_LOOKUPS, N_COPY_COLUMNS, N_PHASE1_COLUMNS, N_PHASE2_COLUMNS, - }, - step::ExecutionState, - EvmCircuit, - }, - stats::print_circuit_stats_by_states, + evm_circuit::EvmCircuit, test_util::CircuitTestBuilder, util::{unusable_rows, SubCircuit}, witness::block_convert, }; use bus_mapping::{circuit_input_builder::CircuitsParams, mock::BlockData}; - use cli_table::{print_stdout, Cell, Style, Table}; - use eth_types::{bytecode, evm_types::OpcodeId, geth_types::GethData, ToWord}; - use halo2_proofs::{ - dev::MockProver, - halo2curves::bn256::Fr, - plonk::{Circuit, ConstraintSystem}, - }; - use itertools::Itertools; - use mock::{ - test_ctx::{ - helpers::{account_0_code_account_1_no_code, tx_from_1_to_0}, - TestContext, - }, - MOCK_ACCOUNTS, + + use eth_types::{bytecode, geth_types::GethData}; + use halo2_proofs::{self, dev::MockProver, halo2curves::bn256::Fr}; + + use mock::test_ctx::{ + helpers::{account_0_code_account_1_no_code, tx_from_1_to_0}, + TestContext, }; #[test] @@ -496,127 +481,6 @@ mod evm_circuit_stats { .run(); } - /// Prints the stats of EVM circuit per execution state. See - /// `print_circuit_stats_by_states` for more details. - /// - /// Run with: - /// `cargo test -p zkevm-circuits --release --all-features - /// get_evm_states_stats -- --nocapture --ignored` - #[ignore] - #[test] - fn get_evm_states_stats() { - print_circuit_stats_by_states( - |state| { - // TODO: Enable CREATE/CREATE2 once they are supported - !matches!( - state, - ExecutionState::ErrorInvalidOpcode - | ExecutionState::CREATE - | ExecutionState::CREATE2 - | ExecutionState::SELFDESTRUCT - ) - }, - |opcode| match opcode { - OpcodeId::RETURNDATACOPY => { - bytecode! { - PUSH1(0x00) // retLength - PUSH1(0x00) // retOffset - PUSH1(0x00) // argsLength - PUSH1(0x00) // argsOffset - PUSH1(0x00) // value - PUSH32(MOCK_ACCOUNTS[3].to_word()) - PUSH32(0x1_0000) // gas - CALL - PUSH2(0x01) // size - PUSH2(0x00) // offset - PUSH2(0x00) // destOffset - } - } - _ => bytecode! { - PUSH2(0x40) - PUSH2(0x50) - }, - }, - |_, state, _| state.get_step_height_option().unwrap(), - ); - } - - /// This function prints to stdout a table with the top X ExecutionState - /// cell consumers of each EVM Cell type. - /// - /// Run with: - /// `cargo test -p zkevm-circuits --release get_exec_steps_occupancy - /// --features test -- --nocapture --ignored` - #[ignore] - #[test] - fn get_exec_steps_occupancy() { - let mut meta = ConstraintSystem::::default(); - let circuit = EvmCircuit::configure(&mut meta); - - let report = circuit.0.execution.instrument().clone().analyze(); - macro_rules! gen_report { - ($report:expr, $($id:ident, $cols:expr), +) => { - $( - let row_report = report - .iter() - .sorted_by(|a, b| a.$id.utilization.partial_cmp(&b.$id.utilization).unwrap()) - .rev() - .take(10) - .map(|exec| { - vec![ - format!("{:?}", exec.state), - format!("{:?}", exec.$id.available_cells), - format!("{:?}", exec.$id.unused_cells), - format!("{:?}", exec.$id.used_cells), - format!("{:?}", exec.$id.top_height), - format!("{:?}", exec.$id.used_columns), - format!("{:?}", exec.$id.utilization), - ] - }) - .collect::>>(); - - let table = row_report.table().title(vec![ - format!("{:?}", stringify!($id)).cell().bold(true), - format!("total_available_cells").cell().bold(true), - format!("unused_cells").cell().bold(true), - format!("cells").cell().bold(true), - format!("top_height").cell().bold(true), - format!("used columns (Max: {:?})", $cols).cell().bold(true), - format!("Utilization").cell().bold(true), - ]); - print_stdout(table).unwrap(); - )* - }; - } - - gen_report!( - report, - storage_1, - N_PHASE1_COLUMNS, - storage_2, - N_PHASE2_COLUMNS, - storage_perm, - N_COPY_COLUMNS, - byte_lookup, - N_BYTE_LOOKUPS, - fixed_table, - LOOKUP_CONFIG[0].1, - tx_table, - LOOKUP_CONFIG[1].1, - rw_table, - LOOKUP_CONFIG[2].1, - bytecode_table, - LOOKUP_CONFIG[3].1, - block_table, - LOOKUP_CONFIG[4].1, - copy_table, - LOOKUP_CONFIG[5].1, - keccak_table, - LOOKUP_CONFIG[6].1, - exp_table, - LOOKUP_CONFIG[7].1 - ); - } #[test] fn variadic_size_check() { let params = CircuitsParams { diff --git a/zkevm-circuits/src/evm_circuit/execution.rs b/zkevm-circuits/src/evm_circuit/execution.rs index 93668e521d..ff3e48e92d 100644 --- a/zkevm-circuits/src/evm_circuit/execution.rs +++ b/zkevm-circuits/src/evm_circuit/execution.rs @@ -198,7 +198,7 @@ pub(crate) trait ExecutionGadget { } #[derive(Clone, Debug)] -pub(crate) struct ExecutionConfig { +pub struct ExecutionConfig { // EVM Circuit selector, which enables all usable rows. The rows where this selector is // disabled won't verify any constraint (they can be unused rows or rows with blinding // factors). @@ -596,7 +596,7 @@ impl ExecutionConfig { config } - pub(crate) fn instrument(&self) -> &Instrument { + pub fn instrument(&self) -> &Instrument { &self.instrument } diff --git a/zkevm-circuits/src/evm_circuit/param.rs b/zkevm-circuits/src/evm_circuit/param.rs index a2262d21e9..e21530bb9c 100644 --- a/zkevm-circuits/src/evm_circuit/param.rs +++ b/zkevm-circuits/src/evm_circuit/param.rs @@ -16,16 +16,16 @@ pub const MAX_STEP_HEIGHT: usize = 21; pub(crate) const STEP_STATE_HEIGHT: usize = 1; /// Number of Advice Phase2 columns in the EVM circuit -pub(crate) const N_PHASE2_COLUMNS: usize = 4; +pub const N_PHASE2_COLUMNS: usize = 4; /// Number of Advice Phase1 columns in the EVM circuit -pub(crate) const N_PHASE1_COLUMNS: usize = +pub const N_PHASE1_COLUMNS: usize = STEP_WIDTH - EVM_LOOKUP_COLS - N_PHASE2_COLUMNS - N_COPY_COLUMNS - N_BYTE_LOOKUPS; // Number of copy columns -pub(crate) const N_COPY_COLUMNS: usize = 2; +pub const N_COPY_COLUMNS: usize = 2; -pub(crate) const N_BYTE_LOOKUPS: usize = 24; +pub const N_BYTE_LOOKUPS: usize = 24; /// Amount of lookup columns in the EVM circuit dedicated to lookups. pub(crate) const EVM_LOOKUP_COLS: usize = FIXED_TABLE_LOOKUPS @@ -38,7 +38,7 @@ pub(crate) const EVM_LOOKUP_COLS: usize = FIXED_TABLE_LOOKUPS + EXP_TABLE_LOOKUPS; /// Lookups done per row. -pub(crate) const LOOKUP_CONFIG: &[(Table, usize)] = &[ +pub const LOOKUP_CONFIG: &[(Table, usize)] = &[ (Table::Fixed, FIXED_TABLE_LOOKUPS), (Table::Tx, TX_TABLE_LOOKUPS), (Table::Rw, RW_TABLE_LOOKUPS), diff --git a/zkevm-circuits/src/evm_circuit/step.rs b/zkevm-circuits/src/evm_circuit/step.rs index b0693794da..61323f557a 100644 --- a/zkevm-circuits/src/evm_circuit/step.rs +++ b/zkevm-circuits/src/evm_circuit/step.rs @@ -315,7 +315,7 @@ impl ExecutionState { || self.halts_in_exception() } - pub(crate) fn responsible_opcodes(&self) -> Vec { + pub fn responsible_opcodes(&self) -> Vec { if matches!(self, Self::ErrorStack) { return OpcodeId::valid_opcodes() .into_iter() @@ -486,7 +486,7 @@ impl ExecutionState { /// Enum of Responsible opcode mapping to execution state. #[derive(Debug)] -pub(crate) enum ResponsibleOp { +pub enum ResponsibleOp { /// Raw opcode Op(OpcodeId), /// Corresponding to ExecutionState::ErrorStack @@ -501,7 +501,7 @@ impl From for ResponsibleOp { } impl ResponsibleOp { - pub(crate) fn opcode(&self) -> OpcodeId { + pub fn opcode(&self) -> OpcodeId { *match self { ResponsibleOp::Op(opcode) => opcode, ResponsibleOp::InvalidStackPtr(opcode, _) => opcode, diff --git a/zkevm-circuits/src/evm_circuit/table.rs b/zkevm-circuits/src/evm_circuit/table.rs index e7d5572d00..cc71063003 100644 --- a/zkevm-circuits/src/evm_circuit/table.rs +++ b/zkevm-circuits/src/evm_circuit/table.rs @@ -122,7 +122,7 @@ impl FixedTableTag { } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, EnumIter)] -pub(crate) enum Table { +pub enum Table { Fixed, Tx, Rw, diff --git a/zkevm-circuits/src/evm_circuit/util/instrumentation.rs b/zkevm-circuits/src/evm_circuit/util/instrumentation.rs index 7f03fe6864..eef04dbace 100644 --- a/zkevm-circuits/src/evm_circuit/util/instrumentation.rs +++ b/zkevm-circuits/src/evm_circuit/util/instrumentation.rs @@ -12,7 +12,7 @@ type ColumnSize = (usize, usize, usize); /// Instrument captures metrics during the compilation of a circuit. #[derive(Clone, Debug, Default)] -pub(crate) struct Instrument { +pub struct Instrument { // States -> Cell Types -> (width, height, num_cells) states: Vec<(ExecutionState, StepSize)>, } @@ -38,7 +38,7 @@ impl Instrument { /// Dissasembles the instrumentation data and returns a collection of /// `ExecStateReport`s. One for each EVM `ExecutionState`. - pub(crate) fn analyze(&self) -> Vec { + pub fn analyze(&self) -> Vec { let mut report_collection = vec![]; for (state, sizes) in &self.states { // Create a state report @@ -111,20 +111,20 @@ impl Instrument { /// Struct which contains a Cost/ColumnType report for a particular EVM /// `ExecutionStep`. #[derive(Clone, Debug, Default)] -pub(crate) struct ExecStateReport { - pub(crate) state: ExecutionState, - pub(crate) storage_1: StateReportRow, - pub(crate) storage_2: StateReportRow, - pub(crate) storage_perm: StateReportRow, - pub(crate) byte_lookup: StateReportRow, - pub(crate) fixed_table: StateReportRow, - pub(crate) tx_table: StateReportRow, - pub(crate) rw_table: StateReportRow, - pub(crate) bytecode_table: StateReportRow, - pub(crate) block_table: StateReportRow, - pub(crate) copy_table: StateReportRow, - pub(crate) keccak_table: StateReportRow, - pub(crate) exp_table: StateReportRow, +pub struct ExecStateReport { + pub state: ExecutionState, + pub storage_1: StateReportRow, + pub storage_2: StateReportRow, + pub storage_perm: StateReportRow, + pub byte_lookup: StateReportRow, + pub fixed_table: StateReportRow, + pub tx_table: StateReportRow, + pub rw_table: StateReportRow, + pub bytecode_table: StateReportRow, + pub block_table: StateReportRow, + pub copy_table: StateReportRow, + pub keccak_table: StateReportRow, + pub exp_table: StateReportRow, } impl From for ExecStateReport { @@ -148,17 +148,17 @@ impl From<&ExecutionState> for ExecStateReport { /// Struct that contains all of the measurament values required to evaluate the /// costs of a particular `ColumnType` of an `ExecStateReport` #[derive(Debug, Clone, Default)] -pub(crate) struct StateReportRow { +pub struct StateReportRow { // Given a rigion of x columns and y rows, we have x * y cells available for computation. - pub(crate) available_cells: usize, + pub available_cells: usize, // The cells not used in the computation in the x*y region. These are the wasted cells. - pub(crate) unused_cells: usize, + pub unused_cells: usize, // The cells used in the computation in the x*y region. - pub(crate) used_cells: usize, + pub used_cells: usize, // The largest y within all the `CellType`. - pub(crate) top_height: usize, + pub top_height: usize, // If we fully utilize y, how large is the x really needed? - pub(crate) used_columns: usize, + pub used_columns: usize, // The percentage of cells used in computation in the x * y region. - pub(crate) utilization: f64, + pub utilization: f64, } diff --git a/zkevm-circuits/src/lib.rs b/zkevm-circuits/src/lib.rs index 20777ec3f5..a5eb0ad76f 100644 --- a/zkevm-circuits/src/lib.rs +++ b/zkevm-circuits/src/lib.rs @@ -31,9 +31,6 @@ pub mod table; #[cfg(any(feature = "test", test))] pub mod test_util; -#[cfg(any(feature = "test", test))] -mod stats; - pub mod tx_circuit; pub mod util; pub mod witness; diff --git a/zkevm-circuits/src/state_circuit.rs b/zkevm-circuits/src/state_circuit.rs index 0270ca098d..e07415e288 100644 --- a/zkevm-circuits/src/state_circuit.rs +++ b/zkevm-circuits/src/state_circuit.rs @@ -591,40 +591,3 @@ fn queries(meta: &mut VirtualCells<'_, F>, c: &StateCircuitConfig) state_root_prev: meta.query_advice(c.state_root, Rotation::prev()), } } - -#[cfg(test)] -mod state_circuit_stats { - use crate::{ - evm_circuit::step::ExecutionState, - stats::{bytecode_prefix_op_big_rws, print_circuit_stats_by_states}, - }; - - /// Prints the stats of State circuit per execution state. See - /// `print_circuit_stats_by_states` for more details. - /// - /// Run with: - /// `cargo test -p zkevm-circuits --release --all-features - /// get_state_states_stats -- --nocapture --ignored` - #[ignore] - #[test] - pub fn get_state_states_stats() { - print_circuit_stats_by_states( - |state| { - // TODO: Enable CREATE/CREATE2 once they are supported - !matches!( - state, - ExecutionState::ErrorInvalidOpcode - | ExecutionState::CREATE - | ExecutionState::CREATE2 - | ExecutionState::SELFDESTRUCT - ) - }, - bytecode_prefix_op_big_rws, - |block, _, step_index| { - let step = &block.txs[0].steps()[step_index]; - let step_next = &block.txs[0].steps()[step_index + 1]; - step_next.rwc.0 - step.rwc.0 - }, - ); - } -}