From 986e108bd021237fd0fdd8eaad6ec909f2d93a9b Mon Sep 17 00:00:00 2001 From: Po Date: Mon, 25 Sep 2023 12:58:19 +0200 Subject: [PATCH 1/3] fix dry_run time consuming bug --- crates/cli/src/args.rs | 3 ++- crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/cli/src/args.rs b/crates/cli/src/args.rs index cd71c6e8b..53e42b18c 100644 --- a/crates/cli/src/args.rs +++ b/crates/cli/src/args.rs @@ -62,7 +62,7 @@ pub fn parse_args(values: Vec<&str>) -> Vec { pub fn parse_binary(filepath:String) -> Vec { let bytes = fs::read(filepath).unwrap(); let bytes = bytes.chunks(8); - let data = bytes + let mut data = bytes .into_iter() .map(|x| { let mut data = [0u8; 8]; @@ -71,6 +71,7 @@ pub fn parse_binary(filepath:String) -> Vec { u64::from_be_bytes(data) }) .collect::>(); + data.reverse(); data } diff --git a/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs b/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs index 6a4fac21e..eddcd7fb6 100644 --- a/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs +++ b/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs @@ -43,7 +43,7 @@ impl Context { if self.private_inputs.is_empty() { panic!("failed to read private input, please checkout your input"); } - self.private_inputs.remove(0) + self.private_inputs.pop().unwrap() } pub fn push_public(&mut self, value: u64) { From a3ce87783e77697f7f8add448730db09f5ec7c4b Mon Sep 17 00:00:00 2001 From: Po Date: Mon, 25 Sep 2023 14:15:45 +0200 Subject: [PATCH 2/3] add wasm_output print --- crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs b/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs index eddcd7fb6..c9258a04d 100644 --- a/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs +++ b/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs @@ -82,7 +82,9 @@ pub fn register_wasm_input_foreign( context.push_public(value); value } else { - context.pop_private() + let value = context.pop_private(); + println!("wasm_output:{:?}", value); + value }; Some(wasmi::RuntimeValue::I64(input as i64)) From 326e2992944f54051910c462049914755964beb2 Mon Sep 17 00:00:00 2001 From: Po Date: Tue, 26 Sep 2023 08:30:36 +0200 Subject: [PATCH 3/3] replace vec with vecdeque to avoid reverse bytes --- crates/cli/src/app_builder.rs | 9 +++++---- crates/cli/src/args.rs | 14 +++++++------- crates/cli/src/exec.rs | 9 +++++---- crates/cli/src/main.rs | 8 +++++--- crates/zkwasm/src/foreign/mod.rs | 3 ++- .../src/foreign/wasm_input_helper/runtime.rs | 13 ++++++------- crates/zkwasm/src/loader/mod.rs | 11 ++++++----- 7 files changed, 36 insertions(+), 31 deletions(-) diff --git a/crates/cli/src/app_builder.rs b/crates/cli/src/app_builder.rs index 289b6da0b..efb39a31a 100644 --- a/crates/cli/src/app_builder.rs +++ b/crates/cli/src/app_builder.rs @@ -5,6 +5,7 @@ use delphinus_zkwasm::circuits::config::MIN_K; use log::info; use log::warn; use std::cell::RefCell; +use std::collections::VecDeque; use std::fs; use std::io::Write; use std::path::PathBuf; @@ -115,8 +116,8 @@ pub trait AppBuilder: CommandBuilder { } Some(("dry-run", sub_matches)) => { let public_inputs: Vec = Self::parse_single_public_arg(&sub_matches); - let private_inputs: Vec = match sub_matches.contains_id("private_file") { - true => Self::parse_private_file(&sub_matches), + let private_inputs: VecDeque = match sub_matches.contains_id("private_file") { + true => Self::parse_private_file(&sub_matches).into(), false => Self::parse_single_private_arg(&sub_matches) }; let context_in: Vec = Self::parse_context_in_arg(&sub_matches); @@ -156,7 +157,7 @@ pub trait AppBuilder: CommandBuilder { } Some(("single-prove", sub_matches)) => { let public_inputs: Vec = Self::parse_single_public_arg(&sub_matches); - let private_inputs: Vec = Self::parse_single_private_arg(&sub_matches); + let private_inputs: VecDeque = Self::parse_single_private_arg(&sub_matches); let context_in: Vec = Self::parse_context_in_arg(&sub_matches); let context_out_path: Option = Self::parse_context_out_path_arg(&sub_matches); @@ -197,7 +198,7 @@ pub trait AppBuilder: CommandBuilder { } Some(("aggregate-prove", sub_matches)) => { let public_inputs: Vec> = Self::parse_aggregate_public_args(&sub_matches); - let private_inputs: Vec> = + let private_inputs: Vec> = Self::parse_aggregate_private_args(&sub_matches); let context_inputs = public_inputs.iter().map(|_| vec![]).collect(); let context_outputs = public_inputs diff --git a/crates/cli/src/args.rs b/crates/cli/src/args.rs index 53e42b18c..d6765e04c 100644 --- a/crates/cli/src/args.rs +++ b/crates/cli/src/args.rs @@ -6,6 +6,7 @@ use clap::Arg; use clap::ArgAction; use clap::ArgMatches; use std::fs; +use std::collections::VecDeque; pub fn parse_args(values: Vec<&str>) -> Vec { values @@ -59,10 +60,10 @@ pub fn parse_args(values: Vec<&str>) -> Vec { .collect() } -pub fn parse_binary(filepath:String) -> Vec { +pub fn parse_binary(filepath:String) -> VecDeque { let bytes = fs::read(filepath).unwrap(); let bytes = bytes.chunks(8); - let mut data = bytes + let data = bytes .into_iter() .map(|x| { let mut data = [0u8; 8]; @@ -70,8 +71,7 @@ pub fn parse_binary(filepath:String) -> Vec { u64::from_be_bytes(data) }) - .collect::>(); - data.reverse(); + .collect::>(); data } @@ -175,10 +175,10 @@ pub trait ArgBuilder { fn parse_aggregate_public_args(matches: &ArgMatches) -> Vec>; fn single_private_arg<'a>() -> Arg<'a>; - fn parse_single_private_arg(matches: &ArgMatches) -> Vec; + fn parse_single_private_arg(matches: &ArgMatches) -> VecDeque; fn private_file_arg<'a>() -> Arg<'a>; - fn parse_private_file(matches: &ArgMatches) -> Vec{ + fn parse_private_file(matches: &ArgMatches) -> VecDeque{ let filepath = matches .get_one::("private_file") .expect("private_file is required") @@ -187,7 +187,7 @@ pub trait ArgBuilder { } fn aggregate_private_args<'a>() -> Arg<'a>; - fn parse_aggregate_private_args(matches: &ArgMatches) -> Vec>; + fn parse_aggregate_private_args(matches: &ArgMatches) -> Vec>; fn single_instance_path_arg<'a>() -> Arg<'a> { arg!( diff --git a/crates/cli/src/exec.rs b/crates/cli/src/exec.rs index 44bd48017..78b570e99 100644 --- a/crates/cli/src/exec.rs +++ b/crates/cli/src/exec.rs @@ -29,6 +29,7 @@ use notify::Watcher; use serde::Deserialize; use serde::Serialize; use std::cell::RefCell; +use std::collections::VecDeque; use std::fs; use std::io::Write; use std::path::Path; @@ -163,7 +164,7 @@ pub fn exec_dry_run_service( let private_inputs = parse_args( sequence.private_inputs.iter().map(|s| s.as_str()).collect(), - ); + ).into(); let public_inputs = parse_args( sequence.public_inputs.iter().map(|s| s.as_str()).collect(), ); @@ -228,7 +229,7 @@ pub fn exec_dry_run( wasm_binary: Vec, phantom_functions: Vec, public_inputs: Vec, - private_inputs: Vec, + private_inputs: VecDeque, context_inputs: Vec, context_outputs: Rc>>, ) -> Result<()> { @@ -251,7 +252,7 @@ pub fn exec_create_proof( phantom_functions: Vec, output_dir: &PathBuf, public_inputs: Vec, - private_inputs: Vec, + private_inputs: VecDeque, context_inputs: Vec, context_outputs: Rc>>, ) -> Result<()> { @@ -348,7 +349,7 @@ pub fn exec_aggregate_create_proof( phantom_functions: Vec, output_dir: &PathBuf, public_inputs: Vec>, - private_inputs: Vec>, + private_inputs: Vec>, context_inputs: Vec>, context_outputs: Vec>>>, ) -> Result<()> { diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index cde58395a..0e9510440 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1,3 +1,5 @@ +use std::collections::VecDeque; + use anyhow::Result; use app_builder::AppBuilder; use args::parse_args; @@ -61,21 +63,21 @@ impl ArgBuilder for SampleApp { .help("Private arguments of your wasm program arguments of format value:type where type=i64|bytes|bytes-packed") .min_values(0) } - fn parse_single_private_arg(matches: &ArgMatches) -> Vec { + fn parse_single_private_arg(matches: &ArgMatches) -> VecDeque { let inputs: Vec<&str> = matches .get_many("private") .unwrap_or_default() .map(|v: &String| v.as_str()) .collect(); - parse_args(inputs.into()) + parse_args(inputs.into()).into() } fn aggregate_private_args<'a>() -> Arg<'a> { // We only aggregate one proof in the sample program. Self::single_private_arg() } - fn parse_aggregate_private_args(matches: &ArgMatches) -> Vec> { + fn parse_aggregate_private_args(matches: &ArgMatches) -> Vec> { let inputs = Self::parse_single_private_arg(matches); vec![inputs] diff --git a/crates/zkwasm/src/foreign/mod.rs b/crates/zkwasm/src/foreign/mod.rs index 6db4114ec..0d2bf21cc 100644 --- a/crates/zkwasm/src/foreign/mod.rs +++ b/crates/zkwasm/src/foreign/mod.rs @@ -1,5 +1,6 @@ use std::cell::RefCell; use std::rc::Rc; +use std::collections::VecDeque; use crate::circuits::cell::AllocatedUnlimitedCell; use crate::circuits::config::zkwasm_k; @@ -55,7 +56,7 @@ pub(crate) trait InternalHostPluginBuilder { impl HostEnv { pub fn new_with_full_foreign_plugins( public_inputs: Vec, - private_inputs: Vec, + private_inputs: VecDeque, context_input: Vec, context_output: Rc>>, ) -> (Self, WasmRuntimeIO) { diff --git a/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs b/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs index c9258a04d..58079d9b0 100644 --- a/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs +++ b/crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs @@ -1,5 +1,6 @@ use std::cell::RefCell; use std::rc::Rc; +use std::collections::VecDeque; use specs::host_function::HostPlugin; use specs::types::ValueType; @@ -12,7 +13,7 @@ use super::Op; struct Context { public_inputs: Vec, - private_inputs: Vec, + private_inputs: VecDeque, instance: Rc>>, output: Rc>>, } @@ -20,7 +21,7 @@ struct Context { impl Context { pub fn new( public_inputs: Vec, - private_inputs: Vec, + private_inputs: VecDeque, instance: Rc>>, output: Rc>>, ) -> Self { @@ -43,7 +44,7 @@ impl Context { if self.private_inputs.is_empty() { panic!("failed to read private input, please checkout your input"); } - self.private_inputs.pop().unwrap() + self.private_inputs.pop_front().unwrap() } pub fn push_public(&mut self, value: u64) { @@ -65,7 +66,7 @@ impl ForeignContext for Context {} pub fn register_wasm_input_foreign( env: &mut HostEnv, public_inputs: Vec, - private_inputs: Vec, + private_inputs: VecDeque, ) -> WasmRuntimeIO { let public_inputs_and_outputs = Rc::new(RefCell::new(vec![])); let outputs = Rc::new(RefCell::new(vec![])); @@ -82,9 +83,7 @@ pub fn register_wasm_input_foreign( context.push_public(value); value } else { - let value = context.pop_private(); - println!("wasm_output:{:?}", value); - value + context.pop_private() }; Some(wasmi::RuntimeValue::I64(input as i64)) diff --git a/crates/zkwasm/src/loader/mod.rs b/crates/zkwasm/src/loader/mod.rs index 83fe4d74d..08d1fc6bf 100644 --- a/crates/zkwasm/src/loader/mod.rs +++ b/crates/zkwasm/src/loader/mod.rs @@ -1,6 +1,7 @@ use std::cell::RefCell; use std::marker::PhantomData; use std::rc::Rc; +use std::collections::VecDeque; use anyhow::Result; use halo2_proofs::arithmetic::MultiMillerLoop; @@ -47,7 +48,7 @@ pub struct ExecutionArg { /// Public inputs for `wasm_input(1)` pub public_inputs: Vec, /// Private inputs for `wasm_input(0)` - pub private_inputs: Vec, + pub private_inputs: VecDeque, /// Context inputs for `wasm_read_context()` pub context_inputs: Vec, /// Context outputs for `wasm_write_context()` @@ -111,7 +112,7 @@ impl ZkWasmLoader { fn circuit_without_witness(&self) -> Result> { let (env, wasm_runtime_io) = HostEnv::new_with_full_foreign_plugins( vec![], - vec![], + vec![].into(), vec![], Rc::new(RefCell::new(vec![])), ); @@ -156,7 +157,7 @@ impl ZkWasmLoader { pub fn checksum(&self, params: &Params) -> Result> { let (env, _) = HostEnv::new_with_full_foreign_plugins( vec![], - vec![], + vec![].into(), vec![], Rc::new(RefCell::new(vec![])), ); @@ -175,7 +176,7 @@ impl ZkWasmLoader { pub fn dry_run(&self, arg: ExecutionArg) -> Result> { let (mut env, _) = HostEnv::new_with_full_foreign_plugins( arg.public_inputs, - arg.private_inputs, + arg.private_inputs.into(), arg.context_inputs, arg.context_outputs, ); @@ -188,7 +189,7 @@ impl ZkWasmLoader { pub fn run(&self, arg: ExecutionArg) -> Result> { let (mut env, wasm_runtime_io) = HostEnv::new_with_full_foreign_plugins( arg.public_inputs, - arg.private_inputs, + arg.private_inputs.into(), arg.context_inputs, arg.context_outputs, );