Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix constraint of memory table #276

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/zkwasm/src/circuits/bit_table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct BitTableConfig<F: FieldExt> {
_mark: PhantomData<F>,
}

pub(in crate::circuits) const STEP_SIZE: usize = 11;
pub(self) const STEP_SIZE: usize = 11;
pub(self) const BLOCK_SEL_OFFSET: usize = 1;
pub(self) const U32_OFFSET: [usize; 2] = [1, 6];
pub(self) const U8_OFFSET: [usize; 8] = [2, 3, 4, 5, 7, 8, 9, 10];
Expand Down
48 changes: 16 additions & 32 deletions crates/zkwasm/src/circuits/etable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,25 +444,21 @@ impl<F: FieldExt> EventTableConfig<F> {
* 1. constrains the relation between the last step and termination.
* 2. ignores rows following the termination step.
*/
let sum_ops_expr_with_init =
|init: Expression<F>,
meta: &mut VirtualCells<'_, F>,
get_expr: &dyn Fn(
&mut VirtualCells<'_, F>,
&OpcodeConfig<F>,
) -> Option<Expression<F>>,
enable: Option<&dyn Fn(&mut VirtualCells<'_, F>) -> Expression<F>>| {
let expr = op_bitmaps
.iter()
.filter_map(|(op, op_index)| {
get_expr(meta, op_configs.get(op).unwrap())
.map(|expr| expr * ops[*op_index].curr_expr(meta))
})
.fold(init, |acc, x| acc + x)
* fixed_curr!(meta, step_sel);

enable.map_or(expr.clone(), |enable_expr| expr * enable_expr(meta))
};
let sum_ops_expr_with_init = |init: Expression<F>,
meta: &mut VirtualCells<'_, F>,
get_expr: &dyn Fn(
&mut VirtualCells<'_, F>,
&OpcodeConfig<F>,
) -> Option<Expression<F>>| {
op_bitmaps
.iter()
.filter_map(|(op, op_index)| {
get_expr(meta, op_configs.get(op).unwrap())
.map(|expr| expr * ops[*op_index].curr_expr(meta))
})
.fold(init, |acc, x| acc + x)
* fixed_curr!(meta, step_sel)
};

let sum_ops_expr = |meta: &mut VirtualCells<'_, F>,
get_expr: &dyn Fn(
Expand All @@ -484,7 +480,6 @@ impl<F: FieldExt> EventTableConfig<F> {
rest_mops_cell.next_expr(meta) - rest_mops_cell.curr_expr(meta),
meta,
&|meta, config: &OpcodeConfig<F>| config.0.mops(meta),
None,
)]
});

Expand All @@ -494,13 +489,11 @@ impl<F: FieldExt> EventTableConfig<F> {
rest_call_ops_cell.next_expr(meta) - rest_call_ops_cell.curr_expr(meta),
meta,
&|meta, config: &OpcodeConfig<F>| config.0.call_ops_expr(meta),
None,
),
sum_ops_expr_with_init(
rest_return_ops_cell.next_expr(meta) - rest_return_ops_cell.curr_expr(meta),
meta,
&|meta, config: &OpcodeConfig<F>| config.0.return_ops_expr(meta),
None,
),
]
});
Expand All @@ -512,7 +505,6 @@ impl<F: FieldExt> EventTableConfig<F> {
&|meta, config: &OpcodeConfig<F>| {
config.0.input_index_increase(meta, &common_config)
},
None,
)]
});

Expand All @@ -526,7 +518,6 @@ impl<F: FieldExt> EventTableConfig<F> {
.0
.external_host_call_index_increase(meta, &common_config)
},
None,
)]
});

Expand All @@ -535,7 +526,6 @@ impl<F: FieldExt> EventTableConfig<F> {
sp_cell.curr_expr(meta) - sp_cell.next_expr(meta),
meta,
&|meta, config: &OpcodeConfig<F>| config.0.sp_diff(meta),
None,
)]
});

Expand All @@ -544,7 +534,6 @@ impl<F: FieldExt> EventTableConfig<F> {
mpages_cell.curr_expr(meta) - mpages_cell.next_expr(meta),
meta,
&|meta, config: &OpcodeConfig<F>| config.0.allocated_memory_pages_diff(meta),
None,
)]
});

Expand All @@ -555,7 +544,6 @@ impl<F: FieldExt> EventTableConfig<F> {
&|meta, config: &OpcodeConfig<F>| {
config.0.context_input_index_increase(meta, &common_config)
},
None,
)]
});

Expand All @@ -567,7 +555,6 @@ impl<F: FieldExt> EventTableConfig<F> {
&|meta, config: &OpcodeConfig<F>| {
config.0.context_output_index_increase(meta, &common_config)
},
None,
)]
});

Expand All @@ -590,7 +577,6 @@ impl<F: FieldExt> EventTableConfig<F> {
.next_fid(meta, &common_config)
.map(|x| x - fid_cell.curr_expr(meta))
},
None,
)]
});

Expand All @@ -604,7 +590,6 @@ impl<F: FieldExt> EventTableConfig<F> {
.next_iid(meta, &common_config)
.map(|x| iid_cell.curr_expr(meta) + enabled_cell.curr_expr(meta) - x)
},
None,
)]
});

Expand All @@ -618,7 +603,6 @@ impl<F: FieldExt> EventTableConfig<F> {
.next_frame_id(meta, &common_config)
.map(|x| x - frame_id_cell.curr_expr(meta))
},
None,
)]
});

Expand All @@ -641,7 +625,7 @@ impl<F: FieldExt> EventTableConfig<F> {
brtable_lookup_cell.curr_expr(meta) * fixed_curr!(meta, step_sel)
});

jtable.configure_in_event_table(meta, "c8c. jtable_lookup in jtable", |meta| {
jtable.configure_lookup_in_frame_table(meta, "c8c. jtable_lookup in jtable", |meta| {
(
fixed_curr!(meta, step_sel),
common_config.is_returned_cell.curr_expr(meta) * fixed_curr!(meta, step_sel),
Expand Down
2 changes: 1 addition & 1 deletion crates/zkwasm/src/circuits/jtable/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<F: FieldExt> JTableConstraint<F> for JumpTableConfig<F> {

impl<F: FieldExt> JumpTableConfig<F> {
/// Frame Table Constraint 4. Etable step's call/return record can be found on jtable_entry
pub(in crate::circuits) fn configure_in_event_table(
pub(in crate::circuits) fn configure_lookup_in_frame_table(
&self,
meta: &mut ConstraintSystem<F>,
key: &'static str,
Expand Down
11 changes: 10 additions & 1 deletion crates/zkwasm/src/circuits/mtable/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,23 @@ impl<F: FieldExt> MemoryTableChip<F> {
fn assign_fixed(&self, ctx: &mut Context<'_, F>) -> Result<(), Error> {
let capability = self.maximal_available_rows / MEMORY_TABLE_ENTRY_ROWS as usize;

for _ in 0..capability {
for i in 0..capability {
ctx.region.assign_fixed(
|| "mtable: sel",
self.config.entry_sel,
ctx.offset,
|| Ok(F::one()),
)?;

if i == capability - 1 {
ctx.region.assign_advice_from_constant(
|| "rest_mops terminate",
self.config.rest_mops_cell.cell.col,
ctx.offset + self.config.rest_mops_cell.cell.rot as usize,
F::zero(),
)?;
}

ctx.step(MEMORY_TABLE_ENTRY_ROWS as usize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<F: FieldExt> PostImageTableConfig<F> {
|meta| curr!(meta, memory_finalized_lookup_encode),
);

frame_table.configure_in_event_table(
frame_table.configure_lookup_in_frame_table(
meta,
"post image table: extract unreturned frame table entries",
|meta| {
Expand Down
Loading