Skip to content

Commit

Permalink
Add test function with EVM circuit stats (privacy-scaling-exploration…
Browse files Browse the repository at this point in the history
…s#616)

* Add test function with EVM circuit stats

* Update zkevm-circuits/src/evm_circuit.rs

Co-authored-by: Chih Cheng Liang <[email protected]>

* Run cargo fmt

Co-authored-by: Chih Cheng Liang <[email protected]>
  • Loading branch information
ed255 and ChihChengLiang authored Jul 22, 2022
1 parent 322004c commit 46dd697
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
81 changes: 80 additions & 1 deletion zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub mod test {
bytecode_table: [Column<Advice>; 5],
block_table: [Column<Advice>; 3],
copy_table: CopyCircuit<F>,
evm_circuit: EvmCircuit<F>,
pub evm_circuit: EvmCircuit<F>,
}

impl<F: Field> TestCircuitConfig<F> {
Expand Down Expand Up @@ -515,3 +515,82 @@ pub mod test {
run_test_circuit(block, FixedTableTag::iter().collect())
}
}

#[cfg(test)]
mod evm_circuit_stats {
use super::test::*;
use super::*;
use crate::evm_circuit::step::ExecutionState;
use eth_types::{bytecode, evm_types::OpcodeId, geth_types::GethData};
use halo2_proofs::pairing::bn256::Fr;
use halo2_proofs::plonk::ConstraintSystem;
use mock::test_ctx::{helpers::*, TestContext};
use strum::IntoEnumIterator;

/// This function prints to stdout a table with all the implemented states
/// and their responsible opcodes with the following stats:
/// - height: number of rows used by the execution state
/// - gas: gas value used for the opcode execution
/// - height/gas: ratio between circuit cost and gas cost
///
/// Run with:
/// `cargo test -p zkevm-circuits --release get_evm_states_stats --
/// --nocapture --ignored`
#[ignore]
#[test]
pub fn get_evm_states_stats() {
let mut meta = ConstraintSystem::<Fr>::default();
let circuit = TestCircuit::configure(&mut meta);

let mut implemented_states = Vec::new();
for state in ExecutionState::iter() {
let height = circuit.evm_circuit.execution.get_step_height_option(state);
if let Some(h) = height {
implemented_states.push((state, h));
}
}

let mut stats = Vec::new();
for (state, h) in implemented_states {
for opcode in state.responsible_opcodes() {
let mut code = bytecode! {
PUSH2(0x8000)
PUSH2(0x00)
PUSH2(0x10)
PUSH2(0x20)
PUSH2(0x30)
PUSH2(0x40)
PUSH2(0x50)
};
code.write_op(opcode);
code.write_op(OpcodeId::STOP);
let block: GethData = TestContext::<2, 1>::new(
None,
account_0_code_account_1_no_code(code),
tx_from_1_to_0,
|block, _tx| block.number(0xcafeu64),
)
.unwrap()
.into();
let gas_cost = block.geth_traces[0].struct_logs[7].gas_cost.0;
stats.push((state, opcode, h, gas_cost));
}
}

println!(
"| {: <14} | {: <14} | {: <2} | {: >6} | {: <5} |",
"state", "opcode", "h", "g", "h/g"
);
println!("| --- | --- | ---| --- | --- |");
for (state, opcode, height, gas_cost) in stats {
println!(
"| {: <14} | {: <14} | {: >2} | {: >6} | {: >1.3} |",
format!("{:?}", state),
format!("{:?}", opcode),
height,
gas_cost,
height as f64 / gas_cost as f64
);
}
}
}
8 changes: 5 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,12 @@ impl<F: Field> ExecutionConfig<F> {
config
}

pub fn get_step_height_option(&self, execution_state: ExecutionState) -> Option<usize> {
self.height_map.get(&execution_state).copied()
}

pub fn get_step_height(&self, execution_state: ExecutionState) -> usize {
*self
.height_map
.get(&execution_state)
self.get_step_height_option(execution_state)
.unwrap_or_else(|| panic!("Execution state unknown: {:?}", execution_state))
}

Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/evm_circuit/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ impl<F: FieldExt> CellManager<F> {
.unwrap()
}

/// Returns a map of CellType -> (width, height, num_cells)
pub(crate) fn get_stats(&self) -> BTreeMap<CellType, (usize, usize, usize)> {
let mut data = BTreeMap::new();
for column in self.columns.iter() {
Expand Down

0 comments on commit 46dd697

Please sign in to comment.