Skip to content

Commit

Permalink
Merge branch 'main' into feat/proof-from-trace
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuguan authored Jan 8, 2024
2 parents 9b135e1 + 0c03b5b commit 02dfb10
Show file tree
Hide file tree
Showing 43 changed files with 1,170 additions and 358 deletions.
21 changes: 17 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,20 @@ simulation of wasm execution of target wasm bytecode with particular inputs are
# Command line:
## Setup via WASM image:
```
cargo run --release -- --function <FUNCTION_NAME> --wasm <WASM_BINARY> setup [OPTIONS]
cargo run --release -- --host default --function <FUNCTION_NAME> --wasm <WASM_BINARY> setup [OPTIONS]
```

## Single prove and verify:
```
cargo run --release -- --function <FUNCTION_NAME> --wasm <WASM_BINARY> single-prove [OPTIONS]
cargo run --release -- --function <FUNCTION_NAME> --wasm <WASM_BINARY> single-verify [OPTIONS]
cargo run --release -- --host default --function <FUNCTION_NAME> --wasm <WASM_BINARY> single-prove [OPTIONS]
cargo run --release -- --host default --function <FUNCTION_NAME> --wasm <WASM_BINARY> single-verify [OPTIONS]
```
with OPTIONS:
```
-h, --help
Print help information
--host <HOST_MODE>...
Which host env you would like to run your binary. [possible values: default, standard]
-k [<K>...]
Circuit Size K
Expand Down
166 changes: 123 additions & 43 deletions crates/cli/src/app_builder.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use anyhow::Result;
use clap::App;
use clap::AppSettings;
use delphinus_host::ExecutionArg as StandardArg;
use delphinus_host::HostEnvConfig;
use delphinus_host::StandardHostEnvBuilder as StandardEnvBuilder;
use delphinus_zkwasm::circuits::config::MIN_K;
use delphinus_zkwasm::runtime::host::default_env::DefaultHostEnvBuilder;
use delphinus_zkwasm::runtime::host::default_env::ExecutionArg;

use log::info;
use std::cell::RefCell;
use std::collections::HashMap;
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::args::HostMode;
use crate::exec::exec_dry_run;

use super::command::CommandBuilder;
Expand Down Expand Up @@ -70,7 +78,8 @@ pub trait AppBuilder: CommandBuilder {
.arg(Self::param_path_arg())
.arg(Self::function_name_arg())
.arg(Self::phantom_functions_arg())
.arg(Self::zkwasm_file_arg());
.arg(Self::zkwasm_file_arg())
.arg(Self::host_mode_arg());

let app = Self::append_setup_subcommand(app);
let app = Self::append_dry_run_subcommand(app);
Expand Down Expand Up @@ -115,22 +124,49 @@ pub trait AppBuilder: CommandBuilder {
fs::create_dir_all(&output_dir)?;
fs::create_dir_all(&param_dir)?;

let host_mode = Self::parse_host_mode(&top_matches);

match top_matches.subcommand() {
Some(("setup", _)) => exec_setup::<ExecutionArg, DefaultHostEnvBuilder>(
zkwasm_k,
Self::AGGREGATE_K,
Self::NAME,
wasm_binary,
phantom_functions,
&output_dir,
&param_dir,
),
Some(("checksum", _)) => exec_image_checksum::<ExecutionArg, DefaultHostEnvBuilder>(
zkwasm_k,
wasm_binary,
phantom_functions,
&output_dir,
),
Some(("setup", _)) => match host_mode {
HostMode::DEFAULT => exec_setup::<DefaultHostEnvBuilder>(
zkwasm_k,
Self::AGGREGATE_K,
Self::NAME,
wasm_binary,
phantom_functions,
(),
&output_dir,
&param_dir,
),
HostMode::STANDARD => exec_setup::<StandardEnvBuilder>(
zkwasm_k,
Self::AGGREGATE_K,
Self::NAME,
wasm_binary,
phantom_functions,
HostEnvConfig::default(),
&output_dir,
&param_dir,
),
},

Some(("checksum", _)) => match host_mode {
HostMode::DEFAULT => exec_image_checksum::<DefaultHostEnvBuilder>(
zkwasm_k,
wasm_binary,
(),
phantom_functions,
&output_dir,
),
HostMode::STANDARD => exec_image_checksum::<StandardEnvBuilder>(
zkwasm_k,
wasm_binary,
HostEnvConfig::default(),
phantom_functions,
&output_dir,
),
},

Some(("dry-run", sub_matches)) => {
let public_inputs: Vec<u64> = Self::parse_single_public_arg(&sub_matches);
let private_inputs: Vec<u64> = Self::parse_single_private_arg(&sub_matches);
Expand All @@ -141,22 +177,43 @@ pub trait AppBuilder: CommandBuilder {

let context_output = Arc::new(Mutex::new(vec![]));

exec_dry_run::<ExecutionArg, DefaultHostEnvBuilder>(
zkwasm_k,
wasm_binary,
phantom_functions,
ExecutionArg {
public_inputs,
private_inputs,
context_inputs: context_in,
context_outputs: context_output.clone(),
},
)?;
match host_mode {
HostMode::DEFAULT => {
exec_dry_run::<DefaultHostEnvBuilder>(
zkwasm_k,
wasm_binary,
phantom_functions,
ExecutionArg {
public_inputs,
private_inputs,
context_inputs: context_in,
context_outputs: context_output.clone(),
},
(),
)?;
}
HostMode::STANDARD => {
exec_dry_run::<StandardEnvBuilder>(
zkwasm_k,
wasm_binary,
phantom_functions,
StandardArg {
public_inputs,
private_inputs,
context_inputs: context_in,
context_outputs: context_output.clone(),
indexed_witness: Rc::new(RefCell::new(HashMap::new())),
tree_db: None,
},
HostEnvConfig::default(),
)?;
}
};

write_context_output(&context_output.lock().unwrap(), context_out_path)?;

Ok(())
}

Some(("single-prove", sub_matches)) => {
let public_inputs: Vec<u64> = Self::parse_single_public_arg(&sub_matches);
let private_inputs: Vec<u64> = Self::parse_single_private_arg(&sub_matches);
Expand All @@ -167,21 +224,44 @@ pub trait AppBuilder: CommandBuilder {
let context_out = Arc::new(Mutex::new(vec![]));

assert!(public_inputs.len() <= Self::MAX_PUBLIC_INPUT_SIZE);

exec_create_proof::<ExecutionArg, DefaultHostEnvBuilder>(
Self::NAME,
zkwasm_k,
wasm_binary,
phantom_functions,
&output_dir,
&param_dir,
ExecutionArg {
public_inputs,
private_inputs,
context_inputs: context_in,
context_outputs: context_out.clone(),
},
)?;
match host_mode {
HostMode::DEFAULT => {
exec_create_proof::<DefaultHostEnvBuilder>(
Self::NAME,
zkwasm_k,
wasm_binary,
phantom_functions,
&output_dir,
&param_dir,
ExecutionArg {
public_inputs,
private_inputs,
context_inputs: context_in,
context_outputs: context_out.clone(),
},
(),
)?;
}
HostMode::STANDARD => {
exec_create_proof::<StandardEnvBuilder>(
Self::NAME,
zkwasm_k,
wasm_binary,
phantom_functions,
&output_dir,
&param_dir,
StandardArg {
public_inputs,
private_inputs,
context_inputs: context_in,
context_outputs: context_out.clone(),
indexed_witness: Rc::new(RefCell::new(HashMap::new())),
tree_db: None,
},
HostEnvConfig::default(),
)?;
}
};

write_context_output(&context_out.lock().unwrap(), context_out_path)?;

Expand Down
24 changes: 24 additions & 0 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use clap::ArgMatches;
use specs::args::parse_args;
use std::path::PathBuf;

#[derive(clap::ArgEnum, Clone, Debug)]
pub enum HostMode {
DEFAULT,
STANDARD,
}

pub trait ArgBuilder {
fn zkwasm_k_arg<'a>() -> Arg<'a> {
arg!(
Expand All @@ -23,6 +29,7 @@ pub trait ArgBuilder {
)
.value_parser(value_parser!(PathBuf))
}

fn parse_zkwasm_file_arg(matches: &ArgMatches) -> PathBuf {
matches
.get_one::<PathBuf>("wasm")
Expand All @@ -35,13 +42,30 @@ pub trait ArgBuilder {
-f --function <FUNCTION_NAME> "Function you would like to run."
)
}

fn parse_function_name(matches: &ArgMatches) -> String {
matches
.get_one::<String>("function")
.expect("function is required")
.to_string()
}

fn host_mode_arg<'a>() -> Arg<'a> {
Arg::new("host")
.long("host")
.value_parser(value_parser!(HostMode))
.action(ArgAction::Set)
.help("Specify host functions set.")
.min_values(0)
.max_values(1)
}

fn parse_host_mode(matches: &ArgMatches) -> HostMode {
matches
.get_one::<HostMode>("host")
.map_or(HostMode::DEFAULT, |v| v.clone())
}

fn phantom_functions_arg<'a>() -> Arg<'a> {
Arg::new("phantom")
.long("phantom")
Expand Down
Loading

0 comments on commit 02dfb10

Please sign in to comment.