@@ -12,9 +12,25 @@ mod merkle_verifier;
1212#[ no_mangle]
1313pub extern "C" fn verify_account_inclusion_ffi (
1414 proof_bytes : * const u8 ,
15- proof_len : usize ,
15+ proof_len : u32 ,
1616 pub_input_bytes : * const u8 ,
17- pub_input_len : usize ,
17+ pub_input_len : u32 ,
18+ ) -> i32 {
19+ let result = std:: panic:: catch_unwind ( || {
20+ inner_verify_account_inclusion_ffi ( proof_bytes, proof_len, pub_input_bytes, pub_input_len)
21+ } ) ;
22+
23+ match result {
24+ Ok ( v) => v as i32 ,
25+ Err ( _) => -1 ,
26+ }
27+ }
28+
29+ fn inner_verify_account_inclusion_ffi (
30+ proof_bytes : * const u8 ,
31+ proof_len : u32 ,
32+ pub_input_bytes : * const u8 ,
33+ pub_input_len : u32 ,
1834) -> bool {
1935 if proof_bytes. is_null ( ) || pub_input_bytes. is_null ( ) {
2036 error ! ( "Input buffer null" ) ;
@@ -29,7 +45,7 @@ pub extern "C" fn verify_account_inclusion_ffi(
2945 let proof_bytes = unsafe { std:: slice:: from_raw_parts ( proof_bytes, proof_len as usize ) } ;
3046
3147 let pub_input_bytes =
32- unsafe { std:: slice:: from_raw_parts ( pub_input_bytes, proof_len as usize ) } ;
48+ unsafe { std:: slice:: from_raw_parts ( pub_input_bytes, pub_input_len as usize ) } ;
3349
3450 verify_account_inclusion ( proof_bytes, pub_input_bytes)
3551}
@@ -45,6 +61,7 @@ pub fn verify_account_inclusion(proof_bytes: &[u8], pub_input_bytes: &[u8]) -> b
4561 return false ;
4662 }
4763 } ;
64+ error ! ( "pub input len: {}" , pub_input_bytes. len( ) ) ;
4865 let MinaAccountPubInputs {
4966 ledger_hash,
5067 encoded_account,
@@ -94,80 +111,50 @@ mod test {
94111
95112 #[ test]
96113 fn valid_account_state_proof_verifies ( ) {
97- let result = verify_account_inclusion ( PROOF_BYTES , PUB_INPUT_BYTES ) ;
98- assert ! ( result) ;
99- }
100-
101- #[ test]
102- fn empty_account_state_proof_does_not_verify ( ) {
103- let proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
104- let proof_size = PROOF_BYTES . len ( ) ;
105-
106- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
107- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
108- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
109- pub_input_buffer[ ..pub_input_size] . clone_from_slice ( PUB_INPUT_BYTES ) ;
110-
111- let result = verify_account_inclusion ( & proof_buffer, & pub_input_buffer, pub_input_size) ;
112- assert ! ( !result) ;
113- }
114-
115- #[ test]
116- fn valid_account_state_proof_with_empty_pub_input_does_not_verify ( ) {
117114 let mut proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
118115 let proof_size = PROOF_BYTES . len ( ) ;
119116 assert ! ( proof_size <= proof_buffer. len( ) ) ;
120117 proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
121118
122- let pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
119+ let mut pub_input_buffer = [ 0u8 ; PUB_INPUT_BYTES . len ( ) ] ;
123120 let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
121+ assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
122+ pub_input_buffer[ ..pub_input_size] . clone_from_slice ( PUB_INPUT_BYTES ) ;
124123
125124 let result = verify_account_inclusion_ffi (
126- & proof_buffer,
127- proof_size,
128- & pub_input_buffer,
129- pub_input_size,
125+ proof_buffer. as_ptr ( ) ,
126+ proof_size as u32 ,
127+ pub_input_buffer. as_ptr ( ) ,
128+ pub_input_size as u32 ,
130129 ) ;
131- assert ! ( ! result) ;
130+ assert_eq ! ( result, 1 ) ;
132131 }
133132
134133 #[ test]
135- fn valid_account_state_proof_with_greater_proof_size_does_not_verify ( ) {
136- let mut proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
137- let wrong_proof_size = MAX_PROOF_SIZE + 1 ;
138- proof_buffer[ ..PROOF_BYTES . len ( ) ] . clone_from_slice ( PROOF_BYTES ) ;
139-
140- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
141- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
142- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
143- pub_input_buffer[ ..pub_input_size] . clone_from_slice ( PUB_INPUT_BYTES ) ;
134+ fn empty_account_state_proof_does_not_verify ( ) {
135+ const PROOF_SIZE : usize = PROOF_BYTES . len ( ) ;
136+ let proof_buffer = [ 0u8 ; PROOF_SIZE ] ;
144137
145138 let result = verify_account_inclusion_ffi (
146- & proof_buffer,
147- wrong_proof_size ,
148- & pub_input_buffer ,
149- pub_input_size ,
139+ proof_buffer. as_ptr ( ) ,
140+ PROOF_SIZE as u32 ,
141+ PUB_INPUT_BYTES . as_ptr ( ) ,
142+ PUB_INPUT_BYTES . len ( ) as u32 ,
150143 ) ;
151- assert ! ( ! result) ;
144+ assert_eq ! ( result, 0 ) ;
152145 }
153146
154147 #[ test]
155- fn valid_account_state_proof_with_greater_pub_input_size_does_not_verify ( ) {
156- let mut proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
157- let proof_size = PROOF_BYTES . len ( ) ;
158- assert ! ( proof_size <= proof_buffer. len( ) ) ;
159- proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
160-
161- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
162- let wrong_pub_input_size = MAX_PUB_INPUT_SIZE + 1 ;
163- pub_input_buffer[ ..PUB_INPUT_BYTES . len ( ) ] . clone_from_slice ( PUB_INPUT_BYTES ) ;
148+ fn valid_account_state_proof_with_empty_pub_input_does_not_verify ( ) {
149+ const PUB_INPUT_SIZE : usize = PUB_INPUT_BYTES . len ( ) ;
150+ let pub_input_buffer = [ 0u8 ; PUB_INPUT_SIZE ] ;
164151
165152 let result = verify_account_inclusion_ffi (
166- & proof_buffer ,
167- proof_size ,
168- & pub_input_buffer,
169- wrong_pub_input_size ,
153+ PROOF_BYTES . as_ptr ( ) ,
154+ PROOF_BYTES . len ( ) as u32 ,
155+ pub_input_buffer. as_ptr ( ) ,
156+ PUB_INPUT_SIZE as u32 ,
170157 ) ;
171- assert ! ( ! result) ;
158+ assert_eq ! ( result, 0 ) ;
172159 }
173160}
0 commit comments