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 11, 2024
1 parent bbbfbc0 commit bac3408
Show file tree
Hide file tree
Showing 42 changed files with 1,151 additions and 750 deletions.
8 changes: 6 additions & 2 deletions crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ impl SetupArg {
};

let env = env_builder.create_env_without_value(self.k);
let mut monitor =
TableMonitor::new(self.k, &self.phantom_functions, TraceBackend::Memory, &env);
let mut monitor = TableMonitor::new(
self.k,
&self.phantom_functions,
(TraceBackend::Memory, TraceBackend::Memory),
&env,
);

let loader = ZkWasmLoader::new(self.k, env)?;

Expand Down
19 changes: 16 additions & 3 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ use halo2_proofs::poly::commitment::Params;
use indicatif::ProgressBar;
use serde::Deserialize;
use serde::Serialize;
use specs::etable::EventTable;
use specs::jtable::FrameTable;
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 +244,8 @@ impl Config {
arg: ExecutionArg,
context_output_filename: Option<String>,
mock_test: bool,
backend: TraceBackend,
event_table_backend: TraceBackend<EventTable>,
frame_table_backend: TraceBackend<FrameTable>,
) -> anyhow::Result<()> {
let mut cached_proving_key = None;

Expand All @@ -253,7 +257,12 @@ 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,
(event_table_backend, frame_table_backend),
&env,
);

let (result, tables) = {
println!("{} Executing...", style("[3/8]").bold().dim(),);
Expand Down Expand Up @@ -297,7 +306,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
29 changes: 26 additions & 3 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 @@ -95,10 +98,13 @@ fn main() -> Result<()> {
let context_inputs = parse_args(&arg.running_arg.context_inputs);

let file_backend = arg.file_backend;
let backend = if file_backend {
let event_table_backend: TraceBackend<EventTable> = if file_backend {
let name = cli.name.clone();
let trace_dir = trace_dir.clone();

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

etable.write(&path).unwrap();
Expand All @@ -108,6 +114,22 @@ fn main() -> Result<()> {
} else {
TraceBackend::Memory
};
let frame_table_backend: TraceBackend<FrameTable> = if file_backend {
let name = cli.name.clone();
let trace_dir = trace_dir.clone();

TraceBackend::File(Box::new(move |slice, frame_table| {
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
}))
} else {
TraceBackend::Memory
};

let env_builder: Box<dyn HostEnvBuilder> = match config.host_mode {
HostMode::DEFAULT => Box::new(DefaultHostEnvBuilder),
Expand All @@ -126,7 +148,8 @@ fn main() -> Result<()> {
},
arg.running_arg.context_output,
arg.mock_test,
backend,
event_table_backend,
frame_table_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)
}
7 changes: 6 additions & 1 deletion crates/playground/examples/binary_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ fn main() -> Result<()> {
context_inputs: vec![],
},
);
let mut monitor = TableMonitor::new(K, &vec![], TraceBackend::Memory, &env);
let mut monitor = TableMonitor::new(
K,
&vec![],
(TraceBackend::Memory, TraceBackend::Memory),
&env,
);
let loader = ZkWasmLoader::new(K, env)?;

let runner = loader.compile(&module, &mut monitor)?;
Expand Down
14 changes: 12 additions & 2 deletions crates/playground/examples/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ fn main() -> Result<()> {
},
);

let mut monitor = TableMonitor::new(K, &vec![], TraceBackend::Memory, &env);
let mut monitor = TableMonitor::new(
K,
&vec![],
(TraceBackend::Memory, TraceBackend::Memory),
&env,
);
let loader = ZkWasmLoader::new(K, env)?;

let runner = loader.compile(&module, &mut monitor)?;
Expand All @@ -47,7 +52,12 @@ fn main() -> Result<()> {
},
);

let mut monitor = TableMonitor::new(K, &vec![], TraceBackend::Memory, &env);
let mut monitor = TableMonitor::new(
K,
&vec![],
(TraceBackend::Memory, TraceBackend::Memory),
&env,
);
let loader = ZkWasmLoader::new(K, env)?;

let runner = loader.compile(&module, &mut monitor)?;
Expand Down
7 changes: 6 additions & 1 deletion crates/playground/examples/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ fn main() -> Result<()> {
context_inputs: vec![],
},
);
let mut monitor = TableMonitor::new(K, &vec![], TraceBackend::Memory, &env);
let mut monitor = TableMonitor::new(
K,
&vec![],
(TraceBackend::Memory, TraceBackend::Memory),
&env,
);
let loader = ZkWasmLoader::new(K, env)?;

let runner = loader.compile(&module, &mut monitor)?;
Expand Down
7 changes: 6 additions & 1 deletion crates/playground/examples/phantom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ fn main() -> Result<()> {
context_inputs: vec![],
},
);
let mut monitor = TableMonitor::new(K, &vec!["search".to_string()], TraceBackend::Memory, &env);
let mut monitor = TableMonitor::new(
K,
&vec!["search".to_string()],
(TraceBackend::Memory, TraceBackend::Memory),
&env,
);
let loader = ZkWasmLoader::new(K, env)?;

let runner = loader.compile(&module, &mut monitor)?;
Expand Down
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()
}
}
5 changes: 0 additions & 5 deletions crates/specs/src/etable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,3 @@ impl EventTable {
.collect::<Vec<_>>()
}
}

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

0 comments on commit bac3408

Please sign in to comment.