Skip to content

Commit

Permalink
deduce the type of Arg for HostEnvBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
xgaozoyoe committed Jan 2, 2024
1 parent 0ec2cc1 commit 2a1f88c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
16 changes: 8 additions & 8 deletions crates/cli/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub trait AppBuilder: CommandBuilder {

match top_matches.subcommand() {
Some(("setup", _)) => match host_mode {
HostMode::DEFAULT => exec_setup::<ExecutionArg, DefaultHostEnvBuilder>(
HostMode::DEFAULT => exec_setup::<DefaultHostEnvBuilder>(
zkwasm_k,
Self::AGGREGATE_K,
Self::NAME,
Expand All @@ -119,7 +119,7 @@ pub trait AppBuilder: CommandBuilder {
&output_dir,
&param_dir,
),
HostMode::STANDARD => exec_setup::<StandardArg, StandardEnvBuilder>(
HostMode::STANDARD => exec_setup::<StandardEnvBuilder>(
zkwasm_k,
Self::AGGREGATE_K,
Self::NAME,
Expand All @@ -132,14 +132,14 @@ pub trait AppBuilder: CommandBuilder {
},

Some(("checksum", _)) => match host_mode {
HostMode::DEFAULT => exec_image_checksum::<ExecutionArg, DefaultHostEnvBuilder>(
HostMode::DEFAULT => exec_image_checksum::<DefaultHostEnvBuilder>(
zkwasm_k,
wasm_binary,
(),
phantom_functions,
&output_dir,
),
HostMode::STANDARD => exec_image_checksum::<StandardArg, StandardEnvBuilder>(
HostMode::STANDARD => exec_image_checksum::<StandardEnvBuilder>(
zkwasm_k,
wasm_binary,
HostEnvConfig::default(),
Expand All @@ -160,7 +160,7 @@ pub trait AppBuilder: CommandBuilder {

match host_mode {
HostMode::DEFAULT => {
exec_dry_run::<ExecutionArg, DefaultHostEnvBuilder>(
exec_dry_run::<DefaultHostEnvBuilder>(
zkwasm_k,
wasm_binary,
phantom_functions,
Expand All @@ -174,7 +174,7 @@ pub trait AppBuilder: CommandBuilder {
)?;
}
HostMode::STANDARD => {
exec_dry_run::<StandardArg, StandardEnvBuilder>(
exec_dry_run::<StandardEnvBuilder>(
zkwasm_k,
wasm_binary,
phantom_functions,
Expand Down Expand Up @@ -206,7 +206,7 @@ pub trait AppBuilder: CommandBuilder {
assert!(public_inputs.len() <= Self::MAX_PUBLIC_INPUT_SIZE);
match host_mode {
HostMode::DEFAULT => {
exec_create_proof::<ExecutionArg, DefaultHostEnvBuilder>(
exec_create_proof::<DefaultHostEnvBuilder>(
Self::NAME,
zkwasm_k,
wasm_binary,
Expand All @@ -223,7 +223,7 @@ pub trait AppBuilder: CommandBuilder {
)?;
}
HostMode::STANDARD => {
exec_create_proof::<StandardArg, StandardEnvBuilder>(
exec_create_proof::<StandardEnvBuilder>(
Self::NAME,
zkwasm_k,
wasm_binary,
Expand Down
12 changes: 7 additions & 5 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ pub trait ArgBuilder {
}

fn host_mode_arg<'a>() -> Arg<'a> {
arg!(
-h --host <HOST_MODE> "Which host env you would like to run your binary."
)
.max_values(1)
.value_parser(value_parser!(HostMode))
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 {
Expand Down
47 changes: 28 additions & 19 deletions crates/cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use log::info;
use std::io::Write;
use std::path::PathBuf;

pub fn exec_setup<Arg, Builder>(
pub fn exec_setup<Builder: HostEnvBuilder>(
zkwasm_k: u32,
aggregate_k: u32,
prefix: &str,
Expand All @@ -26,10 +26,7 @@ pub fn exec_setup<Arg, Builder>(
envconfig: Builder::HostConfig,
_output_dir: &PathBuf,
param_dir: &PathBuf,
) -> Result<()>
where
Builder: HostEnvBuilder<Arg = Arg>,
{
) -> Result<()> {
info!("Setup Params and VerifyingKey");

macro_rules! prepare_params {
Expand Down Expand Up @@ -57,8 +54,11 @@ where
info!("Found Verifying at {:?}", vk_path);
} else {
info!("Create Verifying to {:?}", vk_path);
let loader =
ZkWasmLoader::<Bn256, Arg, Builder>::new(zkwasm_k, wasm_binary, phantom_functions)?;
let loader = ZkWasmLoader::<Bn256, Builder::Arg, Builder>::new(
zkwasm_k,
wasm_binary,
phantom_functions,
)?;

let vkey = loader.create_vkey(&params, envconfig)?;

Expand All @@ -70,18 +70,21 @@ where
Ok(())
}

pub fn exec_image_checksum<Arg, Builder>(
pub fn exec_image_checksum<Builder>(
zkwasm_k: u32,
wasm_binary: Vec<u8>,
hostenv: Builder::HostConfig,
phantom_functions: Vec<String>,
output_dir: &PathBuf,
) -> Result<()>
where
Builder: HostEnvBuilder<Arg = Arg>,
Builder: HostEnvBuilder,
{
let loader =
ZkWasmLoader::<Bn256, Arg, Builder>::new(zkwasm_k, wasm_binary, phantom_functions)?;
let loader = ZkWasmLoader::<Bn256, Builder::Arg, Builder>::new(
zkwasm_k,
wasm_binary,
phantom_functions,
)?;

let params = load_or_build_unsafe_params::<Bn256>(
zkwasm_k,
Expand All @@ -102,33 +105,39 @@ where
Ok(())
}

pub fn exec_dry_run<Arg, Builder: HostEnvBuilder<Arg = Arg>>(
pub fn exec_dry_run<Builder: HostEnvBuilder>(
zkwasm_k: u32,
wasm_binary: Vec<u8>,
phantom_functions: Vec<String>,
arg: Arg,
arg: Builder::Arg,
config: Builder::HostConfig,
) -> Result<()> {
let loader =
ZkWasmLoader::<Bn256, Arg, Builder>::new(zkwasm_k, wasm_binary, phantom_functions)?;
let loader = ZkWasmLoader::<Bn256, Builder::Arg, Builder>::new(
zkwasm_k,
wasm_binary,
phantom_functions,
)?;
let result = loader.run(arg, config, true, false)?;
println!("total guest instructions used {:?}", result.guest_statics);
println!("total host api used {:?}", result.host_statics);
Ok(())
}

pub fn exec_create_proof<Arg, Builder: HostEnvBuilder<Arg = Arg>>(
pub fn exec_create_proof<Builder: HostEnvBuilder>(
prefix: &'static str,
zkwasm_k: u32,
wasm_binary: Vec<u8>,
phantom_functions: Vec<String>,
output_dir: &PathBuf,
param_dir: &PathBuf,
arg: Arg,
arg: Builder::Arg,
config: Builder::HostConfig,
) -> Result<()> {
let loader =
ZkWasmLoader::<Bn256, Arg, Builder>::new(zkwasm_k, wasm_binary, phantom_functions)?;
let loader = ZkWasmLoader::<Bn256, Builder::Arg, Builder>::new(
zkwasm_k,
wasm_binary,
phantom_functions,
)?;

let execution_result = loader.run(arg, config, false, true)?;

Expand Down

0 comments on commit 2a1f88c

Please sign in to comment.