don't store cfg in circom context
This commit is contained in:
parent
337a5113ed
commit
06417c3e47
18
src/ffi.rs
18
src/ffi.rs
|
@ -33,7 +33,6 @@ struct CircomBn254 {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct CircomCompatCtx {
|
struct CircomCompatCtx {
|
||||||
circom: *mut CircomBn254,
|
circom: *mut CircomBn254,
|
||||||
cfg: *mut CircomBn254Cfg,
|
|
||||||
rng: ThreadRng,
|
rng: ThreadRng,
|
||||||
_marker: core::marker::PhantomData<(*mut CircomCompatCtx, core::marker::PhantomPinned)>,
|
_marker: core::marker::PhantomData<(*mut CircomCompatCtx, core::marker::PhantomPinned)>,
|
||||||
}
|
}
|
||||||
|
@ -122,7 +121,6 @@ pub unsafe extern "C" fn init_circom_compat(
|
||||||
|
|
||||||
let circom_compat_ctx = CircomCompatCtx {
|
let circom_compat_ctx = CircomCompatCtx {
|
||||||
circom: Box::into_raw(Box::new(circom_bn254)),
|
circom: Box::into_raw(Box::new(circom_bn254)),
|
||||||
cfg: cfg_ptr,
|
|
||||||
rng: rng,
|
rng: rng,
|
||||||
_marker: core::marker::PhantomData,
|
_marker: core::marker::PhantomData,
|
||||||
};
|
};
|
||||||
|
@ -203,13 +201,13 @@ unsafe fn to_circom(ctx_ptr: *mut CircomCompatCtx) -> *mut CircomBn254 {
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
#[allow(private_interfaces)]
|
#[allow(private_interfaces)]
|
||||||
pub unsafe extern "C" fn prove_circuit(
|
pub unsafe extern "C" fn prove_circuit(
|
||||||
|
cfg_ptr: *mut CircomBn254Cfg,
|
||||||
ctx_ptr: *mut CircomCompatCtx,
|
ctx_ptr: *mut CircomCompatCtx,
|
||||||
proof_ptr: &mut *mut Proof, // inputs_bytes_ptr: &mut *mut Buffer,
|
proof_ptr: &mut *mut Proof, // inputs_bytes_ptr: &mut *mut Buffer,
|
||||||
) -> i32 {
|
) -> i32 {
|
||||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||||
let ctx: &mut CircomCompatCtx = &mut *ctx_ptr;
|
|
||||||
let circom = &mut *to_circom(ctx_ptr);
|
let circom = &mut *to_circom(ctx_ptr);
|
||||||
let proving_key = (*ctx.cfg).proving_key;
|
let proving_key = (*(*cfg_ptr).proving_key).clone();
|
||||||
let rng = &mut (*ctx_ptr).rng;
|
let rng = &mut (*ctx_ptr).rng;
|
||||||
|
|
||||||
let circuit = (*circom.builder)
|
let circuit = (*circom.builder)
|
||||||
|
@ -218,7 +216,7 @@ pub unsafe extern "C" fn prove_circuit(
|
||||||
.map_err(|_| ERR_CIRCOM_BUILDER)
|
.map_err(|_| ERR_CIRCOM_BUILDER)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let circom_proof = GrothBn::prove(&*proving_key, circuit, rng)
|
let circom_proof = GrothBn::prove(&proving_key, circuit, rng)
|
||||||
.map_err(|_| ERR_MAKING_PROOF)
|
.map_err(|_| ERR_MAKING_PROOF)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -255,12 +253,12 @@ pub unsafe extern "C" fn get_pub_inputs(
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
#[allow(private_interfaces)]
|
#[allow(private_interfaces)]
|
||||||
pub unsafe extern "C" fn get_verifying_key(
|
pub unsafe extern "C" fn get_verifying_key(
|
||||||
ctx_ptr: *mut CircomCompatCtx,
|
cfg_ptr: *mut CircomBn254Cfg,
|
||||||
vk_ptr: &mut *mut VerifyingKey, // inputs_bytes_ptr: &mut *mut Buffer,
|
vk_ptr: &mut *mut VerifyingKey, // inputs_bytes_ptr: &mut *mut Buffer,
|
||||||
) -> i32 {
|
) -> i32 {
|
||||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||||
let ctx = &mut *ctx_ptr;
|
let ctx = &mut *cfg_ptr;
|
||||||
let proving_key = &(*(*ctx.cfg).proving_key);
|
let proving_key = &(*(*ctx).proving_key);
|
||||||
let vk = prepare_verifying_key(&proving_key.vk).vk;
|
let vk = prepare_verifying_key(&proving_key.vk).vk;
|
||||||
|
|
||||||
*vk_ptr = Box::leak(Box::new((&vk).into()));
|
*vk_ptr = Box::leak(Box::new((&vk).into()));
|
||||||
|
@ -388,10 +386,10 @@ mod test {
|
||||||
assert!(get_pub_inputs(ctx_ptr, &mut inputs_ptr) == ERR_OK);
|
assert!(get_pub_inputs(ctx_ptr, &mut inputs_ptr) == ERR_OK);
|
||||||
assert!(inputs_ptr != std::ptr::null_mut());
|
assert!(inputs_ptr != std::ptr::null_mut());
|
||||||
|
|
||||||
assert!(prove_circuit(ctx_ptr, &mut proof_ptr) == ERR_OK);
|
assert!(prove_circuit(cfg_ptr, ctx_ptr, &mut proof_ptr) == ERR_OK);
|
||||||
assert!(proof_ptr != std::ptr::null_mut());
|
assert!(proof_ptr != std::ptr::null_mut());
|
||||||
|
|
||||||
assert!(get_verifying_key(ctx_ptr, &mut vk_ptr) == ERR_OK);
|
assert!(get_verifying_key(cfg_ptr, &mut vk_ptr) == ERR_OK);
|
||||||
assert!(vk_ptr != std::ptr::null_mut());
|
assert!(vk_ptr != std::ptr::null_mut());
|
||||||
|
|
||||||
assert!(verify_circuit(&(*proof_ptr), &(*inputs_ptr), &(*vk_ptr)) == ERR_OK);
|
assert!(verify_circuit(&(*proof_ptr), &(*inputs_ptr), &(*vk_ptr)) == ERR_OK);
|
||||||
|
|
Loading…
Reference in New Issue