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 10, 2023
1 parent 1d3fde8 commit e348071
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 276 deletions.
2 changes: 1 addition & 1 deletion algebraic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ num-traits = "0.2.8"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = { version = "1.0", features = [ "arbitrary_precision" ] }
hex = "*"
wasmer = { version = "3.1.1", default-features = false }
wasmer = { version = "3.3.0", default-features = false }
thiserror="1.0"
fnv = { version = "1.0.3", default-features = false }
num = { version = "0.4.0" }
Expand Down
107 changes: 57 additions & 50 deletions algebraic/src/witness/circom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,94 @@ use wasmer::{Function, Instance, 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 {
fn get_field_num_len32(&self) -> Result<u32> {
// 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")
}

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

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

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

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

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

fn get_witness_size(&self) -> Result<u32> {
pub(crate) fn get_witness_size(&self) -> Result<u32> {
self.get_u32("getWitnessSize")
}
}

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

fn get_ptr_witness_buffer(&self) -> Result<u32> {
pub(crate) fn get_ptr_witness_buffer(&self) -> Result<u32> {
self.get_u32("getWitnessBuffer")
}

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

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

fn get_signal_offset32(
pub(crate) fn get_signal_offset32(
&self,
p_sig_offset: u32,
component: u32,
Expand All @@ -109,36 +110,42 @@ impl CircomBase for Wasm {
Ok(())
}

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

Ok(())
}

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

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

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

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

0 comments on commit e348071

Please sign in to comment.