circom-compat/src/witness/witness_calculator.rs
src/witness/witness_calculator.rsRaw
1use super::{fnv, CircomBase, SafeMemory, Wasm};
2use color_eyre::Result;
3use num_bigint::BigInt;
4use num_traits::Zero;
5use std::cell::Cell;
6use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, RuntimeError, Store};
7
8#[cfg(feature = "circom-2")]
9use num::ToPrimitive;
10
11#[cfg(feature = "circom-2")]
12use super::Circom2;
13
14use super::Circom;
15
16#[derive(Clone, Debug)]
17pub struct WitnessCalculator {
18 pub instance: Wasm,
19 pub memory: SafeMemory,
20 pub n64: u32,
21 pub circom_version: u32,
22}
23
24// Error type to signal end of execution.
25// From https://docs.wasmer.io/integrations/examples/exit-early
26#[derive(thiserror::Error, Debug, Clone, Copy)]
27#[error("{0}")]
28struct ExitCode(u32);
29
30#[cfg(feature = "circom-2")]
31fn from_array32(arr: Vec<u32>) -> BigInt {
32 let mut res = BigInt::zero();
33 let radix = BigInt::from(0x100000000u64);
34 for &val in arr.iter() {
35 res = res * &radix + BigInt::from(val);
36 }
37 res
38}
39
40#[cfg(feature = "circom-2")]
41fn to_array32(s: &BigInt, size: usize) -> Vec<u32> {
42 let mut res = vec![0; size];
43 let mut rem = s.clone();
44 let radix = BigInt::from(0x100000000u64);
45 let mut c = size;
46 while !rem.is_zero() {
47 c -= 1;
48 res[c] = (&rem % &radix).to_u32().unwrap();
49 rem /= &radix;
50 }
51
52 res
53}
54
55impl WitnessCalculator {
56 pub fn new(path: impl AsRef<std::path::Path>) -> Result<Self> {
57 Self::from_file(path)
58 }
59
60 pub fn from_file(path: impl AsRef<std::path::Path>) -> Result<Self> {
61 let store = Store::default();
62 let module = Module::from_file(&store, path)?;
63 Self::from_module(module)
64 }
65
66 pub fn from_module(module: Module) -> Result<Self> {
67 let store = module.store();
68
69 // Set up the memory
70 let memory = Memory::new(store, MemoryType::new(2000, None, false)).unwrap();
71 let import_object = imports! {
72 "env" => {
73 "memory" => memory.clone(),
74 },
75 // Host function callbacks from the WASM
76 "runtime" => {
77 "error" => runtime::error(store),
78 "logSetSignal" => runtime::log_signal(store),
79 "logGetSignal" => runtime::log_signal(store),
80 "logFinishComponent" => runtime::log_component(store),
81 "logStartComponent" => runtime::log_component(store),
82 "log" => runtime::log_component(store),
83 "exceptionHandler" => runtime::exception_handler(store),
84 "showSharedRWMemory" => runtime::show_memory(store),
85 "printErrorMessage" => runtime::print_error_message(store),
86 "writeBufferMessage" => runtime::write_buffer_message(store),
87 }
88 };
89 let instance = Wasm::new(Instance::new(&module, &import_object)?);
90
91 let version = instance.get_version().unwrap_or(1);
92
93 // Circom 2 feature flag with version 2
94 #[cfg(feature = "circom-2")]
95 fn new_circom2(instance: Wasm, memory: Memory, version: u32) -> Result<WitnessCalculator> {
96 let n32 = instance.get_field_num_len32()?;
97 let mut safe_memory = SafeMemory::new(memory, n32 as usize, BigInt::zero());
98 instance.get_raw_prime()?;
99 let mut arr = vec![0; n32 as usize];
100 for i in 0..n32 {
101 let res = instance.read_shared_rw_memory(i)?;
102 arr[(n32 as usize) - (i as usize) - 1] = res;
103 }
104 let prime = from_array32(arr);
105
106 let n64 = ((prime.bits() - 1) / 64 + 1) as u32;
107 safe_memory.prime = prime;
108
109 Ok(WitnessCalculator {
110 instance,
111 memory: safe_memory,
112 n64,
113 circom_version: version,
114 })
115 }
116
117 fn new_circom1(instance: Wasm, memory: Memory, version: u32) -> Result<WitnessCalculator> {
118 // Fallback to Circom 1 behavior
119 let n32 = (instance.get_fr_len()? >> 2) - 2;
120 let mut safe_memory = SafeMemory::new(memory, n32 as usize, BigInt::zero());
121 let ptr = instance.get_ptr_raw_prime()?;
122 let prime = safe_memory.read_big(ptr as usize, n32 as usize)?;
123
124 let n64 = ((prime.bits() - 1) / 64 + 1) as u32;
125 safe_memory.prime = prime;
126
127 Ok(WitnessCalculator {
128 instance,
129 memory: safe_memory,
130 n64,
131 circom_version: version,
132 })
133 }
134
135 // Three possibilities:
136 // a) Circom 2 feature flag enabled, WASM runtime version 2
137 // b) Circom 2 feature flag enabled, WASM runtime version 1
138 // c) Circom 1 default behavior
139 //
140 // Once Circom 2 support is more stable, feature flag can be removed
141
142 cfg_if::cfg_if! {
143 if #[cfg(feature = "circom-2")] {
144 match version {
145 2 => new_circom2(instance, memory, version),
146 1 => new_circom1(instance, memory, version),
147 _ => panic!("Unknown Circom version")
148 }
149 } else {
150 new_circom1(instance, memory, version)
151 }
152 }
153 }
154
155 pub fn calculate_witness<I: IntoIterator<Item = (String, Vec<BigInt>)>>(
156 &mut self,
157 inputs: I,
158 sanity_check: bool,
159 ) -> Result<Vec<BigInt>> {
160 self.instance.init(sanity_check)?;
161
162 cfg_if::cfg_if! {
163 if #[cfg(feature = "circom-2")] {
164 match self.circom_version {
165 2 => self.calculate_witness_circom2(inputs, sanity_check),
166 1 => self.calculate_witness_circom1(inputs, sanity_check),
167 _ => panic!("Unknown Circom version")
168 }
169 } else {
170 self.calculate_witness_circom1(inputs, sanity_check)
171 }
172 }
173 }
174
175 // Circom 1 default behavior
176 fn calculate_witness_circom1<I: IntoIterator<Item = (String, Vec<BigInt>)>>(
177 &mut self,
178 inputs: I,
179 sanity_check: bool,
180 ) -> Result<Vec<BigInt>> {
181 self.instance.init(sanity_check)?;
182
183 let old_mem_free_pos = self.memory.free_pos();
184 let p_sig_offset = self.memory.alloc_u32();
185 let p_fr = self.memory.alloc_fr();
186
187 // allocate the inputs
188 for (name, values) in inputs.into_iter() {
189 let (msb, lsb) = fnv(&name);
190
191 self.instance
192 .get_signal_offset32(p_sig_offset, 0, msb, lsb)?;
193
194 let sig_offset = self.memory.read_u32(p_sig_offset as usize) as usize;
195
196 for (i, value) in values.into_iter().enumerate() {
197 self.memory.write_fr(p_fr as usize, &value)?;
198 self.instance
199 .set_signal(0, 0, (sig_offset + i) as u32, p_fr)?;
200 }
201 }
202
203 let mut w = Vec::new();
204
205 let n_vars = self.instance.get_n_vars()?;
206 for i in 0..n_vars {
207 let ptr = self.instance.get_ptr_witness(i)? as usize;
208 let el = self.memory.read_fr(ptr)?;
209 w.push(el);
210 }
211
212 self.memory.set_free_pos(old_mem_free_pos);
213
214 Ok(w)
215 }
216
217 // Circom 2 feature flag with version 2
218 #[cfg(feature = "circom-2")]
219 fn calculate_witness_circom2<I: IntoIterator<Item = (String, Vec<BigInt>)>>(
220 &mut self,
221 inputs: I,
222 sanity_check: bool,
223 ) -> Result<Vec<BigInt>> {
224 self.instance.init(sanity_check)?;
225
226 let n32 = self.instance.get_field_num_len32()?;
227
228 // allocate the inputs
229 for (name, values) in inputs.into_iter() {
230 let (msb, lsb) = fnv(&name);
231
232 for (i, value) in values.into_iter().enumerate() {
233 let f_arr = to_array32(&value, n32 as usize);
234 for j in 0..n32 {
235 self.instance
236 .write_shared_rw_memory(j, f_arr[(n32 as usize) - 1 - (j as usize)])?;
237 }
238 self.instance.set_input_signal(msb, lsb, i as u32)?;
239 }
240 }
241
242 let mut w = Vec::new();
243
244 let witness_size = self.instance.get_witness_size()?;
245 for i in 0..witness_size {
246 self.instance.get_witness(i)?;
247 let mut arr = vec![0; n32 as usize];
248 for j in 0..n32 {
249 arr[(n32 as usize) - 1 - (j as usize)] = self.instance.read_shared_rw_memory(j)?;
250 }
251 w.push(from_array32(arr));
252 }
253
254 Ok(w)
255 }
256
257 pub fn calculate_witness_element<
258 E: ark_ec::PairingEngine,
259 I: IntoIterator<Item = (String, Vec<BigInt>)>,
260 >(
261 &mut self,
262 inputs: I,
263 sanity_check: bool,
264 ) -> Result<Vec<E::Fr>> {
265 use ark_ff::{FpParameters, PrimeField};
266 let witness = self.calculate_witness(inputs, sanity_check)?;
267 let modulus = <<E::Fr as PrimeField>::Params as FpParameters>::MODULUS;
268
269 // convert it to field elements
270 use num_traits::Signed;
271 let witness = witness
272 .into_iter()
273 .map(|w| {
274 let w = if w.sign() == num_bigint::Sign::Minus {
275 // Need to negate the witness element if negative
276 modulus.into() - w.abs().to_biguint().unwrap()
277 } else {
278 w.to_biguint().unwrap()
279 };
280 E::Fr::from(w)
281 })
282 .collect::<Vec<_>>();
283
284 Ok(witness)
285 }
286
287 pub fn get_witness_buffer(&self) -> Result<Vec<u8>> {
288 let ptr = self.instance.get_ptr_witness_buffer()? as usize;
289
290 let view = self.memory.memory.view::<u8>();
291
292 let len = self.instance.get_n_vars()? * self.n64 * 8;
293 let arr = view[ptr..ptr + len as usize]
294 .iter()
295 .map(Cell::get)
296 .collect::<Vec<_>>();
297
298 Ok(arr)
299 }
300}
301
302// callback hooks for debugging
303mod runtime {
304 use super::*;
305
306 pub fn error(store: &Store) -> Function {
307 #[allow(unused)]
308 #[allow(clippy::many_single_char_names)]
309 fn func(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> Result<(), RuntimeError> {
310 // NOTE: We can also get more information why it is failing, see p2str etc here:
311 // https://github.com/iden3/circom_runtime/blob/master/js/witness_calculator.js#L52-L64
312 println!("runtime error, exiting early: {a} {b} {c} {d} {e} {f}",);
313 Err(RuntimeError::user(Box::new(ExitCode(1))))
314 }
315 Function::new_native(store, func)
316 }
317
318 // Circom 2.0
319 pub fn exception_handler(store: &Store) -> Function {
320 #[allow(unused)]
321 fn func(a: i32) {}
322 Function::new_native(store, func)
323 }
324
325 // Circom 2.0
326 pub fn show_memory(store: &Store) -> Function {
327 #[allow(unused)]
328 fn func() {}
329 Function::new_native(store, func)
330 }
331
332 // Circom 2.0
333 pub fn print_error_message(store: &Store) -> Function {
334 #[allow(unused)]
335 fn func() {}
336 Function::new_native(store, func)
337 }
338
339 // Circom 2.0
340 pub fn write_buffer_message(store: &Store) -> Function {
341 #[allow(unused)]
342 fn func() {}
343 Function::new_native(store, func)
344 }
345
346 pub fn log_signal(store: &Store) -> Function {
347 #[allow(unused)]
348 fn func(a: i32, b: i32) {}
349 Function::new_native(store, func)
350 }
351
352 pub fn log_component(store: &Store) -> Function {
353 #[allow(unused)]
354 fn func(a: i32) {}
355 Function::new_native(store, func)
356 }
357}
358
359#[cfg(test)]
360mod tests {
361 use super::*;
362 use std::{collections::HashMap, path::PathBuf};
363
364 struct TestCase<'a> {
365 circuit_path: &'a str,
366 inputs_path: &'a str,
367 n_vars: u32,
368 n64: u32,
369 witness: &'a [&'a str],
370 }
371
372 pub fn root_path(p: &str) -> String {
373 let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
374 path.push(p);
375 path.to_string_lossy().to_string()
376 }
377
378 #[test]
379 fn multiplier_1() {
380 run_test(TestCase {
381 circuit_path: root_path("test-vectors/mycircuit.wasm").as_str(),
382 inputs_path: root_path("test-vectors/mycircuit-input1.json").as_str(),
383 n_vars: 4,
384 n64: 4,
385 witness: &["1", "33", "3", "11"],
386 });
387 }
388
389 #[test]
390 fn multiplier_2() {
391 run_test(TestCase {
392 circuit_path: root_path("test-vectors/mycircuit.wasm").as_str(),
393 inputs_path: root_path("test-vectors/mycircuit-input2.json").as_str(),
394 n_vars: 4,
395 n64: 4,
396 witness: &[
397 "1",
398 "21888242871839275222246405745257275088548364400416034343698204186575672693159",
399 "21888242871839275222246405745257275088548364400416034343698204186575796149939",
400 "11",
401 ],
402 });
403 }
404
405 #[test]
406 fn multiplier_3() {
407 run_test(TestCase {
408 circuit_path: root_path("test-vectors/mycircuit.wasm").as_str(),
409 inputs_path: root_path("test-vectors/mycircuit-input3.json").as_str(),
410 n_vars: 4,
411 n64: 4,
412 witness: &[
413 "1",
414 "21888242871839275222246405745257275088548364400416034343698204186575808493616",
415 "10944121435919637611123202872628637544274182200208017171849102093287904246808",
416 "2",
417 ],
418 });
419 }
420
421 #[test]
422 fn safe_multipler() {
423 let witness =
424 std::fs::read_to_string(&root_path("test-vectors/safe-circuit-witness.json")).unwrap();
425 let witness: Vec<String> = serde_json::from_str(&witness).unwrap();
426 let witness = &witness.iter().map(|x| x.as_ref()).collect::<Vec<_>>();
427 run_test(TestCase {
428 circuit_path: root_path("test-vectors/circuit2.wasm").as_str(),
429 inputs_path: root_path("test-vectors/mycircuit-input1.json").as_str(),
430 n_vars: 132, // 128 + 4
431 n64: 4,
432 witness,
433 });
434 }
435
436 #[test]
437 fn smt_verifier() {
438 let witness =
439 std::fs::read_to_string(&root_path("test-vectors/smtverifier10-witness.json")).unwrap();
440 let witness: Vec<String> = serde_json::from_str(&witness).unwrap();
441 let witness = &witness.iter().map(|x| x.as_ref()).collect::<Vec<_>>();
442
443 run_test(TestCase {
444 circuit_path: root_path("test-vectors/smtverifier10.wasm").as_str(),
445 inputs_path: root_path("test-vectors/smtverifier10-input.json").as_str(),
446 n_vars: 4794,
447 n64: 4,
448 witness,
449 });
450 }
451
452 use serde_json::Value;
453 use std::str::FromStr;
454
455 fn value_to_bigint(v: Value) -> BigInt {
456 match v {
457 Value::String(inner) => BigInt::from_str(&inner).unwrap(),
458 Value::Number(inner) => BigInt::from(inner.as_u64().expect("not a u32")),
459 _ => panic!("unsupported type"),
460 }
461 }
462
463 fn run_test(case: TestCase) {
464 let mut wtns = WitnessCalculator::new(case.circuit_path).unwrap();
465 assert_eq!(
466 wtns.memory.prime.to_str_radix(16),
467 "30644E72E131A029B85045B68181585D2833E84879B9709143E1F593F0000001".to_lowercase()
468 );
469 assert_eq!(wtns.instance.get_n_vars().unwrap() as u32, case.n_vars);
470 assert_eq!(wtns.n64 as u32, case.n64);
471
472 let inputs_str = std::fs::read_to_string(case.inputs_path).unwrap();
473 let inputs: std::collections::HashMap<String, serde_json::Value> =
474 serde_json::from_str(&inputs_str).unwrap();
475
476 let inputs = inputs
477 .iter()
478 .map(|(key, value)| {
479 let res = match value {
480 Value::String(inner) => {
481 vec![BigInt::from_str(inner).unwrap()]
482 }
483 Value::Number(inner) => {
484 vec![BigInt::from(inner.as_u64().expect("not a u32"))]
485 }
486 Value::Array(inner) => inner.iter().cloned().map(value_to_bigint).collect(),
487 _ => panic!(),
488 };
489
490 (key.clone(), res)
491 })
492 .collect::<HashMap<_, _>>();
493
494 let res = wtns.calculate_witness(inputs, false).unwrap();
495 for (r, w) in res.iter().zip(case.witness) {
496 assert_eq!(r, &BigInt::from_str(w).unwrap());
497 }
498 }
499}