Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: one way to remove many arguments with HashMap #1122

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! The EVM circuit implementation.

#![allow(missing_docs)]
use std::collections::HashMap;

use halo2_proofs::{
circuit::{Cell, Layouter, SimpleFloorPlanner, Value},
plonk::*,
Expand All @@ -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,
Expand Down Expand Up @@ -118,24 +124,22 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
) -> 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,
&copy_table,
&keccak_table,
&sha256_table,
&exp_table,
&sig_table,
&modexp_table,
&ecc_table,
&pow_of_rand_table,
));
let mut tables: HashMap<&str, &dyn LookupTable<F>> = 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(), &copy_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)| {
Expand Down
76 changes: 9 additions & 67 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -369,25 +368,11 @@ pub(crate) struct ExecutionConfig<F> {
}

impl<F: Field> ExecutionConfig<F> {
#[allow(clippy::too_many_arguments)]
#[allow(clippy::redundant_closure_call)]
pub(crate) fn configure(
meta: &mut ConstraintSystem<F>,
challenges: Challenges<Expression<F>>,
fixed_table: &dyn LookupTable<F>,
byte_table: &dyn LookupTable<F>,
tx_table: &dyn LookupTable<F>,
rw_table: &dyn LookupTable<F>,
bytecode_table: &dyn LookupTable<F>,
block_table: &dyn LookupTable<F>,
copy_table: &dyn LookupTable<F>,
keccak_table: &dyn LookupTable<F>,
sha256_table: &dyn LookupTable<F>,
exp_table: &dyn LookupTable<F>,
sig_table: &dyn LookupTable<F>,
modexp_table: &dyn LookupTable<F>,
ecc_table: &dyn LookupTable<F>,
pow_of_rand_table: &dyn LookupTable<F>,
tables: HashMap<&str, &dyn LookupTable<F>>,
) -> Self {
let mut instrument = Instrument::default();
let q_usable = meta.fixed_column();
Expand Down Expand Up @@ -653,25 +638,7 @@ impl<F: Field> ExecutionConfig<F> {
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
}

Expand Down Expand Up @@ -921,46 +888,17 @@ impl<F: Field> ExecutionConfig<F> {
});
}

#[allow(clippy::too_many_arguments)]
fn configure_lookup(
meta: &mut ConstraintSystem<F>,
fixed_table: &dyn LookupTable<F>,
byte_table: &dyn LookupTable<F>,
tx_table: &dyn LookupTable<F>,
rw_table: &dyn LookupTable<F>,
bytecode_table: &dyn LookupTable<F>,
block_table: &dyn LookupTable<F>,
copy_table: &dyn LookupTable<F>,
keccak_table: &dyn LookupTable<F>,
sha256_table: &dyn LookupTable<F>,
exp_table: &dyn LookupTable<F>,
sig_table: &dyn LookupTable<F>,
modexp_table: &dyn LookupTable<F>,
ecc_table: &dyn LookupTable<F>,
pow_of_rand_table: &dyn LookupTable<F>,
tables: HashMap<&str, &dyn LookupTable<F>>,
challenges: &Challenges<Expression<F>>,
cell_manager: &CellManager<F>,
) {
for column in cell_manager.columns().iter() {
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()),
Expand All @@ -971,7 +909,11 @@ impl<F: Field> ExecutionConfig<F> {
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)]
});
}
Expand Down
17 changes: 15 additions & 2 deletions zkevm-circuits/src/evm_circuit/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -585,3 +585,16 @@ impl<F: Field> Lookup<F> {
.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");
}
}
13 changes: 12 additions & 1 deletion zkevm-circuits/src/evm_circuit/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -305,7 +306,7 @@ impl<F: Field> StoredExpression<F> {
}
}

#[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,
Expand Down Expand Up @@ -776,3 +777,13 @@ impl<F: Field> Inverter<F> {
}
}
}

mod test {

#[test]
fn display_cell_type() {
use crate::evm_circuit::util::CellType;

assert_eq!(CellType::LookupByte.as_ref(), "LookupByte");
}
}
Loading