From 7360d168c8b59a07cf11f787924ff41be37c9e4a Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 23 Nov 2021 21:18:30 +0100 Subject: [PATCH] fix calculation in get_tree_key_for_storage_slot (#35) --- trie/utils/verkle.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/trie/utils/verkle.go b/trie/utils/verkle.go index 46de70fca289..f86f95da6210 100644 --- a/trie/utils/verkle.go +++ b/trie/utils/verkle.go @@ -36,7 +36,7 @@ var ( CodeOffset = uint256.NewInt(128) MainStorageOffset = new(uint256.Int).Lsh(uint256.NewInt(256), 31) VerkleNodeWidth = uint256.NewInt(8) - codeStorageDelta = uint256.NewInt(0).Sub(HeaderStorageOffset, CodeOffset) + codeStorageDelta = uint256.NewInt(0).Sub(CodeOffset, HeaderStorageOffset) ) func GetTreeKey(address []byte, treeIndex *uint256.Int, subIndex byte) []byte { @@ -94,10 +94,14 @@ func GetTreeKeyStorageSlot(address []byte, storageKey *uint256.Int) []byte { treeIndex.Add(MainStorageOffset, storageKey) } treeIndex.Div(treeIndex, VerkleNodeWidth) - subIndexMod := new(uint256.Int).Mod(treeIndex, VerkleNodeWidth).Bytes() + + // calculate the sub_index, i.e. the index in the stem tree. + // Because the modulus is 256, it's the last byte of treeIndex + subIndexMod := treeIndex.Bytes() var subIndex byte if len(subIndexMod) != 0 { - subIndex = subIndexMod[0] + // Get the last byte, as uint256.Int is big-endian + subIndex = subIndexMod[len(subIndexMod)-1] } return GetTreeKey(address, treeIndex, subIndex) }