Skip to content

Commit

Permalink
refact: remove redunction code in generate witness
Browse files Browse the repository at this point in the history
  • Loading branch information
SuccinctPaul committed Nov 12, 2023
1 parent 79e0a9c commit 36d75ad
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 177 deletions.
132 changes: 47 additions & 85 deletions algebraic/src/witness/circom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,111 +5,79 @@ use wasmer::{Function, Instance, Store, Value};
#[derive(Clone, Debug)]
pub struct Wasm(Instance);

// pub trait CircomBase {
// fn init(&self, sanity_check: bool) -> Result<()>;
// fn func(&self, name: &str) -> &Function;
// fn get_ptr_witness_buffer(&self) -> Result<u32>;
// fn get_ptr_witness(&self, w: u32) -> Result<u32>;
// fn get_signal_offset32(
// &self,
// p_sig_offset: u32,
// component: u32,
// hash_msb: u32,
// hash_lsb: u32,
// ) -> Result<()>;
// 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
// fn get_version(&self) -> Result<u32>;
// }
//
// pub trait Circom {
// fn get_field_num_len32(&self) -> Result<u32>;
// fn get_raw_prime(&self) -> Result<()>;
// 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 Wasm {
pub(crate) fn get_field_num_len32(&self) -> Result<u32> {
self.get_u32("getFieldNumLen32")
pub(crate) fn get_field_num_len32(&self, store: &mut Store) -> Result<u32> {
self.get_u32(store, "getFieldNumLen32")
}

pub(crate) fn get_raw_prime(&self) -> Result<()> {
let func = self.func("getRawPrime");
let mut store = Store::default();
func.call(&mut store, &[])?;
pub(crate) fn get_raw_prime(&self, store: &mut Store) -> Result<()> {
let func = self.func(store, "getRawPrime");
func.call(store, &[])?;
Ok(())
}

pub(crate) fn read_shared_rw_memory(&self, i: u32) -> Result<u32> {
let func = self.func("readSharedRWMemory");
let mut store = Store::default();
let result = func.call(&mut store, &[i.into()])?;
pub(crate) fn read_shared_rw_memory(&self, store: &mut Store, i: u32) -> Result<u32> {
let func = self.func(store, "readSharedRWMemory");
let result = func.call(store, &[i.into()])?;
Ok(result[0].unwrap_i32() as u32)
}

pub(crate) fn write_shared_rw_memory(&self, i: u32, v: u32) -> Result<()> {
let func = self.func("writeSharedRWMemory");
let mut store = Store::default();
func.call(&mut store, &[i.into(), v.into()])?;
pub(crate) fn write_shared_rw_memory(&self, store: &mut Store, i: u32, v: u32) -> Result<()> {
let func = self.func(store, "writeSharedRWMemory");
func.call(store, &[i.into(), v.into()])?;
Ok(())
}

pub(crate) fn set_input_signal(&self, hmsb: u32, hlsb: u32, pos: u32) -> Result<()> {
let func = self.func("setInputSignal");
let mut store = Store::default();
func.call(&mut store, &[hmsb.into(), hlsb.into(), pos.into()])?;
pub(crate) fn set_input_signal(
&self,
store: &mut Store,
hmsb: u32,
hlsb: u32,
pos: u32,
) -> Result<()> {
let func = self.func(store, "setInputSignal");
func.call(store, &[hmsb.into(), hlsb.into(), pos.into()])?;
Ok(())
}

pub(crate) fn get_witness(&self, i: u32) -> Result<()> {
let func = self.func("getWitness");
let mut store = Store::default();
func.call(&mut store, &[i.into()])?;
pub(crate) fn get_witness(&self, store: &mut Store, i: u32) -> Result<()> {
let func = self.func(store, "getWitness");
func.call(store, &[i.into()])?;
Ok(())
}

pub(crate) fn get_witness_size(&self) -> Result<u32> {
self.get_u32("getWitnessSize")
pub(crate) fn get_witness_size(&self, store: &mut Store) -> Result<u32> {
self.get_u32(store, "getWitnessSize")
}
// }
//
// impl CircomBase for Wasm {
pub(crate) fn init(&self, sanity_check: bool) -> Result<()> {
let func = self.func("init");
let mut store = Store::default();
func.call(&mut store, &[Value::I32(sanity_check as i32)])?;

pub(crate) fn init(&self, store: &mut Store, sanity_check: bool) -> Result<()> {
let func = self.func(store, "init");
func.call(store, &[Value::I32(sanity_check as i32)])?;
Ok(())
}

pub(crate) fn get_ptr_witness_buffer(&self) -> Result<u32> {
self.get_u32("getWitnessBuffer")
pub(crate) fn get_ptr_witness_buffer(&self, store: &mut Store) -> Result<u32> {
self.get_u32(store, "getWitnessBuffer")
}

pub(crate) fn get_ptr_witness(&self, w: u32) -> Result<u32> {
let func = self.func("getPWitness");
let mut store = Store::default();
let res = func.call(&mut store, &[w.into()])?;
pub(crate) fn get_ptr_witness(&self, store: &mut Store, w: u32) -> Result<u32> {
let func = self.func(store, "getPWitness");
let res = func.call(store, &[w.into()])?;

Ok(res[0].unwrap_i32() as u32)
}

pub(crate) fn get_signal_offset32(
&self,
store: &mut Store,
p_sig_offset: u32,
component: u32,
hash_msb: u32,
hash_lsb: u32,
) -> Result<()> {
let func = self.func("getSignalOffset32");
let mut store = Store::default();
let func = self.func(store, "getSignalOffset32");
func.call(
&mut store,
store,
&[
p_sig_offset.into(),
component.into(),
Expand All @@ -123,48 +91,42 @@ impl Wasm {

pub(crate) fn set_signal(
&self,
store: &mut Store,
c_idx: u32,
component: u32,
signal: u32,
p_val: u32,
) -> Result<()> {
let func = self.func("setSignal");
let mut store = Store::default();
let func = self.func(store, "setSignal");
func.call(
&mut store,
store,
&[c_idx.into(), component.into(), signal.into(), p_val.into()],
)?;

Ok(())
}

// Default to version 1 if it isn't explicitly defined
pub(crate) fn get_version(&self) -> Result<u32> {
pub(crate) fn get_version(&self, store: &mut Store) -> Result<u32> {
match self.0.exports.get_function("getVersion") {
Ok(func) => {
let mut store = Store::default();
Ok(func.call(&mut store, &[])?[0].unwrap_i32() as u32)
}
Ok(func) => Ok(func.call(store, &[])?[0].unwrap_i32() as u32),
Err(_) => Ok(1),
}
}

pub(crate) fn get_u32(&self, name: &str) -> Result<u32> {
let func = self.func(name);
let mut store = Store::default();
let result = func.call(&mut store, &[])?;
pub(crate) fn get_u32(&self, store: &mut Store, name: &str) -> Result<u32> {
let func = self.func(store, name);
let result = func.call(store, &[])?;
Ok(result[0].unwrap_i32() as u32)
}

pub(crate) fn func(&self, name: &str) -> &Function {
pub(crate) fn func(&self, store: &mut Store, name: &str) -> &Function {
self.0
.exports
.get_function(name)
.unwrap_or_else(|_| panic!("function {} not found", name))
}
// }
//
// impl Wasm {

pub fn new(instance: Instance) -> Self {
Self(instance)
}
Expand Down
Loading

0 comments on commit 36d75ad

Please sign in to comment.