Skip to content

Commit 429b73c

Browse files
committed
starknet_committer: hash leaves of both layouts
1 parent 7501306 commit 429b73c

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

crates/starknet_committer/src/hash_function/hash.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,49 @@ impl TreeHashFunctionImpl {
4343
/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>
4444
impl TreeHashFunction<ContractState> for TreeHashFunctionImpl {
4545
fn compute_leaf_hash(contract_state: &ContractState) -> HashOutput {
46-
HashOutput(Pedersen::hash(
47-
&Pedersen::hash(
48-
&Pedersen::hash(&contract_state.class_hash.0, &contract_state.storage_root_hash.0),
49-
&contract_state.nonce.0,
50-
),
51-
&Self::CONTRACT_STATE_HASH_VERSION,
52-
))
46+
compute_contract_state_leaf_hash(contract_state)
5347
}
5448
fn compute_node_hash(node_data: &NodeData<ContractState, HashOutput>) -> HashOutput {
5549
Self::compute_node_hash_with_inner_hash_function::<PedersenHashFunction>(node_data)
5650
}
5751
}
5852

53+
fn compute_contract_state_leaf_hash<L: AsRef<ContractState>>(
54+
contract_state_leaf: &L,
55+
) -> HashOutput {
56+
let contract_state: &ContractState = contract_state_leaf.as_ref();
57+
HashOutput(Pedersen::hash(
58+
&Pedersen::hash(
59+
&Pedersen::hash(&contract_state.class_hash.0, &contract_state.storage_root_hash.0),
60+
&contract_state.nonce.0,
61+
),
62+
&TreeHashFunctionImpl::CONTRACT_STATE_HASH_VERSION,
63+
))
64+
}
65+
5966
/// Implementation of TreeHashFunction for the classes trie.
6067
/// The implementation is based on the following reference:
6168
/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>
6269
impl TreeHashFunction<CompiledClassHash> for TreeHashFunctionImpl {
6370
fn compute_leaf_hash(compiled_class_hash: &CompiledClassHash) -> HashOutput {
64-
let contract_class_leaf_version: Felt = Felt::from_hex(Self::CONTRACT_CLASS_LEAF_V0)
65-
.expect(
66-
"could not parse hex string corresponding to b'CONTRACT_CLASS_LEAF_V0' to Felt",
67-
);
68-
HashOutput(Poseidon::hash(&contract_class_leaf_version, &compiled_class_hash.0))
71+
compute_compiled_class_leaf_hash(compiled_class_hash)
6972
}
7073
fn compute_node_hash(node_data: &NodeData<CompiledClassHash, HashOutput>) -> HashOutput {
7174
Self::compute_node_hash_with_inner_hash_function::<PoseidonHashFunction>(node_data)
7275
}
7376
}
7477

78+
fn compute_compiled_class_leaf_hash<L: AsRef<CompiledClassHash>>(
79+
compiled_class_hash_leaf: &L,
80+
) -> HashOutput {
81+
let compiled_class_hash: &CompiledClassHash = compiled_class_hash_leaf.as_ref();
82+
let contract_class_leaf_version: Felt = Felt::from_hex(
83+
TreeHashFunctionImpl::CONTRACT_CLASS_LEAF_V0,
84+
)
85+
.expect("could not parse hex string corresponding to b'CONTRACT_CLASS_LEAF_V0' to Felt");
86+
HashOutput(Poseidon::hash(&contract_class_leaf_version, &compiled_class_hash.0))
87+
}
88+
7589
/// Implementation of TreeHashFunction for the storage trie.
7690
/// The implementation is based on the following reference:
7791
/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>

crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_impl.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ pub struct ContractState {
2121
pub class_hash: ClassHash,
2222
}
2323

24+
impl AsRef<ContractState> for ContractState {
25+
fn as_ref(&self) -> &ContractState {
26+
self
27+
}
28+
}
29+
2430
impl HasStaticPrefix for StarknetStorageValue {
2531
type KeyContext = EmptyKeyContext;
2632
fn get_static_prefix(_key_context: &Self::KeyContext) -> DbKeyPrefix {

crates/starknet_committer/src/patricia_merkle_tree/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ pub fn class_hash_into_node_index(class_hash: &ClassHash) -> NodeIndex {
2323
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
2424
pub struct CompiledClassHash(pub Felt);
2525

26+
impl AsRef<CompiledClassHash> for CompiledClassHash {
27+
fn as_ref(&self) -> &CompiledClassHash {
28+
self
29+
}
30+
}
31+
2632
impl_from_hex_for_felt_wrapper!(CompiledClassHash);
2733

2834
pub type StorageTrie = FilledTreeImpl<StarknetStorageValue>;

0 commit comments

Comments
 (0)