Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit dfda568

Browse files
div by const
1 parent ea57f1d commit dfda568

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

zkevm-circuits/src/evm_circuit/execution/callop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
271271
let gas_cost = call_gadget.gas_cost_expr(is_warm_prev.expr(), is_call.expr());
272272
// Apply EIP 150
273273
let gas_available = cb.curr.state.gas_left.expr() - gas_cost.clone();
274-
let one_64th_gas = ConstantDivisionGadget::construct(cb, gas_available.clone(), 64);
274+
let one_64th_gas = cb.div_by_const(gas_available.clone(), 64);
275275
let all_but_one_64th_gas = gas_available - one_64th_gas.quotient();
276276
let capped_callee_gas_left =
277277
cb.min_max(call_gadget.gas_expr(), all_but_one_64th_gas.clone());
@@ -476,8 +476,8 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
476476
let callee_gas_left = callee_gas_left.expr()
477477
+ call_gadget.has_value.clone() * GAS_STIPEND_CALL_WITH_VALUE.expr();
478478

479-
let precompile_output_word_size_div: ConstantDivisionGadget<F, N_BYTES_U64> =
480-
ConstantDivisionGadget::construct(cb, precompile_output_rws.expr(), 32);
479+
let precompile_output_word_size_div =
480+
cb.div_by_const(precompile_output_rws.expr(), 32);
481481
let precompile_output_word_size_div_remainder_zero =
482482
cb.is_zero(precompile_output_word_size_div.remainder());
483483
let precompile_output_word_size = precompile_output_word_size_div.quotient()

zkevm-circuits/src/evm_circuit/execution/create.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
168168
);
169169
let gas_cost = GasCost::CREATE.expr() + memory_expansion.gas_cost() + keccak_gas_cost;
170170
let gas_remaining = cb.curr.state.gas_left.expr() - gas_cost.clone();
171-
let gas_left = ConstantDivisionGadget::construct(cb, gas_remaining.clone(), 64);
171+
let gas_left = cb.div_by_const(gas_remaining.clone(), 64);
172172
let callee_gas_left = gas_remaining - gas_left.quotient();
173173

174174
let was_warm = cb.query_bool();

zkevm-circuits/src/evm_circuit/util/constraint_builder.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{
22
math_gadget::{
3-
IsEqualGadget, IsEqualWordGadget, IsZeroGadget, IsZeroWordGadget, LtGadget, LtWordGadget,
4-
MinMaxGadget,
3+
ConstantDivisionGadget, IsEqualGadget, IsEqualWordGadget, IsZeroGadget, IsZeroWordGadget,
4+
LtGadget, LtWordGadget, MinMaxGadget,
55
},
66
rlc, AccountAddress, CachedRegion, CellType, MemoryAddress, StoredExpression, U64Cell,
77
};
@@ -586,18 +586,6 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
586586

587587
// Math gadgets
588588

589-
// abs_word
590-
// add_word
591-
// binary_number
592-
// byte_size
593-
// cmp_words
594-
// comparison
595-
// constant_division
596-
// modulo
597-
// mul_add_words
598-
// mul_add_words512
599-
// mul_word_u64
600-
601589
pub(crate) fn is_zero(&mut self, value: Expression<F>) -> IsZeroGadget<F> {
602590
IsZeroGadget::construct(self, value)
603591
}
@@ -642,6 +630,14 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
642630
MinMaxGadget::construct(self, lhs, rhs)
643631
}
644632

633+
pub(crate) fn div_by_const<const N_BYTES: usize>(
634+
&mut self,
635+
numerator: Expression<F>,
636+
denominator: u64,
637+
) -> ConstantDivisionGadget<F, N_BYTES> {
638+
ConstantDivisionGadget::construct(self, numerator, denominator)
639+
}
640+
645641
// Fixed
646642

647643
pub(crate) fn range_lookup(&mut self, value: Expression<F>, range: u64) {

zkevm-circuits/src/evm_circuit/util/memory_gadget.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ pub(crate) struct MemoryWordSizeGadget<F> {
339339

340340
impl<F: Field> MemoryWordSizeGadget<F> {
341341
pub(crate) fn construct(cb: &mut EVMConstraintBuilder<F>, address: Expression<F>) -> Self {
342-
let memory_word_size = ConstantDivisionGadget::construct(cb, address + 31.expr(), 32);
342+
let memory_word_size = cb.div_by_const(address + 31.expr(), 32);
343343

344344
Self { memory_word_size }
345345
}

zkevm-circuits/src/evm_circuit/util/tx.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ impl<F: Field> TxDataGadget<F> {
240240
// Calculate transaction gas fee
241241
let mul_gas_fee_by_gas = MulWordByU64Gadget::construct(cb, gas_price.clone(), gas.expr());
242242

243-
let call_data_word_length =
244-
ConstantDivisionGadget::construct(cb, call_data_length.expr() + 31.expr(), 32);
243+
let call_data_word_length = cb.div_by_const(call_data_length.expr() + 31.expr(), 32);
245244

246245
let (cost_sum, gas_mul_gas_price_plus_value) = if calculate_total_cost {
247246
let cost_sum = cb.query_word32();

0 commit comments

Comments
 (0)