2025-01-30 17:41:51 -08:00
|
|
|
//! Rust bindings for rapidsnark proving.
|
|
|
|
|
//!
|
|
|
|
|
//! Prebuilt binaries are provided for the following platforms:
|
|
|
|
|
//! - aarch64-apple-ios
|
|
|
|
|
//! - aarch64-apple-ios-sim
|
|
|
|
|
//! - x86_64-apple-ios
|
2025-02-05 11:51:47 +08:00
|
|
|
//! - aarch64-apple-darwin
|
|
|
|
|
//! - x86_64-apple-darwin
|
|
|
|
|
//! - aarch64-linux-android
|
|
|
|
|
//! - x86_64-linux-android
|
|
|
|
|
//! - x86_64 linux
|
|
|
|
|
//! - arm64 linux
|
2025-01-30 17:41:51 -08:00
|
|
|
//!
|
|
|
|
|
//! If a specific target is not included the sysytem will fallback to
|
|
|
|
|
//! the generic architecture, which may cause problems. e.g. if you compile
|
|
|
|
|
//! for aarch64-linux-generic, the system will fallback to aarch64.
|
|
|
|
|
//!
|
|
|
|
|
|
2025-01-29 14:45:49 -08:00
|
|
|
use std::collections::HashMap;
|
2025-01-29 14:42:27 -08:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use num_bigint::BigInt;
|
|
|
|
|
|
2025-01-30 17:41:51 -08:00
|
|
|
/// A function that converts named inputs to a full witness. This should be generated using e.g.
|
|
|
|
|
/// [rust-witness](https://crates.io/crates/rust-witness).
|
2025-01-29 14:42:27 -08:00
|
|
|
pub type WtnsFn = fn(HashMap<String, Vec<BigInt>>) -> Vec<BigInt>;
|
|
|
|
|
|
2025-01-30 17:41:51 -08:00
|
|
|
/// A structure representing a proof and public signals.
|
2025-01-31 16:48:04 +08:00
|
|
|
#[derive(Debug)]
|
2025-01-29 14:42:27 -08:00
|
|
|
pub struct ProofResult {
|
2025-01-31 16:48:04 +08:00
|
|
|
pub proof: String,
|
|
|
|
|
pub public_signals: String,
|
2025-01-29 14:42:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
2025-01-31 16:48:04 +08:00
|
|
|
pub fn groth16_prover_zkey_file(
|
2025-01-31 10:00:58 +08:00
|
|
|
zkey_file_path: *const std::os::raw::c_char,
|
|
|
|
|
wtns_buffer: *const std::os::raw::c_void,
|
|
|
|
|
wtns_size: std::ffi::c_ulong,
|
|
|
|
|
proof_buffer: *mut std::os::raw::c_char,
|
|
|
|
|
proof_size: *mut std::ffi::c_ulong,
|
|
|
|
|
public_buffer: *mut std::os::raw::c_char,
|
|
|
|
|
public_size: *mut std::ffi::c_ulong,
|
|
|
|
|
error_msg: *mut std::os::raw::c_char,
|
|
|
|
|
error_msg_maxsize: std::ffi::c_ulong,
|
|
|
|
|
) -> i32;
|
2025-01-31 16:48:04 +08:00
|
|
|
|
|
|
|
|
pub fn groth16_verify(
|
|
|
|
|
proof: *const std::os::raw::c_char,
|
|
|
|
|
inputs: *const std::os::raw::c_char,
|
|
|
|
|
verification_key: *const std::os::raw::c_char,
|
|
|
|
|
error_msg: *mut std::os::raw::c_char,
|
|
|
|
|
error_msg_maxsize: std::ffi::c_ulong,
|
|
|
|
|
) -> i32;
|
2025-01-29 14:42:27 -08:00
|
|
|
}
|
|
|
|
|
|
2025-01-31 10:00:58 +08:00
|
|
|
use num_traits::ops::bytes::ToBytes;
|
|
|
|
|
use std::io::{self};
|
|
|
|
|
|
|
|
|
|
/// Parse bigints to `wtns` format.<br/>
|
|
|
|
|
/// Reference: [witnesscalc/src/witnesscalc.cpp](https://github.com/0xPolygonID/witnesscalc/blob/4a789880727aa0df50f1c4ef78ec295f5a30a15e/src/witnesscalc.cpp)
|
|
|
|
|
pub fn parse_bigints_to_witness(bigints: Vec<BigInt>) -> io::Result<Vec<u8>> {
|
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
|
let version: u32 = 2;
|
|
|
|
|
let n_sections: u32 = 2;
|
|
|
|
|
let n8: u32 = 32;
|
|
|
|
|
let q = BigInt::from_str(
|
|
|
|
|
"21888242871839275222246405745257275088548364400416034343698204186575808495617",
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let n_witness_values: u32 = bigints.len() as u32;
|
|
|
|
|
|
|
|
|
|
// Write the format bytes (4 bytes)
|
|
|
|
|
let wtns_format = "wtns".as_bytes();
|
|
|
|
|
buffer.extend_from_slice(wtns_format);
|
|
|
|
|
|
|
|
|
|
// Write version (4 bytes)
|
|
|
|
|
buffer.extend_from_slice(&version.to_le_bytes());
|
|
|
|
|
|
|
|
|
|
// Write number of sections (4 bytes)
|
|
|
|
|
buffer.extend_from_slice(&n_sections.to_le_bytes());
|
|
|
|
|
|
|
|
|
|
// Iterate through sections to write the data
|
|
|
|
|
// Section 1 (Field parameters)
|
|
|
|
|
let section_id_1: u32 = 1;
|
|
|
|
|
let section_length_1: u64 = 8 + n8 as u64;
|
|
|
|
|
buffer.extend_from_slice(§ion_id_1.to_le_bytes());
|
|
|
|
|
buffer.extend_from_slice(§ion_length_1.to_le_bytes());
|
|
|
|
|
|
|
|
|
|
// Write n8 (4 bytes), q (32 bytes), and n_witness_values (4 bytes)
|
|
|
|
|
buffer.extend_from_slice(&n8.to_le_bytes());
|
|
|
|
|
buffer.extend_from_slice(&q.to_signed_bytes_le());
|
|
|
|
|
buffer.extend_from_slice(&n_witness_values.to_le_bytes());
|
|
|
|
|
|
|
|
|
|
// Section 2 (Witness data)
|
|
|
|
|
let section_id_2: u32 = 2;
|
|
|
|
|
let section_length_2: u64 = bigints.len() as u64 * n8 as u64; // Witness data size
|
|
|
|
|
buffer.extend_from_slice(§ion_id_2.to_le_bytes());
|
|
|
|
|
buffer.extend_from_slice(§ion_length_2.to_le_bytes());
|
|
|
|
|
|
|
|
|
|
// Write the witness data (each BigInt to n8 bytes)
|
|
|
|
|
for bigint in bigints {
|
|
|
|
|
let mut bytes = bigint.to_le_bytes();
|
|
|
|
|
bytes.resize(n8 as usize, 0); // Ensure each BigInt is padded to n8 bytes
|
|
|
|
|
buffer.extend_from_slice(&bytes);
|
2025-01-29 14:42:27 -08:00
|
|
|
}
|
2025-01-31 10:00:58 +08:00
|
|
|
|
|
|
|
|
// Return the buffer containing the complete witness data
|
|
|
|
|
Ok(buffer)
|
2025-01-29 14:42:27 -08:00
|
|
|
}
|
|
|
|
|
|
2025-01-31 10:00:58 +08:00
|
|
|
/// Wrapper for `groth16_prover_zkey_file`
|
|
|
|
|
pub fn groth16_prover_zkey_file_wrapper(
|
2025-01-29 14:42:27 -08:00
|
|
|
zkey_path: &str,
|
2025-01-31 10:00:58 +08:00
|
|
|
wtns_buffer: Vec<u8>,
|
|
|
|
|
) -> Result<ProofResult> {
|
2025-03-06 12:01:51 +08:00
|
|
|
let formatted_zkey_path = std::ffi::CString::new(zkey_path).unwrap();
|
2025-01-31 10:00:58 +08:00
|
|
|
let wtns_size = wtns_buffer.len() as u64;
|
2025-01-29 14:42:27 -08:00
|
|
|
|
2025-01-31 10:00:58 +08:00
|
|
|
let mut proof_buffer = vec![0u8; 4 * 1024 * 1024]; // Adjust size as needed
|
|
|
|
|
let mut proof_size: u64 = 4 * 1024 * 1024;
|
|
|
|
|
let proof_ptr = proof_buffer.as_mut_ptr() as *mut std::ffi::c_char;
|
2025-01-29 14:42:27 -08:00
|
|
|
|
2025-01-31 10:00:58 +08:00
|
|
|
let mut public_buffer = vec![0u8; 4 * 1024 * 1024]; // Adjust size as needed
|
|
|
|
|
let mut public_size: u64 = 4 * 1024 * 1024;
|
|
|
|
|
let public_ptr = public_buffer.as_mut_ptr() as *mut std::ffi::c_char;
|
2025-01-29 14:42:27 -08:00
|
|
|
|
2025-01-31 10:00:58 +08:00
|
|
|
let mut error_msg = vec![0u8; 256]; // Error message buffer
|
|
|
|
|
let error_msg_ptr = error_msg.as_mut_ptr() as *mut std::ffi::c_char;
|
2025-01-29 14:42:27 -08:00
|
|
|
|
2025-01-31 10:00:58 +08:00
|
|
|
unsafe {
|
|
|
|
|
let result = groth16_prover_zkey_file(
|
2025-03-06 11:32:22 +08:00
|
|
|
formatted_zkey_path.as_ptr() as *const std::ffi::c_char,
|
2025-01-31 10:00:58 +08:00
|
|
|
wtns_buffer.as_ptr() as *const std::os::raw::c_void, // Witness buffer
|
|
|
|
|
wtns_size,
|
|
|
|
|
proof_ptr,
|
|
|
|
|
&mut proof_size,
|
|
|
|
|
public_ptr,
|
|
|
|
|
&mut public_size,
|
|
|
|
|
error_msg_ptr,
|
|
|
|
|
error_msg.len() as u64,
|
|
|
|
|
);
|
|
|
|
|
if result != 0 {
|
|
|
|
|
let error_string = std::ffi::CStr::from_ptr(error_msg_ptr)
|
|
|
|
|
.to_string_lossy()
|
|
|
|
|
.into_owned();
|
|
|
|
|
return Err(anyhow::anyhow!("Proof generation failed: {}", error_string));
|
|
|
|
|
}
|
2025-01-29 14:42:27 -08:00
|
|
|
// Convert both strings
|
2025-01-31 10:00:58 +08:00
|
|
|
let proof = std::ffi::CStr::from_ptr(proof_ptr)
|
2025-01-29 14:42:27 -08:00
|
|
|
.to_string_lossy()
|
|
|
|
|
.into_owned();
|
2025-01-31 10:00:58 +08:00
|
|
|
let public_signals = std::ffi::CStr::from_ptr(public_ptr)
|
2025-01-29 14:42:27 -08:00
|
|
|
.to_string_lossy()
|
|
|
|
|
.into_owned();
|
2025-01-31 10:00:58 +08:00
|
|
|
Ok(ProofResult {
|
|
|
|
|
proof,
|
|
|
|
|
public_signals,
|
|
|
|
|
})
|
2025-01-29 14:42:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-31 16:48:04 +08:00
|
|
|
/// Wrapper for `groth16_verify`
|
|
|
|
|
pub fn groth16_verify_wrapper(proof: &str, inputs: &str, verification_key: &str) -> Result<bool> {
|
2025-01-31 17:30:27 +08:00
|
|
|
let proof_cstr = std::ffi::CString::new(proof).unwrap();
|
|
|
|
|
let inputs_cstr = std::ffi::CString::new(inputs).unwrap();
|
|
|
|
|
let verification_key_cstr = std::ffi::CString::new(verification_key).unwrap();
|
|
|
|
|
|
2025-01-31 16:48:04 +08:00
|
|
|
let mut error_msg = vec![0u8; 256]; // Error message buffer
|
|
|
|
|
let error_msg_ptr = error_msg.as_mut_ptr() as *mut std::ffi::c_char;
|
|
|
|
|
unsafe {
|
|
|
|
|
let result = groth16_verify(
|
2025-01-31 17:30:27 +08:00
|
|
|
proof_cstr.as_ptr() as *const std::ffi::c_char,
|
|
|
|
|
inputs_cstr.as_ptr() as *const std::ffi::c_char,
|
|
|
|
|
verification_key_cstr.as_ptr() as *const std::ffi::c_char,
|
2025-01-31 16:48:04 +08:00
|
|
|
error_msg_ptr,
|
|
|
|
|
error_msg.len() as u64,
|
|
|
|
|
);
|
|
|
|
|
if result == 2 {
|
|
|
|
|
let error_string = std::ffi::CStr::from_ptr(error_msg_ptr)
|
|
|
|
|
.to_string_lossy()
|
|
|
|
|
.into_owned();
|
|
|
|
|
return Err(anyhow::anyhow!(
|
|
|
|
|
"Proof verification failed: {}",
|
|
|
|
|
error_string
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
Ok(result == 0)
|
|
|
|
|
}
|
|
|
|
|
}
|