Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

Background

ERC-7511 proposes using PUSH0 to make the ERC-1167 minimal proxy even smaller.

This article explains and expands on the proposal below.

I also cover other EIPs, BIPs, SLIPs, CAIPs, ENSIPs, RFCs, and ACPs.

Overview

The Shanghai upgrade introduced the PUSH0 opcode, enabling a more efficient implementation of the ERC-1167 Minimal Proxy Contract. The proposed proxy saves approximately 200 gas on deployment and 5 gas per execution. It preserves the existing behavior while making the bytecode smaller and easier to read.

For background on ERC-1167, see the following article.

[ERC-1167] Cloning contracts with gas-efficient minimal proxies

A minimal proxy is a lightweight clone that contains only the instructions necessary to forward execution to an existing implementation contract. Using PUSH0 simplifies stack handling and reduces the number of instructions needed.

Motivation

Reduce contract bytecode by one byte

The original ERC-1167 runtime uses SWAP instructions to arrange addresses and other values on the stack. By using PUSH0, the revised sequence can remove one of those swaps. It still uses SWAP2 after copying the return data, as shown below.

PUSH0 pushes 0 onto the stack. The revised arrangement removes one swap and makes the deployed bytecode 1 byte shorter. Saving a byte matters when a factory deploys a large number of minimal proxies.

Reduce execution gas

The original proxy uses two DUP instructions, each of which copies a stack value and costs 3 gas. The revised arrangement uses two PUSH0 instructions instead.

Two PUSH0 instructions cost 4 gas in total, compared with 6 gas for two DUP instructions. This saves 2 gas per execution. Removing another instruction brings the total saving to about 5 gas per call. These savings accumulate when a proxy receives many calls or operates over a long period.

Make the proxy bytecode easier to read

PUSH0 has one straightforward effect: it pushes zero onto the stack. Using that explicit operation removes unnecessary stack manipulation and makes the intent of the bytecode easier to follow. ERC-1167 prioritizes compactness, and the new arrangement improves readability while preserving the original behavior.

Specification

Standard Proxy Contract

The optimized minimal proxy uses the following 44-byte runtime code.

text
365f5f375f5f365f73bebebebebebebebebebebebebebebebebebebebe5af43d5f5f3e5f3d91602a57fd5bf3

Replace bytes 9 through 28, inclusive, with the 20-byte address of the implementation contract. The proxy delegates execution to this address.

Runtime code disassembly

The table breaks down the bytecode into instructions and shows the stack after each one. The leftmost value is the top of the stack. The source table lists JUMPI at 0x27, which is the immediate byte of PUSH1. Both tables below show its position in the runtime bytecode, 0x28.

pc op Instruction Stack
00 36 CALLDATASIZE (get the calldata size) cds
01 5f PUSH0 (push zero) 0 cds
02 5f PUSH0 0 0 cds
03 37 CALLDATACOPY (copy calldata)
04 5f PUSH0 0
05 5f PUSH0 0 0
06 36 CALLDATASIZE cds 0 0
07 5f PUSH0 0 cds 0 0
08 73… PUSH20 <implementation address> impl 0 cds 0 0
1d 5a GAS gas impl 0 cds 0 0
1e f4 DELEGATECALL (delegate to the implementation) suc
1f 3d RETURNDATASIZE rds suc
20 5f PUSH0 0 rds suc
21 5f PUSH0 0 0 rds suc
22 3e RETURNDATACOPY suc
23 5f PUSH0 0 suc
24 3d RETURNDATASIZE rds 0 suc
25 91 SWAP2 suc 0 rds
26 602a PUSH1 0x2a (push the JUMPDEST position) 0x2a suc 0 rds
28 57 JUMPI (branch on success or failure) 0 rds
29 fd REVERT (exit on failure)
2a 5b JUMPDEST (destination on success) 0 rds
2b f3 RETURN (return the result)

The sequence copies calldata, delegates execution to the implementation with DELEGATECALL, copies the returned data, and then executes either RETURN or REVERT. PUSH0 sets up the stack without the DUP instructions used in the earlier design.

Minimal Creation Code

The creation code used at deployment is 53 bytes long.

text
602c8060095f395ff3365f5f375f5f365f73bebebebebebebebebebebebebebebebebebebebe5af43d5f5f3e5f3d91602a57fd5bf3

The first 9 bytes are initcode. The remainder is stored as the deployed contract's runtime code.

text
602c8060095f395ff3

The initcode copies the runtime code into memory and returns it with RETURN. These are the only operations needed to deploy the proxy.

Deployment

The following Solidity contract deploys a minimal proxy that uses PUSH0.

js
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

// Note: this contract requires `PUSH0`, which is available in solidity > 0.8.20 and EVM version > Shanghai
contract Clone0Factory {
    error FailedCreateClone();

    receive() external payable {}

    /**
     * @dev Deploys and returns the address of a clone0 (Minimal Proxy Contract with `PUSH0`) that mimics the behaviour of `implementation`.
     *
     * This function uses the create opcode, which should never revert.
     */
    function clone0(address impl) public payable returns (address addr) {
        // first 18 bytes of the creation code 
        bytes memory data1 = hex"602c8060095f395ff3365f5f375f5f365f73";
        // last 15 bytes of the creation code
        bytes memory data2 = hex"5af43d5f5f3e5f3d91602a57fd5bf3";
        // complete the creation code of Clone0
        bytes memory _code = abi.encodePacked(data1, impl, data2);

        // deploy with create op
        assembly {
            // create(v, p, n)
            addr := create(callvalue(), add(_code, 0x20), mload(_code))
        }

        if (addr == address(0)) {
            revert FailedCreateClone();
        }
    }
}

The factory concatenates the 18-byte prefix, the implementation address, and the 15-byte suffix to construct the creation code. It deploys the proxy with create and returns the proxy's address.

This Solidity example requires version 0.8.20 or later, and the EVM must support Shanghai or later because the bytecode uses PUSH0.

Additional Notes

The optimized minimal proxy implements only the steps necessary to forward execution. PUSH0 reduces the bytecode size and gas cost. The sequence has four steps.

  1. Copy calldata with CALLDATACOPY.
  2. Delegate execution to the implementation contract with DELEGATECALL.
  3. Copy the returned data with RETURNDATACOPY.
  4. Execute RETURN or REVERT according to the delegate call's result.

The following sections show the bytecode and stack operations for each step.

Step 1. Copy calldata

First, the proxy copies the calldata supplied by the caller into memory. CALLDATACOPY takes three arguments: the destination memory offset, the source calldata offset, and the number of bytes to copy. Here the stack must contain [0, 0, cds] with cds denoting the calldata size.

pc op Instruction Stack
00 36 CALLDATASIZE (get the calldata size) cds
01 5f PUSH0 0 cds
02 5f PUSH0 0 0 cds
03 37 CALLDATACOPY

PUSH0 supplies both zero offsets directly.

Step 2. Execute DELEGATECALL

The proxy prepares a DELEGATECALL to pass calldata to the implementation. DELEGATECALL takes six arguments: gas, target address, input offset, input size, output offset, and output size. The proxy uses zero for both offsets and the output size, reads the remaining gas using GAS, and supplies the target address with PUSH20.

pc op Instruction Stack
04 5f PUSH0 0
05 5f PUSH0 0 0
06 36 CALLDATASIZE cds 0 0
07 5f PUSH0 0 cds 0 0
08 73… PUSH20 (implementation address) impl 0 cds 0 0
1d 5a GAS (get the remaining gas) gas impl 0 cds 0 0
1e f4 DELEGATECALL suc

DELEGATECALL leaves suc on the stack. It is 1 on success and 0 on failure. The later branch uses this result.

Step 3. Copy return data

RETURNDATACOPY copies the data produced by DELEGATECALL into memory. It takes [0, 0, rds], where rds is the size returned by RETURNDATASIZE.

pc op Instruction Stack
1f 3d RETURNDATASIZE (size of return data) rds suc
20 5f PUSH0 0 rds suc
21 5f PUSH0 0 0 rds suc
22 3e RETURNDATACOPY suc

PUSH0 once again supplies both zero offsets directly, keeping the stack setup minimal.

Step 4. Branch to RETURN or REVERT

The proxy must return the result if DELEGATECALL succeeds or revert if it fails. EVM bytecode uses JUMPI and JUMPDEST to express this conditional branch.

The proxy puts suc where the branch expects it and prepares [0, rds] for both RETURN and REVERT. The return data size is only available after DELEGATECALL, and this instruction sequence uses SWAP2 to arrange the branch arguments.

pc op Instruction Stack
23 5f PUSH0 0 suc
24 3d RETURNDATASIZE rds 0 suc
25 91 SWAP2 (reorder the stack) suc 0 rds
26 602a PUSH1 0x2a (jump target on success) 0x2a suc 0 rds
28 57 JUMPI (jump to 0x2a on success) 0 rds
29 fd REVERT
2a 5b JUMPDEST 0 rds
2b f3 RETURN

The proxy runs RETURN on success and REVERT on failure.

Complete runtime code

The full runtime code is as follows.

text
365f5f375f5f365f73bebebebebebebebebebebebebebebebebebebebe5af43d5f5f3e5f3d91602a57fd5bf3

At 44 bytes, it is one byte shorter than the ERC-1167 runtime. The optimization provides three benefits.

  • Approximately 200 gas saved on deployment.
  • Approximately 5 gas saved per execution.
  • Simpler stack operations using PUSH0, which make the bytecode easier to follow.

The proxy retains the behavior of ERC-1167 while reducing its size and gas costs.

Compatibility

The revised minimal proxy depends on PUSH0. Shanghai introduced this opcode, so the proxy requires an EVM environment that supports Shanghai or later.

  • The proxy cannot be deployed against a pre-Shanghai chain state.
  • It cannot run on an L2 or sidechain that has yet to support Shanghai.

The proxy still forwards calls to the implementation with DELEGATECALL, as in ERC-1167. A supported environment can use the optimized variant for new deployments without changing the surrounding architecture.

Security Considerations

The PUSH0 variant follows the same delegation model as ERC-1167, so the same security considerations apply.

Consideration Implication
Fixed implementation The implementation address is immutable once the proxy is deployed.
Initialization The implementation constructor does not initialize proxy storage. Initialization requires a separate function call.
Implementation safety A vulnerability in the implementation affects every proxy that uses it.
Transparency The minimal bytecode can make the actual execution logic hard to identify.

Fixed implementation address

A minimal proxy delegates to a specific implementation contract. PUSH20 embeds the target address in the deployed bytecode, which provides no mechanism for changing that address later.

A deployed ERC-1167-style proxy therefore cannot swap in another implementation. Upgradeable patterns such as Transparent Proxy with an admin or UUPS provide a separate upgrade mechanism.

Initialization

The implementation's constructor does not initialize the minimal proxy. Although the proxy contains very little code of its own, DELEGATECALL writes state to its storage using the implementation's layout.

Provide an initializer in the implementation and invoke it through the proxy after deployment. Missing this step can leave the system in an unsafe state, for example with no owner address set.

The deployment flow must include both proxy deployment and the initializer call.

Implementation contract safety

The minimal proxy contains almost no business logic and delegates processing to the implementation contract. A vulnerability in that implementation affects all proxies pointing to it. Deploying more proxies creates more instances that share the same underlying bug.

Audit and test the implementation thoroughly, especially if a factory will deploy many minimal proxies.

Transparency

A minimal proxy's bytecode is so short that it may look like it does nothing when viewed on a block explorer such as Etherscan. It delegates execution to another address, and that indirection can be difficult to trace.

Users may have trouble finding both the contract that receives the delegated calls and the address where the actual logic lives. Documentation and interfaces can make this clearer by identifying the proxy and displaying its implementation address.

Citation

0xAA (@AmazingAng), vectorized (@Vectorized), 0age (@0age), "ERC-7511: Minimal Proxy Contract with PUSH0 [DRAFT]," Ethereum Improvement Proposals, no. 7511, September 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7511.

Conclusion

ERC-7511 uses PUSH0 to reduce the bytecode size and gas costs of the ERC-1167 minimal proxy.

If you have questions, feel free to send me a message on Twitter.

Twitter @cardene777

You can also find my writing on the following sites.