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.

This article examines EIP-1052, which introduces the EXTCODEHASH opcode to support contract code verification by returning the Keccak-256 hash of contract code.

This article translates, summarizes, and supplements the material linked below.

I have also covered other EIPs.

Overview

This Ethereum Improvement Proposal introduces an opcode that returns the Keccak-256 hash of a contract's code.

Keccak-256 is a cryptographic hash function that produces a fixed-length digest of input data.

The opcode allows applications to retrieve the hash of contract code on Ethereum. Comparing that hash against an expected value can help verify that the code has not changed.

Motivation

Contract interactions sometimes require verifying that another contract's code satisfies specific requirements. This is particularly important for security and compatibility.

For example, a DApp that only wants to interact with contracts implementing known functionality may need to verify its counterparties' bytecode.

One approach is to use EXTCODECOPY, which copies the bytecode deployed at a specified address so that it can be inspected.

However, copying the entire bytecode can consume considerable gas, particularly for large contracts.

EXTCODEHASH provides a more efficient alternative when only the hash is required. Rather than copying and processing the entire bytecode, the caller retrieves its hash directly.

A small change to the bytecode produces a substantially different Keccak-256 digest with overwhelming probability. Comparing code hashes therefore provides an efficient way to check whether deployed code matches an expected bytecode representation.

Specification

The EXTCODEHASH opcode retrieves the Keccak-256 hash of an account's code without copying the actual bytecode into EVM memory.

It takes a 256-bit value from the stack, ignores the upper 96 bits, and interprets the remaining 160 bits as the Ethereum address.

If the account exists and contains code, the opcode returns the hash of that code.

If the account does not exist or is considered empty under EIP-161, the opcode pushes 0 onto the stack. An empty account under EIP-161 has no code, zero nonce, and zero balance.

If the account exists and is not empty but contains no code, the opcode pushes the hash of empty data:

c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470

EIP-1052 originally specified a gas cost of 400 for EXTCODEHASH.

Gas measures the computational resources consumed by Ethereum operations. The original cost reflected the resources required to retrieve the account information.

Subsequent Ethereum upgrades changed account-access gas accounting, so 400 should not be interpreted as the current universal cost of EXTCODEHASH.

Additional Notes

The opcode improves the efficiency of verifying other contracts and their characteristics, particularly when applications need a bytecode hash rather than the bytecode itself.

The original gas cost matches that of the historical BALANCE opcode because both operations require similar account lookups in Ethereum's state trie.

State Trie

Ethereum tracks account information through a data structure called the state trie.

The trie organizes key-value data so that account state can be located and authenticated. Account records include balances, nonces, storage roots, and code hashes; contract code itself is stored separately and identified by its hash.

When executing a transaction or contract operation that requires account information, Ethereum clients retrieve the relevant account state.

For example, a contract querying another account's balance uses its address to identify the corresponding account record. Likewise, EXTCODEHASH retrieves the code hash associated with that account.

The state trie provides an authenticated representation of account state, supporting updates and verification of the blockchain's state.

For EXTCODEHASH, as well as BALANCE, EXTCODESIZE, and EXTCODECOPY, the first 12 bytes of the input word are ignored. Only the final 20 bytes are interpreted as an account address, matching Ethereum's address length.

EXTCODEHASH can also distinguish nonempty accounts without code from nonexistent or EIP-161-empty accounts.

This distinction is useful when checking account state, although it does not by itself prove that an account is a trustworthy contract or determine whether it is controlled by a human.

Compatibility

The proposal identifies no specific backward-compatibility issues.

Tests

  • EXTCODEHASH for an existing, nonempty account without code returns c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470.
    • This is the Keccak-256 hash of empty data.
  • EXTCODEHASH for a nonexistent account returns 0.
  • EXTCODEHASH for a precompiled contract returns either c5d246... or 0, depending on its account state.
  • If EXTCODEHASH for account A returns X, EXTCODEHASH for A + 2**160 also returns X.
  • EXTCODEHASH for an account self-destructed during the current transaction.
  • EXTCODEHASH for an account whose self-destruction was subsequently reverted.
  • EXTCODEHASH for an account created during the current transaction.
  • EXTCODEHASH for an account whose creation was subsequently reverted.
  • EXTCODEHASH for an initially nonexistent account that subsequently becomes empty.
  • EXTCODEHASH for an empty account scheduled for removal under state-clearing rules.

These cases illustrate how the opcode determines its result under different account states and transaction conditions.

Its ability to distinguish accounts without code from nonexistent accounts, and its treatment of the upper address bits and reverted state changes, are particularly important for consistent execution.

The self-destruction cases describe the historical specification; later changes to SELFDESTRUCT semantics must be considered when testing modern Ethereum clients.

Citation

Nick Johnson arachnid@notdot.net, Paweł Bylica pawel@ethereum.org, "EIP-1052: EXTCODEHASH opcode," Ethereum Improvement Proposals, no. 1052, May 2018. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-1052.

Conclusion

EIP-1052 introduces EXTCODEHASH, an efficient opcode for retrieving the Keccak-256 hash of an account's contract code.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

I also publish articles on the following platforms.

https://chaldene.net/