Skip to content

Commit

Permalink
first slice pass
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 committed Nov 4, 2023
1 parent 09ae7cb commit 3618fa3
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 39 deletions.
53 changes: 39 additions & 14 deletions crates/zkwasm/src/circuits/etable/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use log::debug;
use specs::configure_table::ConfigureTable;
use specs::itable::Opcode;
use specs::itable::OpcodeClassPlain;
use specs::jtable::JumpTable;
use specs::jtable::StaticFrameEntry;
use specs::mtable::MTable;
use specs::InitializationState;
use std::collections::BTreeMap;
use std::rc::Rc;
Expand Down Expand Up @@ -196,8 +199,10 @@ impl<F: FieldExt> EventTableChip<F> {
op_configs: &BTreeMap<OpcodeClassPlain, Rc<Box<dyn EventTableOpcodeConfig<F>>>>,
event_table: &EventTableWithMemoryInfo,
configure_table: &ConfigureTable,
rest_ops: Vec<(u32, u32)>,
// rest_ops: Vec<(u32, u32)>,
initialization_state: &InitializationState<u32>,
mut rest_jops: u32,
mut rest_mops: u32,
) -> Result<(), Error> {
#[cfg(feature = "continuation")]
macro_rules! assign_state_advice {
Expand Down Expand Up @@ -275,7 +280,7 @@ impl<F: FieldExt> EventTableChip<F> {
{
*drop
} else {
unreachable!()
0
},
last_jump_eid: 0,
allocated_memory_pages: status.last().unwrap().allocated_memory_pages,
Expand All @@ -286,9 +291,7 @@ impl<F: FieldExt> EventTableChip<F> {
status
};

for (index, (entry, (rest_mops, rest_jops))) in
event_table.0.iter().zip(rest_ops.iter()).enumerate()
{
for (index, entry) in event_table.0.iter().enumerate() {
let step_status = StepStatus {
current: &status[index],
next: &status[index + 1],
Expand All @@ -307,8 +310,8 @@ impl<F: FieldExt> EventTableChip<F> {
}

assign_advice!(enabled_cell, F::one());
assign_advice!(rest_mops_cell, F::from(*rest_mops as u64));
assign_advice!(rest_jops_cell, F::from(*rest_jops as u64));
assign_advice!(rest_mops_cell, F::from(rest_mops as u64));
assign_advice!(rest_jops_cell, F::from(rest_jops as u64));
assign_advice!(input_index_cell, F::from(host_public_inputs as u64));
assign_advice!(context_input_index_cell, F::from(context_in_index as u64));
assign_advice!(context_output_index_cell, F::from(context_out_index as u64));
Expand Down Expand Up @@ -348,10 +351,14 @@ impl<F: FieldExt> EventTableChip<F> {
if op_config.is_external_host_call(&entry.eentry) {
external_host_call_call_index += 1;
}
rest_jops -= op_config.jops();
rest_mops -= op_config.memory_writing_ops(&entry.eentry);

ctx.step(EVENT_TABLE_ENTRY_ROWS as usize);
}

assign_advice!(rest_jops_cell, F::from(rest_jops as u64));

Ok(())
}

Expand Down Expand Up @@ -446,6 +453,8 @@ impl<F: FieldExt> EventTableChip<F> {
&self,
ctx: &mut Context<'_, F>,
event_table: &EventTableWithMemoryInfo,
frame_table: &JumpTable,
static_frame_entries: &Vec<StaticFrameEntry>,
configure_table: &ConfigureTable,
initialization_state: &InitializationState<u32>,
post_initialization_state: &InitializationState<u32>,
Expand All @@ -454,16 +463,30 @@ impl<F: FieldExt> EventTableChip<F> {
debug!("size of execution table: {}", event_table.0.len());
assert!(event_table.0.len() * EVENT_TABLE_ENTRY_ROWS as usize <= self.max_available_rows);

let rest_ops = self.compute_rest_mops_and_jops(&self.config.op_configs, event_table);
let rest_mops = if cfg!(feature = "continuation") {
let rest_ops = self.compute_rest_mops_and_jops(&self.config.op_configs, event_table);

rest_ops.first().map_or(0u32, |(rest_mops, _)| *rest_mops)
} else {
// FIXME: compute from mops, hack here
let rest_ops = self.compute_rest_mops_and_jops(&self.config.op_configs, event_table);

rest_ops.first().map_or(0u32, |(rest_mops, _)| *rest_mops)
};
let rest_jops = frame_table.entries().len() as u32 * 2 + static_frame_entries.len() as u32;

#[cfg(feature = "continuation")]
println!("jops: {:?}", initialization_state.jops);
println!("rest_jops: {:?}", rest_jops);

#[cfg(feature = "continuation")]
let rest_jops = rest_jops - initialization_state.jops;

self.init(ctx)?;
ctx.reset();

let (rest_mops_cell, rest_jops_cell) = self.assign_rest_ops_first_step(
ctx,
rest_ops.first().map_or(0u32, |(rest_mops, _)| *rest_mops),
rest_ops.first().map_or(0u32, |(_, rest_jops)| *rest_jops),
)?;
let (rest_mops_cell, rest_jops_cell) =
self.assign_rest_ops_first_step(ctx, rest_mops, rest_jops)?;
ctx.reset();

let pre_initialization_state_cells =
Expand All @@ -475,8 +498,10 @@ impl<F: FieldExt> EventTableChip<F> {
&self.config.op_configs,
event_table,
configure_table,
rest_ops,
// rest_ops,
&initialization_state,
rest_jops,
rest_mops,
)?;

let post_initialization_state_cells = self.assign_post_initialization_state(
Expand Down
1 change: 1 addition & 0 deletions crates/zkwasm/src/circuits/jtable/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl<F: FieldExt> JumpTableChip<F> {
ctx.reset();

let mut rest_jops = jtable.entries().len() as u64 * 2 + static_entries.len() as u64;
println!("jtable rest_jops: {}", rest_jops);

let frame_table_start_jump_cells =
self.assign_static_entries(ctx, &mut rest_jops, static_entries)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/zkwasm/src/circuits/test_circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ impl<F: FieldExt> Circuit<F> for TestCircuit<F> {
echip.assign(
&mut ctx,
&etable,
&self.tables.execution_tables.jtable,
&self.tables.compilation_tables.static_jtable,
&self.tables.compilation_tables.configure_table,
&self.tables.compilation_tables.initialization_state,
&self.tables.post_image_table.initialization_state,
Expand Down
2 changes: 1 addition & 1 deletion crates/zkwasm/src/continuation/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ use halo2_proofs::arithmetic::MultiMillerLoop;
impl<E: MultiMillerLoop> ZkWasmLoader<E> {
/// Compute the capability(the length of etable entry) with circuit size K.
pub fn slice_capability_with_k(&self) -> usize {
(1 << self.k as usize - 200) / EVENT_TABLE_ENTRY_ROWS as usize
((1 << self.k as usize) - 200) / EVENT_TABLE_ENTRY_ROWS as usize
}
}
50 changes: 38 additions & 12 deletions crates/zkwasm/src/continuation/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use specs::etable::EventTable;
use specs::etable::EventTableEntry;
use specs::CompilationTable;
use specs::ExecutionTable;
use specs::InitializationState;
use specs::Tables;

use crate::circuits::TestCircuit;
Expand Down Expand Up @@ -35,17 +36,9 @@ pub struct Slices {

impl Slices {
pub fn pop_etable_entries(&mut self) -> Vec<EventTableEntry> {
let entries = self
.remaining_etable_entries
self.remaining_etable_entries
.drain(0..self.capability.min(self.remaining_etable_entries.len()))
.collect::<Vec<_>>();

if !self.remaining_etable_entries.is_empty() {
self.remaining_etable_entries
.insert(0, entries.last().unwrap().clone());
};

entries
.collect::<Vec<_>>()
}

pub fn new(table: Tables, capability: usize) -> Self {
Expand Down Expand Up @@ -73,8 +66,41 @@ impl Iterator for Slices {
jtable: self.origin_table.execution_tables.jtable.clone(),
};

let post_image_table =
simulate_execution(&self.current_compilation_table, &execution_tables);
let is_last_slice = self.remaining_etable_entries.is_empty();

let post_state = simulate_execution(&self.current_compilation_table, &execution_tables);

let post_image_table = CompilationTable {
itable: self.origin_table.compilation_tables.itable.clone(),
imtable: post_state.0,
elem_table: self.origin_table.compilation_tables.elem_table.clone(),
configure_table: self.origin_table.compilation_tables.configure_table.clone(),
static_jtable: self.origin_table.compilation_tables.static_jtable.clone(),
initialization_state: if is_last_slice {
post_state.1
} else {
let next_state = self.remaining_etable_entries.first().unwrap();

InitializationState {
eid: next_state.eid,
fid: next_state.inst.fid,
iid: next_state.inst.iid,
frame_id: next_state.last_jump_eid,
sp: next_state.sp,
host_public_inputs: post_state.1.host_public_inputs,
context_in_index: post_state.1.context_in_index,
context_out_index: post_state.1.context_out_index,
external_host_call_call_index: post_state.1.external_host_call_call_index,
initial_memory_pages: next_state.allocated_memory_pages,
maximal_memory_pages: self
.origin_table
.compilation_tables
.configure_table
.maximal_memory_pages,
jops: post_state.1.jops,
}
},
};

let slice = Slice {
table: Tables {
Expand Down
16 changes: 5 additions & 11 deletions crates/zkwasm/src/runtime/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use specs::host_function::HostPlugin;
use specs::imtable::InitMemoryTable;
use specs::itable::Opcode;
use specs::step::StepInfo;
use specs::CompilationTable;
Expand All @@ -8,7 +9,7 @@ use specs::InitializationState;
pub fn simulate_execution(
compilation_table: &CompilationTable,
execution_table: &ExecutionTable,
) -> CompilationTable {
) -> (InitMemoryTable, InitializationState<u32>) {
let mut host_public_inputs = compilation_table.initialization_state.host_public_inputs;
let mut context_in_index = compilation_table.initialization_state.context_in_index;
let mut context_out_index = compilation_table.initialization_state.context_out_index;
Expand Down Expand Up @@ -64,7 +65,7 @@ pub fn simulate_execution(
+ if let Opcode::Return { drop, .. } = last_entry.inst.opcode {
drop
} else {
unreachable!()
0
},

host_public_inputs,
Expand All @@ -79,13 +80,6 @@ pub fn simulate_execution(
jops,
};

CompilationTable {
itable: compilation_table.itable.clone(),
// FIXME: update imtable
imtable: compilation_table.imtable.clone(),
elem_table: compilation_table.elem_table.clone(),
configure_table: compilation_table.configure_table,
static_jtable: compilation_table.static_jtable.clone(),
initialization_state: post_initialization_state,
}
// FIXME: update imtable
(compilation_table.imtable.clone(), post_initialization_state)
}
14 changes: 13 additions & 1 deletion crates/zkwasm/src/runtime/wasmi_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ impl Execution<RuntimeValue>
}
};

let post_image_table = simulate_execution(&self.tables, &execution_tables);
let post_image_table = {
let (imtable, initialization_state) =
simulate_execution(&self.tables, &execution_tables);

CompilationTable {
itable: self.tables.itable.clone(),
imtable,
elem_table: self.tables.elem_table.clone(),
configure_table: self.tables.configure_table,
static_jtable: self.tables.static_jtable.clone(),
initialization_state,
}
};

Ok(ExecutionResult {
tables: Tables {
Expand Down
1 change: 1 addition & 0 deletions crates/zkwasm/src/test/test_rlp_continuation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ fn test_slices() -> Result<()> {
.collect(),
)?;

println!("pass: {}", index);
index += 1;
}

Expand Down

0 comments on commit 3618fa3

Please sign in to comment.