Skip to content

Commit 7318bc2

Browse files
author
Julian Ventura
committed
Add panic catch on operator calling verify_sp1_proof ffi
1 parent dca81e0 commit 7318bc2

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

operator/pkg/operator.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,8 @@ func (o *Operator) verify(verificationData VerificationData, results chan bool)
475475
results <- verificationResult
476476

477477
case common.SP1:
478-
verificationResult := sp1.VerifySp1Proof(verificationData.Proof, verificationData.VmProgramCode)
479-
o.Logger.Infof("SP1 proof verification result: %t", verificationResult)
480-
results <- verificationResult
478+
verificationResult, err := sp1.VerifySp1Proof(verificationData.Proof, verificationData.VmProgramCode)
479+
o.handleVerificationResult(results, verificationResult, err, "SP1 proof verification")
481480

482481
case common.Risc0:
483482
verificationResult := risc_zero.VerifyRiscZeroReceipt(verificationData.Proof,
@@ -491,6 +490,16 @@ func (o *Operator) verify(verificationData VerificationData, results chan bool)
491490
}
492491
}
493492

493+
func (o *Operator) handleVerificationResult(results chan bool, isVerified bool, err error, name string) {
494+
if err != nil {
495+
o.Logger.Errorf("%v failed %v", name, err)
496+
results <- false
497+
} else {
498+
o.Logger.Infof("%v result: %t", name, isVerified)
499+
results <- isVerified
500+
}
501+
}
502+
494503
// VerifyPlonkProofBLS12_381 verifies a PLONK proof using BLS12-381 curve.
495504
func (o *Operator) verifyPlonkProofBLS12_381(proofBytes []byte, pubInputBytes []byte, verificationKeyBytes []byte) bool {
496505
return o.verifyPlonkProof(proofBytes, pubInputBytes, verificationKeyBytes, ecc.BLS12_381)

operator/sp1/lib/sp1.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <stdbool.h>
22
#include <stdint.h>
33

4-
bool verify_sp1_proof_ffi(unsigned char *proof_buffer, uint32_t proof_len,
4+
int32_t verify_sp1_proof_ffi(unsigned char *proof_buffer, uint32_t proof_len,
55
unsigned char *elf_buffer, uint32_t elf_len);

operator/sp1/lib/src/lib.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ lazy_static! {
77
}
88

99
#[no_mangle]
10-
pub extern "C" fn verify_sp1_proof_ffi(
10+
extern "C" fn inner_verify_sp1_proof_ffi(
1111
proof_bytes: *const u8,
1212
proof_len: u32,
1313
elf_bytes: *const u8,
@@ -35,6 +35,23 @@ pub extern "C" fn verify_sp1_proof_ffi(
3535
false
3636
}
3737

38+
#[no_mangle]
39+
pub extern "C" fn verify_sp1_proof_ffi(
40+
proof_bytes: *const u8,
41+
proof_len: u32,
42+
elf_bytes: *const u8,
43+
elf_len: u32,
44+
) -> i32 {
45+
let result = std::panic::catch_unwind(|| {
46+
inner_verify_sp1_proof_ffi(proof_bytes, proof_len, elf_bytes, elf_len)
47+
});
48+
49+
match result {
50+
Ok(v) => v as i32,
51+
Err(_) => -1,
52+
}
53+
}
54+
3855
#[cfg(test)]
3956
mod tests {
4057
use super::*;
@@ -49,7 +66,7 @@ mod tests {
4966

5067
let result =
5168
verify_sp1_proof_ffi(proof_bytes, PROOF.len() as u32, elf_bytes, ELF.len() as u32);
52-
assert!(result)
69+
assert_eq!(result, 1)
5370
}
5471

5572
#[test]
@@ -63,6 +80,6 @@ mod tests {
6380
elf_bytes,
6481
ELF.len() as u32,
6582
);
66-
assert!(!result)
83+
assert_eq!(result, 0)
6784
}
6885
}

operator/sp1/sp1.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,35 @@ package sp1
88
*/
99
import "C"
1010
import "unsafe"
11+
import "fmt"
1112

12-
func VerifySp1Proof(proofBuffer []byte, elfBuffer []byte) bool {
13+
func VerifySp1Proof(proofBuffer []byte, elfBuffer []byte) (isVerified bool, err error) {
14+
// Here we define the return value on failure
15+
isVerified = false
16+
err = nil
1317
if len(proofBuffer) == 0 || len(elfBuffer) == 0 {
14-
return false
18+
return isVerified, err
1519
}
1620

21+
// This will catch any go panic
22+
defer func() {
23+
rec := recover()
24+
if rec != nil {
25+
err = fmt.Errorf("Panic was caught while verifying sp1 proof: %s", rec)
26+
}
27+
}()
28+
1729
proofPtr := (*C.uchar)(unsafe.Pointer(&proofBuffer[0]))
1830
elfPtr := (*C.uchar)(unsafe.Pointer(&elfBuffer[0]))
1931

20-
return (bool)(C.verify_sp1_proof_ffi(proofPtr, (C.uint32_t)(len(proofBuffer)), elfPtr, (C.uint32_t)(len(elfBuffer))))
32+
r := (C.int32_t)(C.verify_sp1_proof_ffi(proofPtr, (C.uint32_t)(len(proofBuffer)), elfPtr, (C.uint32_t)(len(elfBuffer))))
33+
34+
if r == -1 {
35+
err = fmt.Errorf("Panic happened on FFI while verifying sp1 proof")
36+
return isVerified, err
37+
}
38+
39+
isVerified = true
40+
41+
return isVerified, err
2142
}

0 commit comments

Comments
 (0)