diff --git a/crates/specs/src/encode/memory_table.rs b/crates/specs/src/encode/memory_table.rs index f2e5be008..08034f05a 100644 --- a/crates/specs/src/encode/memory_table.rs +++ b/crates/specs/src/encode/memory_table.rs @@ -1,17 +1,26 @@ -use num_bigint::ToBigUint; +use num_bigint::BigUint; +use num_traits::One; +use static_assertions::const_assert; use super::FromBn; use crate::encode::COMMON_RANGE_OFFSET; -pub fn encode_memory_table_entry(offset: T, location_type: T, is_i32: T) -> T { - const END_SHIFT: u32 = OFFSET_SHIFT + COMMON_RANGE_OFFSET; - const OFFSET_SHIFT: u32 = LOCATION_TYPE_SHIFT + COMMON_RANGE_OFFSET; - const LOCATION_TYPE_SHIFT: u32 = IS_I32_SHIFT + 1; - const IS_I32_SHIFT: u32 = 0; +const _END_SHIFT: u32 = OFFSET_SHIFT + COMMON_RANGE_OFFSET; +const OFFSET_SHIFT: u32 = LOCATION_TYPE_SHIFT + COMMON_RANGE_OFFSET; +const LOCATION_TYPE_SHIFT: u32 = IS_I32_SHIFT + 1; +const IS_I32_SHIFT: u32 = 0; + +const_assert!(_END_SHIFT < 240); - assert!(END_SHIFT < 240); +lazy_static! { + pub static ref MEMORY_TABLE_ENTRY_OFFSET: BigUint = BigUint::one() << OFFSET_SHIFT; + pub static ref MEMORY_TABLE_ENTRY_LOCATION_TYPE: BigUint = + BigUint::one() << LOCATION_TYPE_SHIFT; + pub static ref MEMORY_TABLE_ENTRY_IS_I32: BigUint = BigUint::one() << IS_I32_SHIFT; +} - offset * T::from_bn(&(1u64.to_biguint().unwrap() << OFFSET_SHIFT)) - + location_type * T::from_bn(&(1u64.to_biguint().unwrap() << LOCATION_TYPE_SHIFT)) - + is_i32 * T::from_bn(&(1u64.to_biguint().unwrap() << IS_I32_SHIFT)) +pub fn encode_memory_table_entry(offset: T, location_type: T, is_i32: T) -> T { + offset * T::from_bn(&MEMORY_TABLE_ENTRY_OFFSET) + + location_type * T::from_bn(&MEMORY_TABLE_ENTRY_LOCATION_TYPE) + + is_i32 * T::from_bn(&MEMORY_TABLE_ENTRY_IS_I32) } diff --git a/crates/specs/src/imtable.rs b/crates/specs/src/imtable.rs index 51348b05a..b66e9f6db 100644 --- a/crates/specs/src/imtable.rs +++ b/crates/specs/src/imtable.rs @@ -30,10 +30,6 @@ impl InitMemoryTable { Self(map) } - pub fn entries(&self) -> &HashMap<(LocationType, u32), InitMemoryTableEntry> { - &self.0 - } - pub fn to_string(&self) -> String { serde_json::to_string(&self.0).unwrap() } diff --git a/crates/zkwasm/src/circuits/etable/assign.rs b/crates/zkwasm/src/circuits/etable/assign.rs index 8808ab740..45c278ba1 100644 --- a/crates/zkwasm/src/circuits/etable/assign.rs +++ b/crates/zkwasm/src/circuits/etable/assign.rs @@ -21,6 +21,7 @@ use super::EVENT_TABLE_ENTRY_ROWS; use crate::circuits::cell::CellExpression; use crate::circuits::jtable::FrameEtablePermutationCells; use crate::circuits::utils::bn_to_field; +use crate::circuits::utils::step_status::FieldHelper; use crate::circuits::utils::step_status::Status; use crate::circuits::utils::step_status::StepStatus; use crate::circuits::utils::table_entry::EventTableWithMemoryInfo; @@ -395,16 +396,19 @@ impl EventTableChip { let mut ctx = Context::new(region); ctx.offset = (chunk_size * chunk_index) * (EVENT_TABLE_ENTRY_ROWS as usize); + let mut field_helper = FieldHelper::default(); + for (index, entry) in entries.iter().enumerate() { let index = chunk_index * chunk_size + index; let instruction = entry.eentry.get_instruction(itable); - let step_status = StepStatus { + let mut step_status = StepStatus { current: &status[index], next: &status[index + 1], configure_table, frame_table_returned_lookup: &frame_table_returned_lookup, + field_helper: &mut field_helper, }; { @@ -438,7 +442,10 @@ impl EventTableChip { { let op_config = op_configs.get(&((&instruction.opcode).into())).unwrap(); - op_config.0.assign(&mut ctx, &step_status, &entry).unwrap(); + op_config + .0 + .assign(&mut ctx, &mut step_status, &entry) + .unwrap(); } // Be careful, the function will step context. diff --git a/crates/zkwasm/src/circuits/etable/mod.rs b/crates/zkwasm/src/circuits/etable/mod.rs index fe77218ec..0aa7f0931 100644 --- a/crates/zkwasm/src/circuits/etable/mod.rs +++ b/crates/zkwasm/src/circuits/etable/mod.rs @@ -121,7 +121,7 @@ pub trait EventTableOpcodeConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error>; fn memory_writing_ops(&self, _: &EventTableEntry) -> u32 { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_bin.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_bin.rs index 1da10618c..b4f82c712 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_bin.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_bin.rs @@ -380,7 +380,7 @@ impl EventTableOpcodeConfig for BinConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { let (class, var_type, shift, left, right, value) = match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_bin_bit.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_bin_bit.rs index 1e0c7f6ba..0b2053301 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_bin_bit.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_bin_bit.rs @@ -123,7 +123,7 @@ impl EventTableOpcodeConfig for BinBitConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { let (class, vtype, left, right, value) = match entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_bin_shift.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_bin_shift.rs index a20fe1fa4..afca4c7e7 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_bin_shift.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_bin_shift.rs @@ -344,7 +344,7 @@ impl EventTableOpcodeConfig for BinShiftConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { let (class, left, right, value, power, is_eight_bytes, _is_sign) = diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_br.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_br.rs index fc7aebc41..4bf8ec6b8 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_br.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_br.rs @@ -90,7 +90,7 @@ impl EventTableOpcodeConfig for BrConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_br_if.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_br_if.rs index 228e01fbe..72f926444 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_br_if.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_br_if.rs @@ -140,7 +140,7 @@ impl EventTableOpcodeConfig for BrIfConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { @@ -199,8 +199,10 @@ impl EventTableOpcodeConfig for BrIfConfig { } self.cond_cell.assign(ctx, cond)?; - self.cond_inv_cell - .assign(ctx, F::from(cond).invert().unwrap_or(F::zero()))?; + if cond != 0 { + self.cond_inv_cell + .assign(ctx, step.field_helper.invert(cond))?; + } self.cond_is_zero_cell .assign(ctx, if cond == 0 { F::one() } else { F::zero() })?; self.cond_is_not_zero_cell diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_br_if_eqz.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_br_if_eqz.rs index 2d4e0517c..12cfd4202 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_br_if_eqz.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_br_if_eqz.rs @@ -136,7 +136,7 @@ impl EventTableOpcodeConfig for BrIfEqzConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { @@ -193,8 +193,10 @@ impl EventTableOpcodeConfig for BrIfEqzConfig { } } - self.cond_inv_cell - .assign(ctx, F::from(cond).invert().unwrap_or(F::zero()))?; + if cond != 0 { + self.cond_inv_cell + .assign(ctx, step.field_helper.invert(cond))?; + } self.cond_is_zero_cell .assign(ctx, if cond == 0 { F::one() } else { F::zero() })?; self.cond_is_not_zero_cell diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_br_table.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_br_table.rs index 6371e7809..413264908 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_br_table.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_br_table.rs @@ -177,7 +177,7 @@ impl EventTableOpcodeConfig for BrTableConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_call.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_call.rs index a69c2fec8..7e5a849ee 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_call.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_call.rs @@ -74,7 +74,7 @@ impl EventTableOpcodeConfig for CallConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_call_host_foreign_circuit.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_call_host_foreign_circuit.rs index 352204c81..d363bbd5d 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_call_host_foreign_circuit.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_call_host_foreign_circuit.rs @@ -117,7 +117,7 @@ impl EventTableOpcodeConfig for ExternalCallHostCircuitConfig fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_call_indirect.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_call_indirect.rs index 467a06d90..135d990f2 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_call_indirect.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_call_indirect.rs @@ -128,7 +128,7 @@ impl EventTableOpcodeConfig for CallIndirectConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_const.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_const.rs index f7c6036f1..0aaba251d 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_const.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_const.rs @@ -73,7 +73,7 @@ impl EventTableOpcodeConfig for ConstConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { @@ -94,7 +94,6 @@ impl EventTableOpcodeConfig for ConstConfig { } StepInfo::I64Const { value } => { self.value.assign(ctx, *value as u64)?; - self.is_i32.assign(ctx, F::zero())?; self.memory_table_lookup_stack_write.assign( ctx, step.current.eid, diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_conversion.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_conversion.rs index 3fdc1a345..6be6ea66f 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_conversion.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_conversion.rs @@ -219,7 +219,7 @@ impl EventTableOpcodeConfig for ConversionConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { let (is_sign_op, value, value_type, result, result_type, padding, shift) = diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_drop.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_drop.rs index 1fad58b90..0095a7c09 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_drop.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_drop.rs @@ -40,7 +40,7 @@ impl EventTableOpcodeConfig for DropConfig { fn assign( &self, _: &mut Context<'_, F>, - _: &StepStatus, + _: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_global_get.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_global_get.rs index 885e8e30b..1c36f411e 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_global_get.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_global_get.rs @@ -82,7 +82,7 @@ impl EventTableOpcodeConfig for GlobalGetConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_global_set.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_global_set.rs index 9fea49cd3..b56d0aa79 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_global_set.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_global_set.rs @@ -82,7 +82,7 @@ impl EventTableOpcodeConfig for GlobalSetConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_load.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_load.rs index dcdce6936..cbb25abdf 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_load.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_load.rs @@ -426,7 +426,7 @@ impl EventTableOpcodeConfig for LoadConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_local_get.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_local_get.rs index 97fec4720..cb2c8b0dc 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_local_get.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_local_get.rs @@ -90,7 +90,7 @@ impl EventTableOpcodeConfig for LocalGetConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_local_set.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_local_set.rs index 6c4b94b49..03aaedb73 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_local_set.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_local_set.rs @@ -90,7 +90,7 @@ impl EventTableOpcodeConfig for LocalSetConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_local_tee.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_local_tee.rs index 7c9d46d55..c67933bed 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_local_tee.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_local_tee.rs @@ -90,7 +90,7 @@ impl EventTableOpcodeConfig for LocalTeeConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_memory_grow.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_memory_grow.rs index e8c5bf279..49be7cc3f 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_memory_grow.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_memory_grow.rs @@ -119,7 +119,7 @@ impl EventTableOpcodeConfig for MemoryGrowConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_memory_size.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_memory_size.rs index 04ae20bc8..e6c9085b8 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_memory_size.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_memory_size.rs @@ -64,7 +64,7 @@ impl EventTableOpcodeConfig for MemorySizeConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_rel.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_rel.rs index e2a23c619..a43edc6c2 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_rel.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_rel.rs @@ -333,7 +333,7 @@ impl EventTableOpcodeConfig for RelConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { let (class, var_type, lhs, rhs, value, diff) = match entry.eentry.step_info { @@ -413,8 +413,9 @@ impl EventTableOpcodeConfig for RelConfig { .assign(ctx, rhs.into(), var_type == VarType::I32, op_is_sign)?; self.diff.assign(ctx, diff.into())?; - self.diff_inv - .assign(ctx, F::from(diff).invert().unwrap_or(F::zero()))?; + if diff != 0 { + self.diff_inv.assign(ctx, step.field_helper.invert(diff))?; + } { self.res_is_eq.assign_bool(ctx, lhs == rhs)?; self.res_is_gt.assign_bool(ctx, lhs > rhs)?; diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_return.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_return.rs index 7700952cd..a26896bbb 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_return.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_return.rs @@ -131,7 +131,7 @@ impl EventTableOpcodeConfig for ReturnConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_select.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_select.rs index a74043e88..fe966e0ee 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_select.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_select.rs @@ -144,7 +144,7 @@ impl EventTableOpcodeConfig for SelectConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { @@ -158,8 +158,9 @@ impl EventTableOpcodeConfig for SelectConfig { self.val1.assign(ctx, *val1)?; self.val2.assign(ctx, *val2)?; self.cond.assign(ctx, *cond)?; - self.cond_inv - .assign(ctx, F::from(*cond).invert().unwrap_or(F::zero()))?; + if *cond != 0 { + self.cond_inv.assign(ctx, step.field_helper.invert(*cond))?; + } self.res.assign(ctx, *result)?; self.is_i32.assign_bool(ctx, *vtype == VarType::I32)?; diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_store.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_store.rs index 36a62e19e..a48ca62a4 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_store.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_store.rs @@ -450,7 +450,7 @@ impl EventTableOpcodeConfig for StoreConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match entry.eentry.step_info { diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_test.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_test.rs index 1a239ab85..7ffed12ed 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_test.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_test.rs @@ -108,7 +108,7 @@ impl EventTableOpcodeConfig for TestConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { @@ -120,8 +120,10 @@ impl EventTableOpcodeConfig for TestConfig { self.is_i32_cell.assign_u32(ctx, *vtype as u32)?; self.value_cell.assign(ctx, *value)?; - self.value_inv_cell - .assign(ctx, F::from(*value).invert().unwrap_or(F::zero()))?; + if *value != 0 { + self.value_inv_cell + .assign(ctx, step.field_helper.invert(*value))?; + } self.res_cell.assign_u32(ctx, *result as u32)?; self.memory_table_lookup_stack_read.assign( diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_unary.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_unary.rs index 7910c2abc..2b1a9a001 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_unary.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_unary.rs @@ -239,7 +239,7 @@ impl EventTableOpcodeConfig for UnaryConfig { fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { @@ -251,8 +251,10 @@ impl EventTableOpcodeConfig for UnaryConfig { } => { self.is_i32.assign_bool(ctx, *vtype == VarType::I32)?; - self.operand_inv - .assign(ctx, F::from(*operand).invert().unwrap_or(F::zero()))?; + if *operand != 0 { + self.operand_inv + .assign(ctx, step.field_helper.invert(*operand))?; + } self.operand_is_zero.assign_bool(ctx, *operand == 0)?; let (bits, max) = if *vtype == VarType::I32 { diff --git a/crates/zkwasm/src/circuits/utils/step_status.rs b/crates/zkwasm/src/circuits/utils/step_status.rs index 04bb1ebef..9b9f48840 100644 --- a/crates/zkwasm/src/circuits/utils/step_status.rs +++ b/crates/zkwasm/src/circuits/utils/step_status.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use halo2_proofs::arithmetic::FieldExt; use specs::configure_table::ConfigureTable; use specs::itable::InstructionTable; @@ -24,9 +25,22 @@ pub struct Status<'a> { pub itable: &'a InstructionTable, } -pub struct StepStatus<'a, 'b, 'c> { +#[derive(Default)] +pub struct FieldHelper(HashMap); + +impl FieldHelper { + pub fn invert(&mut self, value: u64) -> F { + *self + .0 + .entry(value) + .or_insert_with(|| F::from(value).invert().unwrap_or(F::zero())) + } +} + +pub struct StepStatus<'a, 'b, 'c, 'd, F: FieldExt> { pub current: &'a Status<'b>, pub next: &'a Status<'b>, pub configure_table: &'b ConfigureTable, pub frame_table_returned_lookup: &'c HashMap<(u32, u32), bool>, + pub field_helper: &'d mut FieldHelper, } diff --git a/crates/zkwasm/src/foreign/context/etable_op_configure.rs b/crates/zkwasm/src/foreign/context/etable_op_configure.rs index 82c6c8252..a520576a5 100644 --- a/crates/zkwasm/src/foreign/context/etable_op_configure.rs +++ b/crates/zkwasm/src/foreign/context/etable_op_configure.rs @@ -156,7 +156,7 @@ impl EventTableOpcodeConfig for ETableContextHelperTableConfig, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { diff --git a/crates/zkwasm/src/foreign/require_helper/etable_op_configure.rs b/crates/zkwasm/src/foreign/require_helper/etable_op_configure.rs index 9499d82f2..867027c74 100644 --- a/crates/zkwasm/src/foreign/require_helper/etable_op_configure.rs +++ b/crates/zkwasm/src/foreign/require_helper/etable_op_configure.rs @@ -94,7 +94,7 @@ impl EventTableOpcodeConfig for ETableRequireHelperTableConfig, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info { @@ -102,8 +102,10 @@ impl EventTableOpcodeConfig for ETableRequireHelperTableConfig EventTableOpcodeConfig for ETableWasmInputHelperTableConfig fn assign( &self, ctx: &mut Context<'_, F>, - step: &StepStatus, + step: &mut StepStatus, entry: &EventTableEntryWithMemoryInfo, ) -> Result<(), Error> { match &entry.eentry.step_info {