From fffd2fd1698df12dd50198bf45fde3107a401d77 Mon Sep 17 00:00:00 2001 From: yymone Date: Mon, 9 Oct 2023 20:21:26 +1100 Subject: [PATCH 1/2] change dry run output to use arc mutex Fix context bug in playground fix cargo fmt --- crates/cli/src/app_builder.rs | 10 ++++++---- crates/cli/src/exec.rs | 14 +++++++------- crates/playground/Cargo.lock | 2 +- crates/playground/examples/binary_search.rs | 6 +++--- crates/playground/examples/context.rs | 10 +++++----- crates/playground/examples/fibonacci.rs | 6 +++--- crates/playground/examples/phantom.rs | 6 +++--- crates/zkwasm/src/foreign/context/runtime.rs | 11 ++++++----- crates/zkwasm/src/foreign/mod.rs | 6 +++--- crates/zkwasm/src/loader/mod.rs | 10 +++++----- crates/zkwasm/src/test/test_rlp.rs | 6 +++--- 11 files changed, 45 insertions(+), 42 deletions(-) diff --git a/crates/cli/src/app_builder.rs b/crates/cli/src/app_builder.rs index 33695fadf..ed028b6b4 100644 --- a/crates/cli/src/app_builder.rs +++ b/crates/cli/src/app_builder.rs @@ -9,6 +9,8 @@ use std::fs; use std::io::Write; use std::path::PathBuf; use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use crate::exec::exec_dry_run; @@ -143,7 +145,7 @@ pub trait AppBuilder: CommandBuilder { public_inputs, private_inputs, context_in, - Rc::new(RefCell::new(vec![])), + Arc::new(Mutex::new(vec![])), )?; write_context_output(&context_output.borrow(), context_out_path)?; @@ -158,7 +160,7 @@ pub trait AppBuilder: CommandBuilder { let context_out_path: Option = Self::parse_context_out_path_arg(&sub_matches); - let context_out = Rc::new(RefCell::new(vec![])); + let context_out = Arc::new(Mutex::new(vec![])); assert!(public_inputs.len() <= Self::MAX_PUBLIC_INPUT_SIZE); @@ -174,7 +176,7 @@ pub trait AppBuilder: CommandBuilder { context_out.clone(), )?; - write_context_output(&context_out.borrow(), context_out_path)?; + write_context_output(&context_out.lock().unwrap(), context_out_path)?; Ok(()) } @@ -199,7 +201,7 @@ pub trait AppBuilder: CommandBuilder { let context_inputs = public_inputs.iter().map(|_| vec![]).collect(); let context_outputs = public_inputs .iter() - .map(|_| Rc::new(RefCell::new(vec![]))) + .map(|_| Arc::new(Mutex::new(vec![]))) .collect(); for instances in &public_inputs { diff --git a/crates/cli/src/exec.rs b/crates/cli/src/exec.rs index d515ecc26..9014faaf5 100644 --- a/crates/cli/src/exec.rs +++ b/crates/cli/src/exec.rs @@ -28,12 +28,12 @@ use notify::RecursiveMode; use notify::Watcher; use serde::Deserialize; use serde::Serialize; -use std::cell::RefCell; use std::fs; use std::io::Write; use std::path::Path; use std::path::PathBuf; -use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use wasmi::RuntimeValue; use crate::app_builder::write_context_output; @@ -170,7 +170,7 @@ pub fn exec_dry_run_service( let context_inputs = parse_args( sequence.context_input.iter().map(|s| s.as_str()).collect(), ); - let context_outputs = Rc::new(RefCell::new(vec![])); + let context_outputs = Arc::new(Mutex::new(vec![])); let loader = ZkWasmLoader::::new( zkwasm_k, @@ -190,7 +190,7 @@ pub fn exec_dry_run_service( println!("return value: {:?}", r); write_context_output( - &context_outputs.borrow().to_vec(), + &context_outputs.lock().unwrap().to_vec(), sequence.context_output, ) .unwrap(); @@ -230,7 +230,7 @@ pub fn exec_dry_run( public_inputs: Vec, private_inputs: Vec, context_inputs: Vec, - context_outputs: Rc>>, + context_outputs: Arc>>, ) -> Result<()> { let loader = ZkWasmLoader::::new(zkwasm_k, wasm_binary, phantom_functions)?; @@ -253,7 +253,7 @@ pub fn exec_create_proof( public_inputs: Vec, private_inputs: Vec, context_inputs: Vec, - context_outputs: Rc>>, + context_outputs: Arc>>, ) -> Result<()> { let loader = ZkWasmLoader::::new(zkwasm_k, wasm_binary, phantom_functions)?; @@ -350,7 +350,7 @@ pub fn exec_aggregate_create_proof( public_inputs: Vec>, private_inputs: Vec>, context_inputs: Vec>, - context_outputs: Vec>>>, + context_outputs: Vec>>>, ) -> Result<()> { assert_eq!(public_inputs.len(), private_inputs.len()); diff --git a/crates/playground/Cargo.lock b/crates/playground/Cargo.lock index b3f9b7527..856e10471 100644 --- a/crates/playground/Cargo.lock +++ b/crates/playground/Cargo.lock @@ -652,7 +652,7 @@ dependencies = [ [[package]] name = "halo2aggregator-s" version = "0.1.0" -source = "git+https://github.com/DelphinusLab/halo2aggregator-s.git?tag=bisect-lookup-0.3.4#e4b3b3fd65d2e1c8c938f50721a48538fc43a175" +source = "git+https://github.com/DelphinusLab/halo2aggregator-s.git?branch=main#953bbbd50ee05211e2395e69c9760095b80f6408" dependencies = [ "ark-std", "blake2b_simd", diff --git a/crates/playground/examples/binary_search.rs b/crates/playground/examples/binary_search.rs index 8ad1f65eb..e5164453f 100644 --- a/crates/playground/examples/binary_search.rs +++ b/crates/playground/examples/binary_search.rs @@ -1,5 +1,5 @@ -use std::cell::RefCell; -use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use anyhow::Result; use delphinus_zkwasm::loader::ExecutionArg; @@ -15,7 +15,7 @@ fn main() -> Result<()> { public_inputs: vec![0], private_inputs: vec![], context_inputs: vec![], - context_outputs: Rc::new(RefCell::new(vec![])), + context_outputs: Arc::new(Mutex::new(vec![])), })?; loader.mock_test(&circuit, &instances) } diff --git a/crates/playground/examples/context.rs b/crates/playground/examples/context.rs index 58e2e3bce..0ff82d733 100644 --- a/crates/playground/examples/context.rs +++ b/crates/playground/examples/context.rs @@ -1,5 +1,5 @@ -use std::cell::RefCell; -use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use anyhow::Result; use delphinus_zkwasm::loader::ExecutionArg; @@ -10,7 +10,7 @@ fn main() -> Result<()> { let wasm = std::fs::read("wasm/context.wasm")?; let context_in: Vec = vec![2, 1]; - let context_outputs = Rc::new(RefCell::new(vec![])); + let context_outputs = Arc::new(Mutex::new(vec![])); let loader = ZkWasmLoader::::new(18, wasm, vec![])?; let arg = ExecutionArg { @@ -26,8 +26,8 @@ fn main() -> Result<()> { let arg = ExecutionArg { public_inputs: vec![], private_inputs: vec![], - context_inputs: context_outputs.borrow().to_vec(), - context_outputs: Rc::new(RefCell::new(vec![])), + context_inputs: context_outputs.lock().unwrap().to_vec(), + context_outputs: Arc::new(Mutex::new(vec![])), }; let (circuit, instances) = loader.circuit_with_witness(arg)?; diff --git a/crates/playground/examples/fibonacci.rs b/crates/playground/examples/fibonacci.rs index 5fed10ce4..0b17df69a 100644 --- a/crates/playground/examples/fibonacci.rs +++ b/crates/playground/examples/fibonacci.rs @@ -1,5 +1,5 @@ -use std::cell::RefCell; -use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use anyhow::Result; use delphinus_zkwasm::loader::ExecutionArg; @@ -15,7 +15,7 @@ fn main() -> Result<()> { public_inputs: vec![5], private_inputs: vec![], context_inputs: vec![], - context_outputs: Rc::new(RefCell::new(vec![])), + context_outputs: Arc::new(Mutex::new(vec![])), })?; loader.mock_test(&circuit, &instances) } diff --git a/crates/playground/examples/phantom.rs b/crates/playground/examples/phantom.rs index 1a32022a2..8e31069cb 100644 --- a/crates/playground/examples/phantom.rs +++ b/crates/playground/examples/phantom.rs @@ -1,5 +1,5 @@ -use std::cell::RefCell; -use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use anyhow::Result; use delphinus_zkwasm::loader::ExecutionArg; @@ -19,7 +19,7 @@ fn main() -> Result<()> { public_inputs: vec![2], private_inputs: vec![], context_inputs: vec![], - context_outputs: Rc::new(RefCell::new(vec![])), + context_outputs: Arc::new(Mutex::new(vec![])), })?; loader.mock_test(&circuit, &instances) } diff --git a/crates/zkwasm/src/foreign/context/runtime.rs b/crates/zkwasm/src/foreign/context/runtime.rs index 890634f42..301fa1e01 100644 --- a/crates/zkwasm/src/foreign/context/runtime.rs +++ b/crates/zkwasm/src/foreign/context/runtime.rs @@ -1,5 +1,6 @@ -use std::cell::RefCell; use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use specs::host_function::HostPlugin; use specs::host_function::Signature; @@ -13,11 +14,11 @@ use super::Op; struct Context { inputs: Vec, - outputs: Rc>>, + outputs: Arc>>, } impl Context { - fn new(context_input: Vec, context_output: Rc>>) -> Self { + fn new(context_input: Vec, context_output: Arc>>) -> Self { let mut inputs = context_input.clone(); inputs.reverse(); @@ -28,7 +29,7 @@ impl Context { } fn push_output(&mut self, value: u64) { - self.outputs.borrow_mut().push(value) + self.outputs.lock().unwrap().push(value) } fn pop_input(&mut self) -> u64 { @@ -43,7 +44,7 @@ impl ForeignContext for Context {} pub fn register_context_foreign( env: &mut HostEnv, context_input: Vec, - context_output: Rc>>, + context_output: Arc>>, ) { env.internal_env.register_plugin( HostPlugin::Context, diff --git a/crates/zkwasm/src/foreign/mod.rs b/crates/zkwasm/src/foreign/mod.rs index 6db4114ec..047c91cb5 100644 --- a/crates/zkwasm/src/foreign/mod.rs +++ b/crates/zkwasm/src/foreign/mod.rs @@ -1,5 +1,5 @@ -use std::cell::RefCell; -use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use crate::circuits::cell::AllocatedUnlimitedCell; use crate::circuits::config::zkwasm_k; @@ -57,7 +57,7 @@ impl HostEnv { public_inputs: Vec, private_inputs: Vec, context_input: Vec, - context_output: Rc>>, + context_output: Arc>>, ) -> (Self, WasmRuntimeIO) { let mut env = HostEnv::new(); let wasm_runtime_io = register_wasm_input_foreign(&mut env, public_inputs, private_inputs); diff --git a/crates/zkwasm/src/loader/mod.rs b/crates/zkwasm/src/loader/mod.rs index 83fe4d74d..2b09a4d29 100644 --- a/crates/zkwasm/src/loader/mod.rs +++ b/crates/zkwasm/src/loader/mod.rs @@ -1,6 +1,6 @@ -use std::cell::RefCell; use std::marker::PhantomData; -use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use anyhow::Result; use halo2_proofs::arithmetic::MultiMillerLoop; @@ -51,7 +51,7 @@ pub struct ExecutionArg { /// Context inputs for `wasm_read_context()` pub context_inputs: Vec, /// Context outputs for `wasm_write_context()` - pub context_outputs: Rc>>, + pub context_outputs: Arc>>, } pub struct ExecutionReturn { @@ -113,7 +113,7 @@ impl ZkWasmLoader { vec![], vec![], vec![], - Rc::new(RefCell::new(vec![])), + Arc::new(Mutex::new(vec![])), ); let compiled_module = self.compile(&env)?; @@ -158,7 +158,7 @@ impl ZkWasmLoader { vec![], vec![], vec![], - Rc::new(RefCell::new(vec![])), + Arc::new(Mutex::new(vec![])), ); let compiled = self.compile(&env)?; diff --git a/crates/zkwasm/src/test/test_rlp.rs b/crates/zkwasm/src/test/test_rlp.rs index 46ebb0d9c..377a0374f 100644 --- a/crates/zkwasm/src/test/test_rlp.rs +++ b/crates/zkwasm/src/test/test_rlp.rs @@ -1,5 +1,5 @@ -use std::cell::RefCell; -use std::rc::Rc; +use std::sync::Arc; +use std::sync::Mutex; use crate::circuits::TestCircuit; use crate::loader::ExecutionArg; @@ -155,7 +155,7 @@ fn build_circuit() -> Result<(ZkWasmLoader, TestCircuit, Vec)> { public_inputs, private_inputs, context_inputs: vec![], - context_outputs: Rc::new(RefCell::new(vec![])), + context_outputs: Arc::new(Mutex::new(vec![])), })?; Ok((loader, circuit, instances)) From 54875b470a10fc23f7cccdaa0a550a1fa06df536 Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Mon, 9 Oct 2023 15:29:35 +0800 Subject: [PATCH 2/2] fix wasm_output --- crates/cli/src/app_builder.rs | 8 +++----- crates/zkwasm/src/runtime/wasmi_interpreter.rs | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/cli/src/app_builder.rs b/crates/cli/src/app_builder.rs index ed028b6b4..ba042935e 100644 --- a/crates/cli/src/app_builder.rs +++ b/crates/cli/src/app_builder.rs @@ -4,11 +4,9 @@ use clap::AppSettings; use delphinus_zkwasm::circuits::config::MIN_K; use log::info; use log::warn; -use std::cell::RefCell; use std::fs; use std::io::Write; use std::path::PathBuf; -use std::rc::Rc; use std::sync::Arc; use std::sync::Mutex; @@ -136,7 +134,7 @@ pub trait AppBuilder: CommandBuilder { } else { assert!(public_inputs.len() <= Self::MAX_PUBLIC_INPUT_SIZE); - let context_output = Rc::new(RefCell::new(vec![])); + let context_output = Arc::new(Mutex::new(vec![])); exec_dry_run( zkwasm_k, @@ -145,10 +143,10 @@ pub trait AppBuilder: CommandBuilder { public_inputs, private_inputs, context_in, - Arc::new(Mutex::new(vec![])), + context_output.clone(), )?; - write_context_output(&context_output.borrow(), context_out_path)?; + write_context_output(&context_output.lock().unwrap(), context_out_path)?; Ok(()) } diff --git a/crates/zkwasm/src/runtime/wasmi_interpreter.rs b/crates/zkwasm/src/runtime/wasmi_interpreter.rs index e51ee1df9..937c6f176 100644 --- a/crates/zkwasm/src/runtime/wasmi_interpreter.rs +++ b/crates/zkwasm/src/runtime/wasmi_interpreter.rs @@ -96,7 +96,7 @@ impl Execution }, result, public_inputs_and_outputs: wasm_io.public_inputs_and_outputs.borrow().clone(), - outputs: wasm_io.public_inputs_and_outputs.borrow().clone(), + outputs: wasm_io.outputs.borrow().clone(), }) } }