EIPs Explained

EIP-7686 · Stagnant · Core · 2024-04-15

EIP-7686: Linear EVM memory limits

Replaces quadratic memory pricing with a flat 3 gas per word plus a hard cap, so an EVM execution with N gas can never use more than N bytes of memory.

What problem does it solve?

EVM memory is priced with two interacting rules: memory expansion costs grow quadratically with size, and a call can forward at most 63/64 of its remaining gas to a child call (EIP-150). Together they do keep memory bounded, but working out the worst-case memory a transaction can need is hard. That matters for clients and especially for constrained environments such as ZK provers, which want to allocate a fixed amount of memory up front.

EIP-7686, proposed by Vitalik Buterin, simplifies the rules and gives a clean, provable bound: a transaction with N gas can use at most N bytes of memory in total across all nested calls.

How it works

Three changes:

  1. Linear memory cost. The quadratic term is dropped:

    # before
    memory_cost = (words ** 2) // 512 + 3 * words
    # after
    memory_cost = 3 * words
    
  2. Hard per-call limit. If expanding memory would make the current call's memory size strictly exceed that call's initial gas limit, execution reverts.

  3. Child calls are reduced by memory in use. The gas a call may pass on becomes:

    def max_call_gas(gas, memory_byte_size):
        return gas - max(gas // 64, memory_byte_size)
    

    So a parent that has already used M bytes of memory can only forward gas minus M, and the child's own memory cap (its gas limit) is reduced accordingly.

With these rules a client can run an N-gas transaction inside one N-byte buffer: the current frame uses the bottom of the buffer, a child call uses the space starting right after the parent's memory, and so on recursively.

The 3 gas per word figure matches MCOPY, so clearing a child's memory after it returns can reuse the same logic. The 63/64 rule is kept to preserve today's practical call-depth limit.

Sample code

Comparing old and new expansion costs and the new call-gas rule (pseudocode, Python syntax):

def old_memory_cost(byte_size: int) -> int:
    w = (byte_size + 31) // 32
    return w * w // 512 + 3 * w

def new_memory_cost(byte_size: int) -> int:
    w = (byte_size + 31) // 32
    return 3 * w

def check_expansion(new_byte_size: int, call_initial_gas: int) -> None:
    if new_byte_size > call_initial_gas:
        raise Exception("memory limit exceeded: revert")

def max_call_gas(gas: int, memory_byte_size: int) -> int:
    return gas - max(gas // 64, memory_byte_size)

# 1 MiB of memory
print(old_memory_cost(1 << 20))   # 2,195,456 gas
print(new_memory_cost(1 << 20))   #    98,304 gas, but the call now needs >= 1,048,576 gas

Things to know

  • Status is Stagnant: it has not been scheduled for any network upgrade.
  • Large memory becomes much cheaper per byte, but you can only use as many bytes as the call has gas, so a call with 100k gas can use at most 100k bytes of memory.
  • Code that touches a high memory offset while being called with a small gas limit could start reverting. The spec argues this is rare in practice: any call doing a state write already needs thousands of gas, and therefore has thousands of bytes of memory available.
  • A parent that has expanded memory a lot can forward less gas to its children than before, which can matter for contracts that build large buffers before making calls.

#evm #memory #gas #core #zk

Details

Authors
Vitalik Buterin (@vbuterin)
Status
Stagnant
Type
Standards Track / Core
Created
2024-04-15
Required by

Machine-readable: Markdown · JSON