Skip to content

Commit 3a7d295

Browse files
authored
Fix zktrie Key Traversal (#75)
1 parent d953c47 commit 3a7d295

File tree

5 files changed

+21
-28
lines changed

5 files changed

+21
-28
lines changed

crates/scroll/state-commitment/src/root.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,8 @@ where
233233

234234
let account = ScrollTrieAccount::from((account, storage_root));
235235
let account_hash = PoseidonValueHasher::hash_account(account);
236-
hash_builder.add_leaf(
237-
Nibbles::unpack_and_truncate_bits(hashed_address),
238-
account_hash.as_slice(),
239-
);
236+
hash_builder
237+
.add_leaf(Nibbles::unpack_bits(hashed_address), account_hash.as_slice());
240238

241239
// Decide if we need to return intermediate progress.
242240
let total_updates_len = updated_storage_nodes +
@@ -433,10 +431,7 @@ where
433431
TrieElement::Leaf(hashed_slot, value) => {
434432
let hashed_value = PoseidonValueHasher::hash_storage(value);
435433
tracker.inc_leaf();
436-
hash_builder.add_leaf(
437-
Nibbles::unpack_and_truncate_bits(hashed_slot),
438-
hashed_value.as_ref(),
439-
);
434+
hash_builder.add_leaf(Nibbles::unpack_bits(hashed_slot), hashed_value.as_ref());
440435
}
441436
}
442437
}

crates/scroll/trie/src/hash_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ mod test {
468468

469469
#[test]
470470
fn test_convert_to_bit_representation() {
471-
let nibbles = Nibbles::unpack_and_truncate_bits(vec![7, 8]);
471+
let nibbles = Nibbles::unpack_bits(vec![7, 8]);
472472
let expected = [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0];
473473
assert_eq!(nibbles.as_slice(), expected);
474474
}
@@ -478,8 +478,8 @@ mod test {
478478
// 64 byte nibble
479479
let hex = hex!("0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f01020304");
480480
assert_eq!(hex.len(), 64);
481-
let nibbles = Nibbles::unpack_and_truncate_bits(hex);
482-
assert_eq!(nibbles.len(), 248);
481+
let nibbles = Nibbles::unpack_bits(hex);
482+
assert_eq!(nibbles.len(), 254);
483483
}
484484

485485
#[test]

crates/trie/trie/src/key.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use smallvec::SmallVec;
33
use crate::Nibbles;
44

55
/// The maximum number of bits a key can contain.
6-
pub const MAX_BITS: usize = 248;
6+
pub const MAX_BITS: usize = 254;
77

88
/// The maximum number of bytes a key can contain.
9-
const MAX_BYTES: usize = 31;
9+
const MAX_BYTES: usize = 32;
1010

1111
// TODO(scroll): Refactor this into a trait that is more generic and can be used by any
1212
// implementation that requires converting between nibbles and bits. Better yet we should use a
@@ -17,8 +17,8 @@ pub trait BitsCompatibility: Sized {
1717
/// Unpacks the bits from the provided bytes such that there is a byte for each bit in the
1818
/// input. The representation is big-endian with respect to the input.
1919
///
20-
/// We truncate the Nibbles such that we only have [`MAX_BITS`] (248) bits.
21-
fn unpack_and_truncate_bits<T: AsRef<[u8]>>(data: T) -> Self;
20+
/// We truncate the Nibbles such that we only have [`MAX_BITS`] (bn254 field size) bits.
21+
fn unpack_bits<T: AsRef<[u8]>>(data: T) -> Self;
2222

2323
/// Pack the bits into a byte representation.
2424
fn pack_bits(&self) -> SmallVec<[u8; 32]>;
@@ -32,16 +32,14 @@ pub trait BitsCompatibility: Sized {
3232
}
3333

3434
impl BitsCompatibility for Nibbles {
35-
fn unpack_and_truncate_bits<T: AsRef<[u8]>>(data: T) -> Self {
35+
fn unpack_bits<T: AsRef<[u8]>>(data: T) -> Self {
3636
let data = data.as_ref();
37-
let unpacked_len = core::cmp::min(data.len() * 8, MAX_BITS);
38-
let mut bits = Vec::with_capacity(unpacked_len);
39-
40-
for byte in data.iter().take(MAX_BYTES) {
41-
for i in (0..8).rev() {
42-
bits.push(byte >> i & 1);
43-
}
44-
}
37+
let bits = data
38+
.iter()
39+
.take(MAX_BYTES)
40+
.flat_map(|&byte| (0..8).rev().map(move |i| (byte >> i) & 1))
41+
.take(MAX_BITS)
42+
.collect();
4543

4644
Self::from_vec_unchecked(bits)
4745
}

crates/trie/trie/src/node_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ where
113113
#[cfg(not(feature = "scroll"))]
114114
let cmp = key < &Nibbles::unpack(hashed_key);
115115
#[cfg(feature = "scroll")]
116-
let cmp = key < &Nibbles::unpack_and_truncate_bits(hashed_key);
116+
let cmp = key < &Nibbles::unpack_bits(hashed_key);
117117
cmp
118118
}) {
119119
self.current_walker_key_checked = false;

crates/trie/trie/src/state.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl HashedPostState {
123123
for (hashed_address, account) in &self.accounts {
124124
// TODO(scroll): replace with key abstraction
125125
#[cfg(feature = "scroll")]
126-
let nibbles = Nibbles::unpack_and_truncate_bits(hashed_address);
126+
let nibbles = Nibbles::unpack_bits(hashed_address);
127127
#[cfg(not(feature = "scroll"))]
128128
let nibbles = Nibbles::unpack(hashed_address);
129129
account_prefix_set.insert(nibbles);
@@ -139,7 +139,7 @@ impl HashedPostState {
139139
for (hashed_address, hashed_storage) in &self.storages {
140140
// TODO(scroll): replace this with abstraction.
141141
#[cfg(feature = "scroll")]
142-
let nibbles = Nibbles::unpack_and_truncate_bits(hashed_address);
142+
let nibbles = Nibbles::unpack_bits(hashed_address);
143143
#[cfg(not(feature = "scroll"))]
144144
let nibbles = Nibbles::unpack(hashed_address);
145145
account_prefix_set.insert(nibbles);
@@ -268,7 +268,7 @@ impl HashedStorage {
268268
for hashed_slot in self.storage.keys() {
269269
// TODO(scroll): replace this with key abstraction.
270270
#[cfg(feature = "scroll")]
271-
let nibbles = Nibbles::unpack_and_truncate_bits(hashed_slot);
271+
let nibbles = Nibbles::unpack_bits(hashed_slot);
272272
#[cfg(not(feature = "scroll"))]
273273
let nibbles = Nibbles::unpack(hashed_slot);
274274
prefix_set.insert(nibbles);

0 commit comments

Comments
 (0)