diff --git a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/utils.rs b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/utils.rs index ed25c0cf..217ef211 100644 --- a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/utils.rs +++ b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/utils.rs @@ -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..]] diff --git a/crates/committer/src/patricia_merkle_tree/types.rs b/crates/committer/src/patricia_merkle_tree/types.rs index b722fb2b..2e37670b 100644 --- a/crates/committer/src/patricia_merkle_tree/types.rs +++ b/crates/committer/src/patricia_merkle_tree/types.rs @@ -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. @@ -170,6 +170,14 @@ impl std::ops::Mul for NodeIndex { } } +impl std::ops::Add for NodeIndex { + type Output = Self; + + fn add(self, rhs: u128) -> Self { + self + Self::from(rhs) + } +} + impl std::ops::Shl for NodeIndex { type Output = Self; diff --git a/crates/committer/src/patricia_merkle_tree/types_test.rs b/crates/committer/src/patricia_merkle_tree/types_test.rs index 235ddaa1..98ce688a 100644 --- a/crates/committer/src/patricia_merkle_tree/types_test.rs +++ b/crates/committer/src/patricia_merkle_tree/types_test.rs @@ -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}; @@ -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 { @@ -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)); diff --git a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/compute_updated_skeleton_tree_test.rs b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/compute_updated_skeleton_tree_test.rs index 7fa91dda..0700c446 100644 --- a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/compute_updated_skeleton_tree_test.rs +++ b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/compute_updated_skeleton_tree_test.rs @@ -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)], @@ -352,7 +352,7 @@ 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)], @@ -360,110 +360,110 @@ fn test_update_node_in_empty_tree( &[], )] #[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,