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

Commit

Permalink
branching for non-existing proof
Browse files Browse the repository at this point in the history
  • Loading branch information
miha-stopar committed Apr 2, 2024
1 parent 9817e1d commit 91325b9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
6 changes: 3 additions & 3 deletions geth-utils/gethutil/mpt/witness/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func prepareBranchNode(branch1, branch2, extNode1, extNode2, extListRlpBytes []b
extensionBranch := ExtensionBranchNode{
IsExtension: isExtension,
IsPlaceholder: [2]bool{isBranchSPlaceholder, isBranchCPlaceholder},
IsLastLevel: isLastLevel,
IsLastLevelAndWrongExtCase: isLastLevel,
Extension: extensionNode,
Branch: branchNode,
}
Expand Down Expand Up @@ -283,7 +283,7 @@ func addBranchAndPlaceholder(proof1, proof2 [][]byte,
driftedInd := getDriftedPosition(leafRow0, numberOfNibbles)

node = prepareBranchNode(proof1[len1-2], proof1[len1-2], extNode, extNode, extListRlpBytes, extValues,
key[keyIndex+numberOfNibbles], driftedInd, false, true, isExtension, true)
key[keyIndex+numberOfNibbles], driftedInd, false, true, isExtension, false)

// We now get the first nibble of the leaf that was turned into branch.
// This first nibble presents the position of the leaf once it moved
Expand All @@ -295,7 +295,7 @@ func addBranchAndPlaceholder(proof1, proof2 [][]byte,
driftedInd := getDriftedPosition(leafRow0, numberOfNibbles)

node = prepareBranchNode(proof2[len2-2], proof2[len2-2], extNode, extNode, extListRlpBytes, extValues,
key[keyIndex+numberOfNibbles], driftedInd, true, false, isExtension, true)
key[keyIndex+numberOfNibbles], driftedInd, true, false, isExtension, false)
}

return isModifiedExtNode, isExtension, numberOfNibbles, node
Expand Down
2 changes: 1 addition & 1 deletion geth-utils/gethutil/mpt/witness/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type ExtensionBranchNode struct {
// nibbles of the original extension node).
IsModExtension [2]bool `json:"is_mod_extension"`
IsPlaceholder [2]bool `json:"is_placeholder"`
IsLastLevel bool `json:"is_last_level"`
IsLastLevelAndWrongExtCase bool `json:"is_last_level_and_wrong_ext_case"`
Extension ExtensionNode `json:"extension"`
Branch BranchNode `json:"branch"`
}
Expand Down
19 changes: 17 additions & 2 deletions zkevm-circuits/src/mpt_circuit/account_leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use halo2_proofs::{
};

use super::{
helpers::{ext_key_rlc_calc_value, KeyDataWitness, ListKeyGadget, MainData, ParentDataWitness},
helpers::{KeyDataWitness, ListKeyGadget, MainData, ParentDataWitness},
mod_extension::ModExtensionGadget,
rlp_gadgets::RLPItemWitness,
witness_row::{AccountRowType, Node},
Expand All @@ -22,7 +22,7 @@ use crate::{
evm_circuit::util::from_bytes,
mpt_circuit::{
helpers::{
ext_key_rlc_expr, key_memory, main_memory, num_nibbles, parent_memory, DriftedGadget, Indexable, IsPlaceholderLeafGadget, KeyData, MPTConstraintBuilder, ParentData, WrongExtNodeGadget, WrongLeafGadget, KECCAK
key_memory, main_memory, num_nibbles, parent_memory, DriftedGadget, Indexable, IsPlaceholderLeafGadget, KeyData, MPTConstraintBuilder, ParentData, WrongExtNodeGadget, WrongLeafGadget, KECCAK
},
param::{EMPTY_TRIE_HASH, KEY_LEN_IN_NIBBLES, RLP_LIST_LONG, RLP_LONG},
MPTConfig, MPTContext, MptMemory, RlpItemType,
Expand Down Expand Up @@ -392,6 +392,21 @@ impl<F: Field> AccountLeafConfig<F> {
let wrong_ext_after_nibbles =
ctx.rlp_item(meta, cb, AccountRowType::ShortExtNodeNibbles as usize, RlpItemType::Nibbles);

// The extension_branch in the last level needs has `is_last_level_and_wrong_ext_case = true`
// in the case of wrong extension node.
// All other extension_branches (above it) need to have it `false` (constraint in
// extension_branch.rs)
// TODO: Use is_last_level_and_wrong_ext_case as a flag for one of the three cases
// require!(config.parent_data[1].is_last_level_and_wrong_ext_case.expr() => true.expr());
// TODO: use C proof everywhere for non-existing proof.

// TODO: when non-existing-proof, it needs to be one of the following cases:
// wrong leaf, wrong extension node, nil leaf - we need to check the sum of these
// three cases is 1.
// To check whether it's the wrong leaf case - parent is not extension, leaf is not placeholder
// 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

config.wrong_ext_node = WrongExtNodeGadget::construct(
cb,
key_item.hash_rlc(),
Expand Down
23 changes: 14 additions & 9 deletions zkevm-circuits/src/mpt_circuit/extension_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) struct ExtensionBranchConfig<F> {
parent_data: [ParentData<F>; 2],
is_placeholder: [Cell<F>; 2],
is_extension: Cell<F>,
is_last_level: Cell<F>,
is_last_level_and_wrong_ext_case: Cell<F>,
extension: ExtensionGadget<F>,
branch: BranchGadget<F>,
}
Expand All @@ -42,7 +42,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
circuit!([meta, cb], {
// General inputs
config.is_extension = cb.query_bool();
config.is_last_level = cb.query_bool();
config.is_last_level_and_wrong_ext_case = cb.query_bool();
// If we're in a placeholder, both the extension and the branch parts are
// placeholders
for is_s in [true, false] {
Expand Down Expand Up @@ -140,6 +140,11 @@ impl<F: Field> ExtensionBranchConfig<F> {

// Set the new keys
for is_s in [true, false] {
// The extension_branch in the last level needs to have `is_ext_last_level = true` (checked
// in account_leaf.rs / storage_leaf.rs).
// All other extension_branches need to have it `false`:
require!(config.parent_data[is_s.idx()].is_last_level_and_wrong_ext_case.expr() => false.expr());

ifx! {not!(config.is_placeholder[is_s.idx()].expr()) => {
KeyData::store(
cb,
Expand All @@ -161,7 +166,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
false.expr(),
false.expr(),
config.is_extension.expr(),
config.is_last_level.expr(),
config.is_last_level_and_wrong_ext_case.expr(),
WordLoHi::zero(),
);
} elsex {
Expand Down Expand Up @@ -189,9 +194,9 @@ impl<F: Field> ExtensionBranchConfig<F> {
config.parent_data[is_s.idx()].is_root.expr(),
true.expr(),
config.is_extension.expr(),
config.is_last_level.expr(),
config.is_last_level_and_wrong_ext_case.expr(),
branch.mod_word[is_s.idx()].clone(),
);
);
}}
}
});
Expand All @@ -214,8 +219,8 @@ impl<F: Field> ExtensionBranchConfig<F> {
let is_extension = extension_branch.is_extension.scalar();
self.is_extension.assign(region, offset, is_extension)?;

let is_last_level = extension_branch.is_last_level.scalar();
self.is_last_level.assign(region, offset, is_last_level)?;
let is_last_level_and_wrong_ext_case = extension_branch.is_last_level_and_wrong_ext_case.scalar();
self.is_last_level_and_wrong_ext_case.assign(region, offset, is_last_level_and_wrong_ext_case)?;

let key_data =
self.key_data
Expand Down Expand Up @@ -301,7 +306,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
false,
false,
is_extension == 1.into(),
is_last_level == 1.into(),
is_last_level_and_wrong_ext_case == 1.into(),
WordLoHi::zero(),
)?;
} else {
Expand All @@ -325,7 +330,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
parent_data[is_s.idx()].is_root,
true,
is_extension == 1.into(),
is_last_level == 1.into(),
is_last_level_and_wrong_ext_case == 1.into(),
mod_node_hash_word[is_s.idx()],
)?;
}
Expand Down
15 changes: 8 additions & 7 deletions zkevm-circuits/src/mpt_circuit/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,11 @@ pub(crate) struct ParentData<F> {
// is_extension is used only in a non-existing proof / wrong extension node case -
// in account/storage leaf to check whether the parent is an extension node
pub(crate) is_extension: Cell<F>,
// is_ext_last_level is used only in a non-existing proof - in wrong extension node case the branch
// is a placeholder and the check for a branch hash being in the parent extension node needs to be ignored,
// but it needs to be ignored only in the last branch (the branch above the leaf into which the lookup is made)
pub(crate) is_ext_last_level: Cell<F>,
// is_last_level_and_wrong_ext_case is used only in a non-existing proof in wrong extension node case -
// the last branch is a placeholder in this case and the check for a branch hash being in the parent
// extension node needs to be ignored, but it needs to be ignored only in the last branch
// (the branch above the leaf into which the lookup is made)
pub(crate) is_last_level_and_wrong_ext_case: Cell<F>,
pub(crate) drifted_parent_hash: WordLoHiCell<F>,
}

Expand All @@ -591,7 +592,7 @@ impl<F: Field> ParentData<F> {
is_root: cb.query_cell(),
is_placeholder: cb.query_cell(),
is_extension: cb.query_cell(),
is_ext_last_level: cb.query_cell(),
is_last_level_and_wrong_ext_case: cb.query_cell(),
drifted_parent_hash: cb.query_word_unchecked(),
};
circuit!([meta, cb.base], {
Expand All @@ -605,7 +606,7 @@ impl<F: Field> ParentData<F> {
parent_data.is_root.expr(),
parent_data.is_placeholder.expr(),
parent_data.is_extension.expr(),
parent_data.is_ext_last_level.expr(),
parent_data.is_last_level_and_wrong_ext_case.expr(),
parent_data.drifted_parent_hash.lo().expr(),
parent_data.drifted_parent_hash.hi().expr(),
],
Expand Down Expand Up @@ -686,7 +687,7 @@ impl<F: Field> ParentData<F> {
self.is_root.assign(region, offset, values[3])?;
self.is_placeholder.assign(region, offset, values[4])?;
self.is_extension.assign(region, offset, values[5])?;
self.is_ext_last_level.assign(region, offset, values[6])?;
self.is_last_level_and_wrong_ext_case.assign(region, offset, values[6])?;
self.drifted_parent_hash
.lo()
.assign(region, offset, values[7])?;
Expand Down
2 changes: 2 additions & 0 deletions zkevm-circuits/src/mpt_circuit/witness_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ pub struct ExtensionBranchNode {
/// TODO Doc.
pub is_extension: bool,
/// TODO Doc.
pub is_last_level_and_wrong_ext_case: bool,
/// TODO Doc.
pub(crate) is_mod_extension: [bool; 2],
/// TODO Doc.
pub is_placeholder: [bool; 2],
Expand Down

0 comments on commit 91325b9

Please sign in to comment.