Skip to content

Commit 031bb06

Browse files
committed
starknet_committer: hash leaves of both layouts
1 parent f89d76a commit 031bb06

File tree

6 files changed

+51
-30
lines changed

6 files changed

+51
-30
lines changed

crates/starknet_committer/src/hash_function/hash.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ use crate::block_committer::input::StarknetStorageValue;
1111
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
1212
use crate::patricia_merkle_tree::types::CompiledClassHash;
1313

14+
pub const CONTRACT_STATE_HASH_VERSION: Felt = Felt::ZERO;
15+
16+
// The hex string corresponding to b'CONTRACT_CLASS_LEAF_V0' in big-endian.
17+
pub const CONTRACT_CLASS_LEAF_V0: Felt =
18+
Felt::from_hex_unchecked("0x434f4e54524143545f434c4153535f4c4541465f5630");
19+
1420
/// Implementation of HashFunction for Pedersen hash function.
1521
pub struct PedersenHashFunction;
1622
impl HashFunction for PedersenHashFunction {
@@ -29,49 +35,50 @@ impl HashFunction for PoseidonHashFunction {
2935

3036
pub struct TreeHashFunctionImpl;
3137

32-
impl TreeHashFunctionImpl {
33-
// TODO(Aner, 11/4/24): Verify the correctness of the implementation.
34-
pub const CONTRACT_STATE_HASH_VERSION: Felt = Felt::ZERO;
35-
36-
// The hex string corresponding to b'CONTRACT_CLASS_LEAF_V0' in big-endian.
37-
pub const CONTRACT_CLASS_LEAF_V0: &'static str =
38-
"0x434f4e54524143545f434c4153535f4c4541465f5630";
39-
}
40-
4138
/// Implementation of TreeHashFunction for contracts trie.
4239
/// The implementation is based on the following reference:
4340
/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>
4441
impl TreeHashFunction<ContractState> for TreeHashFunctionImpl {
4542
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-
))
43+
compute_contract_state_leaf_hash(contract_state)
5344
}
5445
fn compute_node_hash(node_data: &NodeData<ContractState, HashOutput>) -> HashOutput {
5546
Self::compute_node_hash_with_inner_hash_function::<PedersenHashFunction>(node_data)
5647
}
5748
}
5849

50+
fn compute_contract_state_leaf_hash<L: AsRef<ContractState>>(
51+
contract_state_leaf: &L,
52+
) -> HashOutput {
53+
let contract_state: &ContractState = contract_state_leaf.as_ref();
54+
HashOutput(Pedersen::hash(
55+
&Pedersen::hash(
56+
&Pedersen::hash(&contract_state.class_hash.0, &contract_state.storage_root_hash.0),
57+
&contract_state.nonce.0,
58+
),
59+
&CONTRACT_STATE_HASH_VERSION,
60+
))
61+
}
62+
5963
/// Implementation of TreeHashFunction for the classes trie.
6064
/// The implementation is based on the following reference:
6165
/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>
6266
impl TreeHashFunction<CompiledClassHash> for TreeHashFunctionImpl {
6367
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))
68+
compute_compiled_class_leaf_hash(compiled_class_hash)
6969
}
7070
fn compute_node_hash(node_data: &NodeData<CompiledClassHash, HashOutput>) -> HashOutput {
7171
Self::compute_node_hash_with_inner_hash_function::<PoseidonHashFunction>(node_data)
7272
}
7373
}
7474

75+
fn compute_compiled_class_leaf_hash<L: AsRef<CompiledClassHash>>(
76+
compiled_class_hash_leaf: &L,
77+
) -> HashOutput {
78+
let compiled_class_hash: &CompiledClassHash = compiled_class_hash_leaf.as_ref();
79+
HashOutput(Poseidon::hash(&CONTRACT_CLASS_LEAF_V0, &compiled_class_hash.0))
80+
}
81+
7582
/// Implementation of TreeHashFunction for the storage trie.
7683
/// The implementation is based on the following reference:
7784
/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>

crates/starknet_committer/src/hash_function/hash_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use starknet_types_core::felt::Felt;
1414
use starknet_types_core::hash::Pedersen;
1515

1616
use crate::block_committer::input::StarknetStorageValue;
17-
use crate::hash_function::hash::TreeHashFunctionImpl;
17+
use crate::hash_function::hash::{TreeHashFunctionImpl, CONTRACT_CLASS_LEAF_V0};
1818
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
1919
use crate::patricia_merkle_tree::types::CompiledClassHash;
2020

@@ -134,7 +134,7 @@ fn test_tree_hash_function_impl_edge_node(
134134
#[rstest]
135135
fn test_constant_contract_class_leaf_v0() {
136136
assert_eq!(
137-
hex::decode(TreeHashFunctionImpl::CONTRACT_CLASS_LEAF_V0.trim_start_matches("0x")).unwrap(),
137+
hex::decode(CONTRACT_CLASS_LEAF_V0.to_hex_string().trim_start_matches("0x")).unwrap(),
138138
b"CONTRACT_CLASS_LEAF_V0".as_slice()
139139
);
140140
}

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>;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ use starknet_committer::block_committer::input::{
1313
use starknet_committer::block_committer::random_structs::DummyRandomValue;
1414
use starknet_committer::db::external_test_utils::single_tree_flow_test;
1515
use starknet_committer::forest::filled_forest::FilledForest;
16-
use starknet_committer::hash_function::hash::TreeHashFunctionImpl;
16+
use starknet_committer::hash_function::hash::{
17+
TreeHashFunctionImpl,
18+
CONTRACT_CLASS_LEAF_V0,
19+
CONTRACT_STATE_HASH_VERSION,
20+
};
1721
use starknet_committer::patricia_merkle_tree::leaf::leaf_impl::ContractState;
1822
use starknet_committer::patricia_merkle_tree::tree::OriginalSkeletonStorageTrieConfig;
1923
use starknet_committer::patricia_merkle_tree::types::CompiledClassHash;
@@ -492,10 +496,8 @@ pub(crate) async fn storage_serialize_test() -> CommitterPythonTestResult {
492496
fn python_hash_constants_compare() -> String {
493497
format!(
494498
"[{:?}, {:?}]",
495-
TreeHashFunctionImpl::CONTRACT_STATE_HASH_VERSION.to_bytes_be(),
496-
Felt::from_hex(TreeHashFunctionImpl::CONTRACT_CLASS_LEAF_V0).expect(
497-
"could not parse hex string corresponding to b'CONTRACT_CLASS_LEAF_V0' to Felt",
498-
).to_bytes_be()
499+
CONTRACT_STATE_HASH_VERSION.to_bytes_be(),
500+
CONTRACT_CLASS_LEAF_V0.to_bytes_be()
499501
)
500502
}
501503

crates/starknet_os/src/constants_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use apollo_starknet_os_program::OS_PROGRAM;
22
use blockifier::abi::constants::{L1_TO_L2_MSG_HEADER_SIZE, L2_TO_L1_MSG_HEADER_SIZE};
33
use starknet_api::contract_class::compiled_class_hash::COMPILED_CLASS_V1;
44
use starknet_api::core::{GLOBAL_STATE_VERSION, L2_ADDRESS_UPPER_BOUND};
5-
use starknet_committer::hash_function::hash::TreeHashFunctionImpl;
5+
use starknet_committer::hash_function::hash::{TreeHashFunctionImpl, CONTRACT_CLASS_LEAF_V0};
66
use starknet_types_core::felt::Felt;
77

88
use crate::hints::hint_implementation::kzg::utils::FIELD_ELEMENTS_PER_BLOB;
@@ -26,7 +26,7 @@ fn test_blob_constants() {
2626
fn test_contract_class_hash_version() {
2727
assert_eq!(
2828
Const::ContractClassLeafVersion.fetch_from_os_program().unwrap(),
29-
Felt::from_hex(TreeHashFunctionImpl::CONTRACT_CLASS_LEAF_V0).unwrap()
29+
CONTRACT_CLASS_LEAF_V0
3030
);
3131
}
3232

0 commit comments

Comments
 (0)