@@ -2,8 +2,14 @@ use std::collections::{HashMap, HashSet};
22
33use num_bigint:: BigUint ;
44use starknet_patricia:: hash:: hash_trait:: HashOutput ;
5- use starknet_patricia:: patricia_merkle_tree:: node_data:: inner_node:: { BinaryData , EdgeData } ;
5+ use starknet_patricia:: patricia_merkle_tree:: node_data:: inner_node:: {
6+ BinaryData ,
7+ EdgeData ,
8+ EdgePathLength ,
9+ PathToBottom ,
10+ } ;
611use starknet_patricia:: patricia_merkle_tree:: types:: SubTreeHeight ;
12+ use starknet_types_core:: felt:: Felt ;
713
814use crate :: hints:: hint_implementation:: patricia:: error:: PatriciaError ;
915
@@ -20,17 +26,20 @@ pub enum Preimage {
2026pub type PreimageMap = HashMap < HashOutput , Preimage > ;
2127
2228impl Preimage {
29+ pub ( crate ) const BINARY_LENGTH : u8 = 2 ;
30+ pub ( crate ) const EDGE_LENGTH : u8 = 3 ;
31+
2332 pub fn length ( & self ) -> u8 {
2433 match self {
25- Preimage :: Binary ( _) => 2 ,
26- Preimage :: Edge ( _) => 3 ,
34+ Self :: Binary ( _) => Self :: BINARY_LENGTH ,
35+ Self :: Edge ( _) => Self :: EDGE_LENGTH ,
2736 }
2837 }
2938
3039 fn get_binary ( & self ) -> Result < & BinaryData , PatriciaError > {
3140 match self {
32- Preimage :: Binary ( binary) => Ok ( binary) ,
33- Preimage :: Edge ( _) => Err ( PatriciaError :: ExpectedBinary ( self . clone ( ) ) ) ,
41+ Self :: Binary ( binary) => Ok ( binary) ,
42+ Self :: Edge ( _) => Err ( PatriciaError :: ExpectedBinary ( self . clone ( ) ) ) ,
3443 }
3544 }
3645}
@@ -173,3 +182,35 @@ pub(crate) fn build_update_tree(
173182 . remove ( & LayerIndex :: FIRST_LEAF )
174183 . expect ( "There should be a root node since modifications are not empty." ) )
175184}
185+
186+ /// Deserializes the preimage mapping from the commitment facts.
187+ fn create_preimage_mapping (
188+ commitment_facts : & HashMap < HashOutput , Vec < Felt > > ,
189+ ) -> Result < PreimageMap , PatriciaError > {
190+ let mut preimage_mapping = PreimageMap :: new ( ) ;
191+ for ( hash, raw_preimage) in commitment_facts. iter ( ) {
192+ match raw_preimage. as_slice ( ) {
193+ [ left, right] => {
194+ let binary_data =
195+ BinaryData { left_hash : HashOutput ( * left) , right_hash : HashOutput ( * right) } ;
196+ preimage_mapping. insert ( * hash, Preimage :: Binary ( binary_data) ) ;
197+ }
198+ [ length, path, bottom] => {
199+ let edge_data = EdgeData {
200+ bottom_hash : HashOutput ( * bottom) ,
201+ path_to_bottom : PathToBottom :: new (
202+ ( * path) . into ( ) ,
203+ EdgePathLength :: new ( ( * length) . try_into ( ) . map_err ( |_| {
204+ PatriciaError :: InvalidRawPreimage ( raw_preimage. clone ( ) )
205+ } ) ?) ?,
206+ ) ?,
207+ } ;
208+ preimage_mapping. insert ( * hash, Preimage :: Edge ( edge_data) ) ;
209+ }
210+ _ => {
211+ return Err ( PatriciaError :: InvalidRawPreimage ( raw_preimage. clone ( ) ) ) ;
212+ }
213+ }
214+ }
215+ Ok ( preimage_mapping)
216+ }
0 commit comments