Skip to content

Commit

Permalink
added count_shared_prefix_len merkle util
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed May 11, 2024
1 parent 7bdc717 commit 3060f13
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/library/merkle_patricia_utils.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fossil::library::words64_utils::words64_to_nibbles;
use fossil::types::Words64Sequence;
use fossil::types::{Words64Sequence, Words64};

pub fn merkle_patricia_input_decode(input: Words64Sequence) -> Array<u64> {
let first_nibble = *words64_to_nibbles(input, 0).at(0);
Expand All @@ -23,6 +23,24 @@ pub fn merkle_patricia_input_decode(input: Words64Sequence) -> Array<u64> {
words64_to_nibbles(input, skip_nibbles)
}

pub fn count_shared_prefix_len(
current_path_offset: usize, path: Words64, node_path: Words64, current_index: usize
) -> usize {
if current_index + current_path_offset >= path.len() && current_index >= node_path.len() {
return current_index;
} else {
let path_nibble = *path.at(current_index + current_path_offset);
let node_path_nibble = *node_path.at(current_index);

if path_nibble == node_path_nibble {
return count_shared_prefix_len(current_path_offset, path, node_path, current_index + 1);
} else {
return current_index;
}

}
}

#[cfg(test)]
mod tests {
#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/types.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Words64 = Span<u64>;
pub type Words64 = Span<u64>;

#[derive(Default, Drop, Serde, starknet::Store)]
pub struct Keccak256Hash {
Expand Down

0 comments on commit 3060f13

Please sign in to comment.