mirror of
https://github.com/logos-storage/circom-compat-ffi.git
synced 2026-01-04 05:43:11 +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
|
||||
- nightly
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
|
||||
- run: cargo build --verbose
|
||||
- run: cargo test --verbose
|
||||
|
||||
@ -7,4 +7,3 @@ template Multiplier() {
|
||||
}
|
||||
|
||||
component main = Multiplier();
|
||||
|
||||
|
||||
22
src/ffi.rs
22
src/ffi.rs
@ -6,15 +6,14 @@ use std::{
|
||||
ptr::slice_from_raw_parts_mut,
|
||||
};
|
||||
|
||||
use crate::ffi_types::*;
|
||||
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::*;
|
||||
|
||||
type GrothBn = Groth16<Bn254, CircomReduction>;
|
||||
|
||||
pub const ERR_UNKNOWN: i32 = -1;
|
||||
@ -50,7 +49,6 @@ struct CircomBn254 {
|
||||
#[derive(Debug, Clone)]
|
||||
struct CircomCompatCtx {
|
||||
circom: *mut CircomBn254,
|
||||
rng: ThreadRng,
|
||||
_marker: core::marker::PhantomData<(*mut CircomCompatCtx, core::marker::PhantomPinned)>,
|
||||
}
|
||||
|
||||
@ -104,12 +102,9 @@ pub unsafe extern "C" fn init_circom_config_with_checks(
|
||||
} else {
|
||||
let mut rng = thread_rng();
|
||||
let builder = CircomBuilder::new(cfg.clone());
|
||||
GrothBn::generate_random_parameters_with_reduction::<_>(
|
||||
builder.setup(),
|
||||
&mut rng,
|
||||
)
|
||||
.map_err(|_| ERR_UNKNOWN)
|
||||
.unwrap()
|
||||
GrothBn::generate_random_parameters_with_reduction::<_>(builder.setup(), &mut rng)
|
||||
.map_err(|_| ERR_UNKNOWN)
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
let circom_bn254_cfg = CircomBn254Cfg {
|
||||
@ -144,7 +139,6 @@ pub unsafe extern "C" fn init_circom_compat(
|
||||
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 +147,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,
|
||||
};
|
||||
|
||||
@ -242,7 +235,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 mut rng = thread_rng();
|
||||
|
||||
let circuit = (*circom.builder)
|
||||
.clone()
|
||||
@ -250,7 +243,7 @@ pub unsafe extern "C" fn prove_circuit(
|
||||
.map_err(|_| ERR_CIRCOM_BUILDER)
|
||||
.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)
|
||||
.unwrap();
|
||||
|
||||
@ -407,6 +400,7 @@ mod test {
|
||||
use std::ffi::CString;
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "circom-2")]
|
||||
fn proof_verify() {
|
||||
let r1cs_path = CString::new("./fixtures/circom2_multiplier2.r1cs".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 {
|
||||
fn from(src: &[Fr]) -> Self {
|
||||
let mut els: Vec<[u8; 32]> = src
|
||||
.iter()
|
||||
.map(|point| point_to_slice(*point))
|
||||
.collect();
|
||||
let mut els: Vec<[u8; 32]> = src.iter().map(|point| point_to_slice(*point)).collect();
|
||||
|
||||
els.shrink_to_fit();
|
||||
let len = els.len();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user