diff --git a/algebraic/src/witness/circom.rs b/algebraic/src/witness/circom.rs index efb8e272..84a8732a 100644 --- a/algebraic/src/witness/circom.rs +++ b/algebraic/src/witness/circom.rs @@ -1,6 +1,6 @@ // copied and modified by https://github.com/arkworks-rs/circom-compat/blob/master/src/witness/circom.rs use crate::errors::Result; -use wasmer::{Function, Instance, Value}; +use wasmer::{Function, Instance, Store, Value}; #[derive(Clone, Debug)] pub struct Wasm(Instance); @@ -41,31 +41,36 @@ impl Wasm { pub(crate) fn get_raw_prime(&self) -> Result<()> { let func = self.func("getRawPrime"); - func.call(&[])?; + let mut store = Store::default(); + func.call(&mut store, &[])?; Ok(()) } pub(crate) fn read_shared_rw_memory(&self, i: u32) -> Result { let func = self.func("readSharedRWMemory"); - let result = func.call(&[i.into()])?; + let mut store = Store::default(); + let result = func.call(&mut store, &[i.into()])?; Ok(result[0].unwrap_i32() as u32) } pub(crate) fn write_shared_rw_memory(&self, i: u32, v: u32) -> Result<()> { let func = self.func("writeSharedRWMemory"); - func.call(&[i.into(), v.into()])?; + let mut store = Store::default(); + func.call(&mut store, &[i.into(), v.into()])?; Ok(()) } pub(crate) fn set_input_signal(&self, hmsb: u32, hlsb: u32, pos: u32) -> Result<()> { let func = self.func("setInputSignal"); - func.call(&[hmsb.into(), hlsb.into(), pos.into()])?; + let mut store = Store::default(); + func.call(&mut store, &[hmsb.into(), hlsb.into(), pos.into()])?; Ok(()) } pub(crate) fn get_witness(&self, i: u32) -> Result<()> { let func = self.func("getWitness"); - func.call(&[i.into()])?; + let mut store = Store::default(); + func.call(&mut store, &[i.into()])?; Ok(()) } @@ -77,7 +82,8 @@ impl Wasm { // impl CircomBase for Wasm { pub(crate) fn init(&self, sanity_check: bool) -> Result<()> { let func = self.func("init"); - func.call(&[Value::I32(sanity_check as i32)])?; + let mut store = Store::default(); + func.call(&mut store, &[Value::I32(sanity_check as i32)])?; Ok(()) } @@ -87,7 +93,8 @@ impl Wasm { pub(crate) fn get_ptr_witness(&self, w: u32) -> Result { let func = self.func("getPWitness"); - let res = func.call(&[w.into()])?; + let mut store = Store::default(); + let res = func.call(&mut store, &[w.into()])?; Ok(res[0].unwrap_i32() as u32) } @@ -100,12 +107,16 @@ impl Wasm { hash_lsb: u32, ) -> Result<()> { let func = self.func("getSignalOffset32"); - func.call(&[ - p_sig_offset.into(), - component.into(), - hash_msb.into(), - hash_lsb.into(), - ])?; + let mut store = Store::default(); + func.call( + &mut store, + &[ + p_sig_offset.into(), + component.into(), + hash_msb.into(), + hash_lsb.into(), + ], + )?; Ok(()) } @@ -118,7 +129,11 @@ impl Wasm { p_val: u32, ) -> Result<()> { let func = self.func("setSignal"); - func.call(&[c_idx.into(), component.into(), signal.into(), p_val.into()])?; + let mut store = Store::default(); + func.call( + &mut store, + &[c_idx.into(), component.into(), signal.into(), p_val.into()], + )?; Ok(()) } @@ -126,14 +141,18 @@ impl Wasm { // Default to version 1 if it isn't explicitly defined pub(crate) fn get_version(&self) -> Result { match self.0.exports.get_function("getVersion") { - Ok(func) => Ok(func.call(&[])?[0].unwrap_i32() as u32), + Ok(func) => { + let mut store = Store::default(); + Ok(func.call(&mut store, &[])?[0].unwrap_i32() as u32) + } Err(_) => Ok(1), } } pub(crate) fn get_u32(&self, name: &str) -> Result { let func = self.func(name); - let result = func.call(&[])?; + let mut store = Store::default(); + let result = func.call(&mut store, &[])?; Ok(result[0].unwrap_i32() as u32) } diff --git a/algebraic/src/witness/mod.rs b/algebraic/src/witness/mod.rs index 6670fcd1..7479090b 100644 --- a/algebraic/src/witness/mod.rs +++ b/algebraic/src/witness/mod.rs @@ -12,9 +12,9 @@ pub(crate) mod memory; // pub(super) use memory::SafeMemory; mod circom; -pub(super) use circom::{CircomBase, Wasm}; +// pub(super) use circom::{CircomBase, Wasm}; -pub(super) use circom::Circom; +// pub(super) use circom::Circom; use fnv::FnvHasher; use std::hash::Hasher; diff --git a/algebraic/src/witness/witness_calculator.rs b/algebraic/src/witness/witness_calculator.rs index dc599437..0faa1da3 100644 --- a/algebraic/src/witness/witness_calculator.rs +++ b/algebraic/src/witness/witness_calculator.rs @@ -1,9 +1,9 @@ // copied and modified by https://github.com/arkworks-rs/circom-compat/blob/master/src/witness/witness_calculator.rs -use super::Circom; -use super::{fnv, CircomBase, Wasm}; +// use super::Circom; +// use super::{fnv, Wasm}; use crate::bellman_ce::{PrimeField, ScalarEngine}; use crate::errors::{EigenError, Result}; -use crate::witness::memory::SafeMemory; +use crate::witness::{circom::Wasm, fnv, memory::SafeMemory}; use num::ToPrimitive; use num_bigint::BigInt; use num_bigint::BigUint; @@ -17,6 +17,7 @@ use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, Store}; use std::fs::OpenOptions; #[cfg(not(feature = "wasm"))] use std::io::{BufWriter, Write}; +use std::thread::scope; use byteorder::{LittleEndian, WriteBytesExt}; @@ -74,19 +75,28 @@ impl WitnessCalculator { }, // Host function callbacks from the WASM "runtime" => { - "error" => runtime::error(store), - "logSetSignal" => runtime::log_signal(store), - "logGetSignal" => runtime::log_signal(store), - "logFinishComponent" => runtime::log_component(store), - "logStartComponent" => runtime::log_component(store), - "log" => runtime::log_component(store), - "exceptionHandler" => runtime::exception_handler(store), - "showSharedRWMemory" => runtime::show_memory(store), - "printErrorMessage" => runtime::print_error_message(store), - "writeBufferMessage" => runtime::write_buffer_message(store), + "error" => runtime::error(&mut store), + "logSetSignal" => runtime::log_signal(&mut store), + "logGetSignal" => runtime::log_signal(&mut store), + "logFinishComponent" => runtime::log_component(&mut store), + "logStartComponent" => runtime::log_component(&mut store), + "log" => runtime::log_component(&mut store), + "exceptionHandler" => runtime::exception_handler(&mut store), + "showSharedRWMemory" => runtime::show_memory(&mut store), + "printErrorMessage" => runtime::print_error_message(&mut store), + "writeBufferMessage" => runtime::write_buffer_message(&mut store), } }; - let instance = Wasm::new(Instance::new(&module, &import_object)?); + let instance = Wasm::new(Instance::new(&mut scope, &module, &import_object)?); + let mut store = Store::default(); + let env = FunctionEnv::new(&mut store, ()); + let module = Module::new(&store, "(module)")?; + let imports = imports! { + "host" => { + "var" => Global::new(&mut store, Value::I32(2)) + } + }; + let instance = Instance::new(&mut store, &module, &imports)?; // Circom 2 feature flag with version 2 fn new_circom(instance: Wasm, memory: Memory) -> Result { @@ -331,7 +341,7 @@ pub fn flat_array(v: &[Value]) -> Vec { mod runtime { use super::*; - pub fn error(store: &Store) -> Function { + pub fn error(store: &mut Store) -> Function { #[allow(unused)] #[allow(clippy::many_single_char_names)] fn func(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> Result<()> { @@ -346,40 +356,40 @@ mod runtime { } // Circom 2.0 - pub fn exception_handler(store: &Store) -> Function { + pub fn exception_handler(store: &mut Store) -> Function { #[allow(unused)] fn func(a: i32) {} Function::new_native(store, func) } // Circom 2.0 - pub fn show_memory(store: &Store) -> Function { + pub fn show_memory(store: &mut Store) -> Function { #[allow(unused)] fn func() {} Function::new_native(store, func) } // Circom 2.0 - pub fn print_error_message(store: &Store) -> Function { + pub fn print_error_message(store: &mut Store) -> Function { #[allow(unused)] fn func() {} Function::new_native(store, func) } // Circom 2.0 - pub fn write_buffer_message(store: &Store) -> Function { + pub fn write_buffer_message(store: &mut Store) -> Function { #[allow(unused)] fn func() {} Function::new_native(store, func) } - pub fn log_signal(store: &Store) -> Function { + pub fn log_signal(store: &mut Store) -> Function { #[allow(unused)] fn func(a: i32, b: i32) {} Function::new_native(store, func) } - pub fn log_component(store: &Store) -> Function { + pub fn log_component(store: &mut Store) -> Function { #[allow(unused)] fn func(a: i32) {} Function::new_native(store, func)