From d10281f8f4066b9d078d4b58b06db307cc8082f0 Mon Sep 17 00:00:00 2001 From: miha-stopar Date: Tue, 16 Apr 2024 12:08:28 +0200 Subject: [PATCH] Fixing problems introduced by adding wrong extension node gadget --- .../gethutil/mpt/witness/prepare_witness.go | 14 +++-- .../src/mpt_circuit/account_leaf.rs | 60 ++++++++++--------- zkevm-circuits/src/mpt_circuit/helpers.rs | 10 ++-- .../src/mpt_circuit/storage_leaf.rs | 58 +++++++++--------- 4 files changed, 77 insertions(+), 65 deletions(-) diff --git a/geth-utils/gethutil/mpt/witness/prepare_witness.go b/geth-utils/gethutil/mpt/witness/prepare_witness.go index 30c68a3356..e3c6ed77b8 100644 --- a/geth-utils/gethutil/mpt/witness/prepare_witness.go +++ b/geth-utils/gethutil/mpt/witness/prepare_witness.go @@ -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) @@ -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) diff --git a/zkevm-circuits/src/mpt_circuit/account_leaf.rs b/zkevm-circuits/src/mpt_circuit/account_leaf.rs index c5fec451a2..702bfc8a69 100644 --- a/zkevm-circuits/src/mpt_circuit/account_leaf.rs +++ b/zkevm-circuits/src/mpt_circuit/account_leaf.rs @@ -745,38 +745,40 @@ impl AccountLeafConfig { 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( diff --git a/zkevm-circuits/src/mpt_circuit/helpers.rs b/zkevm-circuits/src/mpt_circuit/helpers.rs index f66c782745..79b005c520 100644 --- a/zkevm-circuits/src/mpt_circuit/helpers.rs +++ b/zkevm-circuits/src/mpt_circuit/helpers.rs @@ -1453,11 +1453,13 @@ impl WrongExtNodeGadget { 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); diff --git a/zkevm-circuits/src/mpt_circuit/storage_leaf.rs b/zkevm-circuits/src/mpt_circuit/storage_leaf.rs index 11a2dd2808..b5f4c52476 100644 --- a/zkevm-circuits/src/mpt_circuit/storage_leaf.rs +++ b/zkevm-circuits/src/mpt_circuit/storage_leaf.rs @@ -604,35 +604,37 @@ impl StorageLeafConfig { 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(