1+ use std:: convert:: AsRef ;
2+
13use starknet_api:: hash:: HashOutput ;
24use starknet_patricia:: patricia_merkle_tree:: node_data:: inner_node:: NodeData ;
35use starknet_patricia:: patricia_merkle_tree:: updated_skeleton_tree:: hash_function:: {
@@ -8,9 +10,13 @@ use starknet_types_core::felt::Felt;
810use starknet_types_core:: hash:: { Pedersen , Poseidon , StarkHash } ;
911
1012use crate :: block_committer:: input:: StarknetStorageValue ;
13+ use crate :: db:: index_db:: leaves:: {
14+ IndexLayoutCompiledClassHash ,
15+ IndexLayoutContractState ,
16+ IndexLayoutStarknetStorageValue ,
17+ } ;
1118use crate :: patricia_merkle_tree:: leaf:: leaf_impl:: ContractState ;
1219use crate :: patricia_merkle_tree:: types:: CompiledClassHash ;
13-
1420/// Implementation of HashFunction for Pedersen hash function.
1521pub struct PedersenHashFunction ;
1622impl HashFunction for PedersenHashFunction {
@@ -43,35 +49,69 @@ impl TreeHashFunctionImpl {
4349/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>
4450impl TreeHashFunction < ContractState > for TreeHashFunctionImpl {
4551 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- ) )
52+ compute_contract_state_leaf_hash ( contract_state)
5353 }
5454 fn compute_node_hash ( node_data : & NodeData < ContractState , HashOutput > ) -> HashOutput {
5555 Self :: compute_node_hash_with_inner_hash_function :: < PedersenHashFunction > ( node_data)
5656 }
5757}
5858
59+ impl TreeHashFunction < IndexLayoutContractState > for TreeHashFunctionImpl {
60+ fn compute_leaf_hash ( contract_state : & IndexLayoutContractState ) -> HashOutput {
61+ compute_contract_state_leaf_hash ( contract_state)
62+ }
63+ fn compute_node_hash ( node_data : & NodeData < IndexLayoutContractState , HashOutput > ) -> HashOutput {
64+ Self :: compute_node_hash_with_inner_hash_function :: < PedersenHashFunction > ( node_data)
65+ }
66+ }
67+
68+ fn compute_contract_state_leaf_hash < L : AsRef < ContractState > > (
69+ contract_state_leaf : & L ,
70+ ) -> HashOutput {
71+ let contract_state: & ContractState = contract_state_leaf. as_ref ( ) ;
72+ HashOutput ( Pedersen :: hash (
73+ & Pedersen :: hash (
74+ & Pedersen :: hash ( & contract_state. class_hash . 0 , & contract_state. storage_root_hash . 0 ) ,
75+ & contract_state. nonce . 0 ,
76+ ) ,
77+ & TreeHashFunctionImpl :: CONTRACT_STATE_HASH_VERSION ,
78+ ) )
79+ }
80+
5981/// Implementation of TreeHashFunction for the classes trie.
6082/// The implementation is based on the following reference:
6183/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>
6284impl TreeHashFunction < CompiledClassHash > for TreeHashFunctionImpl {
6385 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 ) )
86+ compute_compiled_class_leaf_hash ( compiled_class_hash)
6987 }
7088 fn compute_node_hash ( node_data : & NodeData < CompiledClassHash , HashOutput > ) -> HashOutput {
7189 Self :: compute_node_hash_with_inner_hash_function :: < PoseidonHashFunction > ( node_data)
7290 }
7391}
7492
93+ impl TreeHashFunction < IndexLayoutCompiledClassHash > for TreeHashFunctionImpl {
94+ fn compute_leaf_hash ( compiled_class_hash : & IndexLayoutCompiledClassHash ) -> HashOutput {
95+ compute_compiled_class_leaf_hash ( compiled_class_hash)
96+ }
97+ fn compute_node_hash (
98+ node_data : & NodeData < IndexLayoutCompiledClassHash , HashOutput > ,
99+ ) -> HashOutput {
100+ Self :: compute_node_hash_with_inner_hash_function :: < PoseidonHashFunction > ( node_data)
101+ }
102+ }
103+
104+ fn compute_compiled_class_leaf_hash < L : AsRef < CompiledClassHash > > (
105+ compiled_class_hash_leaf : & L ,
106+ ) -> HashOutput {
107+ let compiled_class_hash: & CompiledClassHash = compiled_class_hash_leaf. as_ref ( ) ;
108+ let contract_class_leaf_version: Felt = Felt :: from_hex (
109+ TreeHashFunctionImpl :: CONTRACT_CLASS_LEAF_V0 ,
110+ )
111+ . expect ( "could not parse hex string corresponding to b'CONTRACT_CLASS_LEAF_V0' to Felt" ) ;
112+ HashOutput ( Poseidon :: hash ( & contract_class_leaf_version, & compiled_class_hash. 0 ) )
113+ }
114+
75115/// Implementation of TreeHashFunction for the storage trie.
76116/// The implementation is based on the following reference:
77117/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>
@@ -84,6 +124,17 @@ impl TreeHashFunction<StarknetStorageValue> for TreeHashFunctionImpl {
84124 }
85125}
86126
127+ impl TreeHashFunction < IndexLayoutStarknetStorageValue > for TreeHashFunctionImpl {
128+ fn compute_leaf_hash ( storage_value : & IndexLayoutStarknetStorageValue ) -> HashOutput {
129+ HashOutput ( storage_value. 0 . 0 )
130+ }
131+ fn compute_node_hash (
132+ node_data : & NodeData < IndexLayoutStarknetStorageValue , HashOutput > ,
133+ ) -> HashOutput {
134+ Self :: compute_node_hash_with_inner_hash_function :: < PedersenHashFunction > ( node_data)
135+ }
136+ }
137+
87138/// Combined trait for all specific implementations.
88139pub ( crate ) trait ForestHashFunction :
89140 TreeHashFunction < ContractState >
0 commit comments