@@ -3,7 +3,7 @@ use color_eyre::Result;
3
3
use num_bigint:: BigInt ;
4
4
use num_traits:: Zero ;
5
5
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 ;
7
7
8
8
#[ cfg( feature = "circom-2" ) ]
9
9
use num:: ToPrimitive ;
@@ -57,15 +57,23 @@ impl WitnessCalculator {
57
57
Self :: from_file ( store, path)
58
58
}
59
59
60
+ pub fn new_from_wasm ( store : & mut Store , wasm : Wasm ) -> Result < Self > {
61
+ Self :: from_wasm ( store, wasm)
62
+ }
63
+
60
64
pub fn from_file ( store : & mut Store , path : impl AsRef < std:: path:: Path > ) -> Result < Self > {
61
65
let module = Module :: from_file ( & store, path) ?;
62
66
Self :: from_module ( store, module)
63
67
}
64
68
65
69
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 > {
67
75
let memory = Memory :: new ( store, MemoryType :: new ( 2000 , None , false ) ) . unwrap ( ) ;
68
- let mut import_object = imports ! {
76
+ let import_object = imports ! {
69
77
"env" => {
70
78
"memory" => memory. clone( ) ,
71
79
} ,
@@ -83,17 +91,15 @@ impl WitnessCalculator {
83
91
"writeBufferMessage" => runtime:: write_buffer_message( store) ,
84
92
}
85
93
} ;
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
-
92
94
let instance = Instance :: new ( store, & module, & import_object) ?;
93
95
let exports = instance. exports . clone ( ) ;
94
- let wasm = Wasm :: new ( exports ) ;
96
+ let mut wasi_env = WasiEnv :: builder ( "calculateWitness" ) . finalize ( store ) ? ;
95
97
wasi_env. initialize_with_memory ( store, instance, Some ( memory. clone ( ) ) , false ) ?;
98
+ let wasm = Wasm :: new ( exports, memory) ;
99
+ Ok ( wasm)
100
+ }
96
101
102
+ pub fn from_wasm ( store : & mut Store , wasm : Wasm ) -> Result < Self > {
97
103
let version = wasm. get_version ( store) . unwrap_or ( 1 ) ;
98
104
// Circom 2 feature flag with version 2
99
105
#[ cfg( feature = "circom-2" ) ]
@@ -125,12 +131,12 @@ impl WitnessCalculator {
125
131
fn new_circom1 (
126
132
instance : Wasm ,
127
133
store : & mut Store ,
128
- memory : Memory ,
129
134
version : u32 ,
130
135
) -> Result < WitnessCalculator > {
131
136
// Fallback to Circom 1 behavior
132
137
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 ( ) ) ;
134
140
let ptr = instance. get_ptr_raw_prime ( store) ?;
135
141
let prime = safe_memory. read_big ( store, ptr as usize , n32 as usize ) ?;
136
142
@@ -157,7 +163,7 @@ impl WitnessCalculator {
157
163
if #[ cfg( feature = "circom-2" ) ] {
158
164
match version {
159
165
2 => new_circom2( wasm, store, version) ,
160
- 1 => new_circom1( wasm, store, memory , version) ,
166
+ 1 => new_circom1( wasm, store, version) ,
161
167
162
168
_ => panic!( "Unknown Circom version" )
163
169
}
0 commit comments