Skip to content

Commit 1afed30

Browse files
authored
fix calculation in get_tree_key_for_storage_slot (#35)
1 parent 5b9f1db commit 1afed30

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

trie/utils/verkle.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var (
3636
CodeOffset = uint256.NewInt(128)
3737
MainStorageOffset = new(uint256.Int).Lsh(uint256.NewInt(256), 31)
3838
VerkleNodeWidth = uint256.NewInt(8)
39-
codeStorageDelta = uint256.NewInt(0).Sub(HeaderStorageOffset, CodeOffset)
39+
codeStorageDelta = uint256.NewInt(0).Sub(CodeOffset, HeaderStorageOffset)
4040
)
4141

4242
func GetTreeKey(address []byte, treeIndex *uint256.Int, subIndex byte) []byte {
@@ -94,10 +94,14 @@ func GetTreeKeyStorageSlot(address []byte, storageKey *uint256.Int) []byte {
9494
treeIndex.Add(MainStorageOffset, storageKey)
9595
}
9696
treeIndex.Div(treeIndex, VerkleNodeWidth)
97-
subIndexMod := new(uint256.Int).Mod(treeIndex, VerkleNodeWidth).Bytes()
97+
98+
// calculate the sub_index, i.e. the index in the stem tree.
99+
// Because the modulus is 256, it's the last byte of treeIndex
100+
subIndexMod := treeIndex.Bytes()
98101
var subIndex byte
99102
if len(subIndexMod) != 0 {
100-
subIndex = subIndexMod[0]
103+
// Get the last byte, as uint256.Int is big-endian
104+
subIndex = subIndexMod[len(subIndexMod)-1]
101105
}
102106
return GetTreeKey(address, treeIndex, subIndex)
103107
}

0 commit comments

Comments
 (0)