Skip to content

Commit

Permalink
allow creating CircomConfig from existing wasm instance
Browse files Browse the repository at this point in the history
  • Loading branch information
martyall committed Jul 4, 2024
1 parent e7af8fe commit b91ad6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 17 additions & 1 deletion src/circom/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use super::{CircomCircuit, R1CS};
use num_bigint::BigInt;
use std::collections::HashMap;

use crate::{circom::R1CSFile, witness::WitnessCalculator};
use crate::{
circom::R1CSFile,
witness::{Wasm, WitnessCalculator},
};
use color_eyre::Result;

#[derive(Debug)]
Expand Down Expand Up @@ -38,6 +41,19 @@ impl<E: Pairing> CircomConfig<E> {
sanity_check: false,
})
}

pub fn new_from_wasm(wasm: Wasm, r1cs: impl AsRef<Path>) -> Result<Self> {
let mut store = Store::default();
let wtns = WitnessCalculator::new_from_wasm(&mut store, wasm).unwrap();
let reader = File::open(r1cs)?;
let r1cs = R1CSFile::new(reader)?.into();
Ok(Self {
wtns,
r1cs,
store,
sanity_check: false,
})
}
}

impl<E: Pairing> CircomBuilder<E> {
Expand Down
8 changes: 2 additions & 6 deletions src/witness/witness_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,14 @@ impl WitnessCalculator {
Self::from_file(store, path)
}

pub fn new_from_wasm(store: &mut Store, wasm: Wasm) -> Result<Self> {
Self::from_wasm(store, wasm)
}

pub fn from_file(store: &mut Store, path: impl AsRef<std::path::Path>) -> Result<Self> {
let module = Module::from_file(&store, path)?;
Self::from_module(store, module)
}

pub fn from_module(store: &mut Store, module: Module) -> Result<Self> {
let wasm = Self::make_wasm_runtime(store, module)?;
Self::from_wasm(store, wasm)
Self::new_from_wasm(store, wasm)
}

pub fn make_wasm_runtime(store: &mut Store, module: Module) -> Result<Wasm> {
Expand Down Expand Up @@ -99,7 +95,7 @@ impl WitnessCalculator {
Ok(wasm)
}

pub fn from_wasm(store: &mut Store, wasm: Wasm) -> Result<Self> {
pub fn new_from_wasm(store: &mut Store, wasm: Wasm) -> Result<Self> {
let version = wasm.get_version(store).unwrap_or(1);
// Circom 2 feature flag with version 2
#[cfg(feature = "circom-2")]
Expand Down

0 comments on commit b91ad6b

Please sign in to comment.