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