circom-compat/src/witness/circom.rs
src/witness/circom.rsRaw
1use color_eyre::Result;
2use wasmer::{Function, Instance, Value};
3
4#[derive(Clone, Debug)]
5pub struct Wasm(Instance);
6
7impl Wasm {
8 pub fn new(instance: Instance) -> Self {
9 Self(instance)
10 }
11
12 pub fn init(&self, sanity_check: bool) -> Result<()> {
13 let func = self.func("init");
14 func.call(&[Value::I32(sanity_check as i32)])?;
15 Ok(())
16 }
17
18 pub fn get_fr_len(&self) -> Result<i32> {
19 self.get_i32("getFrLen")
20 }
21
22 pub fn get_ptr_raw_prime(&self) -> Result<i32> {
23 self.get_i32("getPRawPrime")
24 }
25
26 pub fn get_n_vars(&self) -> Result<i32> {
27 self.get_i32("getNVars")
28 }
29
30 pub fn get_ptr_witness_buffer(&self) -> Result<i32> {
31 self.get_i32("getWitnessBuffer")
32 }
33
34 pub fn get_ptr_witness(&self, w: i32) -> Result<i32> {
35 let func = self.func("getPWitness");
36 let res = func.call(&[w.into()])?;
37
38 Ok(res[0].unwrap_i32())
39 }
40
41 pub fn get_signal_offset32(
42 &self,
43 p_sig_offset: u32,
44 component: u32,
45 hash_msb: u32,
46 hash_lsb: u32,
47 ) -> Result<()> {
48 let func = self.func("getSignalOffset32");
49 func.call(&[
50 p_sig_offset.into(),
51 component.into(),
52 hash_msb.into(),
53 hash_lsb.into(),
54 ])?;
55
56 Ok(())
57 }
58
59 pub fn set_signal(&self, c_idx: i32, component: i32, signal: i32, p_val: i32) -> Result<()> {
60 let func = self.func("setSignal");
61 func.call(&[c_idx.into(), component.into(), signal.into(), p_val.into()])?;
62
63 Ok(())
64 }
65
66 fn get_i32(&self, name: &str) -> Result<i32> {
67 let func = self.func(name);
68 let result = func.call(&[])?;
69 Ok(result[0].unwrap_i32())
70 }
71
72 fn func(&self, name: &str) -> &Function {
73 self.0
74 .exports
75 .get_function(name)
76 .unwrap_or_else(|_| panic!("function {} not found", name))
77 }
78}