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

MPT review #9

Open
wants to merge 21 commits into
base: mpt-refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
4 changes: 3 additions & 1 deletion zkevm-circuits/src/circuit_tools/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl<F: Field> ConstraintBuilder<F> {
meta: &mut ConstraintSystem<F>,
lookup_names: &[S],
) {
for lookup_name in lookup_names.iter() {
for lookup_name in lookup_names.iter() { // "keccek", "fixed", "s_parent", ...
let lookups = self
.lookups
.iter()
Expand All @@ -186,7 +186,9 @@ impl<F: Field> ConstraintBuilder<F> {
.collect::<Vec<_>>();
for lookup in lookups.iter() {
meta.lookup_any(lookup.description, |_meta| {
// 拿对应的表
let table = self.get_lookup_table_values(lookup_name);
// 拿要查的值
let mut values: Vec<_> = lookup
.values
.iter()
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ impl<F: Field> ExecutionConfig<F> {
let num_rows_left_next = meta.query_advice(num_rows_until_next_step, Rotation::next());
let num_rows_left_inverse = meta.query_advice(num_rows_inv, Rotation::cur());


let mut cb = BaseConstraintBuilder::default();
// q_step needs to be enabled on the first row
cb.condition(q_step_first, |cb| {
Expand Down
230 changes: 187 additions & 43 deletions zkevm-circuits/src/mpt_circuit.rs

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion zkevm-circuits/src/mpt_circuit/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,15 @@ impl<F: Field> ExtensionGadget<F> {

config.rlp_key = ListKeyGadget::construct(&mut cb.base, &key_bytes[0]);
// TODO(Brecht): add lookup constraint
// let is_key_part_odd = key_bytes[0][rlp_key.key_value.num_rlp_bytes()] >> 4 == 1;
config.is_key_part_odd = cb.base.query_cell();

let odd_flag = matchx! {
config.rlp_key.key_value.is_short() => key_bytes[0][0].expr(),
config.rlp_key.key_value.is_long() => key_bytes[0][1].expr(),
config.rlp_key.key_value.is_very_long() => key_bytes[0][2].expr(),
};
require!((FixedTableTag::ExtOddKey.expr(), odd_flag, config.is_key_part_odd.expr()) => @"fixed");

// We need to check that the nibbles we stored in s are between 0 and 15.
cb.set_range_s(FixedTableTag::RangeKeyLen16.expr());

Expand Down
9 changes: 5 additions & 4 deletions zkevm-circuits/src/mpt_circuit/extension_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use eth_types::Field;
use gadgets::util::Scalar;
use halo2_proofs::{
circuit::Region,
plonk::{Error, VirtualCells},
plonk::{Error, VirtualCells}, poly::Rotation,
};

use super::{
Expand Down Expand Up @@ -41,6 +41,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
let mut config = ExtensionBranchConfig::default();

circuit!([meta, cb.base], {
let q_enable = f!(ctx.q_enable);
// General inputs
config.is_extension = cb.base.query_bool();
// If we're in a placeholder, both the extension and the branch parts are
Expand Down Expand Up @@ -252,7 +253,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
region,
offset,
&mut pv.memory[key_memory(is_s)],
key_rlc_post_branch,
key_rlc_post_branch, //后面新算的
key_mult_post_branch,
num_nibbles,
false,
Expand All @@ -273,8 +274,8 @@ impl<F: Field> ExtensionBranchConfig<F> {
region,
offset,
&mut pv.memory[key_memory(is_s)],
key_data.rlc,
key_data.mult,
key_data.rlc,
key_data.mult, //维持原样,相当于重新入栈
key_data.num_nibbles,
is_key_odd,
key_rlc_post_drifted,
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/mpt_circuit/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub const RLP_HASH_VALUE: u8 = 128 + 32; // 0x80
pub const KEY_LEN: usize = 32;
pub const KEY_LEN_IN_NIBBLES: usize = KEY_LEN * 2;


// Empty trie
pub const EMPTY_TRIE_HASH: [u8; 32] = [
86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153,
Expand Down
63 changes: 50 additions & 13 deletions zkevm-circuits/src/mpt_circuit/rlp_gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ use crate::{
cell_manager::Cell,
constraint_builder::{ConstraintBuilder, RLCable, RLCableValue},
},
matchr, matchw,
mpt_circuit::param::{RLP_LIST_LONG, RLP_LIST_SHORT, RLP_SHORT},
matchr, matchw,
mpt_circuit::{
FixedTableTag,
param::{RLP_LIST_LONG, RLP_LIST_SHORT, RLP_SHORT}
},
util::Expr,
};
use eth_types::Field;
use gadgets::util::{not, Scalar};
use halo2_proofs::{
circuit::Region,
circuit::{Region, self},
plonk::{Error, Expression},
};

Expand All @@ -37,12 +40,26 @@ pub(crate) struct RLPListWitness {

impl<F: Field> RLPListGadget<F> {
pub(crate) fn construct(cb: &mut ConstraintBuilder<F>, bytes: &[Expression<F>]) -> Self {
// TODO(Brecht): add lookup/LtGadget
let is_short = cb.query_cell();
let is_long = cb.query_cell();
let is_very_long = cb.query_cell();
let is_string = cb.query_cell();

circuit!([meta, cb], {
require!(vec![
FixedTableTag::RLP.expr(),
bytes[0].clone(),
is_string.expr(),
is_short.expr(),
is_very_long.expr()] => @"fixed"
Comment on lines +52 to +54
Copy link
Owner

Choose a reason for hiding this comment

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

is_long seems to be missing? I think that's actually smart to only do 2 out of 3 in the lookup table because it saves a cell and you can deduce one of the three values e.g. is_long is 1 - is_short - is_very_long, but I don't see that being done so not sure what's up? :)

);
});

RLPListGadget {
is_short: cb.query_cell(),
is_long: cb.query_cell(),
is_very_long: cb.query_cell(),
is_string: cb.query_cell(),
is_short,
is_long,
is_very_long,
is_string,
bytes: bytes.to_vec(),
}
}
Expand All @@ -61,8 +78,11 @@ impl<F: Field> RLPListGadget<F> {
let mut is_very_long = false;
let mut is_string = false;
match bytes[0] {
// 192 - 247
RLP_LIST_SHORT..=RLP_LIST_LONG => is_short = true,
// 248
RLP_LIST_LONG_1 => is_long = true,
// 249
RLP_LIST_LONG_2 => is_very_long = true,
_ => is_string = true,
}
Expand Down Expand Up @@ -261,12 +281,26 @@ pub(crate) struct RLPValueWitness {

impl<F: Field> RLPValueGadget<F> {
pub(crate) fn construct(cb: &mut ConstraintBuilder<F>, bytes: &[Expression<F>]) -> Self {
// TODO(Brecht): add lookup
let is_short = cb.query_cell();
let is_long = cb.query_cell();
let is_very_long = cb.query_cell();
let is_list = cb.query_cell();

circuit!([meta, cb], {
require!(vec![
FixedTableTag::RLP.expr(),
bytes[0].clone(),
not::expr(is_list.expr()),
is_short.expr(),
is_very_long.expr()] => @"fixed"
);
});

RLPValueGadget {
is_short: cb.query_cell(),
is_long: cb.query_cell(),
is_very_long: cb.query_cell(),
is_list: cb.query_cell(),
is_short,
is_long,
is_very_long,
is_list,
bytes: bytes.to_vec(),
}
}
Expand All @@ -286,8 +320,11 @@ impl<F: Field> RLPValueGadget<F> {
let mut is_very_long = false;
let mut is_list = false;
match bytes[0] {
// 0 - 127
0..=RLP_SHORT_INCLUSIVE => is_short = true,
// 128 - 183
RLP_SHORT..=RLP_LONG => is_long = true,
// 189 - 191
RLP_LONG_EXCLUSIVE..=RLP_VALUE_MAX => is_very_long = true,
_ => is_list = true,
}
Expand Down