|
@ -7,9 +7,9 @@ pub struct Wasm(Instance); |
|
|
pub trait CircomBase {
|
|
|
pub trait CircomBase {
|
|
|
fn init(&self, sanity_check: bool) -> Result<()>;
|
|
|
fn init(&self, sanity_check: bool) -> Result<()>;
|
|
|
fn func(&self, name: &str) -> &Function;
|
|
|
fn func(&self, name: &str) -> &Function;
|
|
|
fn get_ptr_witness_buffer(&self) -> Result<i32>;
|
|
|
|
|
|
fn get_ptr_witness(&self, w: i32) -> Result<i32>;
|
|
|
|
|
|
fn get_n_vars(&self) -> Result<i32>;
|
|
|
|
|
|
|
|
|
fn get_ptr_witness_buffer(&self) -> Result<u32>;
|
|
|
|
|
|
fn get_ptr_witness(&self, w: u32) -> Result<u32>;
|
|
|
|
|
|
fn get_n_vars(&self) -> Result<u32>;
|
|
|
fn get_signal_offset32(
|
|
|
fn get_signal_offset32(
|
|
|
&self,
|
|
|
&self,
|
|
|
p_sig_offset: u32,
|
|
|
p_sig_offset: u32,
|
|
@ -17,41 +17,41 @@ pub trait CircomBase { |
|
|
hash_msb: u32,
|
|
|
hash_msb: u32,
|
|
|
hash_lsb: u32,
|
|
|
hash_lsb: u32,
|
|
|
) -> Result<()>;
|
|
|
) -> Result<()>;
|
|
|
fn set_signal(&self, c_idx: i32, component: i32, signal: i32, p_val: i32) -> Result<()>;
|
|
|
|
|
|
fn get_i32(&self, name: &str) -> Result<i32>;
|
|
|
|
|
|
|
|
|
fn set_signal(&self, c_idx: u32, component: u32, signal: u32, p_val: u32) -> Result<()>;
|
|
|
|
|
|
fn get_u32(&self, name: &str) -> Result<u32>;
|
|
|
// Only exists natively in Circom2, hardcoded for Circom
|
|
|
// Only exists natively in Circom2, hardcoded for Circom
|
|
|
fn get_version(&self) -> Result<i32>;
|
|
|
|
|
|
|
|
|
fn get_version(&self) -> Result<u32>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub trait Circom {
|
|
|
pub trait Circom {
|
|
|
fn get_fr_len(&self) -> Result<i32>;
|
|
|
|
|
|
fn get_ptr_raw_prime(&self) -> Result<i32>;
|
|
|
|
|
|
|
|
|
fn get_fr_len(&self) -> Result<u32>;
|
|
|
|
|
|
fn get_ptr_raw_prime(&self) -> Result<u32>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub trait Circom2 {
|
|
|
pub trait Circom2 {
|
|
|
fn get_field_num_len32(&self) -> Result<i32>;
|
|
|
|
|
|
|
|
|
fn get_field_num_len32(&self) -> Result<u32>;
|
|
|
fn get_raw_prime(&self) -> Result<()>;
|
|
|
fn get_raw_prime(&self) -> Result<()>;
|
|
|
fn read_shared_rw_memory(&self, i: i32) -> Result<i32>;
|
|
|
|
|
|
fn write_shared_rw_memory(&self, i: i32, v: i32) -> Result<()>;
|
|
|
|
|
|
fn set_input_signal(&self, hmsb: i32, hlsb: i32, pos: i32) -> Result<()>;
|
|
|
|
|
|
fn get_witness(&self, i: i32) -> Result<()>;
|
|
|
|
|
|
fn get_witness_size(&self) -> Result<i32>;
|
|
|
|
|
|
|
|
|
fn read_shared_rw_memory(&self, i: u32) -> Result<u32>;
|
|
|
|
|
|
fn write_shared_rw_memory(&self, i: u32, v: u32) -> Result<()>;
|
|
|
|
|
|
fn set_input_signal(&self, hmsb: u32, hlsb: u32, pos: u32) -> Result<()>;
|
|
|
|
|
|
fn get_witness(&self, i: u32) -> Result<()>;
|
|
|
|
|
|
fn get_witness_size(&self) -> Result<u32>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl Circom for Wasm {
|
|
|
impl Circom for Wasm {
|
|
|
fn get_fr_len(&self) -> Result<i32> {
|
|
|
|
|
|
self.get_i32("getFrLen")
|
|
|
|
|
|
|
|
|
fn get_fr_len(&self) -> Result<u32> {
|
|
|
|
|
|
self.get_u32("getFrLen")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_ptr_raw_prime(&self) -> Result<i32> {
|
|
|
|
|
|
self.get_i32("getPRawPrime")
|
|
|
|
|
|
|
|
|
fn get_ptr_raw_prime(&self) -> Result<u32> {
|
|
|
|
|
|
self.get_u32("getPRawPrime")
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#[cfg(feature = "circom-2")]
|
|
|
#[cfg(feature = "circom-2")]
|
|
|
impl Circom2 for Wasm {
|
|
|
impl Circom2 for Wasm {
|
|
|
fn get_field_num_len32(&self) -> Result<i32> {
|
|
|
|
|
|
self.get_i32("getFieldNumLen32")
|
|
|
|
|
|
|
|
|
fn get_field_num_len32(&self) -> Result<u32> {
|
|
|
|
|
|
self.get_u32("getFieldNumLen32")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_raw_prime(&self) -> Result<()> {
|
|
|
fn get_raw_prime(&self) -> Result<()> {
|
|
@ -60,34 +60,32 @@ impl Circom2 for Wasm { |
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn read_shared_rw_memory(&self, i: i32) -> Result<i32> {
|
|
|
|
|
|
|
|
|
fn read_shared_rw_memory(&self, i: u32) -> Result<u32> {
|
|
|
let func = self.func("readSharedRWMemory");
|
|
|
let func = self.func("readSharedRWMemory");
|
|
|
let result = func.call(&[i.into()])?;
|
|
|
let result = func.call(&[i.into()])?;
|
|
|
Ok(result[0].unwrap_i32())
|
|
|
|
|
|
|
|
|
Ok(result[0].unwrap_i32() as u32)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn write_shared_rw_memory(&self, i: i32, v: i32) -> Result<()> {
|
|
|
|
|
|
|
|
|
fn write_shared_rw_memory(&self, i: u32, v: u32) -> Result<()> {
|
|
|
let func = self.func("writeSharedRWMemory");
|
|
|
let func = self.func("writeSharedRWMemory");
|
|
|
func.call(&[i.into(), v.into()])?;
|
|
|
func.call(&[i.into(), v.into()])?;
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn set_input_signal(&self, hmsb: i32, hlsb: i32, pos: i32) -> Result<()> {
|
|
|
|
|
|
|
|
|
fn set_input_signal(&self, hmsb: u32, hlsb: u32, pos: u32) -> Result<()> {
|
|
|
let func = self.func("setInputSignal");
|
|
|
let func = self.func("setInputSignal");
|
|
|
func.call(&[hmsb.into(), hlsb.into(), pos.into()])?;
|
|
|
func.call(&[hmsb.into(), hlsb.into(), pos.into()])?;
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_witness(&self, i: i32) -> Result<()> {
|
|
|
|
|
|
|
|
|
fn get_witness(&self, i: u32) -> Result<()> {
|
|
|
let func = self.func("getWitness");
|
|
|
let func = self.func("getWitness");
|
|
|
func.call(&[i.into()])?;
|
|
|
func.call(&[i.into()])?;
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_witness_size(&self) -> Result<i32> {
|
|
|
|
|
|
let func = self.func("getWitnessSize");
|
|
|
|
|
|
let result = func.call(&[])?;
|
|
|
|
|
|
Ok(result[0].unwrap_i32())
|
|
|
|
|
|
|
|
|
fn get_witness_size(&self) -> Result<u32> {
|
|
|
|
|
|
self.get_u32("getWitnessSize")
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
@ -98,19 +96,19 @@ impl CircomBase for Wasm { |
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_ptr_witness_buffer(&self) -> Result<i32> {
|
|
|
|
|
|
self.get_i32("getWitnessBuffer")
|
|
|
|
|
|
|
|
|
fn get_ptr_witness_buffer(&self) -> Result<u32> {
|
|
|
|
|
|
self.get_u32("getWitnessBuffer")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_ptr_witness(&self, w: i32) -> Result<i32> {
|
|
|
|
|
|
|
|
|
fn get_ptr_witness(&self, w: u32) -> Result<u32> {
|
|
|
let func = self.func("getPWitness");
|
|
|
let func = self.func("getPWitness");
|
|
|
let res = func.call(&[w.into()])?;
|
|
|
let res = func.call(&[w.into()])?;
|
|
|
|
|
|
|
|
|
Ok(res[0].unwrap_i32())
|
|
|
|
|
|
|
|
|
Ok(res[0].unwrap_i32() as u32)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_n_vars(&self) -> Result<i32> {
|
|
|
|
|
|
self.get_i32("getNVars")
|
|
|
|
|
|
|
|
|
fn get_n_vars(&self) -> Result<u32> {
|
|
|
|
|
|
self.get_u32("getNVars")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_signal_offset32(
|
|
|
fn get_signal_offset32(
|
|
@ -131,7 +129,7 @@ impl CircomBase for Wasm { |
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn set_signal(&self, c_idx: i32, component: i32, signal: i32, p_val: i32) -> Result<()> {
|
|
|
|
|
|
|
|
|
fn set_signal(&self, c_idx: u32, component: u32, signal: u32, p_val: u32) -> Result<()> {
|
|
|
let func = self.func("setSignal");
|
|
|
let func = self.func("setSignal");
|
|
|
func.call(&[c_idx.into(), component.into(), signal.into(), p_val.into()])?;
|
|
|
func.call(&[c_idx.into(), component.into(), signal.into(), p_val.into()])?;
|
|
|
|
|
|
|
|
@ -139,17 +137,17 @@ impl CircomBase for Wasm { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// Default to version 1 if it isn't explicitly defined
|
|
|
// Default to version 1 if it isn't explicitly defined
|
|
|
fn get_version(&self) -> Result<i32> {
|
|
|
|
|
|
|
|
|
fn get_version(&self) -> Result<u32> {
|
|
|
match self.0.exports.get_function("getVersion") {
|
|
|
match self.0.exports.get_function("getVersion") {
|
|
|
Ok(func) => Ok(func.call(&[])?[0].unwrap_i32()),
|
|
|
|
|
|
|
|
|
Ok(func) => Ok(func.call(&[])?[0].unwrap_i32() as u32),
|
|
|
Err(_) => Ok(1),
|
|
|
Err(_) => Ok(1),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_i32(&self, name: &str) -> Result<i32> {
|
|
|
|
|
|
|
|
|
fn get_u32(&self, name: &str) -> Result<u32> {
|
|
|
let func = self.func(name);
|
|
|
let func = self.func(name);
|
|
|
let result = func.call(&[])?;
|
|
|
let result = func.call(&[])?;
|
|
|
Ok(result[0].unwrap_i32())
|
|
|
|
|
|
|
|
|
Ok(result[0].unwrap_i32() as u32)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn func(&self, name: &str) -> &Function {
|
|
|
fn func(&self, name: &str) -> &Function {
|
|
|