Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Wrong extension node case - constraints that don't apply ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
miha-stopar committed Apr 9, 2024
1 parent 91325b9 commit 133d8b2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
21 changes: 15 additions & 6 deletions zkevm-circuits/src/mpt_circuit/account_leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ impl<F: Field> AccountLeafConfig<F> {
MPTProofType::CodeHashChanged.expr(),
);

let is_wrong_ext_case = parent_data[1].is_last_level_and_wrong_ext_case.expr();

for is_s in [true, false] {
ifx! {not!(config.is_mod_extension[is_s.idx()].expr()) => {
// Placeholder leaf checks
Expand Down Expand Up @@ -273,7 +275,9 @@ impl<F: Field> AccountLeafConfig<F> {
// Total number of nibbles needs to be KEY_LEN_IN_NIBBLES.
let num_nibbles =
num_nibbles::expr(rlp_key.key_value.len(), key_data[is_s.idx()].is_odd.expr());
require!(key_data[is_s.idx()].num_nibbles.expr() + num_nibbles.expr() => KEY_LEN_IN_NIBBLES);
ifx! {not!(is_wrong_ext_case) => {
require!(key_data[is_s.idx()].num_nibbles.expr() + num_nibbles.expr() => KEY_LEN_IN_NIBBLES);
}}

// Check if the leaf is in its parent.
// Check is skipped for placeholder leaves which are dummy leaves.
Expand All @@ -282,7 +286,8 @@ impl<F: Field> AccountLeafConfig<F> {
// Note that the constraint works for the case when there is the placeholder branch above
// the leaf too - in this case `parent_data.hash` contains the hash of the node above the placeholder
// branch.
ifx! {not!(config.is_placeholder_leaf[is_s.idx()]) => {
ifx! {not!(or::expr(&[config.is_placeholder_leaf[is_s.idx()].expr(), is_wrong_ext_case.clone()])) => {
// ifx! {not!(config.is_placeholder_leaf[is_s.idx()]) => {
let hash = parent_data[is_s.idx()].hash.expr();
require!((1.expr(), leaf_rlc, rlp_key.rlp_list.num_bytes(), hash.lo(), hash.hi()) =>> @KECCAK);
} elsex {
Expand All @@ -305,7 +310,9 @@ impl<F: Field> AccountLeafConfig<F> {
// Note that this does not hold when there is NonExistingAccountProof wrong leaf scenario,
// in this case there is a non-nil leaf. However, in this case the leaf is not a placeholder,
// so the check below is not triggered.
require!(parent_data[is_s.idx()].rlc.expr() => 128.expr());
ifx! {not!(is_wrong_ext_case) => {
require!(parent_data[is_s.idx()].rlc.expr() => 128.expr());
}}
}}
}}
}}
Expand Down Expand Up @@ -407,18 +414,20 @@ impl<F: Field> AccountLeafConfig<F> {
// To check whether it's wrong extension node - is_last_level_and_wrong_ext_case = 1, parent is extension
// To check whether it's the nil leaf - use IsPlaceholderLeafGadget

// TODO: when is_last_level_and_wrong_ext_case, the proof type needs to be non-existing
// config.is_non_existing_account_proof.expr(),
// config.parent_data[true.idx()].is_extension.expr(),

config.wrong_ext_node = WrongExtNodeGadget::construct(
cb,
key_item.hash_rlc(),
config.is_non_existing_account_proof.expr(),
is_wrong_ext_case,
&wrong_ext_middle,
&wrong_ext_middle_nibbles,
&wrong_ext_after,
&wrong_ext_after_nibbles,
config.parent_data[true.idx()].is_extension.expr(),
config.key_data[true.idx()].clone(),
config.key_data_prev.clone(),
&cb.key_r.expr(),
);

// Anything following this node is below the account
Expand Down
3 changes: 2 additions & 1 deletion zkevm-circuits/src/mpt_circuit/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl<F: Field> BranchGadget<F> {
key_mult: Expression<F>,
num_nibbles: Expression<F>,
is_key_odd: Expression<F>,
is_last_level_and_wrong_ext_case: Expression<F>,
) -> Self {
let mut config = BranchGadget::default();

Expand Down Expand Up @@ -193,7 +194,7 @@ impl<F: Field> BranchGadget<F> {
)
};

ifx! {not!(is_placeholder[is_s.idx()]) => {
ifx! {not!(or::expr(&[is_placeholder[is_s.idx()].expr(), is_last_level_and_wrong_ext_case.clone()])) => {
ifx!{or::expr(&[is_root[is_s.idx()].expr(), not!(is_not_hashed)]) => {
// Hashed branch hash in parent branch
let hash = &parent_hash[is_s.idx()];
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/mpt_circuit/extension_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
key_mult_post_ext.expr(),
num_nibbles.expr(),
is_key_odd.expr(),
config.is_last_level_and_wrong_ext_case.expr(),
);
let branch = config.branch.get_post_state();

Expand Down
7 changes: 2 additions & 5 deletions zkevm-circuits/src/mpt_circuit/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,20 +1329,17 @@ impl<F: Field> WrongExtNodeGadget<F> {
pub(crate) fn construct(
cb: &mut MPTConstraintBuilder<F>,
expected_key: Expression<F>,
is_non_existing: Expression<F>,
is_wrong_ext_case: Expression<F>,
wrong_ext_middle: &RLPItemView<F>,
wrong_ext_middle_nibbles: &RLPItemView<F>,
wrong_ext_after: &RLPItemView<F>,
wrong_ext_after_nibbles: &RLPItemView<F>,
is_parent_extension: Expression<F>,
key_data: KeyData<F>,
key_data_prev: KeyData<F>,
r: &Expression<F>,
) -> Self {
let mut config = WrongExtNodeGadget::default();
circuit!([meta, cb.base], {
// TODO: distinguish between wrong extension node / wrong leaf / nil leaf
ifx! {and::expr(&[is_non_existing, is_parent_extension]) => {
ifx! {is_wrong_ext_case => {
// We have a key split into three parts,
// meaning that there the first part parity doesn't
// tell us about the parity of the second part (depends on the third part as well).
Expand Down

0 comments on commit 133d8b2

Please sign in to comment.