Skip to content

Commit d8f0cc3

Browse files
committed
refactor to expose public from_wasm
1 parent 3902140 commit d8f0cc3

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/witness/circom.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use color_eyre::Result;
2-
use wasmer::{Exports, Function, Store, Value};
2+
use wasmer::{Exports, Function, Memory, Store, Value};
33

44
#[derive(Debug)]
55
pub struct Wasm {
66
pub exports: Exports,
7+
pub memory: Memory,
78
}
89

910
pub trait CircomBase {
@@ -178,7 +179,7 @@ impl CircomBase for Wasm {
178179
}
179180

180181
impl Wasm {
181-
pub fn new(exports: Exports) -> Self {
182-
Self { exports }
182+
pub fn new(exports: Exports, memory: Memory) -> Self {
183+
Self { exports, memory }
183184
}
184185
}

src/witness/witness_calculator.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use color_eyre::Result;
33
use num_bigint::BigInt;
44
use num_traits::Zero;
55
use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, RuntimeError, Store};
6-
use wasmer_wasix::{generate_import_object_from_env, WasiEnv, WasiVersion};
6+
use wasmer_wasix::WasiEnv;
77

88
#[cfg(feature = "circom-2")]
99
use num::ToPrimitive;
@@ -57,15 +57,23 @@ impl WitnessCalculator {
5757
Self::from_file(store, path)
5858
}
5959

60+
pub fn new_from_wasm(store: &mut Store, wasm: Wasm) -> Result<Self> {
61+
Self::from_wasm(store, wasm)
62+
}
63+
6064
pub fn from_file(store: &mut Store, path: impl AsRef<std::path::Path>) -> Result<Self> {
6165
let module = Module::from_file(&store, path)?;
6266
Self::from_module(store, module)
6367
}
6468

6569
pub fn from_module(store: &mut Store, module: Module) -> Result<Self> {
66-
// Set up the memory
70+
let wasm = Self::make_wasm_runtime(store, module)?;
71+
Self::from_wasm(store, wasm)
72+
}
73+
74+
fn make_wasm_runtime(store: &mut Store, module: Module) -> Result<Wasm> {
6775
let memory = Memory::new(store, MemoryType::new(2000, None, false)).unwrap();
68-
let mut import_object = imports! {
76+
let import_object = imports! {
6977
"env" => {
7078
"memory" => memory.clone(),
7179
},
@@ -83,17 +91,15 @@ impl WitnessCalculator {
8391
"writeBufferMessage" => runtime::write_buffer_message(store),
8492
}
8593
};
86-
87-
let mut wasi_env = WasiEnv::builder("calculateWitness").finalize(store)?;
88-
let wasi_env_imports =
89-
generate_import_object_from_env(store, &wasi_env.env, WasiVersion::Snapshot1);
90-
import_object.extend(&wasi_env_imports);
91-
9294
let instance = Instance::new(store, &module, &import_object)?;
9395
let exports = instance.exports.clone();
94-
let wasm = Wasm::new(exports);
96+
let mut wasi_env = WasiEnv::builder("calculateWitness").finalize(store)?;
9597
wasi_env.initialize_with_memory(store, instance, Some(memory.clone()), false)?;
98+
let wasm = Wasm::new(exports, memory);
99+
Ok(wasm)
100+
}
96101

102+
pub fn from_wasm(store: &mut Store, wasm: Wasm) -> Result<Self> {
97103
let version = wasm.get_version(store).unwrap_or(1);
98104
// Circom 2 feature flag with version 2
99105
#[cfg(feature = "circom-2")]
@@ -125,12 +131,12 @@ impl WitnessCalculator {
125131
fn new_circom1(
126132
instance: Wasm,
127133
store: &mut Store,
128-
memory: Memory,
129134
version: u32,
130135
) -> Result<WitnessCalculator> {
131136
// Fallback to Circom 1 behavior
132137
let n32 = (instance.get_fr_len(store)? >> 2) - 2;
133-
let mut safe_memory = SafeMemory::new(memory, n32 as usize, BigInt::zero());
138+
let mut safe_memory =
139+
SafeMemory::new(instance.memory.clone(), n32 as usize, BigInt::zero());
134140
let ptr = instance.get_ptr_raw_prime(store)?;
135141
let prime = safe_memory.read_big(store, ptr as usize, n32 as usize)?;
136142

@@ -157,7 +163,7 @@ impl WitnessCalculator {
157163
if #[cfg(feature = "circom-2")] {
158164
match version {
159165
2 => new_circom2(wasm, store, version),
160-
1 => new_circom1(wasm, store, memory, version),
166+
1 => new_circom1(wasm, store, version),
161167

162168
_ => panic!("Unknown Circom version")
163169
}

0 commit comments

Comments
 (0)