@@ -11,6 +11,12 @@ use crate::block_committer::input::StarknetStorageValue;
1111use crate :: patricia_merkle_tree:: leaf:: leaf_impl:: ContractState ;
1212use 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.
1521pub struct PedersenHashFunction ;
1622impl HashFunction for PedersenHashFunction {
@@ -29,49 +35,50 @@ impl HashFunction for PoseidonHashFunction {
2935
3036pub 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>
4441impl 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>
6266impl 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>
0 commit comments