From 97f80aa5d19f91b030f4719227feee000d657ed8 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 4 Mar 2026 18:13:56 +0100 Subject: [PATCH] review feedback + fix endianness bug --- trie/bintrie/key_encoding.go | 43 ++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/trie/bintrie/key_encoding.go b/trie/bintrie/key_encoding.go index c5fc1d2af6..5492d3822e 100644 --- a/trie/bintrie/key_encoding.go +++ b/trie/bintrie/key_encoding.go @@ -50,18 +50,23 @@ func GetBinaryTreeKey(addr common.Address, key []byte) []byte { return getBinaryTreeKey(addr, key, false) } -func getBinaryTreeKey(addr common.Address, key []byte, overflow bool) []byte { +func getBinaryTreeKey(addr common.Address, offset []byte, overflow bool) []byte { hasher := sha256.New() hasher.Write(zeroHash[:12]) hasher.Write(addr[:]) - hasher.Write(key[:31]) + // key is big endian, hashed value is little endian + for i := range offset[:31] { + hasher.Write([]byte{offset[30-i]}) + } if overflow { + // Overflow detected when adding MAIN_STORAGE_OFFSET, + // reporting it in the shifter 32 byte value. hasher.Write([]byte{1}) } else { hasher.Write([]byte{0}) } k := hasher.Sum(nil) - k[31] = key[31] + k[31] = offset[31] return k } @@ -77,25 +82,29 @@ func GetBinaryTreeKeyCodeHash(addr common.Address) []byte { return GetBinaryTreeKey(addr, k[:]) } -func GetBinaryTreeKeyStorageSlot(address common.Address, key []byte) []byte { - var k [32]byte +func GetBinaryTreeKeyStorageSlot(address common.Address, slotnum []byte) []byte { + var offset [32]byte // Case when the key belongs to the account header - if bytes.Equal(key[:31], zeroHash[:31]) && key[31] < 64 { - k[31] = 64 + key[31] - return GetBinaryTreeKey(address, k[:]) + if bytes.Equal(slotnum[:31], zeroHash[:31]) && slotnum[31] < 64 { + offset[31] = 64 + slotnum[31] + return GetBinaryTreeKey(address, offset[:]) } - // Set the main storage offset - // note that the first 64 bytes of the main offset storage - // are unreachable, which is consistent with the spec and - // what verkle does. - copy(k[:31], key[:31]) - overflow := k[0] == 255 - k[0] += 1 // 1 << 248, handle overflow out of band - k[31] = key[31] + // Set the main storage offset offset = MAIN_STORAGE_OFFSET + slotnum + // * Note that MAIN_STORAGE_OFFSET is 1 << 248, so the number + // can overflow into a 33rd byte, but since the value is + // shifted by one byte in getBinaryTreeKey, this only takes + // note of the overflow, and the value will be added after + // the shift, in order to avoid allocating an extra byte. + // * Note that the first 64 bytes of the main offset storage + // are unreachable, which is consistent with the spec. + // * Note that `slotnum` is big-endian + overflow := slotnum[0] == 255 + copy(offset[:], slotnum) + offset[0] += 1 // 1 << 248, handle overflow out of band - return getBinaryTreeKey(address, k[:], overflow) + return getBinaryTreeKey(address, offset[:], overflow) } func GetBinaryTreeKeyCodeChunk(address common.Address, chunknr *uint256.Int) []byte {