Skip to content

Commit

Permalink
feat: add proof-from-trace cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuguan committed Jan 4, 2024
1 parent 6423006 commit d0f8850
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/output
crates/playground/wasm
crates/playground/target
params/
output/
23 changes: 23 additions & 0 deletions crates/cli/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::exec::exec_create_proof;
use super::exec::exec_image_checksum;
use super::exec::exec_setup;
use super::exec::exec_verify_proof;
use super::exec::exec_create_proof_from_trace;

fn load_or_generate_output_path(
wasm_md5: &String,
Expand Down Expand Up @@ -74,6 +75,7 @@ pub trait AppBuilder: CommandBuilder {
let app = Self::append_setup_subcommand(app);
let app = Self::append_dry_run_subcommand(app);
let app = Self::append_create_single_proof_subcommand(app);
let app = Self::append_create_proof_from_trace_subcommand(app);
let app = Self::append_verify_single_proof_subcommand(app);
let app = Self::append_image_checksum_subcommand(app);

Expand Down Expand Up @@ -185,6 +187,27 @@ pub trait AppBuilder: CommandBuilder {

Ok(())
}
Some(("proof-from-trace", sub_matches)) => {
let tables_dir = sub_matches.get_one::<PathBuf>("tables").unwrap();
let proof_dir = sub_matches.get_one::<PathBuf>("proof").unwrap();

let context_out_path: Option<PathBuf> =
Self::parse_context_out_path_arg(&sub_matches);

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

exec_create_proof_from_trace(
Self::NAME,
zkwasm_k,
tables_dir,
&proof_dir,
&param_dir,
)?;

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

Ok(())
}
Some(("single-verify", _)) => exec_verify_proof(Self::NAME, &output_dir, &param_dir),
Some((_, _)) => todo!(),
None => todo!(),
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ pub trait ArgBuilder {
).value_parser(value_parser!(PathBuf))
}

fn tables_path_arg<'a>() -> Arg<'a> {
arg!(
-t --tables [TABLES_PATH] "Path of the tables files.\nMust be provided."
).value_parser(value_parser!(PathBuf))
}

fn proof_path_arg<'a>() -> Arg<'a> {
arg!(
-p --proof <PROOF_PATH> "Path of proof."
Expand Down
8 changes: 8 additions & 0 deletions crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ pub trait CommandBuilder: ArgBuilder {
app.subcommand(command)
}

fn append_create_proof_from_trace_subcommand(app: App) -> App {
let command = Command::new("proof-from-trace")
.arg(Self::proof_path_arg())
.arg(Self::tables_path_arg());

app.subcommand(command)
}

fn append_verify_single_proof_subcommand(app: App) -> App {
let command = Command::new("single-verify");
app.subcommand(command)
Expand Down
36 changes: 36 additions & 0 deletions crates/cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use circuits_batcher::proof::CircuitInfo;
use circuits_batcher::proof::ProofInfo;
use circuits_batcher::proof::ProofLoadInfo;
use delphinus_zkwasm::circuits::TestCircuit;
use delphinus_zkwasm::circuits::ZkWasmCircuitBuilder;
use delphinus_zkwasm::loader::ZkWasmLoader;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use halo2_proofs::pairing::bn256::Bn256;
Expand All @@ -12,6 +13,7 @@ use halo2aggregator_s::circuits::utils::load_or_build_unsafe_params;
use halo2aggregator_s::circuits::utils::TranscriptHash;
use halo2aggregator_s::native_verifier;
use log::info;
use specs::Tables;
use std::io::Write;
use std::path::PathBuf;

Expand Down Expand Up @@ -145,6 +147,40 @@ pub fn exec_create_proof<Arg, Builder: HostEnvBuilder<Arg = Arg>>(
Ok(())
}

pub fn exec_create_proof_from_trace(
prefix: &'static str,
zkwasm_k: u32,
tables_dir: &PathBuf,
proof_dir: &PathBuf,
param_dir: &PathBuf,
) -> Result<()> {
let (tables, public_inputs_and_outputs) = Tables::load_table(tables_dir.clone());
let builder = ZkWasmCircuitBuilder {
tables,
public_inputs_and_outputs: public_inputs_and_outputs.clone(),
};
let circuit = builder.build_circuit();

let instances = public_inputs_and_outputs
.iter()
.map(|v| (*v).into())
.collect();

let circuit: CircuitInfo<Bn256, TestCircuit<Fr>> = CircuitInfo::new(
circuit,
prefix.to_string(),
vec![instances],
zkwasm_k as usize,
circuits_batcher::args::HashType::Poseidon,
);
circuit.proofloadinfo.save(proof_dir);
circuit.exec_create_proof(proof_dir, param_dir, 0);

info!("Proof has been created.");

Ok(())
}

pub fn exec_verify_proof(
prefix: &'static str,
output_dir: &PathBuf,
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/test_cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ RUST_LOG=info cargo run --release --features cuda -- -k 18 --function zkmain --p

RUST_LOG=info cargo run --release --features cuda -- -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-prove --public 133:i64 --public 2:i64
RUST_LOG=info cargo run --release --features cuda -- -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-verify

RUST_LOG=info cargo run --release --features cuda -- -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm proof-from-trace --tables ./ --proof ./output
51 changes: 34 additions & 17 deletions crates/specs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![deny(dead_code)]

use std::env;
use std::io::BufReader;
use std::io::Write;
use std::path::PathBuf;

Expand All @@ -16,6 +17,7 @@ use jtable::StaticFrameEntry;
use mtable::MTable;
use serde::Deserialize;
use serde::Serialize;
use serde::de::DeserializeOwned;

#[macro_use]
extern crate lazy_static;
Expand Down Expand Up @@ -51,15 +53,20 @@ pub struct ExecutionTable {
pub jtable: JumpTable,
}

pub enum FileType {
JSON,
FLEXBUFFERS,
}

#[derive(Default, Clone)]
pub struct Tables {
pub compilation_tables: CompilationTable,
pub execution_tables: ExecutionTable,
pub execution_tables: ExecutionTable
}

impl Tables {
pub fn write_json(&self, dir: Option<PathBuf>) {
fn write_file(folder: &PathBuf, filename: &str, buf: &String) {
pub fn write_json(&self, dir: Option<PathBuf>, public_inputs_and_outputs: &Vec<u64>) {
fn write_file(folder: &PathBuf, filename: &str, buf: String) {
let mut folder = folder.clone();
folder.push(filename);
let mut fd = std::fs::File::create(folder.as_path()).unwrap();
Expand All @@ -68,25 +75,35 @@ impl Tables {
fd.write(buf.as_bytes()).unwrap();
}

let itable = serde_json::to_string_pretty(&self.compilation_tables.itable).unwrap();
let imtable = serde_json::to_string_pretty(&self.compilation_tables.imtable).unwrap();
let etable = serde_json::to_string_pretty(&self.execution_tables.etable).unwrap();
let dir = dir.unwrap_or(env::current_dir().unwrap());

let compilation_table =
serde_json::to_string_pretty(&self.compilation_tables).unwrap();
let execution_table = serde_json::to_string_pretty(&self.execution_tables).unwrap();
let external_host_call_table = serde_json::to_string_pretty(
&self
.execution_tables
.etable
.filter_external_host_call_table(),
)
.unwrap();
let mtable = serde_json::to_string_pretty(&self.execution_tables.mtable).unwrap();
let jtable = serde_json::to_string_pretty(&self.execution_tables.jtable).unwrap();
).unwrap();
let instances = serde_json::to_string_pretty(&public_inputs_and_outputs).unwrap();
write_file(&dir, "compilation.json", compilation_table);
write_file(&dir, "execution.json", execution_table);
write_file(&dir, "instance.json", instances);
write_file(&dir, "external_host_table.json", external_host_call_table);
}

let dir = dir.unwrap_or(env::current_dir().unwrap());
write_file(&dir, "itable.json", &itable);
write_file(&dir, "imtable.json", &imtable);
write_file(&dir, "etable.json", &etable);
write_file(&dir, "mtable.json", &mtable);
write_file(&dir, "jtable.json", &jtable);
write_file(&dir, "external_host_table.json", &external_host_call_table);
pub fn load_table(dir: PathBuf) -> (Tables, Vec<u64>) {
fn load_file<T:DeserializeOwned>(folder: &PathBuf, filename: &str) -> T {
let mut folder = folder.clone();
folder.push(filename);
let file = std::fs::File::open(folder.as_path()).unwrap();
let reader = BufReader::new(file);
serde_json::from_reader(reader).unwrap()
}
let compilation_tables: CompilationTable = load_file(&dir, "compilation.json");
let execution_tables: ExecutionTable = load_file(&dir, "execution.json");
let public_inputs_and_outputs: Vec<u64> = load_file(&dir, "instance.json");
(Tables { compilation_tables, execution_tables}, public_inputs_and_outputs)
}
}
2 changes: 1 addition & 1 deletion crates/zkwasm/src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<E: MultiMillerLoop, T, EnvBuilder: HostEnvBuilder<Arg = T>> ZkWasmLoader<E,
result.tables.profile_tables();

if write_to_file {
result.tables.write_json(None);
result.tables.write_json(None, &result.public_inputs_and_outputs);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/zkwasm/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn test_circuit_mock<F: FieldExt>(
v
};

execution_result.tables.write_json(None);
execution_result.tables.write_json(None, &execution_result.public_inputs_and_outputs);
let memory_writing_table: MemoryWritingTable = execution_result
.tables
.execution_tables
Expand Down

0 comments on commit d0f8850

Please sign in to comment.