Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

RETURN_REVERT: check not invalid first byte if creating #996

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions eth-types/src/evm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ impl fmt::Debug for Gas {
}
}

/// According to EIP-3541, disallow new code starting with 0xEF to be deployed.
pub const INVALID_INIT_CODE_FIRST_BYTE: u8 = 0xef;
/// Maximum bytecode size to permit for a contract.
pub const MAX_CODE_SIZE: u64 = 24576;
/// This constant ((2^32 - 1) * 32) is the highest number that can be used without overflowing the
Expand Down
40 changes: 39 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/return_revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
};
use bus_mapping::{circuit_input_builder::CopyDataType, state_db::CodeDB};
use eth_types::{
evm_types::{GasCost, OpcodeId},
evm_types::{GasCost, OpcodeId, INVALID_INIT_CODE_FIRST_BYTE},
Field, ToScalar, U256,
};
use ethers_core::utils::keccak256;
Expand All @@ -40,6 +40,10 @@ pub(crate) struct ReturnRevertGadget<F> {
is_success: Cell<F>,
restore_context: RestoreContextGadget<F>,

// Used to check first byte of create init code must not be 0xef (EIP-3541).
init_code_first_byte: Cell<F>,
is_init_code_first_byte_invalid: IsEqualGadget<F>,

copy_length: MinMaxGadget<F, N_BYTES_MEMORY_ADDRESS>,
copy_rw_increase: Cell<F>,
copy_rw_increase_is_zero: IsZeroGadget<F>,
Expand Down Expand Up @@ -125,7 +129,23 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
prev_keccak_code_hash,
code_size,
deployed_bytecode_rlc,
init_code_first_byte,
is_init_code_first_byte_invalid,
) = cb.condition(is_contract_deployment.clone(), |cb| {
// Read the first byte and check it must not be 0xef (EIP-3541).
let init_code_first_byte = cb.query_cell();
// lookup memory word to ensure the frist byte is really part of word ? ;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frist -> first

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the init_code_first_byte is not constrained?


let is_init_code_first_byte_invalid = IsEqualGadget::construct(
cb,
init_code_first_byte.expr(),
INVALID_INIT_CODE_FIRST_BYTE.expr(),
);
cb.require_zero(
"First byte of create init code must not be 0xef",
is_init_code_first_byte_invalid.expr(),
);

// poseidon hash of code.
//
// We don't need to place any additional constraints on code_hash. The lookup to
Expand Down Expand Up @@ -213,6 +233,8 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
prev_keccak_code_hash,
code_size,
deployed_bytecode_rlc,
init_code_first_byte,
is_init_code_first_byte_invalid,
)
});

Expand Down Expand Up @@ -328,6 +350,8 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
range,
deployed_bytecode_rlc,
is_success,
init_code_first_byte,
is_init_code_first_byte_invalid,
copy_length,
copy_rw_increase,
copy_rw_increase_is_zero,
Expand Down Expand Up @@ -397,12 +421,14 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
};

let copy_rwc_inc = step.copy_rw_counter_delta;
let mut init_code_first_byte: u8 = 0;

if call.is_create && call.is_success {
// read memory word and get real copy bytes
let deployed_bytecode: Vec<u8> =
get_copy_bytes(&mut rws, copy_rwc_inc as usize, shift, valid_length);

init_code_first_byte = *deployed_bytecode.first().unwrap_or(&0u8);
self.deployed_bytecode_rlc.assign(
region,
offset,
Expand Down Expand Up @@ -451,6 +477,18 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
}
}

self.init_code_first_byte.assign(
region,
offset,
Value::known(F::from(init_code_first_byte as u64)),
)?;
self.is_init_code_first_byte_invalid.assign(
region,
offset,
F::from(init_code_first_byte as u64),
F::from(INVALID_INIT_CODE_FIRST_BYTE as u64),
)?;

self.copy_rw_increase
.assign(region, offset, Value::known(F::from(copy_rwc_inc)))?;
self.copy_rw_increase_is_zero
Expand Down