Skip to content

Commit c4727df

Browse files
committed
starknet_committer,starknet_os,starknet_patricia: make node data generic
1 parent 27a9372 commit c4727df

File tree

16 files changed

+114
-109
lines changed

16 files changed

+114
-109
lines changed

crates/starknet_committer/src/block_committer/random_structs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ impl RandomValue for ContractState {
9090
}
9191
}
9292

93-
impl RandomValue for BinaryData {
93+
impl RandomValue for BinaryData<HashOutput> {
9494
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
95-
Self { left_hash: HashOutput::random(rng, max), right_hash: HashOutput::random(rng, max) }
95+
Self { left_data: HashOutput::random(rng, max), right_data: HashOutput::random(rng, max) }
9696
}
9797
}
9898

@@ -134,18 +134,18 @@ impl RandomValue for EdgePath {
134134
}
135135
}
136136

137-
impl RandomValue for EdgeData {
137+
impl RandomValue for EdgeData<HashOutput> {
138138
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
139139
Self {
140-
bottom_hash: HashOutput::random(rng, max),
140+
bottom_data: HashOutput::random(rng, max),
141141
path_to_bottom: PathToBottom::random(rng, max),
142142
}
143143
}
144144
}
145145

146146
macro_rules! random_node_data {
147147
($leaf:ty) => {
148-
impl RandomValue for NodeData<$leaf> {
148+
impl RandomValue for NodeData<$leaf, HashOutput> {
149149
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
150150
match NodeDataVariants::iter()
151151
.choose(rng)

crates/starknet_committer/src/db/facts_db/traversal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub(crate) async fn fetch_patricia_paths_inner<'a, L: Leaf>(
113113
NodeData::Binary(binary_data) => {
114114
witnesses.insert(subtree.root_hash, Preimage::Binary(binary_data.clone()));
115115
let (left_subtree, right_subtree) = subtree
116-
.get_children_subtrees(binary_data.left_hash, binary_data.right_hash);
116+
.get_children_subtrees(binary_data.left_data, binary_data.right_data);
117117
next_subtrees.push(left_subtree);
118118
next_subtrees.push(right_subtree);
119119
}
@@ -122,7 +122,7 @@ pub(crate) async fn fetch_patricia_paths_inner<'a, L: Leaf>(
122122
witnesses.insert(subtree.root_hash, Preimage::Edge(edge_data));
123123
// Parse bottom.
124124
let (bottom_subtree, empty_leaves_indices) = subtree
125-
.get_bottom_subtree(&edge_data.path_to_bottom, edge_data.bottom_hash);
125+
.get_bottom_subtree(&edge_data.path_to_bottom, edge_data.bottom_data);
126126
if let Some(ref mut leaves_map) = leaves {
127127
// Insert empty leaves descendent of the current subtree, that are not
128128
// descendents of the bottom node.

crates/starknet_committer/src/db/facts_db/traversal_test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,24 +465,24 @@ async fn test_fetch_patricia_paths_inner_impl(
465465
PreimageMap::from([
466466
(HashOutput(Felt::from_hex_unchecked("0xdd6634d8228819c6b4aec64cf4e5a39a420c77b75cdf08a85f73ae2f7afcc1")),
467467
Preimage::Binary(BinaryData {
468-
left_hash: HashOutput(Felt::from_hex_unchecked("0x111afbf8374248dc3a584bbd5f7c868f1dd76c3f17a326b5c77e692d736ece5")),
469-
right_hash: HashOutput(Felt::from_hex_unchecked("0x20eec267afb39fcff7c97f9aa9e46ab73f61bf2e7db51c85a8f17cc313447fe")),
468+
left_data: HashOutput(Felt::from_hex_unchecked("0x111afbf8374248dc3a584bbd5f7c868f1dd76c3f17a326b5c77e692d736ece5")),
469+
right_data: HashOutput(Felt::from_hex_unchecked("0x20eec267afb39fcff7c97f9aa9e46ab73f61bf2e7db51c85a8f17cc313447fe")),
470470
})),
471471
(HashOutput(Felt::from_hex_unchecked("0x111afbf8374248dc3a584bbd5f7c868f1dd76c3f17a326b5c77e692d736ece5")),
472472
Preimage::Binary(BinaryData {
473-
left_hash: HashOutput(Felt::from_hex_unchecked("0x610eec7d913ae704e188746bc82767430e39e6f096188f4671712791c563a67")),
474-
right_hash: HashOutput(Felt::from_hex_unchecked("0x25177dfc7f358239f3b7c4c1771ddcd7eaf74a1b2b2ac952f2c2dd52f5b860d")),
473+
left_data: HashOutput(Felt::from_hex_unchecked("0x610eec7d913ae704e188746bc82767430e39e6f096188f4671712791c563a67")),
474+
right_data: HashOutput(Felt::from_hex_unchecked("0x25177dfc7f358239f3b7c4c1771ddcd7eaf74a1b2b2ac952f2c2dd52f5b860d")),
475475
})),
476476
(HashOutput(Felt::from_hex_unchecked("0x610eec7d913ae704e188746bc82767430e39e6f096188f4671712791c563a67")),
477477
Preimage::Edge(EdgeData {
478-
bottom_hash: HashOutput(Felt::from_hex_unchecked("0x8")),
478+
bottom_data: HashOutput(Felt::from_hex_unchecked("0x8")),
479479
path_to_bottom: PathToBottom::new(EdgePath(U256::from(0_u128)),
480480
EdgePathLength::new(1).unwrap())
481481
.unwrap()
482482
})),
483483
(HashOutput(Felt::from_hex_unchecked("0x25177dfc7f358239f3b7c4c1771ddcd7eaf74a1b2b2ac952f2c2dd52f5b860d")),
484484
Preimage::Edge(EdgeData {
485-
bottom_hash: HashOutput(Felt::from_hex_unchecked("0xb")),
485+
bottom_data: HashOutput(Felt::from_hex_unchecked("0xb")),
486486
path_to_bottom: PathToBottom::new(EdgePath(U256::from(1_u128)),
487487
EdgePathLength::new(1).unwrap())
488488
.unwrap()

crates/starknet_committer/src/hash_function/hash.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl TreeHashFunction<ContractState> for TreeHashFunctionImpl {
5151
&Self::CONTRACT_STATE_HASH_VERSION,
5252
))
5353
}
54-
fn compute_node_hash(node_data: &NodeData<ContractState>) -> HashOutput {
54+
fn compute_node_hash(node_data: &NodeData<ContractState, HashOutput>) -> HashOutput {
5555
Self::compute_node_hash_with_inner_hash_function::<PedersenHashFunction>(node_data)
5656
}
5757
}
@@ -67,7 +67,7 @@ impl TreeHashFunction<CompiledClassHash> for TreeHashFunctionImpl {
6767
);
6868
HashOutput(Poseidon::hash(&contract_class_leaf_version, &compiled_class_hash.0))
6969
}
70-
fn compute_node_hash(node_data: &NodeData<CompiledClassHash>) -> HashOutput {
70+
fn compute_node_hash(node_data: &NodeData<CompiledClassHash, HashOutput>) -> HashOutput {
7171
Self::compute_node_hash_with_inner_hash_function::<PoseidonHashFunction>(node_data)
7272
}
7373
}
@@ -79,7 +79,7 @@ impl TreeHashFunction<StarknetStorageValue> for TreeHashFunctionImpl {
7979
fn compute_leaf_hash(storage_value: &StarknetStorageValue) -> HashOutput {
8080
HashOutput(storage_value.0)
8181
}
82-
fn compute_node_hash(node_data: &NodeData<StarknetStorageValue>) -> HashOutput {
82+
fn compute_node_hash(node_data: &NodeData<StarknetStorageValue, HashOutput>) -> HashOutput {
8383
Self::compute_node_hash_with_inner_hash_function::<PedersenHashFunction>(node_data)
8484
}
8585
}

crates/starknet_committer/src/hash_function/hash_test.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use crate::patricia_merkle_tree::types::CompiledClassHash;
5252
Felt::from_hex("0x1b20bbb35009bf03f86fb092b56a9c44deedbcca6addf8f7640f54a48ba5bbc").unwrap()
5353
)]
5454
fn test_tree_hash_function_contract_state_leaf(
55-
#[case] node_data: NodeData<ContractState>,
55+
#[case] node_data: NodeData<ContractState, HashOutput>,
5656
#[case] expected_hash: Felt,
5757
) {
5858
let hash_output = TreeHashFunctionImpl::compute_node_hash(&node_data);
@@ -65,7 +65,7 @@ fn test_tree_hash_function_contract_state_leaf(
6565
Felt::from_hex("0x49ed9a06987e7e55770d6c4d7d16b819ad984bf4aed552042847380cc31210d").unwrap()
6666
)]
6767
fn test_tree_hash_function_compiled_class_hash_leaf(
68-
#[case] node_data: NodeData<CompiledClassHash>,
68+
#[case] node_data: NodeData<CompiledClassHash, HashOutput>,
6969
#[case] expected_hash: Felt,
7070
) {
7171
let hash_output = TreeHashFunctionImpl::compute_node_hash(&node_data);
@@ -76,7 +76,7 @@ fn test_tree_hash_function_compiled_class_hash_leaf(
7676
// Expected hash value was computed independently.
7777
#[case(NodeData::Leaf(StarknetStorageValue(Felt::from_hex("0xDEAFBEEF").unwrap())), Felt::from_hex("0xDEAFBEEF").unwrap())]
7878
fn test_tree_hash_function_storage_leaf(
79-
#[case] node_data: NodeData<StarknetStorageValue>,
79+
#[case] node_data: NodeData<StarknetStorageValue, HashOutput>,
8080
#[case] expected_hash: Felt,
8181
) {
8282
let hash_output = TreeHashFunctionImpl::compute_node_hash(&node_data);
@@ -94,10 +94,13 @@ fn test_tree_hash_function_impl_binary_node(
9494
) {
9595
use starknet_types_core::hash::StarkHash;
9696

97-
let hash_output =
98-
TreeHashFunctionImpl::compute_node_hash(&NodeData::<StarknetStorageValue>::Binary(
99-
BinaryData { left_hash: HashOutput(left_hash), right_hash: HashOutput(right_hash) },
100-
));
97+
let hash_output = TreeHashFunctionImpl::compute_node_hash(&NodeData::<
98+
StarknetStorageValue,
99+
HashOutput,
100+
>::Binary(BinaryData {
101+
left_data: HashOutput(left_hash),
102+
right_data: HashOutput(right_hash),
103+
}));
101104
assert_eq!(hash_output, HashOutput(Pedersen::hash(&left_hash, &right_hash)));
102105
assert_eq!(hash_output, HashOutput(expected_hash));
103106
}
@@ -114,16 +117,14 @@ fn test_tree_hash_function_impl_edge_node(
114117
) {
115118
use starknet_types_core::hash::StarkHash;
116119

117-
let hash_output = TreeHashFunctionImpl::compute_node_hash(
118-
&NodeData::<StarknetStorageValue>::Edge(EdgeData {
119-
bottom_hash: HashOutput(bottom_hash),
120-
path_to_bottom: PathToBottom::new(
121-
edge_path.into(),
122-
EdgePathLength::new(length).unwrap(),
123-
)
120+
let hash_output = TreeHashFunctionImpl::compute_node_hash(&NodeData::<
121+
StarknetStorageValue,
122+
HashOutput,
123+
>::Edge(EdgeData {
124+
bottom_data: HashOutput(bottom_hash),
125+
path_to_bottom: PathToBottom::new(edge_path.into(), EdgePathLength::new(length).unwrap())
124126
.unwrap(),
125-
}),
126-
);
127+
}));
127128
let direct_hash_computation =
128129
HashOutput(Pedersen::hash(&bottom_hash, &edge_path.into()) + Felt::from(length));
129130
assert_eq!(hash_output, HashOutput(expected_hash));

crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ pub(crate) fn test_binary_serialize_test(binary_input: HashMap<String, u128>) ->
264264

265265
// Create binary data from the left and right values.
266266
let binary_data = BinaryData {
267-
left_hash: HashOutput(Felt::from(*left)),
268-
right_hash: HashOutput(Felt::from(*right)),
267+
left_data: HashOutput(Felt::from(*left)),
268+
right_data: HashOutput(Felt::from(*right)),
269269
};
270270

271271
// 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 {
418418
let hash = HashOutput(zero);
419419

420420
let binary_node: FilledNode<StarknetStorageValue> = FilledNode {
421-
data: NodeData::Binary(BinaryData { left_hash: hash, right_hash: hash }),
421+
data: NodeData::Binary(BinaryData { left_data: hash, right_data: hash }),
422422
hash,
423423
};
424424
let binary_node_key = binary_node.db_key(EmptyKeyContext).0;
425425

426426
let edge_node: FilledNode<StarknetStorageValue> = FilledNode {
427-
data: NodeData::Edge(EdgeData { bottom_hash: hash, path_to_bottom: Default::default() }),
427+
data: NodeData::Edge(EdgeData { bottom_data: hash, path_to_bottom: Default::default() }),
428428
hash,
429429
};
430430

@@ -511,8 +511,8 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
511511
// Create a binary node from the parsed data.
512512
let binary_rust: FilledNode<StarknetStorageValue> = FilledNode {
513513
data: NodeData::Binary(BinaryData {
514-
left_hash: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "left")?)),
515-
right_hash: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "right")?)),
514+
left_data: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "left")?)),
515+
right_data: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "right")?)),
516516
}),
517517
hash: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "hash")?)),
518518
};
@@ -527,7 +527,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
527527
// Create an edge node from the parsed data.
528528
let edge_rust: FilledNode<StarknetStorageValue> = FilledNode {
529529
data: NodeData::Edge(EdgeData {
530-
bottom_hash: HashOutput(Felt::from(*get_or_key_not_found(&edge_data, "bottom")?)),
530+
bottom_data: HashOutput(Felt::from(*get_or_key_not_found(&edge_data, "bottom")?)),
531531
path_to_bottom: PathToBottom::new(
532532
U256::from(*get_or_key_not_found(&edge_data, "path")?).into(),
533533
EdgePathLength::new(

crates/starknet_os/src/hints/hint_implementation/patricia/implementation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ pub(crate) fn prepare_preimage_validation_non_deterministic_hashes<S: StateReade
214214
get_ptr_from_var_name(Ids::CurrentHash.into(), vm, ids_data, ap_tracking)?;
215215

216216
let nested_fields_and_values =
217-
[("x", binary_data.left_hash.0.into()), ("y", binary_data.right_hash.0.into())];
217+
[("x", binary_data.left_data.0.into()), ("y", binary_data.right_data.0.into())];
218218
insert_values_to_fields(
219219
current_hash_address,
220220
hint_processor.commitment_type.hash_builtin_struct(),
@@ -402,7 +402,7 @@ pub(crate) fn load_edge<S: StateReader>(
402402
let commitment_facts = &hint_processor.get_commitment_info()?.commitment_facts;
403403
let preimage =
404404
Preimage::try_from(commitment_facts.get(&node).ok_or(OsHintError::MissingPreimage(node))?)?;
405-
let Preimage::Edge(EdgeData { bottom_hash, path_to_bottom }) = preimage else {
405+
let Preimage::Edge(EdgeData { bottom_data: bottom_hash, path_to_bottom }) = preimage else {
406406
// We expect an edge node.
407407
return Err(OsHintError::AssertionFailed {
408408
message: format!("An edge node is expected, found {preimage:?}"),
@@ -459,7 +459,7 @@ pub(crate) fn load_bottom<S: StateReader>(
459459

460460
let hash_ptr_address = get_ptr_from_var_name(Ids::HashPtr.into(), vm, ids_data, ap_tracking)?;
461461
let nested_fields_and_values =
462-
[("x", binary_data.left_hash.0.into()), ("y", binary_data.right_hash.0.into())];
462+
[("x", binary_data.left_data.0.into()), ("y", binary_data.right_data.0.into())];
463463
insert_values_to_fields(
464464
hash_ptr_address,
465465
hint_processor.commitment_type.hash_builtin_struct(),

crates/starknet_os/src/hints/hint_implementation/patricia/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,19 @@ fn get_children(
252252

253253
let binary = preimage.get_binary()?;
254254
Ok((
255-
CanonicNode::new(preimage_map, &binary.left_hash),
256-
CanonicNode::new(preimage_map, &binary.right_hash),
255+
CanonicNode::new(preimage_map, &binary.left_data),
256+
CanonicNode::new(preimage_map, &binary.right_data),
257257
))
258258
}
259259
CanonicNode::Edge(edge) => {
260-
let hash = edge.bottom_hash;
260+
let hash = edge.bottom_data;
261261
let path_to_bottom = edge.path_to_bottom;
262262

263263
let child = if u8::from(path_to_bottom.length) == 1 {
264264
CanonicNode::BinaryOrLeaf(hash)
265265
} else {
266266
let new_path = path_to_bottom.remove_first_edges(EdgePathLength::new(1)?)?;
267-
CanonicNode::Edge(EdgeData { bottom_hash: hash, path_to_bottom: new_path })
267+
CanonicNode::Edge(EdgeData { bottom_data: hash, path_to_bottom: new_path })
268268
};
269269

270270
if path_to_bottom.is_left_descendant() {

0 commit comments

Comments
 (0)