Skip to content
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

chore: support u128 addition with NodeIndex #203

Merged
merged 1 commit into from
Jun 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub(crate) fn split_leaves<'a>(
);
}

let right_child_index = (*root_index << 1) + 1.into();
let right_child_index = (*root_index << 1) + 1;
let leftmost_index_in_right_subtree = right_child_index << (u8::from(root_height) - 1);
let leaves_split = bisect_left(leaf_indices, &leftmost_index_in_right_subtree);
[&leaf_indices[..leaves_split], &leaf_indices[leaves_split..]]
Expand Down
10 changes: 9 additions & 1 deletion crates/committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl NodeIndex {

pub(crate) fn get_children_indices(&self) -> [Self; 2] {
let left_child = *self << 1;
[left_child, left_child + 1.into()]
[left_child, left_child + 1]
}

/// Returns the number of leading zeroes when represented with Self::BITS bits.
Expand Down Expand Up @@ -170,6 +170,14 @@ impl std::ops::Mul for NodeIndex {
}
}

impl std::ops::Add<u128> for NodeIndex {
type Output = Self;

fn add(self, rhs: u128) -> Self {
self + Self::from(rhs)
}
}

impl std::ops::Shl<u8> for NodeIndex {
type Output = Self;

Expand Down
5 changes: 3 additions & 2 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,7 @@ use crate::felt::Felt;
use crate::patricia_merkle_tree::node_data::inner_node::{EdgePathLength, PathToBottom};
use crate::patricia_merkle_tree::test_utils::{get_random_u256, random};
use crate::patricia_merkle_tree::types::NodeIndex;

use rand::rngs::ThreadRng;

use ethnum::{uint, U256};
Expand Down Expand Up @@ -33,7 +34,7 @@ fn test_cast_to_node_index(
#[values(0, 15, 0xDEADBEEF)] leaf_index: u128,
#[values(true, false)] from_contract_address: bool,
) {
let expected_node_index = NodeIndex::FIRST_LEAF + leaf_index.into();
let expected_node_index = NodeIndex::FIRST_LEAF + leaf_index;
let actual = if from_contract_address {
NodeIndex::from_contract_address(&ContractAddress(Felt::from(leaf_index)))
} else {
Expand Down Expand Up @@ -82,7 +83,7 @@ fn test_get_lca_big(mut random: ThreadRng) {
));

let left_child = lca << 1;
let right_child = left_child + 1.into();
let right_child = left_child + 1;
let mut random_extension = |index: NodeIndex| {
let extension_bits = index.leading_zeros();
let extension: u128 = random.gen_range(0..(1 << extension_bits));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ fn test_update_node_in_empty_tree(
#[case::modified_leaf(
&NodeIndex::FIRST_LEAF,
vec![
(NodeIndex::FIRST_LEAF+1.into(),
(NodeIndex::FIRST_LEAF + 1,
OriginalSkeletonNode::UnmodifiedSubTree(HashOutput(Felt::ONE)))
],
&[(U256::from(NodeIndex::FIRST_LEAF), 1)],
Expand All @@ -352,118 +352,118 @@ fn test_update_node_in_empty_tree(
#[case::deleted_leaf(
&NodeIndex::FIRST_LEAF,
vec![
(NodeIndex::FIRST_LEAF+1.into(),
(NodeIndex::FIRST_LEAF + 1,
OriginalSkeletonNode::UnmodifiedSubTree(HashOutput(Felt::ONE)))
],
&[(U256::from(NodeIndex::FIRST_LEAF), 0)],
TempSkeletonNode::Empty,
&[],
)]
#[case::orig_binary_with_modified_leaf(
&(NodeIndex::FIRST_LEAF>>1),
&(NodeIndex::FIRST_LEAF >> 1),
vec![
(NodeIndex::FIRST_LEAF+1.into(),
(NodeIndex::FIRST_LEAF + 1,
OriginalSkeletonNode::UnmodifiedSubTree(HashOutput(Felt::ONE))),
(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Binary)
(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Binary)
],
&[(U256::from(NodeIndex::FIRST_LEAF), 1)],
TempSkeletonNode::Original(OriginalSkeletonNode::Binary),
&[],
)]
#[case::orig_binary_with_deleted_leaf(
&(NodeIndex::FIRST_LEAF>>1),
&(NodeIndex::FIRST_LEAF >> 1),
vec![
(NodeIndex::FIRST_LEAF+1.into(),
(NodeIndex::FIRST_LEAF + 1,
OriginalSkeletonNode::UnmodifiedSubTree(HashOutput(Felt::ONE))),
(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Binary)
(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Binary)
],
&[(U256::from(NodeIndex::FIRST_LEAF), 0)],
TempSkeletonNode::Original(OriginalSkeletonNode::Edge(PathToBottom::RIGHT_CHILD)),
&[],
)]
#[case::orig_binary_with_deleted_leaves(
&(NodeIndex::FIRST_LEAF>>1),
vec![(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Binary)],
&[(U256::from(NodeIndex::FIRST_LEAF), 0), (U256::from(NodeIndex::FIRST_LEAF + 1.into()), 0)],
&(NodeIndex::FIRST_LEAF >> 1),
vec![(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Binary)],
&[(U256::from(NodeIndex::FIRST_LEAF), 0), (U256::from(NodeIndex::FIRST_LEAF + 1), 0)],
TempSkeletonNode::Empty,
&[],
)]
#[case::orig_binary_with_binary_modified_children(
&(NodeIndex::FIRST_LEAF>>2),
&(NodeIndex::FIRST_LEAF >> 2),
vec![
(NodeIndex::FIRST_LEAF>>2, OriginalSkeletonNode::Binary),
(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Binary),
((NodeIndex::FIRST_LEAF>>1) + 1.into(),OriginalSkeletonNode::Binary)
(NodeIndex::FIRST_LEAF >> 2, OriginalSkeletonNode::Binary),
(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Binary),
((NodeIndex::FIRST_LEAF >> 1) + 1,OriginalSkeletonNode::Binary)
],
&[
(U256::from(NodeIndex::FIRST_LEAF), 1),
(U256::from(NodeIndex::FIRST_LEAF + 1.into()), 1),
(U256::from(NodeIndex::FIRST_LEAF + 2.into()), 1),
(U256::from(NodeIndex::FIRST_LEAF + 3.into()), 1)
(U256::from(NodeIndex::FIRST_LEAF + 1), 1),
(U256::from(NodeIndex::FIRST_LEAF + 2), 1),
(U256::from(NodeIndex::FIRST_LEAF + 3), 1)
],
TempSkeletonNode::Original(OriginalSkeletonNode::Binary),
&[
(NodeIndex::FIRST_LEAF>>1, UpdatedSkeletonNode::Binary),
((NodeIndex::FIRST_LEAF>>1) + 1.into(),UpdatedSkeletonNode::Binary)
(NodeIndex::FIRST_LEAF >> 1, UpdatedSkeletonNode::Binary),
((NodeIndex::FIRST_LEAF >> 1) + 1, UpdatedSkeletonNode::Binary)
],
)]
// The following cases test the `update_edge_node` function as well.
#[case::orig_edge_with_deleted_bottom(
&(NodeIndex::FIRST_LEAF>>1),
&(NodeIndex::FIRST_LEAF >> 1),
vec![
(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
],
&[(U256::from(NodeIndex::FIRST_LEAF), 0)],
TempSkeletonNode::Empty,
&[],
)]
#[case::orig_edge_with_modified_bottom(
&(NodeIndex::FIRST_LEAF>>1),
&(NodeIndex::FIRST_LEAF >> 1),
vec![
(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
],
&[(U256::from(NodeIndex::FIRST_LEAF), 1)],
TempSkeletonNode::Original(OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
&[],
)]
#[case::orig_edge_with_two_modified_leaves(
&(NodeIndex::FIRST_LEAF>>1),
vec![(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD))],
&[(U256::from(NodeIndex::FIRST_LEAF), 1), (U256::from(NodeIndex::FIRST_LEAF+1.into()), 1)],
&(NodeIndex::FIRST_LEAF >> 1),
vec![(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD))],
&[(U256::from(NodeIndex::FIRST_LEAF), 1), (U256::from(NodeIndex::FIRST_LEAF + 1), 1)],
TempSkeletonNode::Original(OriginalSkeletonNode::Binary),
&[
(NodeIndex::FIRST_LEAF, UpdatedSkeletonNode::Leaf),
(NodeIndex::FIRST_LEAF+1.into(), UpdatedSkeletonNode::Leaf)
(NodeIndex::FIRST_LEAF + 1, UpdatedSkeletonNode::Leaf)
],
)]
#[case::orig_edge_with_unmodified_bottom_and_added_leaf(
&(NodeIndex::FIRST_LEAF>>1),
&(NodeIndex::FIRST_LEAF >> 1),
vec![
(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
(NodeIndex::FIRST_LEAF, OriginalSkeletonNode::UnmodifiedSubTree(HashOutput(Felt::ONE)))
],
&[(U256::from(NodeIndex::FIRST_LEAF+1.into()), 1)],
&[(U256::from(NodeIndex::FIRST_LEAF + 1), 1)],
TempSkeletonNode::Original(OriginalSkeletonNode::Binary),
&[],
)]
#[case::orig_edge_with_deleted_bottom_and_added_leaf(
&(NodeIndex::FIRST_LEAF>>1),
&(NodeIndex::FIRST_LEAF >> 1),
vec![
(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
],
&[(U256::from(NodeIndex::FIRST_LEAF), 0), (U256::from(NodeIndex::FIRST_LEAF+1.into()), 1)],
&[(U256::from(NodeIndex::FIRST_LEAF), 0), (U256::from(NodeIndex::FIRST_LEAF + 1), 1)],
TempSkeletonNode::Original(OriginalSkeletonNode::Edge(PathToBottom::RIGHT_CHILD)),
&[],
)]
#[case::orig_edge_with_modified_leaves_beneath_bottom(
&(NodeIndex::FIRST_LEAF>>2),
&(NodeIndex::FIRST_LEAF >> 2),
vec![
(NodeIndex::FIRST_LEAF>>2, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
(NodeIndex::FIRST_LEAF>>1, OriginalSkeletonNode::Binary),
(NodeIndex::FIRST_LEAF >> 2, OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
(NodeIndex::FIRST_LEAF >> 1, OriginalSkeletonNode::Binary),
],
&[(U256::from(NodeIndex::FIRST_LEAF), 1), (U256::from(NodeIndex::FIRST_LEAF+1.into()), 1)],
&[(U256::from(NodeIndex::FIRST_LEAF), 1), (U256::from(NodeIndex::FIRST_LEAF + 1), 1)],
TempSkeletonNode::Original(OriginalSkeletonNode::Edge(PathToBottom::LEFT_CHILD)),
&[(NodeIndex::FIRST_LEAF>>1, UpdatedSkeletonNode::Binary)],
&[(NodeIndex::FIRST_LEAF >> 1, UpdatedSkeletonNode::Binary)],
)]
fn test_update_node_in_nonempty_tree(
#[case] root_index: &NodeIndex,
Expand Down
Loading