mirror of
https://github.com/logos-storage/circom-compat-ffi.git
synced 2026-01-03 21:33:07 +00:00
Merge bb8ef86fefabc15a3af778e49a0264a8ed745de2 into ddf956e310fbd170c01083404815e858aa616be0
This commit is contained in:
commit
2680c02f94
2
.gitignore
vendored
2
.gitignore
vendored
@ -5,7 +5,7 @@ target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
# Cargo.lock # otherwise things break
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
3167
Cargo.lock
generated
Normal file
3167
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -18,7 +18,7 @@ crate-type = [
|
||||
|
||||
[dependencies]
|
||||
|
||||
ark-circom = { git = "https://github.com/codex-storage/circom-compat.git#71f1ceb11aef27256", features = ["circom-2", "ethereum"]}
|
||||
ark-circom = { git = "https://github.com/codex-storage/circom-compat.git", rev = "71f1ceb11aef27256", features = ["circom-2", "ethereum"]}
|
||||
|
||||
ark-crypto-primitives = { version = "=0.4.0" }
|
||||
ark-ec = { version = "=0.4.1", default-features = false, features = ["parallel"] }
|
||||
|
||||
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[toolchain]
|
||||
channel = "1.73.0"
|
||||
51
src/ffi.rs
51
src/ffi.rs
@ -10,7 +10,7 @@ use ark_bn254::{Bn254, Fr};
|
||||
use ark_circom::{read_zkey, CircomBuilder, CircomConfig, CircomReduction};
|
||||
use ark_crypto_primitives::snark::SNARK;
|
||||
use ark_groth16::{prepare_verifying_key, Groth16, ProvingKey};
|
||||
use ark_std::rand::{rngs::ThreadRng, thread_rng};
|
||||
use ark_std::rand::thread_rng;
|
||||
use ruint::aliases::U256;
|
||||
|
||||
use crate::ffi_types::*;
|
||||
@ -35,22 +35,24 @@ pub const ERR_SERIALIZE_PROOF: i32 = 13;
|
||||
pub const ERR_SERIALIZE_INPUTS: i32 = 14;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct CircomBn254Cfg {
|
||||
#[repr(C)]
|
||||
pub struct CircomBn254Cfg {
|
||||
cfg: *mut CircomConfig<Bn254>,
|
||||
proving_key: *mut ProvingKey<Bn254>,
|
||||
_marker: core::marker::PhantomData<(*mut CircomBn254Cfg, core::marker::PhantomPinned)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct CircomBn254 {
|
||||
#[repr(C)]
|
||||
pub struct CircomBn254 {
|
||||
builder: *mut CircomBuilder<Bn254>,
|
||||
_marker: core::marker::PhantomData<(*mut CircomBn254, core::marker::PhantomPinned)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct CircomCompatCtx {
|
||||
#[repr(C)]
|
||||
pub struct CircomCompatCtx {
|
||||
circom: *mut CircomBn254,
|
||||
rng: ThreadRng,
|
||||
_marker: core::marker::PhantomData<(*mut CircomCompatCtx, core::marker::PhantomPinned)>,
|
||||
}
|
||||
|
||||
@ -65,7 +67,30 @@ fn to_err_code(result: Result<i32, Box<dyn Any + Send>>) -> i32 {
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn duplicate_circom_config(
|
||||
orig_cfg_ptr: *mut CircomBn254Cfg,
|
||||
cfg_ptr: &mut *mut CircomBn254Cfg,
|
||||
) -> i32 {
|
||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||
|
||||
let cfg = (*(*orig_cfg_ptr).cfg).clone();
|
||||
let proving_key = (*(*orig_cfg_ptr).proving_key).clone();
|
||||
|
||||
let circom_bn254_cfg = CircomBn254Cfg {
|
||||
cfg: Box::into_raw(Box::new(cfg)),
|
||||
proving_key: Box::into_raw(Box::new(proving_key)),
|
||||
_marker: std::marker::PhantomData,
|
||||
};
|
||||
|
||||
*cfg_ptr = Box::into_raw(Box::new(circom_bn254_cfg));
|
||||
|
||||
ERR_OK
|
||||
}));
|
||||
|
||||
to_err_code(result)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn init_circom_config_with_checks(
|
||||
r1cs_path: *const c_char,
|
||||
wasm_path: *const c_char,
|
||||
@ -127,7 +152,6 @@ pub unsafe extern "C" fn init_circom_config_with_checks(
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn init_circom_config(
|
||||
r1cs_path: *const c_char,
|
||||
wasm_path: *const c_char,
|
||||
@ -138,13 +162,11 @@ pub unsafe extern "C" fn init_circom_config(
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn init_circom_compat(
|
||||
cfg_ptr: *mut CircomBn254Cfg,
|
||||
ctx_ptr: &mut *mut CircomCompatCtx,
|
||||
) -> i32 {
|
||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||
let rng = thread_rng(); // TODO: use a shared rng - how?
|
||||
let builder = CircomBuilder::new((*(*cfg_ptr).cfg).clone()); // clone the config
|
||||
let circom_bn254 = CircomBn254 {
|
||||
builder: Box::into_raw(Box::new(builder)),
|
||||
@ -153,7 +175,6 @@ pub unsafe extern "C" fn init_circom_compat(
|
||||
|
||||
let circom_compat_ctx = CircomCompatCtx {
|
||||
circom: Box::into_raw(Box::new(circom_bn254)),
|
||||
rng: rng,
|
||||
_marker: core::marker::PhantomData,
|
||||
};
|
||||
|
||||
@ -166,7 +187,6 @@ pub unsafe extern "C" fn init_circom_compat(
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn release_circom_compat(ctx_ptr: &mut *mut CircomCompatCtx) {
|
||||
if !ctx_ptr.is_null() {
|
||||
let ctx = &mut Box::from_raw(*ctx_ptr);
|
||||
@ -182,7 +202,6 @@ pub unsafe extern "C" fn release_circom_compat(ctx_ptr: &mut *mut CircomCompatCt
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn release_cfg(cfg_ptr: &mut *mut CircomBn254Cfg) {
|
||||
if !cfg_ptr.is_null() && !(*cfg_ptr).is_null() {
|
||||
let cfg = Box::from_raw(*cfg_ptr);
|
||||
@ -233,7 +252,6 @@ unsafe fn to_circom(ctx_ptr: *mut CircomCompatCtx) -> *mut CircomBn254 {
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn prove_circuit(
|
||||
cfg_ptr: *mut CircomBn254Cfg,
|
||||
ctx_ptr: *mut CircomCompatCtx,
|
||||
@ -242,7 +260,7 @@ pub unsafe extern "C" fn prove_circuit(
|
||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||
let circom = &mut *to_circom(ctx_ptr);
|
||||
let proving_key = (*(*cfg_ptr).proving_key).clone();
|
||||
let rng = &mut (*ctx_ptr).rng;
|
||||
let rng = &mut thread_rng();
|
||||
|
||||
let circuit = (*circom.builder)
|
||||
.clone()
|
||||
@ -263,7 +281,6 @@ pub unsafe extern "C" fn prove_circuit(
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn get_pub_inputs(
|
||||
ctx_ptr: *mut CircomCompatCtx,
|
||||
inputs_ptr: &mut *mut Inputs, // inputs_bytes_ptr: &mut *mut Buffer,
|
||||
@ -289,7 +306,6 @@ pub unsafe extern "C" fn get_pub_inputs(
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn get_verifying_key(
|
||||
cfg_ptr: *mut CircomBn254Cfg,
|
||||
vk_ptr: &mut *mut VerifyingKey, // inputs_bytes_ptr: &mut *mut Buffer,
|
||||
@ -308,7 +324,6 @@ pub unsafe extern "C" fn get_verifying_key(
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn verify_circuit(
|
||||
proof: *const Proof,
|
||||
inputs: *const Inputs,
|
||||
@ -338,7 +353,6 @@ pub unsafe extern "C" fn verify_circuit(
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn push_input_u256_array(
|
||||
ctx_ptr: *mut CircomCompatCtx,
|
||||
name_ptr: *const c_char,
|
||||
@ -372,7 +386,6 @@ macro_rules! build_fn
|
||||
{
|
||||
($name:tt, $($v:ident: $t:ty),*) => {
|
||||
#[no_mangle]
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe extern "C" fn $name(
|
||||
ctx_ptr: *mut CircomCompatCtx,
|
||||
name_ptr: *const c_char,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user