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

Commit

Permalink
Fixing problems introduced by adding wrong extension node gadget
Browse files Browse the repository at this point in the history
  • Loading branch information
miha-stopar committed Apr 16, 2024
1 parent b1c8824 commit d10281f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 65 deletions.
14 changes: 10 additions & 4 deletions geth-utils/gethutil/mpt/witness/prepare_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,16 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
leafRow0 = proof2[len2-1]
}

extensionNibblesS := []byte{}
extensionNibblesC := []byte{}
if len1 > 2 {
extensionNibblesS = extNibblesS[len1-3]
}
if len2 > 2 {
extensionNibblesC = extNibblesC[len2-3]
}
isModifiedExtNode, _, numberOfNibbles, bNode := addBranchAndPlaceholder(proof1, proof2,
extNibblesS[len1-1], extNibblesC[len2-1],
extensionNibblesS, extensionNibblesC,
leafRow0, key,
keyIndex, isShorterProofLastLeaf)

Expand Down Expand Up @@ -484,9 +492,7 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
// When non existing proof and only the branches are returned, we add a placeholder leaf.
// This is to enable the lookup (in account leaf row), most constraints are disabled for these rows.

isLastBranch := isBranch(proof2[len(proof2)-1])

if (len1 == 0 && len2 == 0) || isLastBranch {
if (len1 == 0 && len2 == 0) || isBranch(proof2[len(proof2)-1]) {
// We need to add a placeholder leaf
if isAccountProof {
node := prepareAccountLeafPlaceholderNode(addr, addrh, key, keyIndex)
Expand Down
60 changes: 31 additions & 29 deletions zkevm-circuits/src/mpt_circuit/account_leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,38 +745,40 @@ impl<F: Field> AccountLeafConfig<F> {
region.key_r,
)?;

// Wrong leaf / extension node handling
let mut key_data_prev = KeyDataWitness::default();
if offset > 2 {
key_data_prev = self.key_data_prev.witness_load(
if is_non_existing_proof {
// Wrong leaf / extension node handling
let mut key_data_prev = KeyDataWitness::default();
if offset > 2 {
key_data_prev = self.key_data_prev.witness_load(
region,
offset,
&mut memory[key_memory(false)],
2, // 2 instead of 1 because default values have already been stored above
)?;
}
self.wrong_leaf.assign(
region,
offset,
&mut memory[key_memory(false)],
2, // 2 instead of 1 because default values have already been stored above
)?;
}
self.wrong_leaf.assign(
region,
offset,
is_non_existing_proof,
&key_rlc,
&account.wrong_rlp_bytes,
&expected_item,
true,
key_data[1].clone(),
region.key_r,
)?;
is_non_existing_proof,
&key_rlc,
&account.wrong_rlp_bytes,
&expected_item,
true,
key_data[1].clone(),
region.key_r,
)?;

let wrong_ext_middle = rlp_values[AccountRowType::LongExtNodeKey as usize].clone();
let wrong_ext_middle_nibbles = rlp_values[AccountRowType::LongExtNodeNibbles as usize].clone();
self.wrong_ext_node.assign(
region,
offset,
wrong_ext_middle,
wrong_ext_middle_nibbles,
key_data[1].clone(),
key_data_prev.clone(),
);
let wrong_ext_middle = rlp_values[AccountRowType::LongExtNodeKey as usize].clone();
let wrong_ext_middle_nibbles = rlp_values[AccountRowType::LongExtNodeNibbles as usize].clone();
self.wrong_ext_node.assign(
region,
offset,
wrong_ext_middle,
wrong_ext_middle_nibbles,
key_data[1].clone(),
key_data_prev.clone(),
);
}

// Anything following this node is below the account
MainData::witness_store(
Expand Down
10 changes: 6 additions & 4 deletions zkevm-circuits/src/mpt_circuit/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,11 +1453,13 @@ impl<F: Field> WrongExtNodeGadget<F> {

let mut mult = key_data.mult;
if !key_data.is_odd {
let iters = (key_data.num_nibbles - 1 - 1) / 2; // -1 because of the branch nibble, -1 because of being odd
mult = F::one();
if key_data.num_nibbles > 1 {
let iters = (key_data.num_nibbles - 1 - 1) / 2; // -1 because of the branch nibble, -1 because of being odd
mult = F::one();

for _ in 0..iters {
mult = mult * region.key_r;
for _ in 0..iters {
mult = mult * region.key_r;
}
}
}
let _ = self.mult_without_branch_nibble.assign(region, offset, mult);
Expand Down
58 changes: 30 additions & 28 deletions zkevm-circuits/src/mpt_circuit/storage_leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,35 +604,37 @@ impl<F: Field> StorageLeafConfig<F> {
region.key_r,
)?;

// Wrong leaf / extension node handling
let key_data_prev = self.key_data_prev.witness_load(
region,
offset,
&mut memory[key_memory(false)],
2, // 2 instead of 1 because default values have already been stored above
)?;
let (_key_rlc, _) = self.wrong_leaf.assign(
region,
offset,
is_non_existing_proof,
&key_rlc,
&storage.wrong_rlp_bytes,
&expected_item,
false,
key_data[1].clone(),
region.key_r,
)?;
if is_non_existing_proof {
// Wrong leaf / extension node handling
let key_data_prev = self.key_data_prev.witness_load(
region,
offset,
&mut memory[key_memory(false)],
2, // 2 instead of 1 because default values have already been stored above
)?;
let (_key_rlc, _) = self.wrong_leaf.assign(
region,
offset,
is_non_existing_proof,
&key_rlc,
&storage.wrong_rlp_bytes,
&expected_item,
false,
key_data[1].clone(),
region.key_r,
)?;

let wrong_ext_middle = rlp_values[StorageRowType::LongExtNodeKey as usize].clone();
let wrong_ext_middle_nibbles = rlp_values[StorageRowType::LongExtNodeNibbles as usize].clone();
self.wrong_ext_node.assign(
region,
offset,
wrong_ext_middle,
wrong_ext_middle_nibbles,
key_data[1].clone(),
key_data_prev.clone(),
);
let wrong_ext_middle = rlp_values[StorageRowType::LongExtNodeKey as usize].clone();
let wrong_ext_middle_nibbles = rlp_values[StorageRowType::LongExtNodeNibbles as usize].clone();
self.wrong_ext_node.assign(
region,
offset,
wrong_ext_middle,
wrong_ext_middle_nibbles,
key_data[1].clone(),
key_data_prev.clone(),
);
}

// Reset the main memory
MainData::witness_store(
Expand Down

0 comments on commit d10281f

Please sign in to comment.