|
1 | 1 | // copied and modified by https://github.com/arkworks-rs/circom-compat/blob/master/src/witness/circom.rs |
2 | 2 | use crate::errors::Result; |
3 | | -use wasmer::{Function, Instance, Value}; |
| 3 | +use wasmer::{Function, Instance, Store, Value}; |
4 | 4 |
|
5 | 5 | #[derive(Clone, Debug)] |
6 | 6 | pub struct Wasm(Instance); |
7 | 7 |
|
8 | | -pub trait CircomBase { |
9 | | - fn init(&self, sanity_check: bool) -> Result<()>; |
10 | | - fn func(&self, name: &str) -> &Function; |
11 | | - fn get_ptr_witness_buffer(&self) -> Result<u32>; |
12 | | - fn get_ptr_witness(&self, w: u32) -> Result<u32>; |
13 | | - fn get_signal_offset32( |
14 | | - &self, |
15 | | - p_sig_offset: u32, |
16 | | - component: u32, |
17 | | - hash_msb: u32, |
18 | | - hash_lsb: u32, |
19 | | - ) -> Result<()>; |
20 | | - fn set_signal(&self, c_idx: u32, component: u32, signal: u32, p_val: u32) -> Result<()>; |
21 | | - fn get_u32(&self, name: &str) -> Result<u32>; |
22 | | - // Only exists natively in Circom2, hardcoded for Circom |
23 | | - fn get_version(&self) -> Result<u32>; |
24 | | -} |
25 | | - |
26 | | -pub trait Circom { |
27 | | - fn get_field_num_len32(&self) -> Result<u32>; |
28 | | - fn get_raw_prime(&self) -> Result<()>; |
29 | | - fn read_shared_rw_memory(&self, i: u32) -> Result<u32>; |
30 | | - fn write_shared_rw_memory(&self, i: u32, v: u32) -> Result<()>; |
31 | | - fn set_input_signal(&self, hmsb: u32, hlsb: u32, pos: u32) -> Result<()>; |
32 | | - fn get_witness(&self, i: u32) -> Result<()>; |
33 | | - fn get_witness_size(&self) -> Result<u32>; |
34 | | -} |
35 | | - |
36 | | -impl Circom for Wasm { |
37 | | - fn get_field_num_len32(&self) -> Result<u32> { |
38 | | - self.get_u32("getFieldNumLen32") |
| 8 | +impl Wasm { |
| 9 | + pub(crate) fn get_field_num_len32(&self, store: &mut Store) -> Result<u32> { |
| 10 | + self.get_u32(store, "getFieldNumLen32") |
39 | 11 | } |
40 | 12 |
|
41 | | - fn get_raw_prime(&self) -> Result<()> { |
| 13 | + pub(crate) fn get_raw_prime(&self, store: &mut Store) -> Result<()> { |
42 | 14 | let func = self.func("getRawPrime"); |
43 | | - func.call(&[])?; |
| 15 | + func.call(store, &[])?; |
44 | 16 | Ok(()) |
45 | 17 | } |
46 | 18 |
|
47 | | - fn read_shared_rw_memory(&self, i: u32) -> Result<u32> { |
| 19 | + pub(crate) fn read_shared_rw_memory(&self, store: &mut Store, i: u32) -> Result<u32> { |
48 | 20 | let func = self.func("readSharedRWMemory"); |
49 | | - let result = func.call(&[i.into()])?; |
| 21 | + let result = func.call(store, &[i.into()])?; |
50 | 22 | Ok(result[0].unwrap_i32() as u32) |
51 | 23 | } |
52 | 24 |
|
53 | | - fn write_shared_rw_memory(&self, i: u32, v: u32) -> Result<()> { |
| 25 | + pub(crate) fn write_shared_rw_memory(&self, store: &mut Store, i: u32, v: u32) -> Result<()> { |
54 | 26 | let func = self.func("writeSharedRWMemory"); |
55 | | - func.call(&[i.into(), v.into()])?; |
| 27 | + func.call(store, &[i.into(), v.into()])?; |
56 | 28 | Ok(()) |
57 | 29 | } |
58 | 30 |
|
59 | | - fn set_input_signal(&self, hmsb: u32, hlsb: u32, pos: u32) -> Result<()> { |
| 31 | + pub(crate) fn set_input_signal( |
| 32 | + &self, |
| 33 | + store: &mut Store, |
| 34 | + hmsb: u32, |
| 35 | + hlsb: u32, |
| 36 | + pos: u32, |
| 37 | + ) -> Result<()> { |
60 | 38 | let func = self.func("setInputSignal"); |
61 | | - func.call(&[hmsb.into(), hlsb.into(), pos.into()])?; |
| 39 | + func.call(store, &[hmsb.into(), hlsb.into(), pos.into()])?; |
62 | 40 | Ok(()) |
63 | 41 | } |
64 | 42 |
|
65 | | - fn get_witness(&self, i: u32) -> Result<()> { |
| 43 | + pub(crate) fn get_witness(&self, store: &mut Store, i: u32) -> Result<()> { |
66 | 44 | let func = self.func("getWitness"); |
67 | | - func.call(&[i.into()])?; |
| 45 | + func.call(store, &[i.into()])?; |
68 | 46 | Ok(()) |
69 | 47 | } |
70 | 48 |
|
71 | | - fn get_witness_size(&self) -> Result<u32> { |
72 | | - self.get_u32("getWitnessSize") |
| 49 | + pub(crate) fn get_witness_size(&self, store: &mut Store) -> Result<u32> { |
| 50 | + self.get_u32(store, "getWitnessSize") |
73 | 51 | } |
74 | | -} |
75 | 52 |
|
76 | | -impl CircomBase for Wasm { |
77 | | - fn init(&self, sanity_check: bool) -> Result<()> { |
| 53 | + pub(crate) fn init(&self, store: &mut Store, sanity_check: bool) -> Result<()> { |
78 | 54 | let func = self.func("init"); |
79 | | - func.call(&[Value::I32(sanity_check as i32)])?; |
| 55 | + func.call(store, &[Value::I32(sanity_check as i32)])?; |
80 | 56 | Ok(()) |
81 | 57 | } |
82 | 58 |
|
83 | | - fn get_ptr_witness_buffer(&self) -> Result<u32> { |
84 | | - self.get_u32("getWitnessBuffer") |
85 | | - } |
86 | | - |
87 | | - fn get_ptr_witness(&self, w: u32) -> Result<u32> { |
88 | | - let func = self.func("getPWitness"); |
89 | | - let res = func.call(&[w.into()])?; |
90 | | - |
91 | | - Ok(res[0].unwrap_i32() as u32) |
92 | | - } |
93 | | - |
94 | | - fn get_signal_offset32( |
95 | | - &self, |
96 | | - p_sig_offset: u32, |
97 | | - component: u32, |
98 | | - hash_msb: u32, |
99 | | - hash_lsb: u32, |
100 | | - ) -> Result<()> { |
101 | | - let func = self.func("getSignalOffset32"); |
102 | | - func.call(&[ |
103 | | - p_sig_offset.into(), |
104 | | - component.into(), |
105 | | - hash_msb.into(), |
106 | | - hash_lsb.into(), |
107 | | - ])?; |
108 | | - |
109 | | - Ok(()) |
110 | | - } |
111 | | - |
112 | | - fn set_signal(&self, c_idx: u32, component: u32, signal: u32, p_val: u32) -> Result<()> { |
113 | | - let func = self.func("setSignal"); |
114 | | - func.call(&[c_idx.into(), component.into(), signal.into(), p_val.into()])?; |
115 | | - |
116 | | - Ok(()) |
117 | | - } |
| 59 | + // pub(crate) fn get_ptr_witness_buffer(&self, store: &mut Store) -> Result<u32> { |
| 60 | + // self.get_u32(store, "getWitnessBuffer") |
| 61 | + // } |
| 62 | + |
| 63 | + // pub(crate) fn get_ptr_witness(&self, store: &mut Store, w: u32) -> Result<u32> { |
| 64 | + // let func = self.func( "getPWitness"); |
| 65 | + // let res = func.call(store, &[w.into()])?; |
| 66 | + // |
| 67 | + // Ok(res[0].unwrap_i32() as u32) |
| 68 | + // } |
| 69 | + |
| 70 | + // pub(crate) fn get_signal_offset32( |
| 71 | + // &self, |
| 72 | + // store: &mut Store, |
| 73 | + // p_sig_offset: u32, |
| 74 | + // component: u32, |
| 75 | + // hash_msb: u32, |
| 76 | + // hash_lsb: u32, |
| 77 | + // ) -> Result<()> { |
| 78 | + // let func = self.func( "getSignalOffset32"); |
| 79 | + // func.call( |
| 80 | + // store, |
| 81 | + // &[ |
| 82 | + // p_sig_offset.into(), |
| 83 | + // component.into(), |
| 84 | + // hash_msb.into(), |
| 85 | + // hash_lsb.into(), |
| 86 | + // ], |
| 87 | + // )?; |
| 88 | + // |
| 89 | + // Ok(()) |
| 90 | + // } |
| 91 | + // |
| 92 | + // pub(crate) fn set_signal( |
| 93 | + // &self, |
| 94 | + // store: &mut Store, |
| 95 | + // c_idx: u32, |
| 96 | + // component: u32, |
| 97 | + // signal: u32, |
| 98 | + // p_val: u32, |
| 99 | + // ) -> Result<()> { |
| 100 | + // let func = self.func( "setSignal"); |
| 101 | + // func.call( |
| 102 | + // store, |
| 103 | + // &[c_idx.into(), component.into(), signal.into(), p_val.into()], |
| 104 | + // )?; |
| 105 | + // |
| 106 | + // Ok(()) |
| 107 | + // } |
118 | 108 |
|
119 | 109 | // Default to version 1 if it isn't explicitly defined |
120 | | - fn get_version(&self) -> Result<u32> { |
| 110 | + pub(crate) fn get_version(&self, store: &mut Store) -> Result<u32> { |
121 | 111 | match self.0.exports.get_function("getVersion") { |
122 | | - Ok(func) => Ok(func.call(&[])?[0].unwrap_i32() as u32), |
| 112 | + Ok(func) => Ok(func.call(store, &[])?[0].unwrap_i32() as u32), |
123 | 113 | Err(_) => Ok(1), |
124 | 114 | } |
125 | 115 | } |
126 | 116 |
|
127 | | - fn get_u32(&self, name: &str) -> Result<u32> { |
| 117 | + pub(crate) fn get_u32(&self, store: &mut Store, name: &str) -> Result<u32> { |
128 | 118 | let func = self.func(name); |
129 | | - let result = func.call(&[])?; |
| 119 | + let result = func.call(store, &[])?; |
130 | 120 | Ok(result[0].unwrap_i32() as u32) |
131 | 121 | } |
132 | 122 |
|
133 | | - fn func(&self, name: &str) -> &Function { |
| 123 | + pub(crate) fn func(&self, name: &str) -> &Function { |
134 | 124 | self.0 |
135 | 125 | .exports |
136 | 126 | .get_function(name) |
137 | 127 | .unwrap_or_else(|_| panic!("function {} not found", name)) |
138 | 128 | } |
139 | | -} |
140 | 129 |
|
141 | | -impl Wasm { |
142 | 130 | pub fn new(instance: Instance) -> Self { |
143 | 131 | Self(instance) |
144 | 132 | } |
|
0 commit comments