Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit 46dd697

Browse files
Add test function with EVM circuit stats (#616)
* Add test function with EVM circuit stats * Update zkevm-circuits/src/evm_circuit.rs Co-authored-by: Chih Cheng Liang <chihchengliang@gmail.com> * Run cargo fmt Co-authored-by: Chih Cheng Liang <chihchengliang@gmail.com>
1 parent 322004c commit 46dd697

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

zkevm-circuits/src/evm_circuit.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub mod test {
195195
bytecode_table: [Column<Advice>; 5],
196196
block_table: [Column<Advice>; 3],
197197
copy_table: CopyCircuit<F>,
198-
evm_circuit: EvmCircuit<F>,
198+
pub evm_circuit: EvmCircuit<F>,
199199
}
200200

201201
impl<F: Field> TestCircuitConfig<F> {
@@ -515,3 +515,82 @@ pub mod test {
515515
run_test_circuit(block, FixedTableTag::iter().collect())
516516
}
517517
}
518+
519+
#[cfg(test)]
520+
mod evm_circuit_stats {
521+
use super::test::*;
522+
use super::*;
523+
use crate::evm_circuit::step::ExecutionState;
524+
use eth_types::{bytecode, evm_types::OpcodeId, geth_types::GethData};
525+
use halo2_proofs::pairing::bn256::Fr;
526+
use halo2_proofs::plonk::ConstraintSystem;
527+
use mock::test_ctx::{helpers::*, TestContext};
528+
use strum::IntoEnumIterator;
529+
530+
/// This function prints to stdout a table with all the implemented states
531+
/// and their responsible opcodes with the following stats:
532+
/// - height: number of rows used by the execution state
533+
/// - gas: gas value used for the opcode execution
534+
/// - height/gas: ratio between circuit cost and gas cost
535+
///
536+
/// Run with:
537+
/// `cargo test -p zkevm-circuits --release get_evm_states_stats --
538+
/// --nocapture --ignored`
539+
#[ignore]
540+
#[test]
541+
pub fn get_evm_states_stats() {
542+
let mut meta = ConstraintSystem::<Fr>::default();
543+
let circuit = TestCircuit::configure(&mut meta);
544+
545+
let mut implemented_states = Vec::new();
546+
for state in ExecutionState::iter() {
547+
let height = circuit.evm_circuit.execution.get_step_height_option(state);
548+
if let Some(h) = height {
549+
implemented_states.push((state, h));
550+
}
551+
}
552+
553+
let mut stats = Vec::new();
554+
for (state, h) in implemented_states {
555+
for opcode in state.responsible_opcodes() {
556+
let mut code = bytecode! {
557+
PUSH2(0x8000)
558+
PUSH2(0x00)
559+
PUSH2(0x10)
560+
PUSH2(0x20)
561+
PUSH2(0x30)
562+
PUSH2(0x40)
563+
PUSH2(0x50)
564+
};
565+
code.write_op(opcode);
566+
code.write_op(OpcodeId::STOP);
567+
let block: GethData = TestContext::<2, 1>::new(
568+
None,
569+
account_0_code_account_1_no_code(code),
570+
tx_from_1_to_0,
571+
|block, _tx| block.number(0xcafeu64),
572+
)
573+
.unwrap()
574+
.into();
575+
let gas_cost = block.geth_traces[0].struct_logs[7].gas_cost.0;
576+
stats.push((state, opcode, h, gas_cost));
577+
}
578+
}
579+
580+
println!(
581+
"| {: <14} | {: <14} | {: <2} | {: >6} | {: <5} |",
582+
"state", "opcode", "h", "g", "h/g"
583+
);
584+
println!("| --- | --- | ---| --- | --- |");
585+
for (state, opcode, height, gas_cost) in stats {
586+
println!(
587+
"| {: <14} | {: <14} | {: >2} | {: >6} | {: >1.3} |",
588+
format!("{:?}", state),
589+
format!("{:?}", opcode),
590+
height,
591+
gas_cost,
592+
height as f64 / gas_cost as f64
593+
);
594+
}
595+
}
596+
}

zkevm-circuits/src/evm_circuit/execution.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,12 @@ impl<F: Field> ExecutionConfig<F> {
452452
config
453453
}
454454

455+
pub fn get_step_height_option(&self, execution_state: ExecutionState) -> Option<usize> {
456+
self.height_map.get(&execution_state).copied()
457+
}
458+
455459
pub fn get_step_height(&self, execution_state: ExecutionState) -> usize {
456-
*self
457-
.height_map
458-
.get(&execution_state)
460+
self.get_step_height_option(execution_state)
459461
.unwrap_or_else(|| panic!("Execution state unknown: {:?}", execution_state))
460462
}
461463

zkevm-circuits/src/evm_circuit/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ impl<F: FieldExt> CellManager<F> {
278278
.unwrap()
279279
}
280280

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

0 commit comments

Comments
 (0)