Skip to content

Commit

Permalink
refactor frame table
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 committed May 14, 2024
1 parent bbbfbc0 commit 6c872c4
Show file tree
Hide file tree
Showing 37 changed files with 1,769 additions and 1,366 deletions.
17 changes: 12 additions & 5 deletions crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,18 @@ impl SetupArg {
Slice::from_compilation_table(compilation_tables, is_last_slice),
)?;

SetupArg::_setup_circuit_data(
params,
&setup_circuit,
params_dir.join(name_of_circuit_data(name, is_last_slice)),
)
match setup_circuit {
ZkWasmCircuit::Ongoing(circuit) => SetupArg::_setup_circuit_data(
params,
&circuit,
params_dir.join(name_of_circuit_data(name, is_last_slice)),
),
ZkWasmCircuit::LastSliceCircuit(circuit) => SetupArg::_setup_circuit_data(
params,
&circuit,
params_dir.join(name_of_circuit_data(name, is_last_slice)),
),
}
};

#[cfg(feature = "continuation")]
Expand Down
39 changes: 28 additions & 11 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use circuits_batcher::proof::ProofInfo;
use circuits_batcher::proof::ProofPieceInfo;
use circuits_batcher::proof::Prover;
use console::style;
use delphinus_zkwasm::circuits::ZkWasmCircuit;
use delphinus_zkwasm::loader::slice::Slices;
use delphinus_zkwasm::loader::Module;
use delphinus_zkwasm::loader::ZkWasmLoader;
Expand All @@ -31,6 +32,7 @@ use specs::TraceBackend;
use crate::args::HostMode;
use crate::names::name_of_circuit_data;
use crate::names::name_of_etable_slice;
use crate::names::name_of_frame_table_slice;
use crate::names::name_of_instance;
use crate::names::name_of_loadinfo;
use crate::names::name_of_params;
Expand Down Expand Up @@ -241,7 +243,7 @@ impl Config {
arg: ExecutionArg,
context_output_filename: Option<String>,
mock_test: bool,
backend: TraceBackend,
table_backend: TraceBackend,
) -> anyhow::Result<()> {
let mut cached_proving_key = None;

Expand All @@ -253,7 +255,7 @@ impl Config {

let env = env_builder.create_env(self.k, arg);

let mut monitor = TableMonitor::new(self.k, &self.phantom_functions, backend, &env);
let mut monitor = TableMonitor::new(self.k, &self.phantom_functions, table_backend, &env);

let (result, tables) = {
println!("{} Executing...", style("[3/8]").bold().dim(),);
Expand Down Expand Up @@ -297,7 +299,11 @@ impl Config {
style("[5/8]").bold().dim(),
dir
);
tables.write(&dir, |slice| name_of_etable_slice(&self.name, slice));
tables.write(
&dir,
|slice| name_of_etable_slice(&self.name, slice),
|slice| name_of_frame_table_slice(&self.name, slice),
);
}

println!("{} Build circuit(s)...", style("[6/8]").bold().dim(),);
Expand Down Expand Up @@ -381,14 +387,25 @@ impl Config {
transcript: name_of_transcript(&self.name, index),
};

let proof = proof_piece_info.create_proof::<Bn256, _>(
&circuit,
&vec![instances.clone()],
&params,
&cached_proving_key.as_ref().unwrap().1,
proof_load_info.hashtype,
OpenSchema::Shplonk,
);
let proof = match circuit {
ZkWasmCircuit::Ongoing(circuit) => proof_piece_info.create_proof::<Bn256, _>(
&circuit,
&vec![instances.clone()],
&params,
&cached_proving_key.as_ref().unwrap().1,
proof_load_info.hashtype,
OpenSchema::Shplonk,
),
ZkWasmCircuit::LastSliceCircuit(circuit) => proof_piece_info
.create_proof::<Bn256, _>(
&circuit,
&vec![instances.clone()],
&params,
&cached_proving_key.as_ref().unwrap().1,
proof_load_info.hashtype,
OpenSchema::Shplonk,
),
};

proof_piece_info.save_proof_data(&vec![instances.clone()], &proof, &output_dir);

Expand Down
47 changes: 37 additions & 10 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use config::Config;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use names::name_of_config;
use names::name_of_etable_slice;
use names::name_of_frame_table_slice;
use specs::args::parse_args;
use specs::etable::EventTable;
use specs::jtable::FrameTable;
use specs::TraceBackend;

mod app_builder;
Expand Down Expand Up @@ -94,17 +97,41 @@ fn main() -> Result<()> {
let private_inputs = parse_args(&arg.running_arg.private_inputs);
let context_inputs = parse_args(&arg.running_arg.context_inputs);

let file_backend = arg.file_backend;
let backend = if file_backend {
TraceBackend::File(Box::new(move |slice, etable| {
let filename_of_etable_slice =
PathBuf::from(name_of_etable_slice(&cli.name, slice));
let path = trace_dir.join(&filename_of_etable_slice);
let trace_backend: TraceBackend = if arg.file_backend {
let event_table_writer = {
let name = cli.name.clone();
let trace_dir = trace_dir.clone();

etable.write(&path).unwrap();
Box::new(move |slice, etable: &EventTable| {
let filename_of_etable_slice =
PathBuf::from(name_of_etable_slice(&name, slice));
let path = trace_dir.join(&filename_of_etable_slice);

path
}))
etable.write(&path).unwrap();

path
})
};

let frame_table_writer = {
let name = cli.name.clone();
let trace_dir = trace_dir.clone();

Box::new(move |slice, frame_table: &FrameTable| {
let filename_of_frame_table_slice =
PathBuf::from(name_of_frame_table_slice(&name, slice));
let path = trace_dir.join(&filename_of_frame_table_slice);

frame_table.write(&path).unwrap();

path
})
};

TraceBackend::File {
event_table_writer,
frame_table_writer,
}
} else {
TraceBackend::Memory
};
Expand All @@ -126,7 +153,7 @@ fn main() -> Result<()> {
},
arg.running_arg.context_output,
arg.mock_test,
backend,
trace_backend,
)?;
}
Subcommands::Verify(arg) => {
Expand Down
5 changes: 5 additions & 0 deletions crates/cli/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ pub(crate) fn name_of_transcript(name: &str, index: usize) -> String {
pub(crate) fn name_of_etable_slice(name: &str, index: usize) -> String {
format!("{}.etable.{}.data", name, index)
}

#[inline(always)]
pub(crate) fn name_of_frame_table_slice(name: &str, index: usize) -> String {
format!("{}.frame_table.{}.data", name, index)
}
23 changes: 12 additions & 11 deletions crates/specs/src/encode/frame_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use num_bigint::BigUint;
use num_bigint::ToBigUint;

use crate::encode::COMMON_RANGE_OFFSET;
use crate::jtable::JumpTableEntry;
use crate::jtable::StaticFrameEntry;
use crate::jtable::CalledFrameTableEntry;
use crate::jtable::FrameTableEntryInternal;
use crate::jtable::InheritedFrameTableEntry;

use super::FromBn;

Expand All @@ -27,7 +28,7 @@ pub fn encode_frame_table_entry<T: FromBn>(
+ iid
}

impl StaticFrameEntry {
impl FrameTableEntryInternal {
pub fn encode(&self) -> BigUint {
encode_frame_table_entry(
self.frame_id.to_biguint().unwrap(),
Expand All @@ -39,14 +40,14 @@ impl StaticFrameEntry {
}
}

impl JumpTableEntry {
impl CalledFrameTableEntry {
pub fn encode(&self) -> BigUint {
encode_frame_table_entry(
self.eid.to_biguint().unwrap(),
self.last_jump_eid.to_biguint().unwrap(),
self.callee_fid.to_biguint().unwrap(),
self.fid.to_biguint().unwrap(),
self.iid.to_biguint().unwrap(),
)
self.0.encode()
}
}

impl InheritedFrameTableEntry {
pub fn encode(&self) -> BigUint {
self.internal.encode()
}
}
22 changes: 14 additions & 8 deletions crates/specs/src/etable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::itable::InstructionTable;
use crate::itable::InstructionTableEntry;
use crate::step::StepInfo;

const JSON: bool = false;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EventTableEntry {
pub eid: u32,
Expand Down Expand Up @@ -61,16 +63,25 @@ impl EventTable {

pub fn write(&self, path: &PathBuf) -> std::io::Result<()> {
let mut fd = std::fs::File::create(path)?;
fd.write_all(&bincode::serialize(self).unwrap())?;

if JSON {
fd.write_all(&serde_json::to_string_pretty(self).unwrap().as_bytes())?;
} else {
fd.write_all(&bincode::serialize(self).unwrap())?;
}
Ok(())
}

pub fn read(path: &PathBuf) -> std::io::Result<Self> {
let mut fd = std::fs::File::open(path)?;
let mut buf = Vec::new();
fd.read_to_end(&mut buf)?;
let etable = bincode::deserialize(&mut buf).unwrap();
Ok(etable)

if JSON {
Ok(serde_json::from_slice(&buf).unwrap())
} else {
Ok(bincode::deserialize(&mut buf).unwrap())
}
}

pub fn unwrap(self) -> Vec<EventTableEntry> {
Expand All @@ -96,8 +107,3 @@ impl EventTable {
.collect::<Vec<_>>()
}
}

pub enum EventTableBackend {
Memory(EventTable),
Json(PathBuf),
}
Loading

0 comments on commit 6c872c4

Please sign in to comment.