1+ #[ cfg( feature = "os_input" ) ]
2+ use blake2:: Blake2s256 ;
3+ #[ cfg( feature = "os_input" ) ]
4+ use digest:: Digest ;
15use serde:: { Deserialize , Serialize } ;
26use starknet_api:: block:: BlockNumber ;
7+ #[ cfg( feature = "os_input" ) ]
8+ use starknet_patricia:: patricia_merkle_tree:: types:: NodeIndex ;
39use starknet_patricia_storage:: storage_trait:: DbValue ;
410use starknet_types_core:: felt:: Felt ;
511
12+ #[ cfg( feature = "os_input" ) ]
13+ use crate :: patricia_merkle_tree:: tree:: SortedLeavesRequest ;
14+
615#[ derive( Clone , Debug , Default , Deserialize , Eq , PartialEq , Hash , Serialize ) ]
716pub struct DbBlockNumber ( pub BlockNumber ) ;
817
@@ -23,3 +32,42 @@ pub fn serialize_felt_no_packing(felt: Felt) -> DbValue {
2332pub fn deserialize_felt_no_packing ( value : & DbValue ) -> Felt {
2433 Felt :: from_bytes_be_slice ( & value. 0 )
2534}
35+
36+ /// BLAKE2s-256 digest over a deterministic encoding of [`SortedLeavesRequest`].
37+ #[ cfg( feature = "os_input" ) ]
38+ pub fn accessed_keys_digest ( sorted : & SortedLeavesRequest < ' _ > ) -> [ u8 ; 32 ] {
39+ let mut payload = Vec :: new ( ) ;
40+
41+ let class = sorted. class_sorted . get_indices ( ) ;
42+ payload. extend_from_slice ( & encode_usize ( class. len ( ) ) ) ;
43+ for idx in class {
44+ payload. extend_from_slice ( & idx. 0 . to_be_bytes ( ) ) ;
45+ }
46+
47+ let contract = sorted. contract_sorted . get_indices ( ) ;
48+ payload. extend_from_slice ( & encode_usize ( contract. len ( ) ) ) ;
49+ for idx in contract {
50+ payload. extend_from_slice ( & idx. 0 . to_be_bytes ( ) ) ;
51+ }
52+
53+ let mut contract_indices: Vec < NodeIndex > = sorted. storage_sorted . keys ( ) . copied ( ) . collect ( ) ;
54+ contract_indices. sort_unstable ( ) ;
55+
56+ payload. extend_from_slice ( & encode_usize ( contract_indices. len ( ) ) ) ;
57+ for contract_idx in contract_indices {
58+ let sorted_slots = sorted. storage_sorted . get ( & contract_idx) . unwrap ( ) ;
59+ payload. extend_from_slice ( & contract_idx. 0 . to_be_bytes ( ) ) ;
60+ let slot_indices = sorted_slots. get_indices ( ) ;
61+ payload. extend_from_slice ( & encode_usize ( slot_indices. len ( ) ) ) ;
62+ for slot in slot_indices {
63+ payload. extend_from_slice ( & slot. 0 . to_be_bytes ( ) ) ;
64+ }
65+ }
66+
67+ Blake2s256 :: digest ( & payload) . into ( )
68+ }
69+
70+ #[ cfg( feature = "os_input" ) ]
71+ fn encode_usize ( n : usize ) -> [ u8 ; 8 ] {
72+ ( n as u64 ) . to_be_bytes ( )
73+ }
0 commit comments