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

Commit 9817e1d

Browse files
committed
Adding info whether ext. node is in the last level
1 parent 8ef1481 commit 9817e1d

File tree

8 files changed

+44
-18
lines changed

8 files changed

+44
-18
lines changed

geth-utils/gethutil/mpt/witness/branch.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func prepareBranchWitness(rows [][]byte, branch []byte, branchStart int, branchR
6666
}
6767

6868
func prepareBranchNode(branch1, branch2, extNode1, extNode2, extListRlpBytes []byte, extValues [][]byte, key, driftedInd byte,
69-
isBranchSPlaceholder, isBranchCPlaceholder, isExtension bool) Node {
69+
isBranchSPlaceholder, isBranchCPlaceholder, isExtension, isLastLevel bool) Node {
7070
extensionNode := ExtensionNode{
7171
ListRlpBytes: extListRlpBytes,
7272
}
@@ -115,6 +115,7 @@ func prepareBranchNode(branch1, branch2, extNode1, extNode2, extListRlpBytes []b
115115
extensionBranch := ExtensionBranchNode{
116116
IsExtension: isExtension,
117117
IsPlaceholder: [2]bool{isBranchSPlaceholder, isBranchCPlaceholder},
118+
IsLastLevel: isLastLevel,
118119
Extension: extensionNode,
119120
Branch: branchNode,
120121
}
@@ -206,10 +207,8 @@ func getDriftedPosition(leafKeyRow []byte, numberOfNibbles int) byte {
206207
// (used when one of the proofs have one branch more than the other).
207208
func addBranchAndPlaceholder(proof1, proof2 [][]byte,
208209
extNibblesS, extNibblesC []byte,
209-
leafRow0, key, neighbourNode []byte,
210-
keyIndex int,
211-
additionalBranch, isAccountProof, nonExistingAccountProof,
212-
isShorterProofLastLeaf bool) (bool, bool, int, Node) {
210+
leafRow0, key []byte,
211+
keyIndex int, isShorterProofLastLeaf bool) (bool, bool, int, Node) {
213212
len1 := len(proof1)
214213
len2 := len(proof2)
215214

@@ -284,7 +283,7 @@ func addBranchAndPlaceholder(proof1, proof2 [][]byte,
284283
driftedInd := getDriftedPosition(leafRow0, numberOfNibbles)
285284

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

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

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

302301
return isModifiedExtNode, isExtension, numberOfNibbles, node

geth-utils/gethutil/mpt/witness/nodes.go

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type ExtensionBranchNode struct {
7070
// nibbles of the original extension node).
7171
IsModExtension [2]bool `json:"is_mod_extension"`
7272
IsPlaceholder [2]bool `json:"is_placeholder"`
73+
IsLastLevel bool `json:"is_last_level"`
7374
Extension ExtensionNode `json:"extension"`
7475
Branch BranchNode `json:"branch"`
7576
}

geth-utils/gethutil/mpt/witness/prepare_witness.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
412412
}
413413

414414
bNode := prepareBranchNode(proof1[i], proof2[i], extNode1, extNode2, extListRlpBytes, extValues,
415-
key[keyIndex], key[keyIndex], false, false, isExtension)
415+
key[keyIndex], key[keyIndex], false, false, isExtension, false)
416416
nodes = append(nodes, bNode)
417417

418418
keyIndex += 1
@@ -430,9 +430,8 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
430430

431431
isModifiedExtNode, _, numberOfNibbles, bNode := addBranchAndPlaceholder(proof1, proof2,
432432
extNibblesS[len1-1], extNibblesC[len2-1],
433-
leafRow0, key, neighbourNode,
434-
keyIndex, additionalBranch,
435-
isAccountProof, nonExistingAccountProof, isShorterProofLastLeaf)
433+
leafRow0, key,
434+
keyIndex, isShorterProofLastLeaf)
436435

437436
nodes = append(nodes, bNode)
438437

@@ -531,7 +530,7 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
531530

532531
extNode := proof2[len(proof2)-1] // Let's name it E1
533532
bNode := prepareBranchNode(branchRlp, branchRlp, extNode, extNode, extListRlpBytes, extValues,
534-
key[keyIndex], key[keyIndex], false, false, isExtension)
533+
key[keyIndex], key[keyIndex], false, false, isExtension, true)
535534
nodes = append(nodes, bNode)
536535

537536
// Let's construct the leaf L1 that will have the correct key (the queried one)

zkevm-circuits/src/mpt_circuit/account_leaf.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ impl<F: Field> AccountLeafConfig<F> {
339339
true.expr(),
340340
false.expr(),
341341
false.expr(),
342+
false.expr(),
342343
storage_items[is_s.idx()].word(),
343344
);
344345
}
@@ -671,6 +672,7 @@ impl<F: Field> AccountLeafConfig<F> {
671672
true,
672673
false,
673674
false,
675+
false,
674676
storage_items[is_s.idx()].word(),
675677
)?;
676678
}
@@ -740,7 +742,6 @@ impl<F: Field> AccountLeafConfig<F> {
740742
&account.wrong_rlp_bytes,
741743
&expected_item,
742744
true,
743-
parent_data[1].is_extension,
744745
key_data[true.idx()].clone(),
745746
region.key_r,
746747
)?;

zkevm-circuits/src/mpt_circuit/extension_branch.rs

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub(crate) struct ExtensionBranchConfig<F> {
2626
parent_data: [ParentData<F>; 2],
2727
is_placeholder: [Cell<F>; 2],
2828
is_extension: Cell<F>,
29+
is_last_level: Cell<F>,
2930
extension: ExtensionGadget<F>,
3031
branch: BranchGadget<F>,
3132
}
@@ -41,6 +42,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
4142
circuit!([meta, cb], {
4243
// General inputs
4344
config.is_extension = cb.query_bool();
45+
config.is_last_level = cb.query_bool();
4446
// If we're in a placeholder, both the extension and the branch parts are
4547
// placeholders
4648
for is_s in [true, false] {
@@ -159,6 +161,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
159161
false.expr(),
160162
false.expr(),
161163
config.is_extension.expr(),
164+
config.is_last_level.expr(),
162165
WordLoHi::zero(),
163166
);
164167
} elsex {
@@ -186,6 +189,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
186189
config.parent_data[is_s.idx()].is_root.expr(),
187190
true.expr(),
188191
config.is_extension.expr(),
192+
config.is_last_level.expr(),
189193
branch.mod_word[is_s.idx()].clone(),
190194
);
191195
}}
@@ -210,6 +214,9 @@ impl<F: Field> ExtensionBranchConfig<F> {
210214
let is_extension = extension_branch.is_extension.scalar();
211215
self.is_extension.assign(region, offset, is_extension)?;
212216

217+
let is_last_level = extension_branch.is_last_level.scalar();
218+
self.is_last_level.assign(region, offset, is_last_level)?;
219+
213220
let key_data =
214221
self.key_data
215222
.witness_load(region, offset, &mut memory[key_memory(true)], 0)?;
@@ -294,6 +301,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
294301
false,
295302
false,
296303
is_extension == 1.into(),
304+
is_last_level == 1.into(),
297305
WordLoHi::zero(),
298306
)?;
299307
} else {
@@ -317,6 +325,7 @@ impl<F: Field> ExtensionBranchConfig<F> {
317325
parent_data[is_s.idx()].is_root,
318326
true,
319327
is_extension == 1.into(),
328+
is_last_level == 1.into(),
320329
mod_node_hash_word[is_s.idx()],
321330
)?;
322331
}

zkevm-circuits/src/mpt_circuit/helpers.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,13 @@ pub(crate) struct ParentData<F> {
558558
pub(crate) rlc: Cell<F>,
559559
pub(crate) is_root: Cell<F>,
560560
pub(crate) is_placeholder: Cell<F>,
561+
// is_extension is used only in a non-existing proof / wrong extension node case -
562+
// in account/storage leaf to check whether the parent is an extension node
561563
pub(crate) is_extension: Cell<F>,
564+
// is_ext_last_level is used only in a non-existing proof - in wrong extension node case the branch
565+
// is a placeholder and the check for a branch hash being in the parent extension node needs to be ignored,
566+
// but it needs to be ignored only in the last branch (the branch above the leaf into which the lookup is made)
567+
pub(crate) is_ext_last_level: Cell<F>,
562568
pub(crate) drifted_parent_hash: WordLoHiCell<F>,
563569
}
564570

@@ -569,6 +575,7 @@ pub(crate) struct ParentDataWitness<F> {
569575
pub(crate) is_root: bool,
570576
pub(crate) is_placeholder: bool,
571577
pub(crate) is_extension: bool,
578+
pub(crate) is_ext_last_level: bool,
572579
pub(crate) drifted_parent_hash: WordLoHi<F>,
573580
}
574581

@@ -584,6 +591,7 @@ impl<F: Field> ParentData<F> {
584591
is_root: cb.query_cell(),
585592
is_placeholder: cb.query_cell(),
586593
is_extension: cb.query_cell(),
594+
is_ext_last_level: cb.query_cell(),
587595
drifted_parent_hash: cb.query_word_unchecked(),
588596
};
589597
circuit!([meta, cb.base], {
@@ -597,6 +605,7 @@ impl<F: Field> ParentData<F> {
597605
parent_data.is_root.expr(),
598606
parent_data.is_placeholder.expr(),
599607
parent_data.is_extension.expr(),
608+
parent_data.is_ext_last_level.expr(),
600609
parent_data.drifted_parent_hash.lo().expr(),
601610
parent_data.drifted_parent_hash.hi().expr(),
602611
],
@@ -613,6 +622,7 @@ impl<F: Field> ParentData<F> {
613622
is_root: Expression<F>,
614623
is_placeholder: Expression<F>,
615624
is_extension: Expression<F>,
625+
is_ext_last_level: Expression<F>,
616626
drifted_parent_hash: WordLoHi<Expression<F>>,
617627
) {
618628
memory.store(
@@ -624,6 +634,7 @@ impl<F: Field> ParentData<F> {
624634
is_root,
625635
is_placeholder,
626636
is_extension,
637+
is_ext_last_level,
627638
drifted_parent_hash.lo(),
628639
drifted_parent_hash.hi(),
629640
],
@@ -640,6 +651,7 @@ impl<F: Field> ParentData<F> {
640651
force_hashed: bool,
641652
is_placeholder: bool,
642653
is_extension: bool,
654+
is_ext_last_level: bool,
643655
drifted_parent_hash: WordLoHi<F>,
644656
) -> Result<(), Error> {
645657
memory.witness_store(
@@ -651,6 +663,7 @@ impl<F: Field> ParentData<F> {
651663
force_hashed.scalar(),
652664
is_placeholder.scalar(),
653665
is_extension.scalar(),
666+
is_ext_last_level.scalar(),
654667
drifted_parent_hash.lo(),
655668
drifted_parent_hash.hi(),
656669
],
@@ -673,20 +686,22 @@ impl<F: Field> ParentData<F> {
673686
self.is_root.assign(region, offset, values[3])?;
674687
self.is_placeholder.assign(region, offset, values[4])?;
675688
self.is_extension.assign(region, offset, values[5])?;
689+
self.is_ext_last_level.assign(region, offset, values[6])?;
676690
self.drifted_parent_hash
677691
.lo()
678-
.assign(region, offset, values[6])?;
692+
.assign(region, offset, values[7])?;
679693
self.drifted_parent_hash
680694
.hi()
681-
.assign(region, offset, values[7])?;
695+
.assign(region, offset, values[8])?;
682696

683697
Ok(ParentDataWitness {
684698
hash: WordLoHi::new([values[0], values[1]]),
685699
rlc: values[2],
686700
is_root: values[3] == 1.scalar(),
687701
is_placeholder: values[4] == 1.scalar(),
688702
is_extension: values[5] == 1.scalar(),
689-
drifted_parent_hash: WordLoHi::new([values[6], values[7]]),
703+
is_ext_last_level: values[6] == 1.scalar(),
704+
drifted_parent_hash: WordLoHi::new([values[7], values[8]]),
690705
})
691706
}
692707
}
@@ -1273,7 +1288,6 @@ impl<F: Field> WrongLeafGadget<F> {
12731288
list_bytes: &[u8],
12741289
expected_item: &RLPItemWitness,
12751290
for_placeholder_s: bool,
1276-
is_parent_extension: bool,
12771291
key_data: KeyDataWitness<F>,
12781292
r: F,
12791293
) -> Result<(F, F), Error> {

zkevm-circuits/src/mpt_circuit/start.rs

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl<F: Field> StartConfig<F> {
6868
true.expr(),
6969
false.expr(),
7070
false.expr(),
71+
false.expr(),
7172
root[is_s.idx()].clone(),
7273
);
7374
KeyData::store_defaults(cb, &mut ctx.memory[key_memory(is_s)]);
@@ -123,6 +124,7 @@ impl<F: Field> StartConfig<F> {
123124
true,
124125
false,
125126
false,
127+
false,
126128
root[is_s.idx()],
127129
)?;
128130
KeyData::witness_store(

zkevm-circuits/src/mpt_circuit/storage_leaf.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ impl<F: Field> StorageLeafConfig<F> {
254254
true.expr(),
255255
false.expr(),
256256
false.expr(),
257+
false.expr(),
257258
WordLoHi::zero(),
258259
);
259260
}
@@ -523,6 +524,7 @@ impl<F: Field> StorageLeafConfig<F> {
523524
true,
524525
false,
525526
false,
527+
false,
526528
WordLoHi::<F>::new([F::ZERO, F::ZERO]),
527529
)?;
528530

@@ -571,7 +573,6 @@ impl<F: Field> StorageLeafConfig<F> {
571573
&storage.wrong_rlp_bytes,
572574
&expected_item,
573575
false,
574-
parent_data[1].is_extension,
575576
key_data[true.idx()].clone(),
576577
region.key_r,
577578
)?;

0 commit comments

Comments
 (0)