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

Commit

Permalink
Add precompile contracts' addresses to the access list (#1640)
Browse files Browse the repository at this point in the history
### Description

We upstream scroll's precompile handling in bus mapping so that the EVM
circuit warms the precompile addresses at the beginning of the
transaction.

### Issue Link

#1626

### Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

### Content

- Warm precompile addresses
- Fix some minor issues in StepRws offset and the Rws debugging
messages.

---------

Co-authored-by: adria0.eth <5526331+adria0@users.noreply.github.com>
  • Loading branch information
ChihChengLiang and adria0 authored Oct 5, 2023
1 parent 5022d5d commit ed866f0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
15 changes: 14 additions & 1 deletion bus-mapping/src/evm/opcodes/begin_end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Error,
};
use eth_types::{
evm_types::{GasCost, MAX_REFUND_QUOTIENT_OF_GAS_USED},
evm_types::{GasCost, MAX_REFUND_QUOTIENT_OF_GAS_USED, PRECOMPILE_COUNT},
evm_unimplemented, ToWord, Word,
};
use ethers_core::utils::get_contract_address;
Expand Down Expand Up @@ -60,6 +60,19 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Erro
false,
)?;

// Add precompile contract address to access list
for address in 1..=PRECOMPILE_COUNT {
let address = eth_types::Address::from_low_u64_be(address);
let is_warm_prev = !state.sdb.add_account_to_access_list(address);
state.tx_accesslist_account_write(
&mut exec_step,
state.tx_ctx.id(),
address,
true,
is_warm_prev,
)?;
}

// Add caller, callee and coinbase (for EIP-3651) to access list.
for address in [call.caller_address, call.address, state.block.coinbase] {
let is_warm_prev = !state.sdb.add_account_to_access_list(address);
Expand Down
3 changes: 3 additions & 0 deletions eth-types/src/evm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ impl GasCost {
/// it from 10 to 50.
pub const EXP_BYTE_TIMES: u64 = 50;
}

/// This constant is used to iterate through precompile contract addresses 0x01 to 0x09
pub const PRECOMPILE_COUNT: u64 = 9;
33 changes: 29 additions & 4 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ use crate::{
},
};
use bus_mapping::state_db::CodeDB;
use eth_types::{evm_types::GasCost, keccak256, Field, ToWord, U256};
use eth_types::{
evm_types::{GasCost, PRECOMPILE_COUNT},
keccak256, Field, ToWord, U256,
};
use halo2_proofs::{
circuit::Value,
plonk::{Error, Expression},
Expand Down Expand Up @@ -170,6 +173,17 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
let gas_left = tx_gas.expr() - intrinsic_gas_cost;
let sufficient_gas_left = RangeCheckGadget::construct(cb, gas_left.clone());

// Add precompile contract address to access list
for addr in 1..=PRECOMPILE_COUNT {
cb.account_access_list_write_unchecked(
tx_id.expr(),
Word::new([addr.expr(), 0.expr()]),
1.expr(),
0.expr(),
None,
);
} // rwc_delta += PRECOMPILE_COUNT

// Prepare access list of caller and callee
cb.account_access_list_write_unchecked(
tx_id.expr(),
Expand Down Expand Up @@ -320,6 +334,7 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
// - Write CallContext IsPersistent
// - Write CallContext IsSuccess
// - Write Account (Caller) Nonce
// - Write TxAccessListAccount (Precompile) x PRECOMPILE_COUNT
// - Write TxAccessListAccount (Caller)
// - Write TxAccessListAccount (Callee)
// - Write TxAccessListAccount (Coinbase) for EIP-3651
Expand All @@ -338,7 +353,9 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
// - Write CallContext IsRoot
// - Write CallContext IsCreate
// - Write CallContext CodeHash
rw_counter: Delta(23.expr() + transfer_with_gas_fee.rw_delta()),
rw_counter: Delta(
23.expr() + transfer_with_gas_fee.rw_delta() + PRECOMPILE_COUNT.expr(),
),
call_id: To(call_id.expr()),
is_root: To(true.expr()),
is_create: To(tx_is_create.expr()),
Expand Down Expand Up @@ -377,12 +394,15 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
// - Write CallContext IsPersistent
// - Write CallContext IsSuccess
// - Write Account Nonce
// - Write TxAccessListAccount (Precompile) x PRECOMPILE_COUNT
// - Write TxAccessListAccount (Caller)
// - Write TxAccessListAccount (Callee)
// - Write TxAccessListAccount (Coinbase) for EIP-3651
// - Read Account CodeHash
// - a TransferWithGasFeeGadget
rw_counter: Delta(9.expr() + transfer_with_gas_fee.rw_delta()),
rw_counter: Delta(
9.expr() + transfer_with_gas_fee.rw_delta() + PRECOMPILE_COUNT.expr(),
),
call_id: To(call_id.expr()),
..StepStateTransition::any()
});
Expand Down Expand Up @@ -437,6 +457,7 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
// - Write CallContext IsPersistent
// - Write CallContext IsSuccess
// - Write Account Nonce
// - Write TxAccessListAccount (Precompile) x PRECOMPILE_COUNT
// - Write TxAccessListAccount (Caller)
// - Write TxAccessListAccount (Callee)
// - Write TxAccessListAccount (Coinbase) for EIP-3651
Expand All @@ -455,7 +476,9 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
// - Write CallContext IsRoot
// - Write CallContext IsCreate
// - Write CallContext CodeHash
rw_counter: Delta(22.expr() + transfer_with_gas_fee.rw_delta()),
rw_counter: Delta(
22.expr() + transfer_with_gas_fee.rw_delta() + PRECOMPILE_COUNT.expr(),
),
call_id: To(call_id.expr()),
is_root: To(true.expr()),
is_create: To(tx_is_create.expr()),
Expand Down Expand Up @@ -511,6 +534,8 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
let mut rws = StepRws::new(block, step);
rws.offset_add(7);

rws.offset_add(PRECOMPILE_COUNT as usize);

let is_coinbase_warm = rws.next().tx_access_list_value_pair().1;
let mut callee_code_hash = zero;
if !is_precompiled(&tx.to_or_contract_addr()) {
Expand Down
6 changes: 3 additions & 3 deletions zkevm-circuits/src/evm_circuit/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,9 @@ impl<'a> StepRws<'a> {
offset: 0,
}
}
/// Increment the step rw operation offset by `offset`.
pub(crate) fn offset_add(&mut self, offset: usize) {
self.offset = offset
/// Increment the step rw operation offset by `inc`.
pub(crate) fn offset_add(&mut self, inc: usize) {
self.offset += inc
}
/// Return the next rw operation from the step.
pub(crate) fn next(&mut self) -> Rw {
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/witness/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<F: Field> Block<F> {
println!("> Step {:?}", step.exec_state);
for rw_idx in 0..step.bus_mapping_instance.len() {
let rw = self.get_rws(step, rw_idx);
let rw_str = if rw.is_write() { "READ" } else { "WRIT" };
let rw_str = if rw.is_write() { "WRIT" } else { "READ" };
println!(" {} {} {:?}", rw.rw_counter(), rw_str, rw);
}
}
Expand Down

0 comments on commit ed866f0

Please sign in to comment.