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

Commit

Permalink
div by const
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihChengLiang committed Feb 13, 2024
1 parent ea57f1d commit dfda568
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
6 changes: 3 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
let gas_cost = call_gadget.gas_cost_expr(is_warm_prev.expr(), is_call.expr());
// Apply EIP 150
let gas_available = cb.curr.state.gas_left.expr() - gas_cost.clone();
let one_64th_gas = ConstantDivisionGadget::construct(cb, gas_available.clone(), 64);
let one_64th_gas = cb.div_by_const(gas_available.clone(), 64);
let all_but_one_64th_gas = gas_available - one_64th_gas.quotient();
let capped_callee_gas_left =
cb.min_max(call_gadget.gas_expr(), all_but_one_64th_gas.clone());
Expand Down Expand Up @@ -476,8 +476,8 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
let callee_gas_left = callee_gas_left.expr()
+ call_gadget.has_value.clone() * GAS_STIPEND_CALL_WITH_VALUE.expr();

let precompile_output_word_size_div: ConstantDivisionGadget<F, N_BYTES_U64> =
ConstantDivisionGadget::construct(cb, precompile_output_rws.expr(), 32);
let precompile_output_word_size_div =
cb.div_by_const(precompile_output_rws.expr(), 32);
let precompile_output_word_size_div_remainder_zero =
cb.is_zero(precompile_output_word_size_div.remainder());
let precompile_output_word_size = precompile_output_word_size_div.quotient()
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
);
let gas_cost = GasCost::CREATE.expr() + memory_expansion.gas_cost() + keccak_gas_cost;
let gas_remaining = cb.curr.state.gas_left.expr() - gas_cost.clone();
let gas_left = ConstantDivisionGadget::construct(cb, gas_remaining.clone(), 64);
let gas_left = cb.div_by_const(gas_remaining.clone(), 64);
let callee_gas_left = gas_remaining - gas_left.quotient();

let was_warm = cb.query_bool();
Expand Down
24 changes: 10 additions & 14 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
math_gadget::{
IsEqualGadget, IsEqualWordGadget, IsZeroGadget, IsZeroWordGadget, LtGadget, LtWordGadget,
MinMaxGadget,
ConstantDivisionGadget, IsEqualGadget, IsEqualWordGadget, IsZeroGadget, IsZeroWordGadget,
LtGadget, LtWordGadget, MinMaxGadget,
},
rlc, AccountAddress, CachedRegion, CellType, MemoryAddress, StoredExpression, U64Cell,
};
Expand Down Expand Up @@ -586,18 +586,6 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {

// Math gadgets

// abs_word
// add_word
// binary_number
// byte_size
// cmp_words
// comparison
// constant_division
// modulo
// mul_add_words
// mul_add_words512
// mul_word_u64

pub(crate) fn is_zero(&mut self, value: Expression<F>) -> IsZeroGadget<F> {
IsZeroGadget::construct(self, value)
}
Expand Down Expand Up @@ -642,6 +630,14 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
MinMaxGadget::construct(self, lhs, rhs)
}

pub(crate) fn div_by_const<const N_BYTES: usize>(
&mut self,
numerator: Expression<F>,
denominator: u64,
) -> ConstantDivisionGadget<F, N_BYTES> {
ConstantDivisionGadget::construct(self, numerator, denominator)
}

// Fixed

pub(crate) fn range_lookup(&mut self, value: Expression<F>, range: u64) {
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/util/memory_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pub(crate) struct MemoryWordSizeGadget<F> {

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

Self { memory_word_size }
}
Expand Down
3 changes: 1 addition & 2 deletions zkevm-circuits/src/evm_circuit/util/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ impl<F: Field> TxDataGadget<F> {
// Calculate transaction gas fee
let mul_gas_fee_by_gas = MulWordByU64Gadget::construct(cb, gas_price.clone(), gas.expr());

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

let (cost_sum, gas_mul_gas_price_plus_value) = if calculate_total_cost {
let cost_sum = cb.query_word32();
Expand Down

0 comments on commit dfda568

Please sign in to comment.