Skip to content

Commit

Permalink
extend ExecutionArg
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 authored and xgaozoyoe committed Aug 1, 2024
1 parent 0860174 commit ff37c96
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 29 deletions.
7 changes: 7 additions & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![deny(warnings)]
#![allow(clippy::too_many_arguments, clippy::while_let_on_iterator)]

use std::cell::RefCell;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::rc::Rc;

use anyhow::Result;
use app_builder::app;
Expand Down Expand Up @@ -82,6 +85,8 @@ fn main() -> Result<()> {
public_inputs,
private_inputs,
context_inputs,
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
},
arg.running_arg.context_output,
arg.instruction_limit,
Expand Down Expand Up @@ -152,6 +157,8 @@ fn main() -> Result<()> {
public_inputs,
private_inputs,
context_inputs,
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
},
arg.running_arg.context_output,
arg.mock_test,
Expand Down
34 changes: 7 additions & 27 deletions crates/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,31 @@ use std::collections::HashMap;
use zkwasm_host_circuits::host::db::TreeDB;
use zkwasm_host_circuits::proof::OpType;

pub struct StandardExecutionArg {
/// Public inputs for `wasm_input(1)`
pub public_inputs: Vec<u64>,
/// Private inputs for `wasm_input(0)`
pub private_inputs: Vec<u64>,
/// Context inputs for `wasm_read_context()`
pub context_inputs: Vec<u64>,
/// indexed witness context
pub indexed_witness: Rc<RefCell<HashMap<u64, Vec<u64>>>>,
/// db src
pub tree_db: Option<Rc<RefCell<dyn TreeDB>>>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct HostEnvConfig {
pub ops: Vec<OpType>,
}

impl HostEnvConfig {
fn register_op(op: &OpType, env: &mut HostEnv) {
fn register_op(op: &OpType, env: &mut HostEnv, tree_db: Option<Rc<RefCell<dyn TreeDB>>>) {
match op {
OpType::BLS381PAIR => host::ecc_helper::bls381::pair::register_blspair_foreign(env),
OpType::BLS381SUM => host::ecc_helper::bls381::sum::register_blssum_foreign(env),
OpType::BN256PAIR => host::ecc_helper::bn254::pair::register_bn254pair_foreign(env),
OpType::BN256SUM => host::ecc_helper::bn254::sum::register_bn254sum_foreign(env),
OpType::POSEIDONHASH => host::hash_helper::poseidon::register_poseidon_foreign(env),
OpType::MERKLE => {
host::merkle_helper::merkle::register_merkle_foreign(env, None);
host::merkle_helper::datacache::register_datacache_foreign(env, None);
host::merkle_helper::merkle::register_merkle_foreign(env, tree_db.clone());
host::merkle_helper::datacache::register_datacache_foreign(env, tree_db);
}
OpType::JUBJUBSUM => host::ecc_helper::jubjub::sum::register_babyjubjubsum_foreign(env),
OpType::KECCAKHASH => host::hash_helper::keccak256::register_keccak_foreign(env),
}
}

fn register_ops(&self, env: &mut HostEnv) {
fn register_ops(&self, env: &mut HostEnv, tree_db: Option<Rc<RefCell<dyn TreeDB>>>) {
for op in &self.ops {
Self::register_op(op, env);
Self::register_op(op, env, tree_db.clone());
}
}
}
Expand Down Expand Up @@ -92,7 +79,7 @@ impl HostEnvBuilder for StandardHostEnvBuilder {
&mut env,
Rc::new(RefCell::new(HashMap::new())),
);
host_env_config.register_ops(&mut env);
host_env_config.register_ops(&mut env, None);

env.finalize();

Expand All @@ -104,20 +91,13 @@ impl HostEnvBuilder for StandardHostEnvBuilder {
let host_env_config = HostEnvConfig {
ops: self.ops.clone(),
};
let arg = StandardExecutionArg {
public_inputs: arg.public_inputs,
private_inputs: arg.private_inputs,
context_inputs: arg.context_inputs,
indexed_witness: Rc::new(RefCell::new(HashMap::new())),
tree_db: None,
};

register_wasm_input_foreign(&mut env, arg.public_inputs, arg.private_inputs);
register_require_foreign(&mut env);
register_log_foreign(&mut env);
register_context_foreign(&mut env, arg.context_inputs);
host::witness_helper::register_witness_foreign(&mut env, arg.indexed_witness);
host_env_config.register_ops(&mut env);
host_env_config.register_ops(&mut env, arg.tree_db);

env.finalize();

Expand Down
5 changes: 5 additions & 0 deletions crates/playground/examples/binary_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use delphinus_zkwasm::runtime::host::default_env::ExecutionArg;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use delphinus_zkwasm::runtime::monitor::table_monitor::TableMonitor;
use pairing_bn256::bn256::Fr;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

const K: u32 = MIN_K;

Expand All @@ -21,6 +24,8 @@ fn main() -> Result<()> {
public_inputs: vec![0],
private_inputs: vec![],
context_inputs: vec![],
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
},
);
let mut monitor = TableMonitor::new(K, &vec![], TraceBackend::Memory, &env);
Expand Down
7 changes: 7 additions & 0 deletions crates/playground/examples/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use delphinus_zkwasm::runtime::host::default_env::ExecutionArg;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use delphinus_zkwasm::runtime::monitor::table_monitor::TableMonitor;
use pairing_bn256::bn256::Fr;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

const K: u32 = MIN_K;

Expand All @@ -22,6 +25,8 @@ fn main() -> Result<()> {
public_inputs: vec![],
private_inputs: vec![],
context_inputs: vec![2, 1],
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
},
);

Expand All @@ -44,6 +49,8 @@ fn main() -> Result<()> {
public_inputs: vec![],
private_inputs: vec![],
context_inputs: context_output.0,
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
},
);

Expand Down
5 changes: 5 additions & 0 deletions crates/playground/examples/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use delphinus_zkwasm::runtime::host::default_env::ExecutionArg;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use delphinus_zkwasm::runtime::monitor::table_monitor::TableMonitor;
use pairing_bn256::bn256::Fr;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

const K: u32 = MIN_K;

Expand All @@ -21,6 +24,8 @@ fn main() -> Result<()> {
public_inputs: vec![5],
private_inputs: vec![],
context_inputs: vec![],
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
},
);
let mut monitor = TableMonitor::new(K, &vec![], TraceBackend::Memory, &env);
Expand Down
5 changes: 5 additions & 0 deletions crates/playground/examples/phantom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use delphinus_zkwasm::runtime::host::default_env::ExecutionArg;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use delphinus_zkwasm::runtime::monitor::table_monitor::TableMonitor;
use pairing_bn256::bn256::Fr;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

const K: u32 = MIN_K;

Expand All @@ -21,6 +24,8 @@ fn main() -> Result<()> {
public_inputs: vec![2],
private_inputs: vec![],
context_inputs: vec![],
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
},
);
let mut monitor = TableMonitor::new(K, &vec!["search".to_string()], TraceBackend::Memory, &env);
Expand Down
12 changes: 10 additions & 2 deletions crates/zkwasm/src/runtime/host/default_env.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

use zkwasm_host_circuits::host::db::TreeDB;

use crate::foreign::context::runtime::register_context_foreign;
use crate::foreign::log_helper::register_log_foreign;
use crate::foreign::require_helper::register_require_foreign;
Expand All @@ -6,15 +12,17 @@ use crate::foreign::wasm_input_helper::runtime::register_wasm_input_foreign;
use super::host_env::HostEnv;
use super::HostEnvBuilder;

// TODO: remove me after refine tracer
#[derive(Clone)]
pub struct ExecutionArg {
/// Public inputs for `wasm_input(1)`
pub public_inputs: Vec<u64>,
/// Private inputs for `wasm_input(0)`
pub private_inputs: Vec<u64>,
/// Context inputs for `wasm_read_context()`
pub context_inputs: Vec<u64>,
/// indexed witness context
pub indexed_witness: Rc<RefCell<HashMap<u64, Vec<u64>>>>,
/// db src
pub tree_db: Option<Rc<RefCell<dyn TreeDB>>>,
}

pub struct DefaultHostEnvBuilder;
Expand Down

0 comments on commit ff37c96

Please sign in to comment.