11use kimchi:: o1_utils:: FieldHelpers ;
2+ use merkle_verifier:: verify_merkle_proof;
23use mina_curves:: pasta:: Fp ;
4+ use mina_tree:: MerklePath ;
5+
6+ mod merkle_verifier;
37
48// TODO(xqft): check sizes
59const MAX_PROOF_SIZE : usize = 16 * 1024 ;
610const MAX_PUB_INPUT_SIZE : usize = 6 * 1024 ;
711const HASH_SIZE : usize = 32 ;
12+ const MERKLE_PATH_LEN_SIZE : usize = 4 ;
813
914#[ no_mangle]
1015pub extern "C" fn verify_account_inclusion_ffi (
@@ -35,7 +40,7 @@ pub extern "C" fn verify_account_inclusion_ffi(
3540 }
3641 } ;
3742
38- todo ! ( )
43+ verify_merkle_proof ( account_hash , merkle_proof , merkle_root )
3944}
4045
4146pub fn parse_hash ( pub_inputs : & [ u8 ] , offset : & mut usize ) -> Result < Fp , String > {
@@ -58,19 +63,34 @@ pub fn parse_pub_inputs(pub_inputs: &[u8]) -> Result<(Fp, Fp), String> {
5863 Ok ( ( merkle_root, account_hash) )
5964}
6065
61- pub fn parse_proof ( proof_bytes : & [ u8 ] ) -> Result < ( ) , String > {
62- todo ! ( )
63- // std::str::from_utf8(proof_bytes)
64- // .map_err(|err| err.to_string())
65- // .and_then(|base64| {
66- // BASE64_URL_SAFE
67- // .decode(base64)
68- // .map_err(|err| err.to_string())
69- // })
70- // .and_then(|binprot| {
71- // MinaBaseProofStableV2::binprot_read(&mut binprot.as_slice())
72- // .map_err(|err| err.to_string())
73- // })
66+ pub fn parse_proof ( proof_bytes : & [ u8 ] ) -> Result < Vec < MerklePath > , String > {
67+ let merkle_path_bytes = proof_bytes
68+ . get ( MERKLE_PATH_LEN_SIZE ..)
69+ . ok_or ( "Failed to slice merkle path" . to_string ( ) ) ?
70+ . chunks_exact ( HASH_SIZE + 1 ) ;
71+
72+ if !merkle_path_bytes. remainder ( ) . is_empty ( ) {
73+ return Err ( format ! (
74+ "Merkle path bytes not a multiple of HASH_SIZE + 1 ({})" ,
75+ HASH_SIZE + 1
76+ ) ) ;
77+ }
78+
79+ merkle_path_bytes
80+ . map ( |bytes| {
81+ let left_or_right = bytes
82+ . first ( )
83+ . ok_or ( "left_or_right byte not found" . to_string ( ) ) ?;
84+ let hash = Fp :: from_bytes ( bytes) . map_err ( |err| {
85+ format ! ( "Failed to convert merkle hash into field element: {err}" )
86+ } ) ?;
87+ match left_or_right {
88+ 0 => Ok ( MerklePath :: Left ( hash) ) ,
89+ 1 => Ok ( MerklePath :: Right ( hash) ) ,
90+ _ => Err ( "Unexpected left_or_right byte" . to_string ( ) ) ,
91+ }
92+ } )
93+ . collect ( )
7494}
7595
7696#[ cfg( test) ]
0 commit comments