@@ -34,35 +34,56 @@ lazy_static! {
3434 static ref MINA_SRS : SRS <Vesta > = SRS :: <Vesta >:: create( Fq :: SRS_DEPTH ) ;
3535}
3636
37- // TODO(xqft): check proof size
38- const MAX_PROOF_SIZE : usize = 48 * 1024 ;
39- const MAX_PUB_INPUT_SIZE : usize = 6 * 1024 ;
40-
4137#[ no_mangle]
4238pub extern "C" fn verify_mina_state_ffi (
43- proof_buffer : & [ u8 ; MAX_PROOF_SIZE ] ,
44- proof_len : usize ,
45- pub_input_buffer : & [ u8 ; MAX_PUB_INPUT_SIZE ] ,
46- pub_input_len : usize ,
39+ proof_bytes : * const u8 ,
40+ proof_len : u32 ,
41+ pub_input_bytes : * const u8 ,
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 ,
4759) -> bool {
48- let Some ( proof_buffer_slice ) = proof_buffer . get ( ..proof_len ) else {
49- error ! ( "Proof length argument is greater than max proof size " ) ;
60+ if proof_bytes . is_null ( ) || pub_input_bytes . is_null ( ) {
61+ error ! ( "Input buffer null " ) ;
5062 return false ;
51- } ;
63+ }
5264
53- let Some ( pub_input_buffer_slice ) = pub_input_buffer . get ( .. pub_input_len) else {
54- error ! ( "Public input length argument is greater than max public input size" ) ;
65+ if proof_len == 0 || pub_input_len == 0 {
66+ error ! ( "Input buffer length zero size" ) ;
5567 return false ;
56- } ;
68+ }
69+
70+ let proof_bytes = unsafe { std:: slice:: from_raw_parts ( proof_bytes, proof_len as usize ) } ;
71+
72+ let pub_input_bytes =
73+ unsafe { std:: slice:: from_raw_parts ( pub_input_bytes, pub_input_len as usize ) } ;
74+
75+ verify_mina_state ( proof_bytes, pub_input_bytes)
76+ }
5777
58- let proof: MinaStateProof = match bincode:: deserialize ( proof_buffer_slice) {
78+ pub fn verify_mina_state ( proof_bytes : & [ u8 ] , pub_input_bytes : & [ u8 ] ) -> bool {
79+ let proof: MinaStateProof = match bincode:: deserialize ( proof_bytes) {
5980 Ok ( proof) => proof,
6081 Err ( err) => {
6182 error ! ( "Failed to deserialize state proof: {}" , err) ;
6283 return false ;
6384 }
6485 } ;
65- let pub_inputs: MinaStatePubInputs = match bincode:: deserialize ( pub_input_buffer_slice ) {
86+ let pub_inputs: MinaStatePubInputs = match bincode:: deserialize ( pub_input_bytes ) {
6687 Ok ( pub_inputs) => pub_inputs,
6788 Err ( err) => {
6889 error ! ( "Failed to deserialize state pub inputs: {}" , err) ;
@@ -228,105 +249,51 @@ mod test {
228249
229250 #[ test]
230251 fn valid_mina_state_proof_verifies ( ) {
231- let mut proof_buffer = [ 0u8 ; super :: MAX_PROOF_SIZE ] ;
232- let proof_size = PROOF_BYTES . len ( ) ;
233- assert ! ( proof_size <= proof_buffer. len( ) ) ;
234- proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
235-
236- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
237- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
238- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
239- pub_input_buffer[ ..pub_input_size] . clone_from_slice ( PUB_INPUT_BYTES ) ;
240-
241- let result =
242- verify_mina_state_ffi ( & proof_buffer, proof_size, & pub_input_buffer, pub_input_size) ;
243- 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 ) ;
244259 }
245260
246261 #[ test]
247262 fn mina_state_proof_with_bad_bridge_tip_hash_does_not_verify ( ) {
248- let mut proof_buffer = [ 0u8 ; super :: MAX_PROOF_SIZE ] ;
249- let proof_size = PROOF_BYTES . len ( ) ;
250- assert ! ( proof_size <= proof_buffer. len( ) ) ;
251- proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
252-
253- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
254- let pub_input_size = BAD_HASH_PUB_INPUT_BYTES . len ( ) ;
255- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
256- pub_input_buffer[ ..pub_input_size] . clone_from_slice ( BAD_HASH_PUB_INPUT_BYTES ) ;
257-
258- let result =
259- verify_mina_state_ffi ( & proof_buffer, proof_size, & pub_input_buffer, pub_input_size) ;
260- 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 ) ;
261270 }
262271
263272 #[ test]
264273 fn empty_mina_state_proof_does_not_verify ( ) {
265- let proof_buffer = [ 0u8 ; super :: MAX_PROOF_SIZE ] ;
266- let proof_size = PROOF_BYTES . len ( ) ;
267-
268- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
269- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
270- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
271- pub_input_buffer[ ..pub_input_size] . clone_from_slice ( PUB_INPUT_BYTES ) ;
272-
273- let result =
274- verify_mina_state_ffi ( & proof_buffer, proof_size, & pub_input_buffer, pub_input_size) ;
275- assert ! ( !result) ;
276- }
277-
278- #[ test]
279- fn valid_mina_state_proof_with_empty_pub_input_does_not_verify ( ) {
280- let mut proof_buffer = [ 0u8 ; super :: MAX_PROOF_SIZE ] ;
281- let proof_size = PROOF_BYTES . len ( ) ;
282- assert ! ( proof_size <= proof_buffer. len( ) ) ;
283- proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
284-
285- let pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
286- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
287-
288- let result =
289- verify_mina_state_ffi ( & proof_buffer, proof_size, & pub_input_buffer, pub_input_size) ;
290- assert ! ( !result) ;
291- }
292-
293- #[ test]
294- fn valid_mina_state_proof_with_greater_proof_size_does_not_verify ( ) {
295- let mut proof_buffer = [ 0u8 ; super :: MAX_PROOF_SIZE ] ;
296- let wrong_proof_size = super :: MAX_PROOF_SIZE + 1 ;
297- proof_buffer[ ..PROOF_BYTES . len ( ) ] . clone_from_slice ( PROOF_BYTES ) ;
298-
299- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
300- let pub_input_size = PUB_INPUT_BYTES . len ( ) ;
301- assert ! ( pub_input_size <= pub_input_buffer. len( ) ) ;
302- 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 ] ;
303276
304277 let result = verify_mina_state_ffi (
305- & proof_buffer ,
306- wrong_proof_size ,
307- & pub_input_buffer ,
308- 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 ,
309282 ) ;
310- assert ! ( ! result) ;
283+ assert_eq ! ( result, 0 ) ;
311284 }
312285
313286 #[ test]
314- fn valid_mina_state_proof_with_greater_pub_input_size_does_not_verify ( ) {
315- let mut proof_buffer = [ 0u8 ; super :: MAX_PROOF_SIZE ] ;
316- let proof_size = PROOF_BYTES . len ( ) ;
317- assert ! ( proof_size <= proof_buffer. len( ) ) ;
318- proof_buffer[ ..proof_size] . clone_from_slice ( PROOF_BYTES ) ;
319-
320- let mut pub_input_buffer = [ 0u8 ; super :: MAX_PUB_INPUT_SIZE ] ;
321- let wrong_pub_input_size = MAX_PUB_INPUT_SIZE + 1 ;
322- 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 ] ;
323290
324291 let result = verify_mina_state_ffi (
325- & proof_buffer ,
326- proof_size ,
327- & pub_input_buffer ,
328- 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 ,
329296 ) ;
330- assert ! ( ! result) ;
297+ assert_eq ! ( result, 0 ) ;
331298 }
332299}
0 commit comments