Skip to content

chore: get_lca for NodeIndex #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ derive_more = "0.99.17"
ethnum = "1.5.0"
hex = "0.4"
pretty_assertions = "1.2.1"
rand = "0.8.5"
rstest = "0.17.0"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.116"
Expand Down
1 change: 1 addition & 0 deletions crates/committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ workspace = true

[dev-dependencies]
pretty_assertions.workspace = true
rand.workspace = true
rstest.workspace = true

[dependencies]
Expand Down
23 changes: 23 additions & 0 deletions crates/committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ impl NodeIndex {
Self::BITS - self.leading_zeros()
}

#[allow(dead_code)]
/// Get the LCA (Lowest Common Ancestor) of the two nodes.
pub(crate) fn get_lca(&self, other: &NodeIndex) -> NodeIndex {
if self == other {
return *self;
}

let bit_length = self.bit_length();
let other_bit_length = other.bit_length();
// Bring self to the level of other.
let adapted_self = if self < other {
*self << (other_bit_length - bit_length)
} else {
*self >> (bit_length - other_bit_length)
};

let xor = adapted_self.0 ^ other.0;
// The length of the remainder after removing the common prefix of the two nodes.
let post_common_prefix_len = NodeIndex(xor).bit_length();
let lca = adapted_self.0 >> post_common_prefix_len;
NodeIndex(lca)
}

pub(crate) fn from_starknet_storage_key(
key: &StarknetStorageKey,
tree_height: &TreeHeight,
Expand Down
40 changes: 40 additions & 0 deletions crates/committer/src/patricia_merkle_tree/types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::felt::Felt;
use crate::patricia_merkle_tree::node_data::inner_node::{EdgePath, EdgePathLength, PathToBottom};
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::types::TreeHeight;

use ethnum::U256;
use rand::Rng;
use rstest::rstest;

#[rstest]
Expand Down Expand Up @@ -54,3 +57,40 @@ fn test_cast_to_node_index(
};
assert_eq!(actual, expected_node_index.into());
}

fn get_random_u256(min_trailing_zeros: u8) -> U256 {
let msbits: U256 = rand::thread_rng().gen_range(0..u128::MAX).into();
let lsbits: u128 = rand::thread_rng().gen();
((msbits << (128 - lsbits.leading_zeros())) + lsbits) >> min_trailing_zeros
}

#[rstest]
#[case(1, 1, 1)]
#[case(2, 5, 2)]
#[case(5, 2, 2)]
#[case(8, 10, 2)]
#[case(9, 12, 1)]
fn test_get_lca(#[case] node_index: u8, #[case] other: u8, #[case] expected: u8) {
let root_index = NodeIndex(node_index.into());
let other_index = NodeIndex(other.into());
let lca = root_index.get_lca(&other_index);
let expected = NodeIndex(expected.into());
assert_eq!(lca, expected);
}

#[rstest]
fn test_get_lca_big() {
let lca = NodeIndex(get_random_u256(5));

let left_child = lca << 1;
let right_child = left_child + 1.into();
let random_extension = |index: NodeIndex| {
let extension_bits = index.leading_zeros();
let extension: u128 = rand::thread_rng().gen_range(0..(1 << extension_bits));
(index << extension_bits) + NodeIndex(U256::from(extension))
};

let left_descendant = random_extension(left_child);
let right_descendant = random_extension(right_child);
assert_eq!(left_descendant.get_lca(&right_descendant), lca);
}
Loading