EIPs/EIPS/eip-3855.md
Alex Beregszaszi 04cd8b98e2
Add draft for PUSH0 (#3855)
* Add draft for PUSH0

* Rename to eip-3855

* Add discussion-to URL
2021-09-07 04:19:51 +00:00

3.3 KiB

eip title description author discussions-to status type category created
3855 PUSH0 instruction Introduce a new instruction which pushes the constant value 0 onto the stack Alex Beregszaszi (@axic), Hugo De la cruz (@hugo-dc), Paweł Bylica (@chfast) https://ethereum-magicians.org/t/eip-3855-push0-instruction/7014 Draft Standards Track Core 2021-02-19

Abstract

Introduce the PUSH0 (0x5f) instruction, which pushes the constant value 0 onto the stack.

Motivation

Many instructions expect offsets as inputs, which in a number of cases are zero. A good example is the return data parameters of CALLs, which are set to zeroes in case the contract prefers using RETURNDATA*. This is only one example, but there are many other reasons why a contract would need to push a zero value. They can achieve that today by PUSH1 0, which costs 3 gas at runtime, and is encoded as two bytes which means 2 * 200 gas deployment cost.

Because of the overall cost many try to use various other instructions to achieve the same effect. Common examples include PC, MSIZE, CALLDATASIZE, RETURNDATASIZE, CODESIZE, CALLVALUE, and SELFBALANCE. Some of these cost only 2 gas and are a single byte long, but their value can depend on the context.

We have conducted an analysis on Mainnet (block ranges 8,567,259…8,582,058 and 12,205,970…12,817,405), and ~11.5% of all the PUSH* instructions executed push of the value zero.

The main motivations for this change include:

  1. Reducing contract code size.
  2. Reducing the risk of contracts (mis)using various instructions as an optimisation measure. Repricing/changing those instructions can be more risky.
  3. Reduce the need to use DUP instructions for duplicating zeroes.

To put the "waste" into perspective, across existing accounts 340,557,331 bytes are wasted on PUSH1 00 instructions, which means 68,111,466,200 gas was spent to deploy them. In practice a lot of these accounts share identical bytecode with others, so their total stored size in clients is lower, however the deploy time cost must have been paid nevertheless.

An example for 2) is EIP-2733 which changes the behaviour of RETURNDATASIZE such that it may not be guaranteed to be zero at the beginning of the call frame.

Specification

Starting BLOCK_NUMBER >= HF, the instruction PUSH0 is introduced at 0x5f. It has no immediate data, pops no items from the stack, and places a single item with the value 0 onto the stack. The cost of this instruction is 2 gas (aka verylow).

Rationale

Gas cost

The verylow gas cost is used for instructions which place constant values onto the stack, such as ADDRESS, ORIGIN, and so forth.

Opcode

0x5f means it is in a "contiguous" space with the rest of the PUSH implementations and potentially could share the implementation.

If this argument is not strong enough, then 0x5c seems like a good alternative choice.

Backwards Compatibility

This EIP introduces a new opcode which did not exists previously. Already deployed contracts using this opcode could change their behaviour after this EIP.

Test Cases

  • 5F
  • 5F5F..5F (1024 times)
  • 5F5F..5F (1025 times)

Security Considerations

The authors are not aware of any impact on security.

Copyright and related rights waived via CC0.