@@ -37,9 +37,25 @@ lazy_static! {
3737#[ no_mangle]
3838pub extern "C" fn verify_mina_state_ffi (
3939 proof_bytes : * const u8 ,
40- proof_len : usize ,
40+ proof_len : u32 ,
4141 pub_input_bytes : * const u8 ,
42- pub_input_len : usize ,
42+ pub_input_len : u32 ,
43+ ) -> i32 {
44+ let result = std:: panic:: catch_unwind ( || {
45+ inner_verify_mina_state_ffi ( proof_bytes, proof_len, pub_input_bytes, pub_input_len)
46+ } ) ;
47+
48+ match result {
49+ Ok ( v) => v as i32 ,
50+ Err ( _) => -1 ,
51+ }
52+ }
53+
54+ fn inner_verify_mina_state_ffi (
55+ proof_bytes : * const u8 ,
56+ proof_len : u32 ,
57+ pub_input_bytes : * const u8 ,
58+ pub_input_len : u32 ,
4359) -> bool {
4460 if proof_bytes. is_null ( ) || pub_input_bytes. is_null ( ) {
4561 error ! ( "Input buffer null" ) ;
@@ -233,105 +249,51 @@ mod test {
233249
234250 #[ test]
235251 fn valid_mina_state_proof_verifies ( ) {
236- let mut proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
237- let proof_size = PROOF_BYTES . len ( ) ;
238- assert ! ( proof_size <= proof_buffer. len( ) ) ;
239- proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
240-
241- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
242- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
243- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
244- pub_input_buffer[ ..pub_input_size] . clone_from_slice ( PUB_INPUT_BYTES ) ;
245-
246- let result =
247- verify_mina_state_ffi ( & proof_buffer, proof_size, & pub_input_buffer, pub_input_size) ;
248- assert ! ( result) ;
252+ let result = verify_mina_state_ffi (
253+ PROOF_BYTES . as_ptr ( ) ,
254+ PROOF_BYTES . len ( ) as u32 ,
255+ PUB_INPUT_BYTES . as_ptr ( ) ,
256+ PUB_INPUT_BYTES . len ( ) as u32 ,
257+ ) ;
258+ assert_eq ! ( result, 1 ) ;
249259 }
250260
251261 #[ test]
252262 fn mina_state_proof_with_bad_bridge_tip_hash_does_not_verify ( ) {
253- let mut proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
254- let proof_size = PROOF_BYTES . len ( ) ;
255- assert ! ( proof_size <= proof_buffer. len( ) ) ;
256- proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
257-
258- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
259- let pub_input_size = BAD_HASH_PUB_INPUT_BYTES . len ( ) ;
260- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
261- pub_input_buffer[ ..pub_input_size] . clone_from_slice ( BAD_HASH_PUB_INPUT_BYTES ) ;
262-
263- let result =
264- verify_mina_state_ffi ( & proof_buffer, proof_size, & pub_input_buffer, pub_input_size) ;
265- assert ! ( !result) ;
263+ let result = verify_mina_state_ffi (
264+ PROOF_BYTES . as_ptr ( ) ,
265+ PROOF_BYTES . len ( ) as u32 ,
266+ BAD_HASH_PUB_INPUT_BYTES . as_ptr ( ) ,
267+ BAD_HASH_PUB_INPUT_BYTES . len ( ) as u32 ,
268+ ) ;
269+ assert_eq ! ( result, 0 ) ;
266270 }
267271
268272 #[ test]
269273 fn empty_mina_state_proof_does_not_verify ( ) {
270- let proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
271- let proof_size = PROOF_BYTES . len ( ) ;
272-
273- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
274- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
275- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
276- pub_input_buffer[ ..pub_input_size] . clone_from_slice ( PUB_INPUT_BYTES ) ;
277-
278- let result =
279- verify_mina_state_ffi ( & proof_buffer, proof_size, & pub_input_buffer, pub_input_size) ;
280- assert ! ( !result) ;
281- }
282-
283- #[ test]
284- fn valid_mina_state_proof_with_empty_pub_input_does_not_verify ( ) {
285- let mut proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
286- let proof_size = PROOF_BYTES . len ( ) ;
287- assert ! ( proof_size <= proof_buffer. len( ) ) ;
288- proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
289-
290- let pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
291- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
292-
293- let result =
294- verify_mina_state_ffi ( & proof_buffer, proof_size, & pub_input_buffer, pub_input_size) ;
295- assert ! ( !result) ;
296- }
297-
298- #[ test]
299- fn valid_mina_state_proof_with_greater_proof_size_does_not_verify ( ) {
300- let mut proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
301- let wrong_proof_size = PROOF_BYTES . len ( ) + 1 ;
302- proof_buffer[ ..PROOF_BYTES . len ( ) ] . clone_from_slice ( PROOF_BYTES ) ;
303-
304- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
305- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
306- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
307- pub_input_buffer[ ..pub_input_size] . clone_from_slice ( PUB_INPUT_BYTES ) ;
274+ const PROOF_SIZE : usize = PROOF_BYTES . len ( ) ;
275+ let empty_proof_buffer = [ 0u8 ; PROOF_SIZE ] ;
308276
309277 let result = verify_mina_state_ffi (
310- & proof_buffer ,
311- wrong_proof_size ,
312- & pub_input_buffer ,
313- pub_input_size ,
278+ empty_proof_buffer . as_ptr ( ) ,
279+ PROOF_SIZE as u32 ,
280+ PUB_INPUT_BYTES . as_ptr ( ) ,
281+ PUB_INPUT_BYTES . len ( ) as u32 ,
314282 ) ;
315- assert ! ( ! result) ;
283+ assert_eq ! ( result, 0 ) ;
316284 }
317285
318286 #[ test]
319- fn valid_mina_state_proof_with_greater_pub_input_size_does_not_verify ( ) {
320- let mut proof_buffer = [ 0u8 ; PROOF_BYTES . len ( ) ] ;
321- let proof_size = PROOF_BYTES . len ( ) ;
322- assert ! ( proof_size <= proof_buffer. len( ) ) ;
323- proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
324-
325- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
326- let wrong_pub_input_size = MAX_PUB_INPUT_SIZE + 1 ;
327- pub_input_buffer[ ..PUB_INPUT_BYTES . len ( ) ] . clone_from_slice ( PUB_INPUT_BYTES ) ;
287+ fn valid_mina_state_proof_with_empty_pub_input_does_not_verify ( ) {
288+ const PUB_INPUT_SIZE : usize = PUB_INPUT_BYTES . len ( ) ;
289+ let empty_pub_input_buffer = [ 0u8 ; PUB_INPUT_SIZE ] ;
328290
329291 let result = verify_mina_state_ffi (
330- & proof_buffer ,
331- proof_size ,
332- & pub_input_buffer ,
333- wrong_pub_input_size ,
292+ PROOF_BYTES . as_ptr ( ) ,
293+ PROOF_BYTES . len ( ) as u32 ,
294+ empty_pub_input_buffer . as_ptr ( ) ,
295+ PUB_INPUT_SIZE as u32 ,
334296 ) ;
335- assert ! ( ! result) ;
297+ assert_eq ! ( result, 0 ) ;
336298 }
337299}
0 commit comments