mirror of
https://github.com/logos-storage/circom-compat-ffi.git
synced 2026-01-08 15:53:12 +00:00
remove thread rng pointer from context (#8)
* remove thread rng pointer from context * bump actions/checkout to v4
This commit is contained in:
parent
8cd4ed44fd
commit
7bc5482c86
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -20,7 +20,7 @@ jobs:
|
|||||||
- beta
|
- beta
|
||||||
- nightly
|
- nightly
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
|
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
|
||||||
- run: cargo build --verbose
|
- run: cargo build --verbose
|
||||||
- run: cargo test --verbose
|
- run: cargo test --verbose
|
||||||
|
|||||||
@ -7,4 +7,3 @@ template Multiplier() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
component main = Multiplier();
|
component main = Multiplier();
|
||||||
|
|
||||||
|
|||||||
22
src/ffi.rs
22
src/ffi.rs
@ -6,15 +6,14 @@ use std::{
|
|||||||
ptr::slice_from_raw_parts_mut,
|
ptr::slice_from_raw_parts_mut,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::ffi_types::*;
|
||||||
use ark_bn254::{Bn254, Fr};
|
use ark_bn254::{Bn254, Fr};
|
||||||
use ark_circom::{read_zkey, CircomBuilder, CircomConfig, CircomReduction};
|
use ark_circom::{read_zkey, CircomBuilder, CircomConfig, CircomReduction};
|
||||||
use ark_crypto_primitives::snark::SNARK;
|
use ark_crypto_primitives::snark::SNARK;
|
||||||
use ark_groth16::{prepare_verifying_key, Groth16, ProvingKey};
|
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 ruint::aliases::U256;
|
||||||
|
|
||||||
use crate::ffi_types::*;
|
|
||||||
|
|
||||||
type GrothBn = Groth16<Bn254, CircomReduction>;
|
type GrothBn = Groth16<Bn254, CircomReduction>;
|
||||||
|
|
||||||
pub const ERR_UNKNOWN: i32 = -1;
|
pub const ERR_UNKNOWN: i32 = -1;
|
||||||
@ -50,7 +49,6 @@ struct CircomBn254 {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct CircomCompatCtx {
|
struct CircomCompatCtx {
|
||||||
circom: *mut CircomBn254,
|
circom: *mut CircomBn254,
|
||||||
rng: ThreadRng,
|
|
||||||
_marker: core::marker::PhantomData<(*mut CircomCompatCtx, core::marker::PhantomPinned)>,
|
_marker: core::marker::PhantomData<(*mut CircomCompatCtx, core::marker::PhantomPinned)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,12 +102,9 @@ pub unsafe extern "C" fn init_circom_config_with_checks(
|
|||||||
} else {
|
} else {
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
let builder = CircomBuilder::new(cfg.clone());
|
let builder = CircomBuilder::new(cfg.clone());
|
||||||
GrothBn::generate_random_parameters_with_reduction::<_>(
|
GrothBn::generate_random_parameters_with_reduction::<_>(builder.setup(), &mut rng)
|
||||||
builder.setup(),
|
.map_err(|_| ERR_UNKNOWN)
|
||||||
&mut rng,
|
.unwrap()
|
||||||
)
|
|
||||||
.map_err(|_| ERR_UNKNOWN)
|
|
||||||
.unwrap()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let circom_bn254_cfg = CircomBn254Cfg {
|
let circom_bn254_cfg = CircomBn254Cfg {
|
||||||
@ -144,7 +139,6 @@ pub unsafe extern "C" fn init_circom_compat(
|
|||||||
ctx_ptr: &mut *mut CircomCompatCtx,
|
ctx_ptr: &mut *mut CircomCompatCtx,
|
||||||
) -> i32 {
|
) -> i32 {
|
||||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
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 builder = CircomBuilder::new((*(*cfg_ptr).cfg).clone()); // clone the config
|
||||||
let circom_bn254 = CircomBn254 {
|
let circom_bn254 = CircomBn254 {
|
||||||
builder: Box::into_raw(Box::new(builder)),
|
builder: Box::into_raw(Box::new(builder)),
|
||||||
@ -153,7 +147,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)),
|
||||||
rng: rng,
|
|
||||||
_marker: core::marker::PhantomData,
|
_marker: core::marker::PhantomData,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -242,7 +235,7 @@ pub unsafe extern "C" fn prove_circuit(
|
|||||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||||
let circom = &mut *to_circom(ctx_ptr);
|
let circom = &mut *to_circom(ctx_ptr);
|
||||||
let proving_key = (*(*cfg_ptr).proving_key).clone();
|
let proving_key = (*(*cfg_ptr).proving_key).clone();
|
||||||
let rng = &mut (*ctx_ptr).rng;
|
let mut rng = thread_rng();
|
||||||
|
|
||||||
let circuit = (*circom.builder)
|
let circuit = (*circom.builder)
|
||||||
.clone()
|
.clone()
|
||||||
@ -250,7 +243,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, &mut rng)
|
||||||
.map_err(|_| ERR_MAKING_PROOF)
|
.map_err(|_| ERR_MAKING_PROOF)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -407,6 +400,7 @@ mod test {
|
|||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "circom-2")]
|
||||||
fn proof_verify() {
|
fn proof_verify() {
|
||||||
let r1cs_path = CString::new("./fixtures/circom2_multiplier2.r1cs".as_bytes()).unwrap();
|
let r1cs_path = CString::new("./fixtures/circom2_multiplier2.r1cs".as_bytes()).unwrap();
|
||||||
let wasm_path = CString::new("./fixtures/circom2_multiplier2.wasm".as_bytes()).unwrap();
|
let wasm_path = CString::new("./fixtures/circom2_multiplier2.wasm".as_bytes()).unwrap();
|
||||||
|
|||||||
@ -164,10 +164,7 @@ impl From<&ark_groth16::VerifyingKey<Bn254>> for VerifyingKey {
|
|||||||
|
|
||||||
impl From<&[Fr]> for Inputs {
|
impl From<&[Fr]> for Inputs {
|
||||||
fn from(src: &[Fr]) -> Self {
|
fn from(src: &[Fr]) -> Self {
|
||||||
let mut els: Vec<[u8; 32]> = src
|
let mut els: Vec<[u8; 32]> = src.iter().map(|point| point_to_slice(*point)).collect();
|
||||||
.iter()
|
|
||||||
.map(|point| point_to_slice(*point))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
els.shrink_to_fit();
|
els.shrink_to_fit();
|
||||||
let len = els.len();
|
let len = els.len();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user