diff --git a/zkevm-circuits/src/evm_circuit.rs b/zkevm-circuits/src/evm_circuit.rs index 1442c9a508..55765c4bff 100644 --- a/zkevm-circuits/src/evm_circuit.rs +++ b/zkevm-circuits/src/evm_circuit.rs @@ -1,6 +1,8 @@ //! The EVM circuit implementation. #![allow(missing_docs)] +use std::collections::HashMap; + use halo2_proofs::{ circuit::{Cell, Layouter, SimpleFloorPlanner, Value}, plonk::*, @@ -20,7 +22,11 @@ pub use self::EvmCircuit as TestEvmCircuit; pub use crate::witness; use crate::{ - evm_circuit::param::{MAX_STEP_HEIGHT, STEP_STATE_HEIGHT}, + evm_circuit::{ + param::{MAX_STEP_HEIGHT, STEP_STATE_HEIGHT}, + table::Table, + util::CellType, + }, table::{ BlockTable, BytecodeTable, CopyTable, EccTable, ExpTable, KeccakTable, LookupTable, ModExpTable, PowOfRandTable, RwTable, SHA256Table, SigTable, TxTable, @@ -118,24 +124,22 @@ impl SubCircuitConfig for EvmCircuitConfig { ) -> Self { let fixed_table = [(); 4].map(|_| meta.fixed_column()); let byte_table = [(); 1].map(|_| meta.fixed_column()); - let execution = Box::new(ExecutionConfig::configure( - meta, - challenges, - &fixed_table, - &byte_table, - &tx_table, - &rw_table, - &bytecode_table, - &block_table, - ©_table, - &keccak_table, - &sha256_table, - &exp_table, - &sig_table, - &modexp_table, - &ecc_table, - &pow_of_rand_table, - )); + let mut tables: HashMap<&str, &dyn LookupTable> = HashMap::new(); + tables.insert(Table::Fixed.as_ref(), &fixed_table); + tables.insert(CellType::LookupByte.as_ref(), &byte_table); + tables.insert(Table::Tx.as_ref(), &tx_table); + tables.insert(Table::Rw.as_ref(), &rw_table); + tables.insert(Table::Bytecode.as_ref(), &bytecode_table); + tables.insert(Table::Block.as_ref(), &block_table); + tables.insert(Table::Copy.as_ref(), ©_table); + tables.insert(Table::Keccak.as_ref(), &keccak_table); + tables.insert(Table::Sha256.as_ref(), &sha256_table); + tables.insert(Table::Exp.as_ref(), &exp_table); + tables.insert(Table::Sig.as_ref(), &sig_table); + tables.insert(Table::ModExp.as_ref(), &modexp_table); + tables.insert(Table::Ecc.as_ref(), &ecc_table); + tables.insert(Table::PowOfRand.as_ref(), &pow_of_rand_table); + let execution = Box::new(ExecutionConfig::configure(meta, challenges, tables)); meta.annotate_lookup_any_column(byte_table[0], || "byte_range"); fixed_table.iter().enumerate().for_each(|(idx, &col)| { diff --git a/zkevm-circuits/src/evm_circuit/execution.rs b/zkevm-circuits/src/evm_circuit/execution.rs index dae0031d99..46f77c5a3d 100644 --- a/zkevm-circuits/src/evm_circuit/execution.rs +++ b/zkevm-circuits/src/evm_circuit/execution.rs @@ -12,7 +12,6 @@ use crate::{ evm_circuit::{ param::{EVM_LOOKUP_COLS, MAX_STEP_HEIGHT, N_PHASE2_COLUMNS, STEP_WIDTH}, step::{ExecutionState, Step}, - table::Table, util::{ constraint_builder::{ BaseConstraintBuilder, ConstrainBuilderCommon, EVMConstraintBuilder, @@ -369,25 +368,11 @@ pub(crate) struct ExecutionConfig { } impl ExecutionConfig { - #[allow(clippy::too_many_arguments)] #[allow(clippy::redundant_closure_call)] pub(crate) fn configure( meta: &mut ConstraintSystem, challenges: Challenges>, - fixed_table: &dyn LookupTable, - byte_table: &dyn LookupTable, - tx_table: &dyn LookupTable, - rw_table: &dyn LookupTable, - bytecode_table: &dyn LookupTable, - block_table: &dyn LookupTable, - copy_table: &dyn LookupTable, - keccak_table: &dyn LookupTable, - sha256_table: &dyn LookupTable, - exp_table: &dyn LookupTable, - sig_table: &dyn LookupTable, - modexp_table: &dyn LookupTable, - ecc_table: &dyn LookupTable, - pow_of_rand_table: &dyn LookupTable, + tables: HashMap<&str, &dyn LookupTable>, ) -> Self { let mut instrument = Instrument::default(); let q_usable = meta.fixed_column(); @@ -653,25 +638,7 @@ impl ExecutionConfig { instrument, }; - Self::configure_lookup( - meta, - fixed_table, - byte_table, - tx_table, - rw_table, - bytecode_table, - block_table, - copy_table, - keccak_table, - sha256_table, - exp_table, - sig_table, - modexp_table, - ecc_table, - pow_of_rand_table, - &challenges, - &cell_manager, - ); + Self::configure_lookup(meta, tables, &challenges, &cell_manager); config } @@ -921,23 +888,9 @@ impl ExecutionConfig { }); } - #[allow(clippy::too_many_arguments)] fn configure_lookup( meta: &mut ConstraintSystem, - fixed_table: &dyn LookupTable, - byte_table: &dyn LookupTable, - tx_table: &dyn LookupTable, - rw_table: &dyn LookupTable, - bytecode_table: &dyn LookupTable, - block_table: &dyn LookupTable, - copy_table: &dyn LookupTable, - keccak_table: &dyn LookupTable, - sha256_table: &dyn LookupTable, - exp_table: &dyn LookupTable, - sig_table: &dyn LookupTable, - modexp_table: &dyn LookupTable, - ecc_table: &dyn LookupTable, - pow_of_rand_table: &dyn LookupTable, + tables: HashMap<&str, &dyn LookupTable>, challenges: &Challenges>, cell_manager: &CellManager, ) { @@ -945,22 +898,7 @@ impl ExecutionConfig { if let CellType::Lookup(table) = column.cell_type { let name = format!("{table:?}"); meta.lookup_any(Box::leak(name.into_boxed_str()), |meta| { - let table_expressions = match table { - Table::Fixed => fixed_table, - Table::Tx => tx_table, - Table::Rw => rw_table, - Table::Bytecode => bytecode_table, - Table::Block => block_table, - Table::Copy => copy_table, - Table::Keccak => keccak_table, - Table::Sha256 => sha256_table, - Table::Exp => exp_table, - Table::Sig => sig_table, - Table::ModExp => modexp_table, - Table::Ecc => ecc_table, - Table::PowOfRand => pow_of_rand_table, - } - .table_exprs(meta); + let table_expressions = tables.get(table.as_ref()).unwrap().table_exprs(meta); vec![( column.expr(), rlc::expr(&table_expressions, challenges.lookup_input()), @@ -971,7 +909,11 @@ impl ExecutionConfig { for column in cell_manager.columns().iter() { if let CellType::LookupByte = column.cell_type { meta.lookup_any("Byte lookup", |meta| { - let byte_table_expression = byte_table.table_exprs(meta)[0].clone(); + let byte_table_expression = tables + .get(CellType::LookupByte.as_ref()) + .unwrap() + .table_exprs(meta)[0] + .clone(); vec![(column.expr(), byte_table_expression)] }); } diff --git a/zkevm-circuits/src/evm_circuit/table.rs b/zkevm-circuits/src/evm_circuit/table.rs index 62f7da7426..4bf4a75910 100644 --- a/zkevm-circuits/src/evm_circuit/table.rs +++ b/zkevm-circuits/src/evm_circuit/table.rs @@ -8,7 +8,7 @@ use eth_types::Field; use gadgets::util::Expr; use halo2_proofs::plonk::Expression; use strum::IntoEnumIterator; -use strum_macros::EnumIter; +use strum_macros::{AsRefStr, EnumIter}; #[derive(Clone, Copy, Debug, EnumIter)] pub enum FixedTableTag { @@ -145,7 +145,7 @@ impl FixedTableTag { } } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, EnumIter)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, EnumIter, AsRefStr)] pub(crate) enum Table { Fixed, Tx, @@ -585,3 +585,16 @@ impl Lookup { .unwrap() } } + +mod test { + + #[test] + fn display_table() { + use crate::evm_circuit::Table; + + assert_eq!(Table::Fixed.as_ref(), "Fixed"); + assert_eq!(Table::Bytecode.as_ref(), "Bytecode"); + assert_eq!(Table::Block.as_ref(), "Block"); + assert_eq!(Table::Sha256.as_ref(), "Sha256"); + } +} diff --git a/zkevm-circuits/src/evm_circuit/util.rs b/zkevm-circuits/src/evm_circuit/util.rs index aeb6c2db5a..72d0ceba85 100644 --- a/zkevm-circuits/src/evm_circuit/util.rs +++ b/zkevm-circuits/src/evm_circuit/util.rs @@ -23,6 +23,7 @@ use std::{ collections::BTreeMap, hash::{Hash, Hasher}, }; +use strum_macros::AsRefStr; pub(crate) mod common_gadget; pub(crate) mod constraint_builder; @@ -305,7 +306,7 @@ impl StoredExpression { } } -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, AsRefStr)] pub(crate) enum CellType { StoragePhase1, StoragePhase2, @@ -776,3 +777,13 @@ impl Inverter { } } } + +mod test { + + #[test] + fn display_cell_type() { + use crate::evm_circuit::util::CellType; + + assert_eq!(CellType::LookupByte.as_ref(), "LookupByte"); + } +}