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

Better column query in MPT macros #8

Draft
wants to merge 1 commit into
base: mpt-refactor
Choose a base branch
from
Draft
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
54 changes: 52 additions & 2 deletions zkevm-circuits/src/circuit_tools/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -454,6 +458,24 @@ impl<F: Field, E: Expressable<F>> Expressable<F> for (E, E, E, E) {
}
}

/// Enables column query without knowing the Column type
pub trait Querier<F: Field> {
/// Query a column at a relative position
fn query(self, meta: &mut VirtualCells<F>, at: Rotation) -> Expression<F>;
}
Comment on lines +461 to +465
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Makes the API similar to what privacy-scaling-explorations/halo2#154.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, I left a comment, I think the expr method name is confusing and perhaps cur would be better for the method name.


impl<F: Field> Querier<F> for Column<Fixed> {
fn query(self, meta: &mut VirtualCells<F>, at: Rotation) -> Expression<F> {
meta.query_fixed(self, at)
}
}

impl<F: Field> Querier<F> for Column<Advice> {
fn query(self, meta: &mut VirtualCells<F>, at: Rotation) -> Expression<F> {
meta.query_advice(self, at)
}
}

/// Implementation trait `Expressable` for type able to be casted to an
/// Expression
#[macro_export]
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}};
}
Comment on lines +1158 to +1184
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the meta dependency is gone for querying cells, do you still think we need these macro's or just using the Querier syntax directly also works for you?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that column.cur() would work without the meta? In that case the macros would be unnecessary I believe.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep!


#[allow(unused_macros)]
macro_rules! not {
($expr:expr) => {{
Expand Down
20 changes: 10 additions & 10 deletions zkevm-circuits/src/mpt_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ impl<F: Field> MPTConfig<F> {
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()]);
}
}}
Expand All @@ -247,13 +247,13 @@ impl<F: Field> MPTConfig<F> {
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());
},
_ => (),
Expand All @@ -270,18 +270,18 @@ impl<F: Field> MPTConfig<F> {
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");
}
}}*/

Expand All @@ -304,7 +304,7 @@ impl<F: Field> MPTConfig<F> {
}

/* 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 */
Expand Down