diff --git a/crates/starknet_committer/src/block_committer/random_structs.rs b/crates/starknet_committer/src/block_committer/random_structs.rs index 0e296c80e23..75b4d6084af 100644 --- a/crates/starknet_committer/src/block_committer/random_structs.rs +++ b/crates/starknet_committer/src/block_committer/random_structs.rs @@ -90,9 +90,9 @@ impl RandomValue for ContractState { } } -impl RandomValue for BinaryData { +impl RandomValue for BinaryData { fn random(rng: &mut R, max: Option) -> Self { - Self { left_hash: HashOutput::random(rng, max), right_hash: HashOutput::random(rng, max) } + Self { left_data: HashOutput::random(rng, max), right_data: HashOutput::random(rng, max) } } } @@ -134,10 +134,10 @@ impl RandomValue for EdgePath { } } -impl RandomValue for EdgeData { +impl RandomValue for EdgeData { fn random(rng: &mut R, max: Option) -> Self { Self { - bottom_hash: HashOutput::random(rng, max), + bottom_data: HashOutput::random(rng, max), path_to_bottom: PathToBottom::random(rng, max), } } @@ -145,7 +145,7 @@ impl RandomValue for EdgeData { macro_rules! random_node_data { ($leaf:ty) => { - impl RandomValue for NodeData<$leaf> { + impl RandomValue for NodeData<$leaf, HashOutput> { fn random(rng: &mut R, max: Option) -> Self { match NodeDataVariants::iter() .choose(rng) diff --git a/crates/starknet_committer/src/db/facts_db/create_facts_tree.rs b/crates/starknet_committer/src/db/facts_db/create_facts_tree.rs index 340ba80387f..dd1749fd8c8 100644 --- a/crates/starknet_committer/src/db/facts_db/create_facts_tree.rs +++ b/crates/starknet_committer/src/db/facts_db/create_facts_tree.rs @@ -61,7 +61,7 @@ async fn fetch_nodes<'a, L: Leaf>( for (filled_root, subtree) in filled_roots.into_iter().zip(current_subtrees.iter()) { match filled_root.data { // Binary node. - NodeData::Binary(BinaryData { left_hash, right_hash }) => { + NodeData::::Binary(BinaryData { left_data, right_data }) => { if subtree.is_unmodified() { skeleton_tree.nodes.insert( subtree.root_index, @@ -71,7 +71,7 @@ async fn fetch_nodes<'a, L: Leaf>( } skeleton_tree.nodes.insert(subtree.root_index, OriginalSkeletonNode::Binary); let (left_subtree, right_subtree) = - subtree.get_children_subtrees(left_hash, right_hash); + subtree.get_children_subtrees(left_data, right_data); handle_subtree( skeleton_tree, @@ -87,20 +87,20 @@ async fn fetch_nodes<'a, L: Leaf>( ) } // Edge node. - NodeData::Edge(EdgeData { bottom_hash, path_to_bottom }) => { + NodeData::::Edge(EdgeData { bottom_data, path_to_bottom }) => { skeleton_tree .nodes .insert(subtree.root_index, OriginalSkeletonNode::Edge(path_to_bottom)); if subtree.is_unmodified() { skeleton_tree.nodes.insert( path_to_bottom.bottom_index(subtree.root_index), - OriginalSkeletonNode::UnmodifiedSubTree(bottom_hash), + OriginalSkeletonNode::UnmodifiedSubTree(bottom_data), ); continue; } // Parse bottom. let (bottom_subtree, previously_empty_leaves_indices) = - subtree.get_bottom_subtree(&path_to_bottom, bottom_hash); + subtree.get_bottom_subtree(&path_to_bottom, bottom_data); if let Some(ref mut leaves) = previous_leaves { leaves.extend( previously_empty_leaves_indices diff --git a/crates/starknet_committer/src/db/facts_db/traversal.rs b/crates/starknet_committer/src/db/facts_db/traversal.rs index 47eb17a37af..22b63e7fd8a 100644 --- a/crates/starknet_committer/src/db/facts_db/traversal.rs +++ b/crates/starknet_committer/src/db/facts_db/traversal.rs @@ -114,7 +114,7 @@ pub(crate) async fn fetch_patricia_paths_inner<'a, L: Leaf>( NodeData::Binary(binary_data) => { witnesses.insert(subtree.root_hash, Preimage::Binary(binary_data.clone())); let (left_subtree, right_subtree) = subtree - .get_children_subtrees(binary_data.left_hash, binary_data.right_hash); + .get_children_subtrees(binary_data.left_data, binary_data.right_data); if !left_subtree.is_unmodified() { next_subtrees.push(left_subtree); @@ -132,7 +132,7 @@ pub(crate) async fn fetch_patricia_paths_inner<'a, L: Leaf>( witnesses.insert(subtree.root_hash, Preimage::Edge(edge_data)); // Parse bottom. let (bottom_subtree, empty_leaves_indices) = subtree - .get_bottom_subtree(&edge_data.path_to_bottom, edge_data.bottom_hash); + .get_bottom_subtree(&edge_data.path_to_bottom, edge_data.bottom_data); if let Some(ref mut leaves_map) = leaves { // Insert empty leaves descendent of the current subtree, that are not // descendents of the bottom node. diff --git a/crates/starknet_committer/src/db/facts_db/traversal_test.rs b/crates/starknet_committer/src/db/facts_db/traversal_test.rs index a599d2f2d3f..4cc52973d5b 100644 --- a/crates/starknet_committer/src/db/facts_db/traversal_test.rs +++ b/crates/starknet_committer/src/db/facts_db/traversal_test.rs @@ -522,24 +522,24 @@ async fn test_fetch_patricia_paths_inner_impl( PreimageMap::from([ (HashOutput(Felt::from_hex_unchecked("0xdd6634d8228819c6b4aec64cf4e5a39a420c77b75cdf08a85f73ae2f7afcc1")), Preimage::Binary(BinaryData { - left_hash: HashOutput(Felt::from_hex_unchecked("0x111afbf8374248dc3a584bbd5f7c868f1dd76c3f17a326b5c77e692d736ece5")), - right_hash: HashOutput(Felt::from_hex_unchecked("0x20eec267afb39fcff7c97f9aa9e46ab73f61bf2e7db51c85a8f17cc313447fe")), + left_data: HashOutput(Felt::from_hex_unchecked("0x111afbf8374248dc3a584bbd5f7c868f1dd76c3f17a326b5c77e692d736ece5")), + right_data: HashOutput(Felt::from_hex_unchecked("0x20eec267afb39fcff7c97f9aa9e46ab73f61bf2e7db51c85a8f17cc313447fe")), })), (HashOutput(Felt::from_hex_unchecked("0x111afbf8374248dc3a584bbd5f7c868f1dd76c3f17a326b5c77e692d736ece5")), Preimage::Binary(BinaryData { - left_hash: HashOutput(Felt::from_hex_unchecked("0x610eec7d913ae704e188746bc82767430e39e6f096188f4671712791c563a67")), - right_hash: HashOutput(Felt::from_hex_unchecked("0x25177dfc7f358239f3b7c4c1771ddcd7eaf74a1b2b2ac952f2c2dd52f5b860d")), + left_data: HashOutput(Felt::from_hex_unchecked("0x610eec7d913ae704e188746bc82767430e39e6f096188f4671712791c563a67")), + right_data: HashOutput(Felt::from_hex_unchecked("0x25177dfc7f358239f3b7c4c1771ddcd7eaf74a1b2b2ac952f2c2dd52f5b860d")), })), (HashOutput(Felt::from_hex_unchecked("0x610eec7d913ae704e188746bc82767430e39e6f096188f4671712791c563a67")), Preimage::Edge(EdgeData { - bottom_hash: HashOutput(Felt::from_hex_unchecked("0x8")), + bottom_data: HashOutput(Felt::from_hex_unchecked("0x8")), path_to_bottom: PathToBottom::new(EdgePath(U256::from(0_u128)), EdgePathLength::new(1).unwrap()) .unwrap() })), (HashOutput(Felt::from_hex_unchecked("0x25177dfc7f358239f3b7c4c1771ddcd7eaf74a1b2b2ac952f2c2dd52f5b860d")), Preimage::Edge(EdgeData { - bottom_hash: HashOutput(Felt::from_hex_unchecked("0xb")), + bottom_data: HashOutput(Felt::from_hex_unchecked("0xb")), path_to_bottom: PathToBottom::new(EdgePath(U256::from(1_u128)), EdgePathLength::new(1).unwrap()) .unwrap() diff --git a/crates/starknet_committer/src/hash_function/hash.rs b/crates/starknet_committer/src/hash_function/hash.rs index 1144fc48d48..f672708078f 100644 --- a/crates/starknet_committer/src/hash_function/hash.rs +++ b/crates/starknet_committer/src/hash_function/hash.rs @@ -51,7 +51,7 @@ impl TreeHashFunction for TreeHashFunctionImpl { &Self::CONTRACT_STATE_HASH_VERSION, )) } - fn compute_node_hash(node_data: &NodeData) -> HashOutput { + fn compute_node_hash(node_data: &NodeData) -> HashOutput { Self::compute_node_hash_with_inner_hash_function::(node_data) } } @@ -67,7 +67,7 @@ impl TreeHashFunction for TreeHashFunctionImpl { ); HashOutput(Poseidon::hash(&contract_class_leaf_version, &compiled_class_hash.0)) } - fn compute_node_hash(node_data: &NodeData) -> HashOutput { + fn compute_node_hash(node_data: &NodeData) -> HashOutput { Self::compute_node_hash_with_inner_hash_function::(node_data) } } @@ -79,7 +79,7 @@ impl TreeHashFunction for TreeHashFunctionImpl { fn compute_leaf_hash(storage_value: &StarknetStorageValue) -> HashOutput { HashOutput(storage_value.0) } - fn compute_node_hash(node_data: &NodeData) -> HashOutput { + fn compute_node_hash(node_data: &NodeData) -> HashOutput { Self::compute_node_hash_with_inner_hash_function::(node_data) } } diff --git a/crates/starknet_committer/src/hash_function/hash_test.rs b/crates/starknet_committer/src/hash_function/hash_test.rs index 75e8b5e3fda..748736efabe 100644 --- a/crates/starknet_committer/src/hash_function/hash_test.rs +++ b/crates/starknet_committer/src/hash_function/hash_test.rs @@ -52,7 +52,7 @@ use crate::patricia_merkle_tree::types::CompiledClassHash; Felt::from_hex("0x1b20bbb35009bf03f86fb092b56a9c44deedbcca6addf8f7640f54a48ba5bbc").unwrap() )] fn test_tree_hash_function_contract_state_leaf( - #[case] node_data: NodeData, + #[case] node_data: NodeData, #[case] expected_hash: Felt, ) { let hash_output = TreeHashFunctionImpl::compute_node_hash(&node_data); @@ -65,7 +65,7 @@ fn test_tree_hash_function_contract_state_leaf( Felt::from_hex("0x49ed9a06987e7e55770d6c4d7d16b819ad984bf4aed552042847380cc31210d").unwrap() )] fn test_tree_hash_function_compiled_class_hash_leaf( - #[case] node_data: NodeData, + #[case] node_data: NodeData, #[case] expected_hash: Felt, ) { let hash_output = TreeHashFunctionImpl::compute_node_hash(&node_data); @@ -76,7 +76,7 @@ fn test_tree_hash_function_compiled_class_hash_leaf( // Expected hash value was computed independently. #[case(NodeData::Leaf(StarknetStorageValue(Felt::from_hex("0xDEAFBEEF").unwrap())), Felt::from_hex("0xDEAFBEEF").unwrap())] fn test_tree_hash_function_storage_leaf( - #[case] node_data: NodeData, + #[case] node_data: NodeData, #[case] expected_hash: Felt, ) { let hash_output = TreeHashFunctionImpl::compute_node_hash(&node_data); @@ -94,10 +94,13 @@ fn test_tree_hash_function_impl_binary_node( ) { use starknet_types_core::hash::StarkHash; - let hash_output = - TreeHashFunctionImpl::compute_node_hash(&NodeData::::Binary( - BinaryData { left_hash: HashOutput(left_hash), right_hash: HashOutput(right_hash) }, - )); + let hash_output = TreeHashFunctionImpl::compute_node_hash(&NodeData::< + StarknetStorageValue, + HashOutput, + >::Binary(BinaryData { + left_data: HashOutput(left_hash), + right_data: HashOutput(right_hash), + })); assert_eq!(hash_output, HashOutput(Pedersen::hash(&left_hash, &right_hash))); assert_eq!(hash_output, HashOutput(expected_hash)); } @@ -114,16 +117,14 @@ fn test_tree_hash_function_impl_edge_node( ) { use starknet_types_core::hash::StarkHash; - let hash_output = TreeHashFunctionImpl::compute_node_hash( - &NodeData::::Edge(EdgeData { - bottom_hash: HashOutput(bottom_hash), - path_to_bottom: PathToBottom::new( - edge_path.into(), - EdgePathLength::new(length).unwrap(), - ) + let hash_output = TreeHashFunctionImpl::compute_node_hash(&NodeData::< + StarknetStorageValue, + HashOutput, + >::Edge(EdgeData { + bottom_data: HashOutput(bottom_hash), + path_to_bottom: PathToBottom::new(edge_path.into(), EdgePathLength::new(length).unwrap()) .unwrap(), - }), - ); + })); let direct_hash_computation = HashOutput(Pedersen::hash(&bottom_hash, &edge_path.into()) + Felt::from(length)); assert_eq!(hash_output, HashOutput(expected_hash)); diff --git a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs index 731acd39e41..b540da2b09a 100644 --- a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs +++ b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs @@ -264,8 +264,8 @@ pub(crate) fn test_binary_serialize_test(binary_input: HashMap) -> // Create binary data from the left and right values. let binary_data = BinaryData { - left_hash: HashOutput(Felt::from(*left)), - right_hash: HashOutput(Felt::from(*right)), + left_data: HashOutput(Felt::from(*left)), + right_data: HashOutput(Felt::from(*right)), }; // Create a filled node (irrelevant leaf type) with binary data and zero hash. @@ -418,13 +418,13 @@ pub(crate) fn test_node_db_key() -> String { let hash = HashOutput(zero); let binary_node: FilledNode = FilledNode { - data: NodeData::Binary(BinaryData { left_hash: hash, right_hash: hash }), + data: NodeData::Binary(BinaryData { left_data: hash, right_data: hash }), hash, }; let binary_node_key = binary_node.db_key(&EmptyKeyContext).0; let edge_node: FilledNode = FilledNode { - data: NodeData::Edge(EdgeData { bottom_hash: hash, path_to_bottom: Default::default() }), + data: NodeData::Edge(EdgeData { bottom_data: hash, path_to_bottom: Default::default() }), hash, }; @@ -511,8 +511,8 @@ async fn test_storage_node(data: HashMap) -> CommitterPythonTest // Create a binary node from the parsed data. let binary_rust: FilledNode = FilledNode { data: NodeData::Binary(BinaryData { - left_hash: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "left")?)), - right_hash: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "right")?)), + left_data: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "left")?)), + right_data: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "right")?)), }), hash: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "hash")?)), }; @@ -527,7 +527,7 @@ async fn test_storage_node(data: HashMap) -> CommitterPythonTest // Create an edge node from the parsed data. let edge_rust: FilledNode = FilledNode { data: NodeData::Edge(EdgeData { - bottom_hash: HashOutput(Felt::from(*get_or_key_not_found(&edge_data, "bottom")?)), + bottom_data: HashOutput(Felt::from(*get_or_key_not_found(&edge_data, "bottom")?)), path_to_bottom: PathToBottom::new( U256::from(*get_or_key_not_found(&edge_data, "path")?).into(), EdgePathLength::new( diff --git a/crates/starknet_os/src/hints/hint_implementation/patricia/implementation.rs b/crates/starknet_os/src/hints/hint_implementation/patricia/implementation.rs index 84603c57a3d..760faf7e620 100644 --- a/crates/starknet_os/src/hints/hint_implementation/patricia/implementation.rs +++ b/crates/starknet_os/src/hints/hint_implementation/patricia/implementation.rs @@ -214,7 +214,7 @@ pub(crate) fn prepare_preimage_validation_non_deterministic_hashes( let commitment_facts = &hint_processor.get_commitment_info()?.commitment_facts; let preimage = Preimage::try_from(commitment_facts.get(&node).ok_or(OsHintError::MissingPreimage(node))?)?; - let Preimage::Edge(EdgeData { bottom_hash, path_to_bottom }) = preimage else { + let Preimage::Edge(EdgeData { bottom_data: bottom_hash, path_to_bottom }) = preimage else { // We expect an edge node. return Err(OsHintError::AssertionFailed { message: format!("An edge node is expected, found {preimage:?}"), @@ -459,7 +459,7 @@ pub(crate) fn load_bottom( let hash_ptr_address = get_ptr_from_var_name(Ids::HashPtr.into(), vm, ids_data, ap_tracking)?; let nested_fields_and_values = - [("x", binary_data.left_hash.0.into()), ("y", binary_data.right_hash.0.into())]; + [("x", binary_data.left_data.0.into()), ("y", binary_data.right_data.0.into())]; insert_values_to_fields( hash_ptr_address, hint_processor.commitment_type.hash_builtin_struct(), diff --git a/crates/starknet_os/src/hints/hint_implementation/patricia/utils.rs b/crates/starknet_os/src/hints/hint_implementation/patricia/utils.rs index a7d69adef9d..ac267c0e00c 100644 --- a/crates/starknet_os/src/hints/hint_implementation/patricia/utils.rs +++ b/crates/starknet_os/src/hints/hint_implementation/patricia/utils.rs @@ -106,7 +106,7 @@ type TreeLayer = HashMap; #[derive(Clone, Debug, Eq, PartialEq)] pub enum CanonicNode { BinaryOrLeaf(HashOutput), - Edge(EdgeData), + Edge(EdgeData), Empty, } @@ -252,19 +252,19 @@ fn get_children( let binary = preimage.get_binary()?; Ok(( - CanonicNode::new(preimage_map, &binary.left_hash), - CanonicNode::new(preimage_map, &binary.right_hash), + CanonicNode::new(preimage_map, &binary.left_data), + CanonicNode::new(preimage_map, &binary.right_data), )) } CanonicNode::Edge(edge) => { - let hash = edge.bottom_hash; + let hash = edge.bottom_data; let path_to_bottom = edge.path_to_bottom; let child = if u8::from(path_to_bottom.length) == 1 { CanonicNode::BinaryOrLeaf(hash) } else { let new_path = path_to_bottom.remove_first_edges(EdgePathLength::new(1)?)?; - CanonicNode::Edge(EdgeData { bottom_hash: hash, path_to_bottom: new_path }) + CanonicNode::Edge(EdgeData { bottom_data: hash, path_to_bottom: new_path }) }; if path_to_bottom.is_left_descendant() { diff --git a/crates/starknet_os/src/hints/hint_implementation/patricia/utils_test.rs b/crates/starknet_os/src/hints/hint_implementation/patricia/utils_test.rs index d52db48fa6d..bebc6ab02a4 100644 --- a/crates/starknet_os/src/hints/hint_implementation/patricia/utils_test.rs +++ b/crates/starknet_os/src/hints/hint_implementation/patricia/utils_test.rs @@ -46,7 +46,7 @@ fn build_full_preimage_map(height: SubTreeHeight, root: HashOutput) -> PreimageM let left = HashOutput(root.0 * Felt::TWO); let right = HashOutput(left.0 + Felt::ONE); - preimage_map.insert(root, Preimage::Binary(BinaryData { left_hash: left, right_hash: right })); + preimage_map.insert(root, Preimage::Binary(BinaryData { left_data: left, right_data: right })); // We can stop at height 1, the leaf nodes are not relevant. if height.0 > 1 { @@ -78,13 +78,13 @@ fn build_preimage_map_with_edge_node(height: SubTreeHeight, root: HashOutput) -> let next_height = SubTreeHeight(height.0 - 1); let bottom_hash = HashOutput(Felt::from(u128::try_from(right.0).unwrap() << next_height.0)); - preimage_map.insert(root, Preimage::Binary(BinaryData { left_hash: left, right_hash: right })); + preimage_map.insert(root, Preimage::Binary(BinaryData { left_data: left, right_data: right })); preimage_map.extend(build_full_preimage_map(next_height, left)); preimage_map.insert( right, Preimage::Edge(EdgeData { - bottom_hash, + bottom_data: bottom_hash, path_to_bottom: PathToBottom::new( EdgePath::new_u128(0), EdgePathLength::new(next_height.0).unwrap(), @@ -190,7 +190,7 @@ fn test_new_canonic_node() { // Edge. let node_3 = CanonicNode::new(&preimage_map, &HashOutput(Felt::THREE)); let edge_data_3 = EdgeData { - bottom_hash: HashOutput(Felt::from(12)), + bottom_data: HashOutput(Felt::from(12)), path_to_bottom: PathToBottom::new(EdgePath::new_u128(0), EdgePathLength::new(2).unwrap()) .unwrap(), }; @@ -217,7 +217,7 @@ fn test_get_children() { let node_2 = CanonicNode::new(&preimage_map, &HashOutput(Felt::TWO)); let node_3 = CanonicNode::new(&preimage_map, &HashOutput(Felt::THREE)); let node_6 = CanonicNode::Edge(EdgeData { - bottom_hash: HashOutput(Felt::from(12)), + bottom_data: HashOutput(Felt::from(12)), path_to_bottom: PathToBottom::new(EdgePath::new_u128(0), EdgePathLength::new(1).unwrap()) .unwrap(), }); @@ -362,9 +362,9 @@ fn test_guess_descents_update_one_leaf() { let mut preimage_map = PreimageMap::new(); preimage_map - .insert(prev_root, Preimage::Edge(EdgeData { bottom_hash: prev_leaf, path_to_bottom })); + .insert(prev_root, Preimage::Edge(EdgeData { bottom_data: prev_leaf, path_to_bottom })); preimage_map - .insert(new_root, Preimage::Edge(EdgeData { bottom_hash: new_leaf, path_to_bottom })); + .insert(new_root, Preimage::Edge(EdgeData { bottom_data: new_leaf, path_to_bottom })); let descent_map = patricia_guess_descents(height, &update_tree, &preimage_map, prev_root, new_root).unwrap(); @@ -417,22 +417,22 @@ fn test_guess_descents_update_two_adjacent_leaves() { let mut preimage_map = PreimageMap::new(); preimage_map.insert( prev_root, - Preimage::Edge(EdgeData { bottom_hash: prev_inner_node, path_to_bottom }), + Preimage::Edge(EdgeData { bottom_data: prev_inner_node, path_to_bottom }), ); preimage_map - .insert(new_root, Preimage::Edge(EdgeData { bottom_hash: new_inner_node, path_to_bottom })); + .insert(new_root, Preimage::Edge(EdgeData { bottom_data: new_inner_node, path_to_bottom })); preimage_map.insert( prev_inner_node, Preimage::Binary(BinaryData { - left_hash: HashOutput(Felt::from(8)), - right_hash: HashOutput(Felt::from(9)), + left_data: HashOutput(Felt::from(8)), + right_data: HashOutput(Felt::from(9)), }), ); preimage_map.insert( new_inner_node, Preimage::Binary(BinaryData { - left_hash: HashOutput(Felt::from(128)), - right_hash: HashOutput(Felt::from(129)), + left_data: HashOutput(Felt::from(128)), + right_data: HashOutput(Felt::from(129)), }), ); @@ -499,42 +499,42 @@ fn test_guess_descents_update_two_leaves() { preimage_map.insert( prev_root, Preimage::Binary(BinaryData { - left_hash: prev_left_inner_node, - right_hash: prev_right_inner_node, + left_data: prev_left_inner_node, + right_data: prev_right_inner_node, }), ); preimage_map.insert( new_root, Preimage::Binary(BinaryData { - left_hash: new_left_inner_node, - right_hash: new_right_inner_node, + left_data: new_left_inner_node, + right_data: new_right_inner_node, }), ); preimage_map.insert( prev_left_inner_node, Preimage::Edge(EdgeData { - bottom_hash: prev_left_leaf, + bottom_data: prev_left_leaf, path_to_bottom: left_path_to_bottom, }), ); preimage_map.insert( new_left_inner_node, Preimage::Edge(EdgeData { - bottom_hash: new_left_leaf, + bottom_data: new_left_leaf, path_to_bottom: left_path_to_bottom, }), ); preimage_map.insert( prev_right_inner_node, Preimage::Edge(EdgeData { - bottom_hash: prev_right_leaf, + bottom_data: prev_right_leaf, path_to_bottom: right_path_to_bottom, }), ); preimage_map.insert( new_right_inner_node, Preimage::Edge(EdgeData { - bottom_hash: new_right_leaf, + bottom_data: new_right_leaf, path_to_bottom: right_path_to_bottom, }), ); @@ -609,11 +609,11 @@ fn test_guess_descents_change_leaf() { let mut preimage_map = PreimageMap::new(); preimage_map.insert( prev_root, - Preimage::Edge(EdgeData { bottom_hash: prev_leaf, path_to_bottom: prev_path }), + Preimage::Edge(EdgeData { bottom_data: prev_leaf, path_to_bottom: prev_path }), ); preimage_map.insert( new_root, - Preimage::Edge(EdgeData { bottom_hash: new_leaf, path_to_bottom: new_path }), + Preimage::Edge(EdgeData { bottom_data: new_leaf, path_to_bottom: new_path }), ); let descent_map = @@ -671,15 +671,15 @@ fn test_guess_descents_split_edge_node() { let mut preimage_map = PreimageMap::new(); preimage_map.insert( prev_root, - Preimage::Edge(EdgeData { bottom_hash: left_leaf, path_to_bottom: prev_path }), + Preimage::Edge(EdgeData { bottom_data: left_leaf, path_to_bottom: prev_path }), ); preimage_map.insert( new_root, - Preimage::Edge(EdgeData { bottom_hash: new_inner_node, path_to_bottom: new_path }), + Preimage::Edge(EdgeData { bottom_data: new_inner_node, path_to_bottom: new_path }), ); preimage_map.insert( new_inner_node, - Preimage::Binary(BinaryData { left_hash: left_leaf, right_hash: right_leaf }), + Preimage::Binary(BinaryData { left_data: left_leaf, right_data: right_leaf }), ); let descent_map = diff --git a/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node.rs b/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node.rs index ba96813491f..76edf021c9a 100644 --- a/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node.rs +++ b/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node.rs @@ -7,5 +7,5 @@ use crate::patricia_merkle_tree::node_data::leaf::Leaf; /// A node in a Patricia-Merkle tree, complete with its hash and data. pub struct FilledNode { pub hash: HashOutput, - pub data: NodeData, + pub data: NodeData, } diff --git a/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node_serde.rs b/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node_serde.rs index 3fb81a52baa..62a600b173f 100644 --- a/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node_serde.rs +++ b/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node_serde.rs @@ -82,7 +82,7 @@ impl DBObject for FilledNode { /// - For leaf nodes: use leaf.serialize() method. fn serialize(&self) -> DbValue { match &self.data { - NodeData::Binary(BinaryData { left_hash, right_hash }) => { + NodeData::Binary(BinaryData { left_data: left_hash, right_data: right_hash }) => { // Serialize left and right hashes to byte arrays. let left: [u8; SERIALIZE_HASH_BYTES] = left_hash.0.to_bytes_be(); let right: [u8; SERIALIZE_HASH_BYTES] = right_hash.0.to_bytes_be(); @@ -92,7 +92,7 @@ impl DBObject for FilledNode { DbValue(serialized) } - NodeData::Edge(EdgeData { bottom_hash, path_to_bottom }) => { + NodeData::Edge(EdgeData { bottom_data: bottom_hash, path_to_bottom }) => { // Serialize bottom hash, path, and path length to byte arrays. let bottom: [u8; SERIALIZE_HASH_BYTES] = bottom_hash.0.to_bytes_be(); let path: [u8; SERIALIZE_HASH_BYTES] = @@ -123,10 +123,10 @@ impl DBObject for FilledNode { Ok(Self { hash: deserialize_context.node_hash, data: NodeData::Binary(BinaryData { - left_hash: HashOutput(Felt::from_bytes_be_slice( + left_data: HashOutput(Felt::from_bytes_be_slice( &value.0[..SERIALIZE_HASH_BYTES], )), - right_hash: HashOutput(Felt::from_bytes_be_slice( + right_data: HashOutput(Felt::from_bytes_be_slice( &value.0[SERIALIZE_HASH_BYTES..], )), }), @@ -143,7 +143,7 @@ impl DBObject for FilledNode { Ok(Self { hash: deserialize_context.node_hash, data: NodeData::Edge(EdgeData { - bottom_hash: HashOutput(Felt::from_bytes_be_slice( + bottom_data: HashOutput(Felt::from_bytes_be_slice( &value.0[..SERIALIZE_HASH_BYTES], )), path_to_bottom: PathToBottom::new( diff --git a/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree.rs b/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree.rs index 77fb45b3815..2a6d4c89a07 100644 --- a/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree.rs +++ b/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree.rs @@ -211,8 +211,8 @@ impl FilledTreeImpl { ); let data = NodeData::Binary(BinaryData { - left_hash: left_hash.await??, - right_hash: right_hash.await??, + left_data: left_hash.await??, + right_data: right_hash.await??, }); let hash = TH::compute_node_hash(&data); @@ -234,8 +234,10 @@ impl FilledTreeImpl { Arc::clone(&leaf_index_to_leaf_output), ) .await?; - let data = - NodeData::Edge(EdgeData { path_to_bottom: *path_to_bottom, bottom_hash }); + let data = NodeData::Edge(EdgeData { + path_to_bottom: *path_to_bottom, + bottom_data: bottom_hash, + }); let hash = TH::compute_node_hash(&data); Self::write_to_output_map( filled_tree_output_map, diff --git a/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree_test.rs b/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree_test.rs index 17e6ff26850..975179c65f7 100644 --- a/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree_test.rs +++ b/crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree_test.rs @@ -372,8 +372,8 @@ fn create_mock_binary_entry_for_testing( FilledNode { hash: HashOutput(Felt::from_hex(hash).unwrap()), data: NodeData::Binary(BinaryData { - left_hash: HashOutput(Felt::from_hex(left_hash).unwrap()), - right_hash: HashOutput(Felt::from_hex(right_hash).unwrap()), + left_data: HashOutput(Felt::from_hex(left_hash).unwrap()), + right_data: HashOutput(Felt::from_hex(right_hash).unwrap()), }), }, ) @@ -391,7 +391,7 @@ fn create_mock_edge_entry_for_testing( FilledNode { hash: HashOutput(Felt::from_hex(hash).unwrap()), data: NodeData::Edge(EdgeData { - bottom_hash: HashOutput(Felt::from_hex(bottom_hash).unwrap()), + bottom_data: HashOutput(Felt::from_hex(bottom_hash).unwrap()), path_to_bottom: PathToBottom::new( path.into(), EdgePathLength::new(length).unwrap(), diff --git a/crates/starknet_patricia/src/patricia_merkle_tree/internal_test_utils.rs b/crates/starknet_patricia/src/patricia_merkle_tree/internal_test_utils.rs index 291aa5f820d..8b4aef9e6fc 100644 --- a/crates/starknet_patricia/src/patricia_merkle_tree/internal_test_utils.rs +++ b/crates/starknet_patricia/src/patricia_merkle_tree/internal_test_utils.rs @@ -24,7 +24,7 @@ impl TreeHashFunction for TestTreeHashFunction { HashOutput(leaf_data.0) } - fn compute_node_hash(node_data: &NodeData) -> HashOutput { + fn compute_node_hash(node_data: &NodeData) -> HashOutput { Self::compute_node_hash_with_inner_hash_function::(node_data) } } diff --git a/crates/starknet_patricia/src/patricia_merkle_tree/node_data/inner_node.rs b/crates/starknet_patricia/src/patricia_merkle_tree/node_data/inner_node.rs index 703686f3b36..48587e41f9a 100644 --- a/crates/starknet_patricia/src/patricia_merkle_tree/node_data/inner_node.rs +++ b/crates/starknet_patricia/src/patricia_merkle_tree/node_data/inner_node.rs @@ -19,22 +19,22 @@ pub mod inner_node_test; #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(any(test, feature = "testing"), derive(strum_macros::EnumDiscriminants))] #[cfg_attr(any(test, feature = "testing"), strum_discriminants(derive(strum_macros::EnumIter)))] -// A Patricia-Merkle tree node's data, i.e., the pre-image of its hash. -pub enum NodeData { - Binary(BinaryData), - Edge(EdgeData), +// A Patricia-Merkle tree node's data. +pub enum NodeData { + Binary(BinaryData), + Edge(EdgeData), Leaf(L), } #[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct BinaryData { - pub left_hash: HashOutput, - pub right_hash: HashOutput, +pub struct BinaryData { + pub left_data: ChildData, + pub right_data: ChildData, } -impl BinaryData { +impl BinaryData { pub fn flatten(&self) -> Vec { - vec![self.left_hash.0, self.right_hash.0] + vec![self.left_data.0, self.right_data.0] } } @@ -141,17 +141,17 @@ impl PathToBottom { } #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub struct EdgeData { - pub bottom_hash: HashOutput, +pub struct EdgeData { + pub bottom_data: ChildData, pub path_to_bottom: PathToBottom, } -impl EdgeData { +impl EdgeData { pub fn flatten(&self) -> Vec { vec![ self.path_to_bottom.length.into(), (&self.path_to_bottom.path).into(), - self.bottom_hash.0, + self.bottom_data.0, ] } } @@ -200,10 +200,12 @@ impl PathToBottom { } } +// TODO(Ariel 10/12/2025): Move Preimage to the fact_db module in starknet_committer (add a flatten +// trait to be implemented in starknet_committer for BinaryData and EdgeData). #[derive(Clone, Debug, PartialEq)] pub enum Preimage { - Binary(BinaryData), - Edge(EdgeData), + Binary(BinaryData), + Edge(EdgeData), } pub type PreimageMap = HashMap; @@ -223,7 +225,7 @@ impl Preimage { } } - pub fn get_binary(&self) -> Result<&BinaryData, PreimageError> { + pub fn get_binary(&self) -> Result<&BinaryData, PreimageError> { match self { Self::Binary(binary) => Ok(binary), Self::Edge(_) => Err(PreimageError::ExpectedBinary(self.clone())), @@ -244,12 +246,12 @@ impl TryFrom<&Vec> for Preimage { fn try_from(raw_preimage: &Vec) -> Result { match raw_preimage.as_slice() { [left, right] => Ok(Preimage::Binary(BinaryData { - left_hash: HashOutput(*left), - right_hash: HashOutput(*right), + left_data: HashOutput(*left), + right_data: HashOutput(*right), })), [length, path, bottom] => { Ok(Preimage::Edge(EdgeData { - bottom_hash: HashOutput(*bottom), + bottom_data: HashOutput(*bottom), path_to_bottom: PathToBottom::new( (*path).into(), EdgePathLength::new((*length).try_into().map_err(|_| { diff --git a/crates/starknet_patricia/src/patricia_merkle_tree/updated_skeleton_tree/hash_function.rs b/crates/starknet_patricia/src/patricia_merkle_tree/updated_skeleton_tree/hash_function.rs index 12a67b0ff78..1d1897d04fc 100644 --- a/crates/starknet_patricia/src/patricia_merkle_tree/updated_skeleton_tree/hash_function.rs +++ b/crates/starknet_patricia/src/patricia_merkle_tree/updated_skeleton_tree/hash_function.rs @@ -20,19 +20,22 @@ pub trait TreeHashFunction { fn compute_leaf_hash(leaf_data: &L) -> HashOutput; /// Computes the hash for the given node data. - fn compute_node_hash(node_data: &NodeData) -> HashOutput; + fn compute_node_hash(node_data: &NodeData) -> HashOutput; /// The default implementation for internal nodes is based on the following reference: /// + /// This code is layout independent. After computing the children hashes, NodeData can be + /// instantiated with the concrete HashOutput for ChildData, regardless of what's stored in the + /// db. fn compute_node_hash_with_inner_hash_function( - node_data: &NodeData, + node_data: &NodeData, ) -> HashOutput { match node_data { - NodeData::Binary(BinaryData { left_hash, right_hash }) => { + NodeData::Binary(BinaryData { left_data: left_hash, right_data: right_hash }) => { H::hash(&left_hash.0, &right_hash.0) } NodeData::Edge(EdgeData { - bottom_hash: hash_output, + bottom_data: hash_output, path_to_bottom: PathToBottom { path, length, .. }, }) => HashOutput(H::hash(&hash_output.0, &Felt::from(path)).0 + Felt::from(*length)), NodeData::Leaf(leaf_data) => Self::compute_leaf_hash(leaf_data),