Skip to content

Commit

Permalink
feat: lower the log level (#157)
Browse files Browse the repository at this point in the history
* replace debug! with trace

* replace debug! with trace

* update wasmer version to remove the wasmer's info level log

* refact: remove redunction code in generate witness

* refact: remove redunction code in generate witness

* refact: remove redunction code in generate witness

* refact: remove redunction code in generate witness

* fmt

* another try

* fmt
  • Loading branch information
SuccinctPaul authored Nov 12, 2023
1 parent 9161453 commit 8263e14
Show file tree
Hide file tree
Showing 50 changed files with 360 additions and 656 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 = "2.0", 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
4 changes: 2 additions & 2 deletions algebraic/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn load_witness_from_bin_reader<E: ScalarEngine, R: Read>(mut reader: R) ->
return Err(EigenError::from("Invalid file header".to_string()));
}
let version = reader.read_u32::<LittleEndian>()?;
log::debug!("wtns version {}", version);
log::trace!("wtns version {}", version);
if version > 2 {
return Err(EigenError::from("unsupported file version".to_string()));
}
Expand All @@ -126,7 +126,7 @@ pub fn load_witness_from_bin_reader<E: ScalarEngine, R: Read>(mut reader: R) ->
return Err(EigenError::from("invalid curve prime".to_string()));
}
let witness_len = reader.read_u32::<LittleEndian>()?;
log::debug!("witness len {}", witness_len);
log::trace!("witness len {}", witness_len);
let sec_type = reader.read_u32::<LittleEndian>()?;
if sec_type != 2 {
return Err(EigenError::from("invalid section type".to_string()));
Expand Down
168 changes: 78 additions & 90 deletions algebraic/src/witness/circom.rs
Original file line number Diff line number Diff line change
@@ -1,144 +1,132 @@
// copied and modified by https://github.com/arkworks-rs/circom-compat/blob/master/src/witness/circom.rs
use crate::errors::Result;
use wasmer::{Function, Instance, Value};
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 {
fn get_field_num_len32(&self) -> Result<u32> {
self.get_u32("getFieldNumLen32")
impl Wasm {
pub(crate) fn get_field_num_len32(&self, store: &mut Store) -> Result<u32> {
self.get_u32(store, "getFieldNumLen32")
}

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

fn read_shared_rw_memory(&self, i: u32) -> Result<u32> {
pub(crate) fn read_shared_rw_memory(&self, store: &mut Store, i: u32) -> Result<u32> {
let func = self.func("readSharedRWMemory");
let result = func.call(&[i.into()])?;
let result = func.call(store, &[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, store: &mut Store, i: u32, v: u32) -> Result<()> {
let func = self.func("writeSharedRWMemory");
func.call(&[i.into(), v.into()])?;
func.call(store, &[i.into(), v.into()])?;
Ok(())
}

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

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

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

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

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(
&self,
p_sig_offset: u32,
component: u32,
hash_msb: u32,
hash_lsb: u32,
) -> Result<()> {
let func = self.func("getSignalOffset32");
func.call(&[
p_sig_offset.into(),
component.into(),
hash_msb.into(),
hash_lsb.into(),
])?;

Ok(())
}

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(())
}
// 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, store: &mut Store, w: u32) -> Result<u32> {
// let func = self.func( "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");
// func.call(
// store,
// &[
// p_sig_offset.into(),
// component.into(),
// hash_msb.into(),
// hash_lsb.into(),
// ],
// )?;
//
// Ok(())
// }
//
// 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");
// func.call(
// store,
// &[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, store: &mut Store) -> Result<u32> {
match self.0.exports.get_function("getVersion") {
Ok(func) => Ok(func.call(&[])?[0].unwrap_i32() as u32),
Ok(func) => Ok(func.call(store, &[])?[0].unwrap_i32() as u32),
Err(_) => Ok(1),
}
}

fn get_u32(&self, name: &str) -> Result<u32> {
pub(crate) fn get_u32(&self, store: &mut Store, name: &str) -> Result<u32> {
let func = self.func(name);
let result = func.call(&[])?;
let result = func.call(store, &[])?;
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 {
pub fn new(instance: Instance) -> Self {
Self(instance)
}
Expand Down
Loading

0 comments on commit 8263e14

Please sign in to comment.