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 e348071 commit b9a0c17
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
53 changes: 36 additions & 17 deletions algebraic/src/witness/circom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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);
Expand Down Expand Up @@ -41,31 +41,36 @@ impl Wasm {

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

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

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

Expand All @@ -77,7 +82,8 @@ impl Wasm {
// 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)])?;
let mut store = Store::default();
func.call(&mut store, &[Value::I32(sanity_check as i32)])?;
Ok(())
}

Expand All @@ -87,7 +93,8 @@ impl Wasm {

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

Ok(res[0].unwrap_i32() as u32)
}
Expand All @@ -100,12 +107,16 @@ impl Wasm {
hash_lsb: u32,
) -> Result<()> {
let func = self.func("getSignalOffset32");
func.call(&[
p_sig_offset.into(),
component.into(),
hash_msb.into(),
hash_lsb.into(),
])?;
let mut store = Store::default();
func.call(
&mut store,
&[
p_sig_offset.into(),
component.into(),
hash_msb.into(),
hash_lsb.into(),
],
)?;

Ok(())
}
Expand All @@ -118,22 +129,30 @@ impl Wasm {
p_val: u32,
) -> Result<()> {
let func = self.func("setSignal");
func.call(&[c_idx.into(), component.into(), signal.into(), p_val.into()])?;
let mut store = Store::default();
func.call(
&mut 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> {
match self.0.exports.get_function("getVersion") {
Ok(func) => Ok(func.call(&[])?[0].unwrap_i32() as u32),
Ok(func) => {
let mut store = Store::default();
Ok(func.call(&mut 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 result = func.call(&[])?;
let mut store = Store::default();
let result = func.call(&mut store, &[])?;
Ok(result[0].unwrap_i32() as u32)
}

Expand Down
4 changes: 2 additions & 2 deletions algebraic/src/witness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub(crate) mod memory;
// pub(super) use memory::SafeMemory;

mod circom;
pub(super) use circom::{CircomBase, Wasm};
// pub(super) use circom::{CircomBase, Wasm};

pub(super) use circom::Circom;
// pub(super) use circom::Circom;

use fnv::FnvHasher;
use std::hash::Hasher;
Expand Down
52 changes: 31 additions & 21 deletions algebraic/src/witness/witness_calculator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// copied and modified by https://github.com/arkworks-rs/circom-compat/blob/master/src/witness/witness_calculator.rs
use super::Circom;
use super::{fnv, CircomBase, Wasm};
// use super::Circom;
// use super::{fnv, Wasm};
use crate::bellman_ce::{PrimeField, ScalarEngine};
use crate::errors::{EigenError, Result};
use crate::witness::memory::SafeMemory;
use crate::witness::{circom::Wasm, fnv, memory::SafeMemory};
use num::ToPrimitive;
use num_bigint::BigInt;
use num_bigint::BigUint;
Expand All @@ -17,6 +17,7 @@ use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, Store};
use std::fs::OpenOptions;
#[cfg(not(feature = "wasm"))]
use std::io::{BufWriter, Write};
use std::thread::scope;

use byteorder::{LittleEndian, WriteBytesExt};

Expand Down Expand Up @@ -74,19 +75,28 @@ impl WitnessCalculator {
},
// Host function callbacks from the WASM
"runtime" => {
"error" => runtime::error(store),
"logSetSignal" => runtime::log_signal(store),
"logGetSignal" => runtime::log_signal(store),
"logFinishComponent" => runtime::log_component(store),
"logStartComponent" => runtime::log_component(store),
"log" => runtime::log_component(store),
"exceptionHandler" => runtime::exception_handler(store),
"showSharedRWMemory" => runtime::show_memory(store),
"printErrorMessage" => runtime::print_error_message(store),
"writeBufferMessage" => runtime::write_buffer_message(store),
"error" => runtime::error(&mut store),
"logSetSignal" => runtime::log_signal(&mut store),
"logGetSignal" => runtime::log_signal(&mut store),
"logFinishComponent" => runtime::log_component(&mut store),
"logStartComponent" => runtime::log_component(&mut store),
"log" => runtime::log_component(&mut store),
"exceptionHandler" => runtime::exception_handler(&mut store),
"showSharedRWMemory" => runtime::show_memory(&mut store),
"printErrorMessage" => runtime::print_error_message(&mut store),
"writeBufferMessage" => runtime::write_buffer_message(&mut store),
}
};
let instance = Wasm::new(Instance::new(&module, &import_object)?);
let instance = Wasm::new(Instance::new(&mut scope, &module, &import_object)?);
let mut store = Store::default();
let env = FunctionEnv::new(&mut store, ());
let module = Module::new(&store, "(module)")?;
let imports = imports! {
"host" => {
"var" => Global::new(&mut store, Value::I32(2))
}
};
let instance = Instance::new(&mut store, &module, &imports)?;

// Circom 2 feature flag with version 2
fn new_circom(instance: Wasm, memory: Memory) -> Result<WitnessCalculator> {
Expand Down Expand Up @@ -331,7 +341,7 @@ pub fn flat_array(v: &[Value]) -> Vec<BigInt> {
mod runtime {
use super::*;

pub fn error(store: &Store) -> Function {
pub fn error(store: &mut Store) -> Function {
#[allow(unused)]
#[allow(clippy::many_single_char_names)]
fn func(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> Result<()> {
Expand All @@ -346,40 +356,40 @@ mod runtime {
}

// Circom 2.0
pub fn exception_handler(store: &Store) -> Function {
pub fn exception_handler(store: &mut Store) -> Function {
#[allow(unused)]
fn func(a: i32) {}
Function::new_native(store, func)
}

// Circom 2.0
pub fn show_memory(store: &Store) -> Function {
pub fn show_memory(store: &mut Store) -> Function {
#[allow(unused)]
fn func() {}
Function::new_native(store, func)
}

// Circom 2.0
pub fn print_error_message(store: &Store) -> Function {
pub fn print_error_message(store: &mut Store) -> Function {
#[allow(unused)]
fn func() {}
Function::new_native(store, func)
}

// Circom 2.0
pub fn write_buffer_message(store: &Store) -> Function {
pub fn write_buffer_message(store: &mut Store) -> Function {
#[allow(unused)]
fn func() {}
Function::new_native(store, func)
}

pub fn log_signal(store: &Store) -> Function {
pub fn log_signal(store: &mut Store) -> Function {
#[allow(unused)]
fn func(a: i32, b: i32) {}
Function::new_native(store, func)
}

pub fn log_component(store: &Store) -> Function {
pub fn log_component(store: &mut Store) -> Function {
#[allow(unused)]
fn func(a: i32) {}
Function::new_native(store, func)
Expand Down

0 comments on commit b9a0c17

Please sign in to comment.