Skip to content

Commit 71d4756

Browse files
chore(ffi): Change ffi bindings to explicitly be uint32_t (#197)
Co-authored-by: Mariano A. Nicolini <mariano.nicolini.91@gmail.com>
1 parent 04cc1bc commit 71d4756

9 files changed

Lines changed: 24 additions & 22 deletions

File tree

operator/pkg/operator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ func (o *Operator) verify(verificationData VerificationData, results chan bool)
214214
case common.SP1:
215215
proofBytes := make([]byte, sp1.MaxProofSize)
216216
copy(proofBytes, verificationData.Proof)
217-
proofLen := (uint)(len(verificationData.Proof))
217+
proofLen := (uint32)(len(verificationData.Proof))
218218

219219
elf := verificationData.VmProgramCode
220220
elfBytes := make([]byte, sp1.MaxElfBufferSize)
221221
copy(elfBytes, elf)
222-
elfLen := (uint)(len(elf))
222+
elfLen := (uint32)(len(elf))
223223

224224
verificationResult := sp1.VerifySp1Proof(([sp1.MaxProofSize]byte)(proofBytes), proofLen, ([sp1.MaxElfBufferSize]byte)(elfBytes), elfLen)
225225
o.Logger.Infof("SP1 proof verification result: %t", verificationResult)

operator/risc_zero/lib/risc_zero.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#include <stdbool.h>
2+
#include <stdint.h>
23

3-
bool verify_risc_zero_receipt_ffi(unsigned char *receipt_bytes, unsigned int receipt_len, unsigned int *image_id);
4+
bool verify_risc_zero_receipt_ffi(unsigned char *receipt_bytes, uint32_t receipt_len, uint32_t *image_id);

operator/risc_zero/lib/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ pub const MAX_RECEIPT_SIZE: usize = 215523;
55
#[no_mangle]
66
pub extern "C" fn verify_risc_zero_receipt_ffi(
77
receipt_bytes: &[u8; MAX_RECEIPT_SIZE],
8-
receipt_len: usize,
8+
receipt_len: u32,
99
image_id: &[u32; 8],
1010
) -> bool {
11-
if let Ok(receipt) = bincode::deserialize::<Receipt>(&receipt_bytes[..receipt_len]) {
11+
if let Ok(receipt) = bincode::deserialize::<Receipt>(&receipt_bytes[..receipt_len as usize]) {
1212
return receipt.verify(*image_id).is_ok();
1313
}
1414
false
@@ -24,7 +24,7 @@ mod tests {
2424

2525
#[test]
2626
fn verify_risc_zero_receipt_with_image_id_works() {
27-
const RECEIPT_SIZE: usize = RECEIPT.len();
27+
const RECEIPT_SIZE: u32 = RECEIPT.len() as u32;
2828
let mut receipt_buffer = [0u8; super::MAX_RECEIPT_SIZE];
2929
receipt_buffer[..RECEIPT_SIZE].clone_from_slice(RECEIPT);
3030

@@ -36,7 +36,7 @@ mod tests {
3636

3737
#[test]
3838
fn verify_risc_zero_aborts_with_bad_proof() {
39-
const RECEIPT_SIZE: usize = RECEIPT.len();
39+
const RECEIPT_SIZE: u32 = RECEIPT.len() as u32;
4040
let mut receipt_buffer = [42u8; super::MAX_RECEIPT_SIZE];
4141
receipt_buffer[..RECEIPT_SIZE].clone_from_slice(RECEIPT);
4242

operator/risc_zero/risc_zero.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const (
1313
MaxReceiptSize = 215523
1414
)
1515

16-
func VerifyRiscZeroReceipt(receiptBuffer [MaxReceiptSize]byte, receiptLen uint, imageIdBuffer [8]uint32) bool {
16+
func VerifyRiscZeroReceipt(receiptBuffer [MaxReceiptSize]byte, receiptLen uint32, imageIdBuffer [8]uint32) bool {
1717
receiptPtr := (*C.uchar)(unsafe.Pointer(&receiptBuffer[0]))
18-
imageIdPtr := (*C.uint)(unsafe.Pointer(&imageIdBuffer[0]))
19-
return (bool)(C.verify_risc_zero_receipt_ffi(receiptPtr, (C.uint)(receiptLen), imageIdPtr))
18+
imageIdPtr := (*C.uint32_t)(unsafe.Pointer(&imageIdBuffer[0]))
19+
return (bool)(C.verify_risc_zero_receipt_ffi(receiptPtr, (C.uint32_t)(receiptLen), imageIdPtr))
2020
}

operator/risc_zero/risc_zero_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestFibonacciRiscZeroProofVerifies(t *testing.T) {
2121

2222
imageId := getImageIdsFromFile(t, "../../task_sender/test_examples/risc_zero/fibonacci_proof_generator/fibonacci_id.txt")
2323

24-
if !risc_zero.VerifyRiscZeroReceipt(([risc_zero.MaxReceiptSize]byte)(receiptBytes), uint(nReadReceiptBytes), ([8]uint32)(imageId)) {
24+
if !risc_zero.VerifyRiscZeroReceipt(([risc_zero.MaxReceiptSize]byte)(receiptBytes), uint32(nReadReceiptBytes), ([8]uint32)(imageId)) {
2525
t.Errorf("proof did not verify")
2626
}
2727
}

operator/sp1/lib/sp1.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdbool.h>
2+
#include <stdint.h>
23

3-
bool verify_sp1_proof_ffi(unsigned char *proof_buffer, unsigned int proof_len,
4-
unsigned char *elf_buffer, unsigned int elf_len);
4+
bool verify_sp1_proof_ffi(unsigned char *proof_buffer, uint32_t proof_len,
5+
unsigned char *elf_buffer, uint32_t elf_len);

operator/sp1/lib/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ lazy_static! {
1010
#[no_mangle]
1111
pub extern "C" fn verify_sp1_proof_ffi(
1212
proof_bytes: &[u8; MAX_PROOF_SIZE],
13-
proof_len: usize,
13+
proof_len: u32,
1414
elf_bytes: &[u8; MAX_ELF_BUFFER_SIZE],
15-
elf_len: usize,
15+
elf_len: u32,
1616
) -> bool {
17-
let real_elf = &elf_bytes[0..elf_len];
17+
let real_elf = &elf_bytes[0..(elf_len as usize)];
1818

19-
if let Ok(proof) = bincode::deserialize(&proof_bytes[..proof_len]) {
19+
if let Ok(proof) = bincode::deserialize(&proof_bytes[..(proof_len as usize)]) {
2020
let (_pk, vk) = PROVER_CLIENT.setup(real_elf);
2121
return PROVER_CLIENT.verify(&proof, &vk).is_ok();
2222
}
@@ -43,7 +43,7 @@ mod tests {
4343
let elf_size = ELF.len();
4444
elf_buffer[..elf_size].clone_from_slice(ELF);
4545

46-
let result = verify_sp1_proof_ffi(&proof_buffer, proof_size, &elf_buffer, elf_size);
46+
let result = verify_sp1_proof_ffi(&proof_buffer, proof_size as u32, &elf_buffer, elf_size as u32);
4747
assert!(result)
4848
}
4949

@@ -57,7 +57,7 @@ mod tests {
5757
let elf_size = ELF.len();
5858
elf_buffer[..elf_size].clone_from_slice(ELF);
5959

60-
let result = verify_sp1_proof_ffi(&proof_buffer, proof_size - 1, &elf_buffer, elf_size);
60+
let result = verify_sp1_proof_ffi(&proof_buffer, (proof_size - 1) as u32, &elf_buffer, elf_size as u32);
6161
assert!(!result)
6262
}
6363
}

operator/sp1/sp1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const MaxProofSize = 2 * 1024 * 1024
1515
// MaxElfBufferSize 1 MB
1616
const MaxElfBufferSize = 1024 * 1024
1717

18-
func VerifySp1Proof(proofBuffer [MaxProofSize]byte, proofLen uint, elfBuffer [MaxElfBufferSize]byte, elfLen uint) bool {
18+
func VerifySp1Proof(proofBuffer [MaxProofSize]byte, proofLen uint32, elfBuffer [MaxElfBufferSize]byte, elfLen uint32) bool {
1919
proofPtr := (*C.uchar)(unsafe.Pointer(&proofBuffer[0]))
2020
elfPtr := (*C.uchar)(unsafe.Pointer(&elfBuffer[0]))
21-
return (bool)(C.verify_sp1_proof_ffi(proofPtr, (C.uint)(proofLen), elfPtr, (C.uint)(elfLen)))
21+
return (bool)(C.verify_sp1_proof_ffi(proofPtr, (C.uint32_t)(proofLen), elfPtr, (C.uint32_t)(elfLen)))
2222
}

operator/sp1/sp1_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestFibonacciSp1ProofVerifies(t *testing.T) {
2828
t.Errorf("could not read bytes from file")
2929
}
3030

31-
if !sp1.VerifySp1Proof(([sp1.MaxProofSize]byte)(proofBytes), uint(nReadProofBytes), ([sp1.MaxElfBufferSize]byte)(elfBytes), uint(nReadElfBytes)) {
31+
if !sp1.VerifySp1Proof(([sp1.MaxProofSize]byte)(proofBytes), uint32(nReadProofBytes), ([sp1.MaxElfBufferSize]byte)(elfBytes), uint32(nReadElfBytes)) {
3232
t.Errorf("proof did not verify")
3333
}
3434
}

0 commit comments

Comments
 (0)