Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -23,6 +23,7 @@ use crate::exec::exec_dry_run;

use super::command::CommandBuilder;
use super::exec::exec_create_proof;
use super::exec::exec_create_proof_from_trace;
use super::exec::exec_image_checksum;
use super::exec::exec_setup;
use super::exec::exec_verify_proof;
Expand Down Expand Up @@ -83,6 +84,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 @@ -265,6 +267,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
7 changes: 7 additions & 0 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ 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 @@ -5,6 +5,7 @@ use circuits_batcher::proof::ProofInfo;
use circuits_batcher::proof::ProofLoadInfo;
use circuits_batcher::proof::ProvingKeyCache;
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 @@ -14,6 +15,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 @@ -181,6 +183,40 @@ pub fn exec_create_proof<Builder: HostEnvBuilder>(
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 -- --host default -k 18 --func

RUST_LOG=info cargo run --release --features cuda -- --host default -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 -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-verify

RUST_LOG=info cargo run --release --features cuda -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm proof-from-trace --tables ./ --proof ./output
86 changes: 72 additions & 14 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 @@ -14,6 +15,7 @@ use itable::InstructionTable;
use jtable::JumpTable;
use jtable::StaticFrameEntry;
use mtable::MTable;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde::Serialize;

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,
}

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,76 @@ 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());

macro_rules! serialize {
($t:ident, $name:ident) => {
let table = serde_json::to_string_pretty(&self.$t.$name).unwrap();
write_file(&dir, &format!("{}.json", stringify!($name)), table);
};
}

serialize!(compilation_tables, itable);
serialize!(compilation_tables, imtable);
serialize!(compilation_tables, elem_table);
serialize!(compilation_tables, configure_table);
serialize!(compilation_tables, static_jtable);
serialize!(compilation_tables, fid_of_entry);

serialize!(execution_tables, etable);
serialize!(execution_tables, mtable);
serialize!(execution_tables, jtable);

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();
let instances = serde_json::to_string_pretty(&public_inputs_and_outputs).unwrap();

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);
write_file(&dir, "instance.json", instances);
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 itable = load_file(&dir, "itable.json");
let imtable = load_file(&dir, "imtable.json");
let elem_table = load_file(&dir, "elem_table.json");
let configure_table = load_file(&dir, "configure_table.json");
let static_jtable = load_file(&dir, "static_jtable.json");
let fid_of_entry = load_file(&dir, "fid_of_entry.json");

let etable = load_file(&dir, "etable.json");
let mtable = load_file(&dir, "mtable.json");
let jtable = load_file(&dir, "jtable.json");

let public_inputs_and_outputs: Vec<u64> = load_file(&dir, "instance.json");
(
Tables {
compilation_tables: CompilationTable {
itable,
imtable,
elem_table,
configure_table,
static_jtable,
fid_of_entry,
},
execution_tables: ExecutionTable {
etable,
mtable,
jtable,
},
},
public_inputs_and_outputs,
)
}
}
4 changes: 3 additions & 1 deletion crates/zkwasm/src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ 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
4 changes: 3 additions & 1 deletion crates/zkwasm/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ 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