Skip to content
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
10 changes: 5 additions & 5 deletions crates/starknet_committer/src/block_committer/random_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ impl RandomValue for ContractState {
}
}

impl RandomValue for BinaryData {
impl RandomValue for BinaryData<HashOutput> {
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> 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) }
}
}

Expand Down Expand Up @@ -134,18 +134,18 @@ impl RandomValue for EdgePath {
}
}

impl RandomValue for EdgeData {
impl RandomValue for EdgeData<HashOutput> {
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
Self {
bottom_hash: HashOutput::random(rng, max),
bottom_data: HashOutput::random(rng, max),
path_to_bottom: PathToBottom::random(rng, max),
}
}
}

macro_rules! random_node_data {
($leaf:ty) => {
impl RandomValue for NodeData<$leaf> {
impl RandomValue for NodeData<$leaf, HashOutput> {
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
match NodeDataVariants::iter()
.choose(rng)
Expand Down
10 changes: 5 additions & 5 deletions crates/starknet_committer/src/db/facts_db/create_facts_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<L, HashOutput>::Binary(BinaryData { left_data, right_data }) => {
if subtree.is_unmodified() {
skeleton_tree.nodes.insert(
subtree.root_index,
Expand All @@ -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,
Expand All @@ -87,20 +87,20 @@ async fn fetch_nodes<'a, L: Leaf>(
)
}
// Edge node.
NodeData::Edge(EdgeData { bottom_hash, path_to_bottom }) => {
NodeData::<L, HashOutput>::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
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_committer/src/db/facts_db/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions crates/starknet_committer/src/db/facts_db/traversal_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions crates/starknet_committer/src/hash_function/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl TreeHashFunction<ContractState> for TreeHashFunctionImpl {
&Self::CONTRACT_STATE_HASH_VERSION,
))
}
fn compute_node_hash(node_data: &NodeData<ContractState>) -> HashOutput {
fn compute_node_hash(node_data: &NodeData<ContractState, HashOutput>) -> HashOutput {
Self::compute_node_hash_with_inner_hash_function::<PedersenHashFunction>(node_data)
}
}
Expand All @@ -67,7 +67,7 @@ impl TreeHashFunction<CompiledClassHash> for TreeHashFunctionImpl {
);
HashOutput(Poseidon::hash(&contract_class_leaf_version, &compiled_class_hash.0))
}
fn compute_node_hash(node_data: &NodeData<CompiledClassHash>) -> HashOutput {
fn compute_node_hash(node_data: &NodeData<CompiledClassHash, HashOutput>) -> HashOutput {
Self::compute_node_hash_with_inner_hash_function::<PoseidonHashFunction>(node_data)
}
}
Expand All @@ -79,7 +79,7 @@ impl TreeHashFunction<StarknetStorageValue> for TreeHashFunctionImpl {
fn compute_leaf_hash(storage_value: &StarknetStorageValue) -> HashOutput {
HashOutput(storage_value.0)
}
fn compute_node_hash(node_data: &NodeData<StarknetStorageValue>) -> HashOutput {
fn compute_node_hash(node_data: &NodeData<StarknetStorageValue, HashOutput>) -> HashOutput {
Self::compute_node_hash_with_inner_hash_function::<PedersenHashFunction>(node_data)
}
}
Expand Down
33 changes: 17 additions & 16 deletions crates/starknet_committer/src/hash_function/hash_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContractState>,
#[case] node_data: NodeData<ContractState, HashOutput>,
#[case] expected_hash: Felt,
) {
let hash_output = TreeHashFunctionImpl::compute_node_hash(&node_data);
Expand All @@ -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<CompiledClassHash>,
#[case] node_data: NodeData<CompiledClassHash, HashOutput>,
#[case] expected_hash: Felt,
) {
let hash_output = TreeHashFunctionImpl::compute_node_hash(&node_data);
Expand All @@ -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<StarknetStorageValue>,
#[case] node_data: NodeData<StarknetStorageValue, HashOutput>,
#[case] expected_hash: Felt,
) {
let hash_output = TreeHashFunctionImpl::compute_node_hash(&node_data);
Expand All @@ -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::<StarknetStorageValue>::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));
}
Expand All @@ -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::<StarknetStorageValue>::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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ pub(crate) fn test_binary_serialize_test(binary_input: HashMap<String, u128>) ->

// 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.
Expand Down Expand Up @@ -418,13 +418,13 @@ pub(crate) fn test_node_db_key() -> String {
let hash = HashOutput(zero);

let binary_node: FilledNode<StarknetStorageValue> = 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<StarknetStorageValue> = 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,
};

Expand Down Expand Up @@ -511,8 +511,8 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
// Create a binary node from the parsed data.
let binary_rust: FilledNode<StarknetStorageValue> = 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")?)),
};
Expand All @@ -527,7 +527,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
// Create an edge node from the parsed data.
let edge_rust: FilledNode<StarknetStorageValue> = 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub(crate) fn prepare_preimage_validation_non_deterministic_hashes<S: StateReade
get_ptr_from_var_name(Ids::CurrentHash.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(
current_hash_address,
hint_processor.commitment_type.hash_builtin_struct(),
Expand Down Expand Up @@ -402,7 +402,7 @@ pub(crate) fn load_edge<S: StateReader>(
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:?}"),
Expand Down Expand Up @@ -459,7 +459,7 @@ pub(crate) fn load_bottom<S: StateReader>(

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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type TreeLayer = HashMap<LayerIndex, UpdateTree>;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CanonicNode {
BinaryOrLeaf(HashOutput),
Edge(EdgeData),
Edge(EdgeData<HashOutput>),
Empty,
}

Expand Down Expand Up @@ -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() {
Expand Down
Loading
Loading