EIP-7797 · Stagnant · Core · 2024-10-23
EIP-7797: Double speed for hash_tree_root
Proposes a consensus-layer hash variant, SHA-256-512, that skips SHA-256's padding block for the fixed 64-byte inputs of SSZ hash_tree_root, roughly halving hashing work for beacon state roots.
What problem does it solve?
The consensus layer identifies every object (blocks, the beacon state, attestations…) by its SSZ hash_tree_root: data is split into 32-byte chunks and pairs of chunks are hashed together, level by level, until one root is left. With well over a million validators this is a major CPU cost. The author measures the state-root step at about 25 % of CPU time on the Holesky testnet, even with caching and hardware-accelerated SHA-256.
Every hash in that tree has the same input: exactly two 32-byte chunks, 512 bits. Standard SHA-256 still pads that input, which creates a second 512-bit block whose contents are always the same. Half the work is spent compressing a block that carries no information.
How it works
- Define SHA-256-512: SHA-256 with the preprocessing (padding) step removed, accepting only inputs of exactly 512 bits. The input is processed as one SHA-256 message block, giving one compression call instead of two.
- For each composite SSZ type in use, clients add a twin type that merkleizes with SHA-256-512. From the activating fork on, the new types should be preferred.
- Serialization, generalized indices and field meanings do not change; only the hash values do.
- Historical objects, and things like signing roots over pre-fork data or
BeaconState.latest_block_header, may need to be re-hashed with the original SHA-256 to recover their old roots.DepositDataandVoluntaryExitmay keep regular SHA-256. - Hardware acceleration still applies, since it implements the block compression function, which is unchanged.
Sample code
Pseudocode for hashing two child nodes:
IV = SHA256_INITIAL_STATE # the standard 8 x 32-bit words
def sha256_merkle(left: bytes, right: bytes) -> bytes:
# today: 64-byte input + fixed padding block = 2 compressions
return hashlib.sha256(left + right).digest()
def sha256_512(left: bytes, right: bytes) -> bytes:
assert len(left) == len(right) == 32
state = sha256_compress(IV, left + right) # 1 compression, no padding
return state_to_bytes(state)
The fixed padding block that SHA-256-512 drops (bytes 0x40 to 0x7F):
80 00 00 ... 00 | 00 00 00 00 00 00 02 00 # 0x80 marker, zeros, bit length = 512
Things to know
- Status is Stagnant: it is not scheduled for any fork.
- Contracts and light clients that verify beacon-chain proofs (for example through the beacon block root) would need updating and would have to choose the right hash for each fork's data.
- SHA-256-512 output for some 512-bit inputs equals regular SHA-256 of a shorter message, so it must not be used outside SSZ merkleization. Within SSZ this is safe because SSZ never hashes anything other than 512-bit inputs.
- The author argues this also prepares the ground for a future switch to a ZK-friendly hash, because the places where historical hashing must be kept would already be identified.