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

Add precompile contracts' addresses to the access list #1640

Merged
merged 6 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 15 additions & 0 deletions bus-mapping/src/evm/opcodes/begin_end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use eth_types::{
};
use ethers_core::utils::get_contract_address;

const PRECOMPILE_COUNT: u64 = 9;
ChihChengLiang marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Clone, Copy, Debug)]
pub(crate) struct BeginEndTx;

Expand Down Expand Up @@ -60,6 +62,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
30 changes: 27 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use halo2_proofs::{
plonk::{Error, Expression},
};

const PRECOMPILE_COUNT: usize = 9;

#[derive(Clone, Debug)]
pub(crate) struct BeginTxGadget<F> {
// tx_id is query in current scope. The range should be determined here
Expand Down Expand Up @@ -170,6 +172,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 +333,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 +352,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 +393,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 +456,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 +475,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 +533,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);

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
Loading