Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support EIP-3860 (Limit and meter initcode) for Shanghai #507

Merged
merged 2 commits into from
May 18, 2023
Merged
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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bus-mapping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ default = ["test"]
test = ["mock", "rand"]
scroll = ["eth-types/scroll"]
# Enable shanghai feature of mock only if mock is enabled (by test).
shanghai = ["mock?/shanghai"]
shanghai = ["eth-types/shanghai", "mock?/shanghai"]
14 changes: 6 additions & 8 deletions bus-mapping/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub enum OogError {
/// Out of Gas for MLOAD, MSTORE, MSTORE8, which have static memory
/// expansion gas cost
StaticMemoryExpansion,
/// Out of Gas for CREATE, RETURN, REVERT, which have dynamic memory
/// expansion gas cost
/// Out of Gas for RETURN, REVERT, which have dynamic memory expansion gas
/// cost
DynamicMemoryExpansion,
/// Out of Gas for CALLDATACOPY, CODECOPY, EXTCODECOPY, RETURNDATACOPY,
/// which copy a specified chunk of memory
Expand All @@ -92,8 +92,8 @@ pub enum OogError {
SloadSstore,
/// Out of Gas for CALL, CALLCODE, DELEGATECALL and STATICCALL
Call,
/// Out of Gas for CREATE2
Create2,
/// Out of Gas for CREATE and CREATE2
Create,
/// Out of Gas for SELFDESTRUCT
SelfDestruct,
}
Expand Down Expand Up @@ -183,9 +183,7 @@ pub(crate) fn get_step_reported_error(op: &OpcodeId, error: &str) -> ExecError {
OpcodeId::MLOAD | OpcodeId::MSTORE | OpcodeId::MSTORE8 => {
OogError::StaticMemoryExpansion
}
OpcodeId::CREATE | OpcodeId::RETURN | OpcodeId::REVERT => {
OogError::DynamicMemoryExpansion
}
OpcodeId::RETURN | OpcodeId::REVERT => OogError::DynamicMemoryExpansion,
OpcodeId::CALLDATACOPY
| OpcodeId::CODECOPY
| OpcodeId::EXTCODECOPY
Expand All @@ -202,7 +200,7 @@ pub(crate) fn get_step_reported_error(op: &OpcodeId, error: &str) -> ExecError {
OogError::Call
}
OpcodeId::SLOAD | OpcodeId::SSTORE => OogError::SloadSstore,
OpcodeId::CREATE2 => OogError::Create2,
OpcodeId::CREATE | OpcodeId::CREATE2 => OogError::Create,
OpcodeId::SELFDESTRUCT => OogError::SelfDestruct,
_ => OogError::Constant,
};
Expand Down
14 changes: 7 additions & 7 deletions bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ mod error_invalid_creation_code;
mod error_invalid_jump;
mod error_oog_account_access;
mod error_oog_call;
mod error_oog_dynamic_memory;
mod error_oog_log;
mod error_oog_memory_copy;
mod error_oog_sload_sstore;
Expand Down Expand Up @@ -95,7 +94,6 @@ use error_invalid_creation_code::ErrorCreationCode;
use error_invalid_jump::InvalidJump;
use error_oog_account_access::ErrorOOGAccountAccess;
use error_oog_call::OOGCall;
use error_oog_dynamic_memory::OOGDynamicMemory;
use error_oog_log::ErrorOOGLog;
use error_oog_memory_copy::OOGMemoryCopy;
use error_oog_sload_sstore::OOGSloadSstore;
Expand Down Expand Up @@ -299,12 +297,14 @@ fn fn_gen_error_state_associated_ops(
ExecError::OutOfGas(OogError::Constant) => {
Some(StackOnlyOpcode::<0, 0, true>::gen_associated_ops)
}
ExecError::OutOfGas(OogError::Create2) => {
Some(StackOnlyOpcode::<4, 0, true>::gen_associated_ops)
}
ExecError::OutOfGas(OogError::Create) => match geth_step.op {
OpcodeId::CREATE => Some(StackOnlyOpcode::<3, 0, true>::gen_associated_ops),
OpcodeId::CREATE2 => Some(StackOnlyOpcode::<4, 0, true>::gen_associated_ops),
op => unreachable!("OOG Create cannot occur in {op}"),
},
ExecError::OutOfGas(OogError::Log) => Some(ErrorOOGLog::gen_associated_ops),
ExecError::OutOfGas(OogError::DynamicMemoryExpansion) => {
Some(OOGDynamicMemory::gen_associated_ops)
Some(StackOnlyOpcode::<2, 0, true>::gen_associated_ops)
}
ExecError::OutOfGas(OogError::StaticMemoryExpansion) => {
Some(StackOnlyOpcode::<1, 0, true>::gen_associated_ops)
Expand Down Expand Up @@ -443,7 +443,7 @@ pub fn gen_associated_ops(
// For exceptions that already enter next call context, but fail immediately
// (e.g. Depth, InsufficientBalance), we still need to parse the call.
if geth_step.op.is_call_or_create()
&& !matches!(exec_error, ExecError::OutOfGas(OogError::Create2))
&& !matches!(exec_error, ExecError::OutOfGas(OogError::Create))
{
let call = state.parse_call(geth_step)?;
state.push_call(call);
Expand Down
57 changes: 0 additions & 57 deletions bus-mapping/src/evm/opcodes/error_oog_dynamic_memory.rs

This file was deleted.

9 changes: 4 additions & 5 deletions circuit-benchmarks/src/bytecode_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod tests {
use ark_std::{end_timer, start_timer};
use bus_mapping::evm::OpcodeId;
use eth_types::Field;
use eth_types::{evm_types::MAX_CODE_SIZE, Field};
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::{create_proof, keygen_pk, keygen_vk, verify_proof},
Expand Down Expand Up @@ -45,12 +45,11 @@ mod tests {
// Unique string used by bench results module for parsing the result
const BENCHMARK_ID: &str = "Bytecode Circuit";

// Contract code size exceeds 24576 bytes may not be deployable on Mainnet.
const MAX_BYTECODE_LEN: usize = 24576;

let num_rows = 1 << degree;
let max_bytecode_row_num = num_rows - TestBytecodeCircuit::<Fr>::unusable_rows();
let bytecode_len = std::cmp::min(MAX_BYTECODE_LEN, max_bytecode_row_num);

// Contract code size exceeds 24576 bytes may not be deployable on Mainnet.
let bytecode_len = std::cmp::min(MAX_CODE_SIZE as usize, max_bytecode_row_num);
let bytecodes_num: usize = max_bytecode_row_num / bytecode_len;

// Create the circuit
Expand Down
1 change: 1 addition & 0 deletions eth-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ poseidon-circuit = { git = "https://github.com/scroll-tech/poseidon-circuit.git"
[features]
default = ["warn-unimplemented"]
warn-unimplemented = []
shanghai = []
scroll = []
34 changes: 34 additions & 0 deletions eth-types/src/evm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl fmt::Debug for Gas {
}
}

/// 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
/// square operation of gas calculation.
/// <https://github.com/ethereum/go-ethereum/blob/e6b6a8b738069ad0579f6798ee59fde93ed13b43/core/vm/gas_table.go#L38>
Expand All @@ -69,6 +71,38 @@ pub const MAX_REFUND_QUOTIENT_OF_GAS_USED: usize = 5;
/// Gas stipend when CALL or CALLCODE is attached with value.
pub const GAS_STIPEND_CALL_WITH_VALUE: u64 = 2300;

#[cfg(feature = "shanghai")]
mod gas_create {
// For EIP-3860, there are 2 special gas cost constraints in geth
// [gasCreate2Eip3860](https://github.com/ethereum/go-ethereum/blob/eb83e7c54021573eaceb14236af3a7a8c64f6027/core/vm/gas_table.go#L321)
// (similar for CREATE).
// 1. size <= 49152 (MaxInitCodeSize)
// 2. gasCost = memoryGasCost + (2 + 6) * ((size + 31) / 32) should not
// overflow for Uint64.
// No need to constrain the second condition, since the maximum gas cost
// cannot overflow for Uint64 (36028809887100925 calculated by
// `memorySize = 0x1FFFFFFFE0` and `size = 49152`) if the first condition is
// satisfied.

/// Maximum init code size to permit in a creation transaction and create instructions.
pub const MAX_INIT_CODE_SIZE: u64 = 2 * super::MAX_CODE_SIZE;
/// Gas per init code word of CREATE when creating a contract.
pub const CREATE_GAS_PER_CODE_WORD: u64 = 2;
/// Gas per init code word of CREATE2 when creating a contract.
pub const CREATE2_GAS_PER_CODE_WORD: u64 =
CREATE_GAS_PER_CODE_WORD + super::GasCost::COPY_SHA3.0;
}
#[cfg(not(feature = "shanghai"))]
mod gas_create {
/// Maximum init code size (0x1FFFFFFFE0) if not EIP-3860.
pub use super::MAX_EXPANDED_MEMORY_ADDRESS as MAX_INIT_CODE_SIZE;
/// Gas per init code word of CREATE if not EIP-3860.
pub const CREATE_GAS_PER_CODE_WORD: u64 = 0;
/// Gas per init code word of CREATE2 if not EIP-3860.
pub const CREATE2_GAS_PER_CODE_WORD: u64 = super::GasCost::COPY_SHA3.0;
}
pub use gas_create::*;

/// Defines the gas consumption.
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct GasCost(pub u64);
Expand Down
2 changes: 1 addition & 1 deletion mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ rand = "0.8"

[features]
default = []
shanghai = []
shanghai = ["eth-types/shanghai"]
2 changes: 1 addition & 1 deletion testool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ ctor = "0.1.22"
default = ["ignore-test-docker", "skip-self-destruct"]
ignore-test-docker = []
skip-self-destruct = []
shanghai = []
shanghai = ["eth-types/shanghai"]
2 changes: 1 addition & 1 deletion zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test = ["ethers-signers", "mock", "bus-mapping/test"]
# while tests inside this repo should not enable "poseidon-codehash-lookup" feature.
scroll = ["bus-mapping/scroll", "eth-types/scroll", "zktrie", "enable-sign-verify", "reject-eip2718", "poseidon-codehash"]
# Enable shanghai feature of mock only if mock is enabled (by test).
shanghai = ["bus-mapping/shanghai", "mock?/shanghai"]
shanghai = ["bus-mapping/shanghai", "eth-types/shanghai", "mock?/shanghai"]
poseidon-codehash-lookup = []
test-circuits = []
warn-unimplemented = ["eth-types/warn-unimplemented"]
Expand Down
12 changes: 6 additions & 6 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ mod error_invalid_opcode;
mod error_oog_account_access;
mod error_oog_call;
mod error_oog_constant;
mod error_oog_create2;
mod error_oog_create;
mod error_oog_dynamic_memory;
mod error_oog_exp;
mod error_oog_log;
Expand Down Expand Up @@ -170,7 +170,7 @@ use error_invalid_opcode::ErrorInvalidOpcodeGadget;
use error_oog_account_access::ErrorOOGAccountAccessGadget;
use error_oog_call::ErrorOOGCallGadget;
use error_oog_constant::ErrorOOGConstantGadget;
use error_oog_create2::ErrorOOGCreate2Gadget;
use error_oog_create::ErrorOOGCreateGadget;
use error_oog_dynamic_memory::ErrorOOGDynamicMemoryGadget;
use error_oog_exp::ErrorOOGExpGadget;
use error_oog_log::ErrorOOGLogGadget;
Expand Down Expand Up @@ -334,7 +334,7 @@ pub(crate) struct ExecutionConfig<F> {
error_oog_log: Box<ErrorOOGLogGadget<F>>,
error_oog_account_access: Box<ErrorOOGAccountAccessGadget<F>>,
error_oog_sha3: Box<ErrorOOGSha3Gadget<F>>,
error_oog_create2: Box<ErrorOOGCreate2Gadget<F>>,
error_oog_create: Box<ErrorOOGCreateGadget<F>>,
error_code_store: Box<ErrorCodeStoreGadget<F>>,
#[cfg(not(feature = "scroll"))]
error_oog_self_destruct:
Expand Down Expand Up @@ -596,7 +596,7 @@ impl<F: Field> ExecutionConfig<F> {
error_oog_account_access: configure_gadget!(),
error_oog_sha3: configure_gadget!(),
error_oog_exp: configure_gadget!(),
error_oog_create2: configure_gadget!(),
error_oog_create: configure_gadget!(),
#[cfg(not(feature = "scroll"))]
error_oog_self_destruct: configure_gadget!(),
error_code_store: configure_gadget!(),
Expand Down Expand Up @@ -1433,8 +1433,8 @@ impl<F: Field> ExecutionConfig<F> {
ExecutionState::ErrorOutOfGasEXP => {
assign_exec_step!(self.error_oog_exp)
}
ExecutionState::ErrorOutOfGasCREATE2 => {
assign_exec_step!(self.error_oog_create2)
ExecutionState::ErrorOutOfGasCREATE => {
assign_exec_step!(self.error_oog_create)
}
ExecutionState::ErrorOutOfGasSELFDESTRUCT => {
#[cfg(not(feature = "scroll"))]
Expand Down
Loading