Skip to content

Commit ef2b5ec

Browse files
committed
extract initialization state
1 parent 5640a82 commit ef2b5ec

File tree

15 files changed

+413
-140
lines changed

15 files changed

+413
-140
lines changed

crates/specs/src/lib.rs

+37
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,42 @@ pub mod mtable;
3939
pub mod step;
4040
pub mod types;
4141

42+
#[derive(Clone, Debug, Serialize)]
43+
pub struct InitializationState<T> {
44+
pub eid: T,
45+
// FIXME: in StateInitialization or ImageTableLayouter
46+
pub fid: T,
47+
pub iid: T,
48+
pub frame_id: T,
49+
pub sp: T,
50+
// FIXME: in StateInitialization or ImageTableLayouter
51+
pub initial_memory_pages: T,
52+
// FIXME: in StateInitialization or ImageTableLayouter
53+
pub maximal_memory_pages: T,
54+
55+
// TODO: open mtable
56+
// pub rest_mops: Option<T>,
57+
pub rest_jops: T,
58+
59+
pub is_very_first_step: bool,
60+
}
61+
62+
impl Default for InitializationState<u32> {
63+
fn default() -> Self {
64+
Self {
65+
eid: Default::default(),
66+
fid: Default::default(),
67+
iid: Default::default(),
68+
frame_id: Default::default(),
69+
sp: Default::default(),
70+
initial_memory_pages: Default::default(),
71+
maximal_memory_pages: Default::default(),
72+
rest_jops: Default::default(),
73+
is_very_first_step: Default::default(),
74+
}
75+
}
76+
}
77+
4278
#[derive(Default, Serialize, Debug, Clone)]
4379
pub struct CompilationTable {
4480
pub itable: InstructionTable,
@@ -51,6 +87,7 @@ pub struct CompilationTable {
5187

5288
#[derive(Default, Serialize, Clone)]
5389
pub struct ExecutionTable {
90+
pub initialization_state: InitializationState<u32>,
5491
pub etable: EventTable,
5592
pub jtable: JumpTable,
5693
}

crates/zkwasm/src/circuits/cell.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,27 @@ define_cell!(AllocatedU16Cell, F::from(u16::MAX as u64));
178178
define_cell!(AllocatedUnlimitedCell, -F::one());
179179

180180
impl<F: FieldExt> AllocatedU32Cell<F> {
181-
pub(crate) fn assign(&self, ctx: &mut Context<'_, F>, value: u32) -> Result<(), Error> {
181+
pub(crate) fn assign(
182+
&self,
183+
ctx: &mut Context<'_, F>,
184+
value: u32,
185+
) -> Result<AssignedCell<F, F>, Error> {
182186
for i in 0..2 {
183187
self.u16_cells_le[i].assign(ctx, (((value >> (i * 16)) & 0xffffu32) as u64).into())?;
184188
}
185-
self.u32_cell.assign(ctx, (value as u64).into())?;
186-
Ok(())
189+
self.u32_cell.assign(ctx, (value as u64).into())
187190
}
188191

189192
pub(crate) fn assign_constant(
190193
&self,
191194
ctx: &mut Context<'_, F>,
192195
value: u32,
193-
) -> Result<(), Error> {
196+
) -> Result<AssignedCell<F, F>, Error> {
194197
for i in 0..2 {
195198
self.u16_cells_le[i].assign(ctx, (((value >> (i * 16)) & 0xffffu32) as u64).into())?;
196199
}
197200

198-
self.u32_cell.assign_constant(ctx, (value as u64).into())?;
199-
Ok(())
201+
self.u32_cell.assign_constant(ctx, (value as u64).into())
200202
}
201203
}
202204

crates/zkwasm/src/circuits/etable/assign.rs

+124-52
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use log::debug;
55
use specs::configure_table::ConfigureTable;
66
use specs::itable::Opcode;
77
use specs::itable::OpcodeClassPlain;
8+
use specs::InitializationState;
89
use std::collections::BTreeMap;
910
use std::rc::Rc;
1011
use wasmi::DEFAULT_VALUE_STACK_LIMIT;
@@ -111,15 +112,115 @@ impl<F: FieldExt> EventTableChip<F> {
111112
Ok((rest_mops_cell.cell(), rest_mops_jell.cell()))
112113
}
113114

115+
#[cfg(not(feature = "continuation"))]
116+
fn assign_initialization_state(
117+
&self,
118+
ctx: &mut Context<'_, F>,
119+
initialization_state: &InitializationState<u32>,
120+
) -> Result<InitializationState<Cell>, Error> {
121+
macro_rules! assign_advice {
122+
($cell:ident, $value:expr) => {
123+
self.config.common_config.$cell.assign(ctx, $value)?.cell()
124+
};
125+
}
126+
127+
macro_rules! assign_constant {
128+
($cell:ident, $value:expr) => {
129+
self.config
130+
.common_config
131+
.$cell
132+
.assign_constant(ctx, $value)?
133+
.cell()
134+
};
135+
}
136+
137+
let eid = assign_constant!(eid_cell, initialization_state.eid);
138+
let fid = assign_advice!(fid_cell, F::from(initialization_state.fid as u64));
139+
let iid = assign_constant!(iid_cell, F::from(initialization_state.iid as u64));
140+
let sp = assign_constant!(sp_cell, F::from(initialization_state.sp as u64));
141+
let frame_id =
142+
assign_constant!(frame_id_cell, F::from(initialization_state.frame_id as u64));
143+
144+
let initial_memory_pages = assign_advice!(
145+
mpages_cell,
146+
F::from(initialization_state.initial_memory_pages as u64)
147+
);
148+
let maximal_memory_pages = assign_advice!(
149+
maximal_memory_pages_cell,
150+
F::from(initialization_state.maximal_memory_pages as u64)
151+
);
152+
153+
let rest_jops = assign_advice!(
154+
rest_jops_cell,
155+
F::from(initialization_state.rest_jops as u64)
156+
);
157+
158+
Ok(InitializationState {
159+
eid,
160+
fid,
161+
iid,
162+
frame_id,
163+
sp,
164+
initial_memory_pages,
165+
maximal_memory_pages,
166+
rest_jops,
167+
is_very_first_step: initialization_state.is_very_first_step,
168+
})
169+
}
170+
171+
#[cfg(feature = "continuation")]
172+
fn assign_initialization_state(
173+
&self,
174+
ctx: &mut Context<'_, F>,
175+
initialization_state: &InitializationState<u32>,
176+
) -> Result<InitializationState<Cell>, Error> {
177+
macro_rules! assign_advice {
178+
($cell:ident, $value:expr) => {
179+
self.config.common_config.$cell.assign(ctx, $value)?.cell()
180+
};
181+
}
182+
183+
let eid = assign_advice!(eid_cell, initialization_state.eid);
184+
let fid = assign_advice!(fid_cell, F::from(initialization_state.fid as u64));
185+
let iid = assign_advice!(iid_cell, F::from(initialization_state.iid as u64));
186+
let sp = assign_advice!(sp_cell, F::from(initialization_state.sp as u64));
187+
let frame_id = assign_advice!(frame_id_cell, F::from(initialization_state.frame_id as u64));
188+
189+
let initial_memory_pages = assign_advice!(
190+
mpages_cell,
191+
F::from(initialization_state.initial_memory_pages as u64)
192+
);
193+
let maximal_memory_pages = assign_advice!(
194+
maximal_memory_pages_cell,
195+
F::from(initialization_state.maximal_memory_pages as u64)
196+
);
197+
198+
let rest_jops = assign_advice!(
199+
rest_jops_cell,
200+
F::from(initialization_state.rest_jops as u64)
201+
);
202+
203+
Ok(InitializationState {
204+
eid,
205+
fid,
206+
iid,
207+
frame_id,
208+
sp,
209+
initial_memory_pages,
210+
maximal_memory_pages,
211+
rest_jops,
212+
is_very_first_step: initialization_state.is_very_first_step,
213+
})
214+
}
215+
114216
fn assign_entries(
115217
&self,
116218
ctx: &mut Context<'_, F>,
117219
op_configs: &BTreeMap<OpcodeClassPlain, Rc<Box<dyn EventTableOpcodeConfig<F>>>>,
118220
event_table: &EventTableWithMemoryInfo,
119-
configure_table: &ConfigureTable,
120-
fid_of_entry: u32,
221+
initialization_state: &InitializationState<u32>,
121222
rest_ops: Vec<(u32, u32)>,
122-
) -> Result<(Cell, Cell, Cell), Error> {
223+
) -> Result<(), Error> {
123224
macro_rules! assign_advice {
124225
($cell:ident, $value:expr) => {
125226
self.config.common_config.$cell.assign(ctx, $value)?
@@ -153,43 +254,22 @@ impl<F: FieldExt> EventTableChip<F> {
153254
external_host_call_index_cell,
154255
F::from(external_host_call_call_index as u64)
155256
);
156-
let initial_memory_pages_cell = assign_advice!(
157-
mpages_cell,
158-
F::from(configure_table.init_memory_pages as u64)
159-
);
160-
let maximal_memory_pages_cell = assign_advice!(
161-
maximal_memory_pages_cell,
162-
F::from(configure_table.maximal_memory_pages as u64)
163-
);
164-
assign_constant!(sp_cell, F::from(DEFAULT_VALUE_STACK_LIMIT as u64 - 1));
165-
assign_constant!(frame_id_cell, F::zero());
166-
assign_constant!(eid_cell, 1);
167-
let fid_of_entry_cell = assign_advice!(fid_cell, F::from(fid_of_entry as u64));
168-
assign_constant!(iid_cell, F::zero());
169257

170258
/*
171259
* Skip subsequent advice assignment in the first pass to enhance performance.
172260
*/
173261
{
174262
let assigned_cell = assign_advice!(enabled_cell, F::zero());
175263
if assigned_cell.value().is_none() {
176-
return Ok((
177-
fid_of_entry_cell.cell(),
178-
initial_memory_pages_cell.cell(),
179-
maximal_memory_pages_cell.cell(),
180-
));
264+
return Ok(());
181265
}
182266
}
183267

184268
/*
185269
* The length of event_table equals 0: without_witness
186270
*/
187271
if event_table.0.len() == 0 {
188-
return Ok((
189-
fid_of_entry_cell.cell(),
190-
initial_memory_pages_cell.cell(),
191-
maximal_memory_pages_cell.cell(),
192-
));
272+
return Ok(());
193273
}
194274

195275
let status = {
@@ -234,7 +314,7 @@ impl<F: FieldExt> EventTableChip<F> {
234314
current: &status[index],
235315
next: &status[index + 1],
236316
current_external_host_call_index: external_host_call_call_index,
237-
configure_table: *configure_table,
317+
maximal_memory_pages: initialization_state.maximal_memory_pages,
238318
host_public_inputs,
239319
context_in_index,
240320
context_out_index,
@@ -264,7 +344,7 @@ impl<F: FieldExt> EventTableChip<F> {
264344
);
265345
assign_advice!(
266346
maximal_memory_pages_cell,
267-
F::from(configure_table.maximal_memory_pages as u64)
347+
F::from(initialization_state.maximal_memory_pages as u64)
268348
);
269349
assign_advice!(frame_id_cell, F::from(entry.eentry.last_jump_eid as u64));
270350
assign_advice!(eid_cell, entry.eentry.eid);
@@ -308,7 +388,7 @@ impl<F: FieldExt> EventTableChip<F> {
308388
);
309389
assign_advice!(
310390
maximal_memory_pages_cell,
311-
F::from(configure_table.maximal_memory_pages as u64)
391+
F::from(initialization_state.maximal_memory_pages as u64)
312392
);
313393
assign_advice!(input_index_cell, F::from(host_public_inputs as u64));
314394
assign_advice!(context_input_index_cell, F::from(context_in_index as u64));
@@ -318,20 +398,15 @@ impl<F: FieldExt> EventTableChip<F> {
318398
F::from(external_host_call_call_index as u64)
319399
);
320400

321-
Ok((
322-
fid_of_entry_cell.cell(),
323-
initial_memory_pages_cell.cell(),
324-
maximal_memory_pages_cell.cell(),
325-
))
401+
Ok(())
326402
}
327403

328404
pub(in crate::circuits) fn assign(
329405
&self,
330406
ctx: &mut Context<'_, F>,
331407
event_table: &EventTableWithMemoryInfo,
332-
configure_table: &ConfigureTable,
333-
fid_of_entry: u32,
334-
) -> Result<EventTablePermutationCells, Error> {
408+
initialization_state: &InitializationState<u32>,
409+
) -> Result<InitializationState<Cell>, Error> {
335410
debug!("size of execution table: {}", event_table.0.len());
336411
assert!(event_table.0.len() * EVENT_TABLE_ENTRY_ROWS as usize <= self.max_available_rows);
337412

@@ -340,29 +415,26 @@ impl<F: FieldExt> EventTableChip<F> {
340415
self.init(ctx)?;
341416
ctx.reset();
342417

343-
let (rest_mops_cell, rest_jops_cell) = self.assign_rest_ops_first_step(
344-
ctx,
345-
rest_ops.first().map_or(0u32, |(rest_mops, _)| *rest_mops),
346-
rest_ops.first().map_or(0u32, |(_, rest_jops)| *rest_jops),
347-
)?;
418+
// let (rest_mops_cell, rest_jops_cell) = self.assign_rest_ops_first_step(
419+
// ctx,
420+
// rest_ops.first().map_or(0u32, |(rest_mops, _)| *rest_mops),
421+
// rest_ops.first().map_or(0u32, |(_, rest_jops)| *rest_jops),
422+
// )?;
423+
// ctx.reset();
424+
425+
let initialization_state_cells =
426+
self.assign_initialization_state(ctx, initialization_state)?;
348427
ctx.reset();
349428

350-
let (fid_of_entry, initial_memory_pages, maximal_memory_pages) = self.assign_entries(
429+
self.assign_entries(
351430
ctx,
352431
&self.config.op_configs,
353432
event_table,
354-
configure_table,
355-
fid_of_entry,
433+
initialization_state,
356434
rest_ops,
357435
)?;
358436
ctx.reset();
359437

360-
Ok(EventTablePermutationCells {
361-
rest_mops: Some(rest_mops_cell),
362-
rest_jops: Some(rest_jops_cell),
363-
fid_of_entry,
364-
initial_memory_pages,
365-
maximal_memory_pages,
366-
})
438+
Ok(initialization_state_cells)
367439
}
368440
}

crates/zkwasm/src/circuits/etable/op_configure/op_memory_grow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<F: FieldExt> EventTableOpcodeConfig<F> for MemoryGrowConfig<F> {
133133
self.current_maximal_diff.assign(
134134
ctx,
135135
F::from(
136-
(step.configure_table.maximal_memory_pages
136+
(step.maximal_memory_pages
137137
- (step.current.allocated_memory_pages + *grow_size as u32))
138138
as u64,
139139
),

crates/zkwasm/src/circuits/image_table/assign.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ impl<F: FieldExt> ImageTableChip<F> {
3737
}};
3838
}
3939

40+
// FIXME: should not constrain equal if continuation is enabled.
4041
let entry_fid_cell = assign_one_line!(image_table.entry_fid);
4142
ctx.region
4243
.constrain_equal(permutation_cells.entry_fid, entry_fid_cell)?;
4344

4445
let initial_memory_pages_cell = assign_one_line!(image_table.initial_memory_pages);
46+
// FIXME: should not constrain equal if continuation is enabled.
4547
ctx.region.constrain_equal(
4648
permutation_cells.initial_memory_pages,
4749
initial_memory_pages_cell,

crates/zkwasm/src/circuits/jtable/assign.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,10 @@ impl<F: FieldExt> JumpTableChip<F> {
203203
&self,
204204
ctx: &mut Context<'_, F>,
205205
jtable: &JumpTable,
206-
etable_rest_jops_cell: Option<Cell>,
206+
etable_rest_jops_cell: Cell,
207207
static_entries: &Vec<StaticFrameEntry>,
208208
) -> Result<Vec<(Cell, Cell)>, Error> {
209-
if etable_rest_jops_cell.is_some() {
210-
self.constraint_to_etable_jops(ctx, etable_rest_jops_cell.unwrap())?;
211-
}
209+
self.constraint_to_etable_jops(ctx, etable_rest_jops_cell)?;
212210

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

crates/zkwasm/src/circuits/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod config;
2020
pub mod image_table;
2121
pub mod jtable;
2222
pub mod rtable;
23+
pub mod state_table;
2324
pub mod test_circuit;
2425
pub mod utils;
2526

0 commit comments

Comments
 (0)