Skip to main content

ark_circom/witness/
circom.rs

1use color_eyre::Result;
2use wasmer::{Function, Instance, Store, Value};
3
4#[derive(Debug)]
5pub struct Wasm {
6    pub instance: Instance,
7}
8
9impl Wasm {
10    pub(crate) fn get_field_num_len32(&self, store: &mut Store) -> Result<u32> {
11        self.get_u32(store, "getFieldNumLen32")
12    }
13
14    pub(crate) fn get_raw_prime(&self, store: &mut Store) -> Result<()> {
15        let func = self.func("getRawPrime");
16        func.call(store, &[])?;
17        Ok(())
18    }
19
20    pub(crate) fn read_shared_rw_memory(&self, store: &mut Store, i: u32) -> Result<u32> {
21        let func = self.func("readSharedRWMemory");
22        let result = func.call(store, &[i.into()])?;
23        Ok(result[0].unwrap_i32() as u32)
24    }
25
26    pub(crate) fn write_shared_rw_memory(&self, store: &mut Store, i: u32, v: u32) -> Result<()> {
27        let func = self.func("writeSharedRWMemory");
28        func.call(store, &[i.into(), v.into()])?;
29        Ok(())
30    }
31
32    pub(crate) fn set_input_signal(
33        &self,
34        store: &mut Store,
35        hmsb: u32,
36        hlsb: u32,
37        pos: u32,
38    ) -> Result<()> {
39        let func = self.func("setInputSignal");
40        func.call(store, &[hmsb.into(), hlsb.into(), pos.into()])?;
41        Ok(())
42    }
43
44    pub(crate) fn get_witness(&self, store: &mut Store, i: u32) -> Result<()> {
45        let func = self.func("getWitness");
46        func.call(store, &[i.into()])?;
47        Ok(())
48    }
49
50    pub(crate) fn get_witness_size(&self, store: &mut Store) -> Result<u32> {
51        self.get_u32(store, "getWitnessSize")
52    }
53
54    pub(crate) fn init(&self, store: &mut Store, sanity_check: bool) -> Result<()> {
55        let func = self.func("init");
56        func.call(store, &[Value::I32(sanity_check as i32)])?;
57        Ok(())
58    }
59
60    fn get_u32(&self, store: &mut Store, name: &str) -> Result<u32> {
61        let func = &self.func(name);
62        let result = func.call(store, &[])?;
63        Ok(result[0].unwrap_i32() as u32)
64    }
65
66    fn func(&self, name: &str) -> &Function {
67        self.instance
68            .exports
69            .get_function(name)
70            .unwrap_or_else(|_| panic!("function {name} not found"))
71    }
72}
73
74impl Wasm {
75    pub fn new(instance: Instance) -> Self {
76        Self { instance }
77    }
78}