diff --git a/zkevm-circuits/src/circuit_tools/constraint_builder.rs b/zkevm-circuits/src/circuit_tools/constraint_builder.rs index 5891ba9762..567d4d15bc 100644 --- a/zkevm-circuits/src/circuit_tools/constraint_builder.rs +++ b/zkevm-circuits/src/circuit_tools/constraint_builder.rs @@ -2,7 +2,11 @@ use crate::{evm_circuit::util::rlc, util::Expr}; use eth_types::Field; use gadgets::util::{and, select, sum, Scalar}; -use halo2_proofs::plonk::{ConstraintSystem, Expression}; +use halo2_proofs::{ + halo2curves::bn256::Fr, + plonk::{Advice, Column, ConstraintSystem, Expression, Fixed, VirtualCells}, + poly::Rotation, +}; use itertools::Itertools; use super::cell_manager::{Cell, CellManager, CellType, DataTransition, Trackable}; @@ -454,6 +458,24 @@ impl> Expressable for (E, E, E, E) { } } +/// Enables column query without knowing the Column type +pub trait Querier { + /// Query a column at a relative position + fn query(self, meta: &mut VirtualCells, at: Rotation) -> Expression; +} + +impl Querier for Column { + fn query(self, meta: &mut VirtualCells, at: Rotation) -> Expression { + meta.query_fixed(self, at) + } +} + +impl Querier for Column { + fn query(self, meta: &mut VirtualCells, at: Rotation) -> Expression { + meta.query_advice(self, at) + } +} + /// Implementation trait `Expressable` for type able to be casted to an /// Expression #[macro_export] @@ -1111,7 +1133,7 @@ macro_rules! circuit { #[allow(unused_imports)] use gadgets::util::{and, not, or, sum, Expr}; #[allow(unused_imports)] - use $crate::circuit_tools::constraint_builder::{Conditionable, Expressable, Selectable}; + use $crate::circuit_tools::constraint_builder::{Conditionable, Expressable, Selectable, Querier}; #[allow(unused_macros)] macro_rules! f { @@ -1133,6 +1155,34 @@ macro_rules! circuit { }}; } + #[allow(unused_macros)] + macro_rules! cur { + ($column:expr) => {{ + $column.query($meta, Rotation::cur()) + }}; + } + + #[allow(unused_macros)] + macro_rules! next { + ($column:expr) => {{ + $column.query($meta, Rotation::next()) + }}; + } + + #[allow(unused_macros)] + macro_rules! prev { + ($column:expr) => {{ + $column.query($meta, Rotation::prev()) + }}; + } + + #[allow(unused_macros)] + macro_rules! query { + ($column:expr, $rot:expr) => {{ + $column.query($meta, Rotation($rot as i32)) + }}; + } + #[allow(unused_macros)] macro_rules! not { ($expr:expr) => {{ diff --git a/zkevm-circuits/src/mpt_circuit.rs b/zkevm-circuits/src/mpt_circuit.rs index 773fd6f952..1ea79699a5 100644 --- a/zkevm-circuits/src/mpt_circuit.rs +++ b/zkevm-circuits/src/mpt_circuit.rs @@ -234,9 +234,9 @@ impl MPTConfig { KeyData::store_initial_values(&mut cb.base, &ctx.memory[key_memory(false)]); }} // Initial parent values - ifx!{f!(position_cols.q_enable), not!(a!(ctx.position_cols.not_first_level)), a!(is_branch, 1) + a!(is_storage) + a!(is_account, 1) => { + ifx!{f!(position_cols.q_enable), not!(cur!(ctx.position_cols.not_first_level)), next!(is_branch) + cur!(is_storage) + next!(is_account) => { for is_s in [true, false] { - let root = a!(ctx.inter_root(is_s)); + let root = cur!(ctx.inter_root(is_s)); ParentData::store(&mut cb.base, &ctx.memory[parent_memory(is_s)], [root.expr(), true.expr(), false.expr(), root.expr()]); } }} @@ -247,13 +247,13 @@ impl MPTConfig { let account_config; ifx!{f!(position_cols.q_enable) => { matchx! { - a!(is_branch) => { + cur!(is_branch) => { branch_config = BranchConfig::configure(meta, &mut cb, ctx.clone()); }, - a!(is_account) => { + cur!(is_account) => { account_config = AccountLeafConfig::configure(meta, &mut cb, ctx.clone()); }, - a!(is_storage) => { + cur!(is_storage) => { storage_config = StorageLeafConfig::configure(meta, &mut cb, ctx.clone()); }, _ => (), @@ -270,18 +270,18 @@ impl MPTConfig { require!(cb.length_c.sum_conditions() => bool); // Range checks for &byte in ctx.rlp_bytes()[0..2].into_iter() { - require!((FixedTableTag::RangeKeyLen256, a!(byte), 0.expr()) => @"fixed"); + require!((FixedTableTag::RangeKeyLen256, cur!(byte), 0.expr()) => @"fixed"); } for (idx, &byte) in ctx.rlp_bytes()[2..34].into_iter().enumerate() { - require!((cb.get_range_s(), a!(byte), cb.get_length_s() - (idx + 1).expr()) => @"fixed"); + require!((cb.get_range_s(), cur!(byte), cb.get_length_s() - (idx + 1).expr()) => @"fixed"); } ifx!{cb.length_sc => { for (idx, &byte) in ctx.rlp_bytes()[34..36].into_iter().enumerate() { - require!((FixedTableTag::RangeKeyLen256, a!(byte), cb.get_length_s() - 32.expr() - (idx + 1).expr()) => @"fixed"); + require!((FixedTableTag::RangeKeyLen256, cur!(byte), cb.get_length_s() - 32.expr() - (idx + 1).expr()) => @"fixed"); } }} for (idx, &byte) in ctx.rlp_bytes()[36..68].into_iter().enumerate() { - require!((FixedTableTag::RangeKeyLen256, a!(byte), cb.get_length_c() - (idx + 1).expr()) => @"fixed"); + require!((FixedTableTag::RangeKeyLen256, cur!(byte), cb.get_length_c() - (idx + 1).expr()) => @"fixed"); } }}*/ @@ -304,7 +304,7 @@ impl MPTConfig { } /* Populate lookup tables */ - require!(@"keccak" => keccak_table.columns().iter().map(|table| a!(table)).collect()); + require!(@"keccak" => keccak_table.columns().iter().map(|table| cur!(table)).collect()); require!(@"fixed" => fixed_table.iter().map(|table| f!(table)).collect()); /* Memory banks */