Skip to content

Commit

Permalink
add assertion for post/pre image col
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason authored and junyu0312 committed Oct 14, 2024
1 parent 4cdc846 commit 2679c60
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 17 deletions.
8 changes: 4 additions & 4 deletions crates/specs/src/brtable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Deserialize;
use serde::Serialize;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct BrTableEntry {
pub fid: u32,
pub iid: u32,
Expand All @@ -11,7 +11,7 @@ pub struct BrTableEntry {
pub dst_pc: u32,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub struct BrTable(Vec<BrTableEntry>);

impl BrTable {
Expand All @@ -24,15 +24,15 @@ impl BrTable {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ElemEntry {
pub table_idx: u32,
pub type_idx: u32,
pub offset: u32,
pub func_idx: u32,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone)]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub struct ElemTable(Vec<ElemEntry>);

impl ElemTable {
Expand Down
4 changes: 2 additions & 2 deletions crates/specs/src/external_host_call_table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ impl From<ExternalHostCallSignature> for Signature {
}
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, PartialEq)]
pub struct ExternalHostCallEntry {
pub op: usize,
pub value: u64,
pub is_ret: bool,
}

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

impl ExternalHostCallTable {
Expand Down
4 changes: 2 additions & 2 deletions crates/specs/src/imtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::mtable::VarType;
use serde::Deserialize;
use serde::Serialize;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct InitMemoryTableEntry {
pub ltype: LocationType,
pub is_mutable: bool,
Expand All @@ -16,7 +16,7 @@ pub struct InitMemoryTableEntry {
pub eid: u32,
}

#[derive(Default, Debug, Serialize, Deserialize)]
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
pub struct InitMemoryTable(pub HashMap<(LocationType, u32), InitMemoryTableEntry>);

impl InitMemoryTable {
Expand Down
4 changes: 2 additions & 2 deletions crates/specs/src/itable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ impl From<&Opcode> for OpcodeClassPlain {
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct InstructionTableEntry {
pub fid: u32,
// pub function_name: String,
Expand Down Expand Up @@ -648,7 +648,7 @@ impl InstructionTableInternal {
}

// Use Option because iid may be discontinuous
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
pub struct InstructionTable(Arc<Vec<Vec<Option<InstructionTableEntry>>>>);

impl InstructionTable {
Expand Down
10 changes: 5 additions & 5 deletions crates/specs/src/jtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub const INHERITED_FRAME_TABLE_ENTRIES: usize = 4096;

const JSON: bool = false;

#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct FrameTableEntryInternal {
// caller eid (unique)
pub frame_id: u32,
Expand All @@ -32,7 +32,7 @@ pub struct FrameTableEntryInternal {
pub returned: bool,
}

#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct InheritedFrameTableEntry(pub Option<FrameTableEntryInternal>);

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -44,7 +44,7 @@ impl From<Vec<InheritedFrameTableEntry>> for InheritedFrameEntries {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct InheritedFrameTable(pub Box<[InheritedFrameTableEntry; INHERITED_FRAME_TABLE_ENTRIES]>);

impl Default for InheritedFrameTable {
Expand Down Expand Up @@ -140,10 +140,10 @@ impl InheritedFrameTable {
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct CalledFrameTableEntry(pub FrameTableEntryInternal);

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct CalledFrameTable(Vec<CalledFrameTableEntry>);

impl CalledFrameTable {
Expand Down
4 changes: 2 additions & 2 deletions crates/specs/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::mtable::MemoryTableEntry;
use crate::state::InitializationState;
use crate::CompilationTable;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct FrameTableSlice {
pub inherited: Arc<InheritedFrameTable>,
pub called: CalledFrameTable,
Expand Down Expand Up @@ -57,7 +57,7 @@ impl FrameTableSlice {
}
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct Slice {
pub itable: Arc<InstructionTable>,
pub br_table: Arc<BrTable>,
Expand Down
15 changes: 15 additions & 0 deletions crates/specs/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ pub struct InitializationState<T> {
pub maximal_memory_pages: T,
}

impl PartialEq for InitializationState<u32> {
fn eq(&self, other: &Self) -> bool {
self.eid == other.eid
&& self.fid == other.fid
&& self.iid == other.iid
&& self.frame_id == other.frame_id
&& self.sp == other.sp
&& self.host_public_inputs == other.host_public_inputs
&& self.context_in_index == other.context_in_index
&& self.context_out_index == other.context_out_index
&& self.initial_memory_pages == other.initial_memory_pages
&& self.maximal_memory_pages == other.maximal_memory_pages
}
}

impl<T> InitializationState<T> {
// TODO: try to remove the magic number
pub fn field_count() -> usize {
Expand Down
19 changes: 19 additions & 0 deletions crates/zkwasm/src/loader/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ pub struct ZkWasmCircuitIter<F: FieldExt, B: SliceBackend> {
initialization_state: Arc<InitializationState<u32>>,
slices: Peekable<SlicesIter<B>>,

last_slice: Option<Slice>,

mark: PhantomData<F>,
}

Expand All @@ -166,6 +168,7 @@ impl<F: FieldExt, B: SliceBackend> IntoIterator for Slices<F, B> {
imtable: self.imtable,
initialization_state: self.initialization_state,
slices: SlicesWrap(self.slices).into_iter().peekable(),
last_slice: None,
mark: PhantomData,
}
}
Expand Down Expand Up @@ -275,6 +278,22 @@ impl<F: FieldExt, B: SliceBackend> Iterator for ZkWasmCircuitIter<F, B> {
is_last_slice: self.slices.peek().is_none(),
};

if let Some(last_slice) = self.last_slice.take() {
assert_eq!(last_slice.itable, slice.itable,);
assert_eq!(last_slice.br_table, slice.br_table,);
assert_eq!(last_slice.elem_table, slice.elem_table,);
assert_eq!(
last_slice.post_inherited_frame_table,
slice.frame_table.inherited,
);
assert_eq!(
last_slice.post_initialization_state,
slice.initialization_state,
);
assert_eq!(last_slice.post_imtable, slice.imtable,);
};
self.last_slice = Some(slice.clone());

self.imtable = post_imtable;
self.initialization_state = post_initialization_state;

Expand Down

0 comments on commit 2679c60

Please sign in to comment.