Skip to content

Commit

Permalink
fix: fix incorrect context/external tables in continuation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 committed Jul 24, 2024
1 parent 19e9ee3 commit 0860174
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 123 deletions.
7 changes: 1 addition & 6 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ 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;
Expand Down Expand Up @@ -303,11 +302,7 @@ impl Config {
style("[5/8]").bold().dim(),
dir
);
tables.write(
&dir,
|slice| name_of_etable_slice(&self.name, slice),
|slice| name_of_frame_table_slice(&self.name, slice),
);
tables.write(&dir, |slice| name_of_frame_table_slice(&self.name, slice));
}

println!("{} Build circuit(s)...", style("[6/8]").bold().dim(),);
Expand Down
6 changes: 5 additions & 1 deletion crates/specs/src/external_host_call_table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Serialize for ExternalHostCallEntry {
}
}

#[derive(Serialize)]
#[derive(Default, Serialize)]
pub struct ExternalHostCallTable(pub(crate) Vec<ExternalHostCallEntry>);

impl ExternalHostCallTable {
Expand All @@ -65,4 +65,8 @@ impl ExternalHostCallTable {
pub fn entries(&self) -> &Vec<ExternalHostCallEntry> {
&self.0
}

pub fn push(&mut self, entry: ExternalHostCallEntry) {
self.0.push(entry);
}
}
31 changes: 11 additions & 20 deletions crates/specs/src/external_host_call_table/table.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
use super::ExternalHostCallEntry;
use super::ExternalHostCallTable;
use crate::etable::EventTable;
use crate::step::StepInfo;

impl EventTable {
pub fn filter_external_host_call_table(&self) -> ExternalHostCallTable {
let entries = self
.entries()
.iter()
.filter_map(|entry| {
if let StepInfo::ExternalHostCall { op, value, sig, .. } = &entry.step_info {
Some(ExternalHostCallEntry {
op: *op,
value: value.unwrap(),
sig: *sig,
})
} else {
None
}
})
.collect();
impl TryFrom<&StepInfo> for ExternalHostCallEntry {
type Error = ();

ExternalHostCallTable(entries)
fn try_from(value: &StepInfo) -> Result<Self, Self::Error> {
match value {
StepInfo::ExternalHostCall { op, value, sig, .. } => Ok(ExternalHostCallEntry {
op: *op,
value: value.unwrap(),
sig: *sig,
}),
_ => Err(()),
}
}
}
32 changes: 5 additions & 27 deletions crates/specs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ pub struct CompilationTable {
pub struct ExecutionTable {
pub etable: Vec<TableBackend<EventTable>>,
pub frame_table: Vec<TableBackend<FrameTable>>,
pub external_host_call_table: ExternalHostCallTable,
pub context_input_table: Vec<u64>,
pub context_output_table: Vec<u64>,
}

pub struct Tables {
Expand All @@ -79,39 +82,14 @@ pub struct Tables {
}

impl Tables {
pub fn write(
&self,
dir: &Path,
name_of_etable_slice: impl Fn(usize) -> String,
name_of_frame_table_slice: impl Fn(usize) -> String,
) {
pub fn write(&self, dir: &Path, name_of_frame_table_slice: impl Fn(usize) -> String) {
fn write_file(folder: &Path, filename: &str, buf: &String) {
let folder = folder.join(filename);
let mut fd = File::create(folder.as_path()).unwrap();

fd.write_all(buf.as_bytes()).unwrap();
}

let mut external_host_call_table = vec![];
self.execution_tables
.etable
.iter()
.enumerate()
.for_each(|(slice, e)| match e {
TableBackend::Memory(etable) => {
external_host_call_table.extend(etable.filter_external_host_call_table().0);

let path = dir.join(name_of_etable_slice(slice));

etable.write(&path).unwrap();
}
TableBackend::Json(path) => {
let etable = EventTable::read(path).unwrap();
external_host_call_table.extend(etable.filter_external_host_call_table().0);
}
});
let external_host_call_table = ExternalHostCallTable::new(external_host_call_table);

write_file(
dir,
"itable.json",
Expand All @@ -133,7 +111,7 @@ impl Tables {
write_file(
dir,
"external_host_table.json",
&serde_json::to_string_pretty(&external_host_call_table).unwrap(),
&serde_json::to_string_pretty(&self.execution_tables.external_host_call_table).unwrap(),
);
}
}
9 changes: 9 additions & 0 deletions crates/specs/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::brtable::ElemTable;
use crate::configure_table::ConfigureTable;
use crate::etable::EventTable;
use crate::etable::EventTableEntry;
use crate::external_host_call_table::ExternalHostCallTable;
use crate::imtable::InitMemoryTable;
use crate::itable::InstructionTable;
use crate::jtable::CalledFrameTable;
Expand Down Expand Up @@ -71,6 +72,10 @@ pub struct Slice {
pub initialization_state: Arc<InitializationState<u32>>,
pub post_initialization_state: Arc<InitializationState<u32>>,

pub external_host_call_table: Arc<ExternalHostCallTable>,
pub context_input_table: Arc<Vec<u64>>,
pub context_output_table: Arc<Vec<u64>>,

pub is_last_slice: bool,
}

Expand Down Expand Up @@ -99,6 +104,10 @@ impl Slice {
initialization_state: compilation_table.initialization_state.clone(),
post_initialization_state: compilation_table.initialization_state.clone(),

external_host_call_table: ExternalHostCallTable::default().into(),
context_input_table: Arc::new(Vec::new()),
context_output_table: Arc::new(Vec::new()),

is_last_slice,
}
}
Expand Down
15 changes: 8 additions & 7 deletions crates/zkwasm/src/circuits/zkwasm_circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use halo2_proofs::plonk::Fixed;
use log::debug;
use log::info;
use specs::etable::EventTable;
use specs::external_host_call_table::ExternalHostCallTable;
use specs::jtable::CalledFrameTable;
use specs::jtable::INHERITED_FRAME_TABLE_ENTRIES;
use specs::slice::FrameTableSlice;
Expand Down Expand Up @@ -45,7 +46,6 @@ use crate::circuits::utils::table_entry::EventTableWithMemoryInfo;
use crate::circuits::utils::table_entry::MemoryWritingTable;
use crate::exec_with_profile;
use crate::foreign::context::circuits::assign::ContextContHelperTableChip;
use crate::foreign::context::circuits::assign::ExtractContextFromTrace;
use crate::foreign::context::circuits::ContextContHelperTableConfig;
use crate::foreign::context::circuits::CONTEXT_FOREIGN_TABLE_KEY;
use crate::foreign::foreign_table_enable_lines;
Expand Down Expand Up @@ -135,6 +135,10 @@ macro_rules! impl_zkwasm_circuit {
initialization_state: self.slice.initialization_state.clone(),
post_initialization_state: self.slice.initialization_state.clone(),

external_host_call_table: ExternalHostCallTable::default().into(),
context_input_table: Arc::new(Vec::new()),
context_output_table: Arc::new(Vec::new()),

is_last_slice: self.slice.is_last_slice,
},
)
Expand Down Expand Up @@ -392,10 +396,7 @@ macro_rules! impl_zkwasm_circuit {
exec_with_profile!(
|| "Assign external host call table",
external_host_call_chip
.assign(
_layouter,
&self.slice.etable.filter_external_host_call_table(),
)
.assign(_layouter, &self.slice.external_host_call_table,)
.unwrap()
);
});
Expand All @@ -407,8 +408,8 @@ macro_rules! impl_zkwasm_circuit {
context_chip
.assign(
_layouter,
&self.slice.etable.get_context_inputs(),
&self.slice.etable.get_context_outputs()
&self.slice.context_input_table,
&self.slice.context_output_table
)
.unwrap()
);
Expand Down
54 changes: 0 additions & 54 deletions crates/zkwasm/src/foreign/context/circuits/assign.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use halo2_proofs::arithmetic::FieldExt;
use halo2_proofs::circuit::Layouter;
use halo2_proofs::plonk::Error;
use specs::etable::EventTable;
use specs::host_function::HostPlugin;
use specs::step::StepInfo;

use crate::foreign::context::Op;

use super::ContextContHelperTableConfig;

Expand Down Expand Up @@ -52,52 +47,3 @@ impl<F: FieldExt> ContextContHelperTableChip<F> {
Ok(())
}
}

pub trait ExtractContextFromTrace {
fn get_context_inputs(&self) -> Vec<u64>;
fn get_context_outputs(&self) -> Vec<u64>;
}

impl ExtractContextFromTrace for EventTable {
fn get_context_inputs(&self) -> Vec<u64> {
self.entries()
.iter()
.filter_map(|e| match &e.step_info {
StepInfo::CallHost {
plugin: HostPlugin::Context,
op_index_in_plugin,
ret_val,
..
} => {
if *op_index_in_plugin == Op::ReadContext as usize {
*ret_val
} else {
None
}
}
_ => None,
})
.collect()
}

fn get_context_outputs(&self) -> Vec<u64> {
self.entries()
.iter()
.filter_map(|e| match &e.step_info {
StepInfo::CallHost {
plugin: HostPlugin::Context,
op_index_in_plugin,
args,
..
} => {
if *op_index_in_plugin == Op::WriteContext as usize {
Some(args[0])
} else {
None
}
}
_ => None,
})
.collect()
}
}
40 changes: 39 additions & 1 deletion crates/zkwasm/src/foreign/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::fs::File;
use std::io;
use std::io::Write;

use specs::host_function::HostPlugin;
use specs::step::StepInfo;

pub mod circuits;
pub mod etable_op_configure;
pub mod runtime;
Expand All @@ -11,9 +14,44 @@ enum Op {
WriteContext = 1,
}

#[derive(Clone, Default)]
pub struct ContextOutput(pub Vec<u64>);

pub fn try_get_context_input_from_step_info(step_info: &StepInfo) -> Option<u64> {
match step_info {
StepInfo::CallHost {
plugin: HostPlugin::Context,
op_index_in_plugin,
ret_val,
..
} => {
if *op_index_in_plugin == Op::ReadContext as usize {
Some(ret_val.unwrap())
} else {
None
}
}
_ => None,
}
}

pub fn try_get_context_output_from_step_info(step_info: &StepInfo) -> Option<u64> {
match step_info {
StepInfo::CallHost {
plugin: HostPlugin::Context,
op_index_in_plugin,
args,
..
} => {
if *op_index_in_plugin == Op::WriteContext as usize {
Some(args[0])
} else {
None
}
}
_ => None,
}
}

impl ContextOutput {
pub fn write(&self, fd: &mut File) -> io::Result<()> {
fd.write_all("0x".as_bytes())?;
Expand Down
16 changes: 16 additions & 0 deletions crates/zkwasm/src/loader/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use specs::brtable::BrTable;
use specs::brtable::ElemTable;
use specs::configure_table::ConfigureTable;
use specs::etable::EventTable;
use specs::external_host_call_table::ExternalHostCallTable;
use specs::imtable::InitMemoryTable;
use specs::itable::InstructionTable;
use specs::jtable::CalledFrameTable;
Expand Down Expand Up @@ -38,6 +39,10 @@ pub struct Slices<F: FieldExt> {
initialization_state: Arc<InitializationState<u32>>,
etables: VecDeque<TableBackend<EventTable>>,

external_host_call_table: Arc<ExternalHostCallTable>,
context_input_table: Arc<Vec<u64>>,
context_output_table: Arc<Vec<u64>>,

_marker: std::marker::PhantomData<F>,
}

Expand Down Expand Up @@ -75,6 +80,9 @@ impl<F: FieldExt> Slices<F> {
initialization_state: tables.compilation_tables.initialization_state,

etables: tables.execution_tables.etable.into(),
external_host_call_table: tables.execution_tables.external_host_call_table.into(),
context_input_table: tables.execution_tables.context_input_table.into(),
context_output_table: tables.execution_tables.context_output_table.into(),

_marker: std::marker::PhantomData,
})
Expand Down Expand Up @@ -130,6 +138,10 @@ impl<F: FieldExt> Slices<F> {
post_initialization_state: self.initialization_state.clone(),

etable: Arc::new(EventTable::new(vec![])),
external_host_call_table: self.external_host_call_table.clone(),
context_input_table: self.context_input_table.clone(),
context_output_table: self.context_output_table.clone(),

is_last_slice: false,
};

Expand Down Expand Up @@ -210,6 +222,10 @@ impl<F: FieldExt> Iterator for Slices<F> {
post_initialization_state: post_initialization_state.clone(),

etable: Arc::new(etable),
external_host_call_table: self.external_host_call_table.clone(),
context_input_table: self.context_input_table.clone(),
context_output_table: self.context_output_table.clone(),

is_last_slice: self.etables.is_empty(),
};

Expand Down
Loading

0 comments on commit 0860174

Please sign in to comment.