EIP-7667 Raising hash function gas costs (Raise gas costs of hash functions)

No call decision · Fees & gas · EIP status Stagnant · created 2024-03-31

A proposal to raise the gas costs of hash operators such as KECCAK, SHA256, and BLAKE2 to match the actual cost incurred in ZK proving (ZK-SNARKs) rather than CPU execution time.

Why it is needed

The gas costs of hash functions were originally set based on the time they take to run on a typical CPU. But as proving Ethereum blocks with ZK-SNARKs has become important, it has become clear that the same hash operation takes far longer in a ZK-proving environment than on a CPU. Blocks with heavy hash usage take significantly longer than average to generate proofs for, and several layer 2s (L2s) work around this with stopgaps such as arbitrary limits or centralized sequencer rules. These stopgaps raise concerns around security and centralization. In the long run, proving layer 1 blocks themselves with ZK-SNARKs will require narrowing the gap between average and worst-case proving time, and hash operation calls are the single biggest contributor to that gap. While introducing Verkle trees would resolve the KECCAK hashing issue tied to Merkle-Patricia tries, hashing on the order of hundreds of millions of bytes within a single block is still possible using EVM operators alone.

What changes

Today

After EIP-7667

Who is affected

In depth

Background: the cost gap between CPU and ZK proving

The gas costs of hash function opcodes and precompiles were originally set based on execution time on a typical CPU. But proving EVM execution with ZK-SNARKs has become another important execution benchmark, and by that benchmark hash operations are severely underpriced in gas relative to other operations. Blocks with heavy hash usage take far longer to generate ZK proofs for than an average block. Many layer 2s work around this with stopgaps such as arbitrary limits or centralized sequencer rules, which raise concerns around security and centralization. There is also a long-term goal of proving layer 1 blocks themselves with ZK-SNARKs, which requires narrowing the gap between average and worst-case proving time, and hash operation calls are identified as the single biggest contributor to that gap.

Worst case: maximum hashing volume by opcode

The source text presents a table of the maximum hashing volume possible for each hash operation within a 30-million-gas block. KECCAK (opcode) costs 6 gas per word, allowing up to 160 million bytes; SHA256 (precompile) costs 12 gas per word, allowing up to 80 million bytes; RIPEMD costs 120 gas per word, allowing up to 8 million bytes; BLAKE2 costs 12 gas per 128 bytes, allowing up to 320 million bytes; and the LOG operation's data hashing costs 8 gas per byte, allowing up to 3.75 million bytes. Even if Verkle trees resolve the KECCAK hashing issue tied to Merkle-Patricia tries, hashing at this scale remains possible within a single block using EVM opcodes alone, which remains a problem.

Specification: gas parameters that change

KECCAK_BASE_COST rises from 30 to 300, and KECCAK_WORD_COST rises from 6 to 60. SHA256_BASE_COST rises from 60 to 300, and SHA256_WORD_COST rises from 12 to 60. BLAKE2_GFROUND rises from 1 to 10, and GLOGBYTE, the per-byte cost of the LOG operation, rises from 8 to 10. RIPEMD_BASE_COST (600) and RIPEMD_WORD_COST (120) already exceed the baseline and remain unchanged.

Rationale: the 300-per-hash-plus-60-per-word baseline and alternatives considered

This change is based on aligning the cost of every hash operation to 300 gas per hash plus 60 gas per word (RIPEMD, already above this, is left unchanged). Alternatives considered included multidimensional gas pricing (a separate base fee, block target, and limit for hashing) and a 'total gas cost floor' approach similar to what EIP-7623 applies to calldata. However, it explains that unlike calldata or blobs, gas costs internal to the EVM are determined not only by users but also by contract-to-contract subcalls, making such alternatives much harder to implement for hash gas costs.

Backward compatibility: changes to Merkle proof costs

Assuming that in most applications, the data the EVM hashes arrives as calldata, we compute the impact based on the case where 64 bytes (2 words) are hashed for every 32 bytes of data, as in binary Merkle proof verification. Adding the Keccak hash cost to the per-word calldata cost of 512 gas, the previous cost was 554 gas per step (including 42 gas for hashing), or 11080 gas total for a proof of length 20. Under the new cost, the hash cost rises to 420 gas, making it 932 gas per step and 18640 gas total. The original text assesses this as less than a doubling of that component's gas consumption, a small increase when weighed against other costs.

Security considerations

Since this proposal only raises costs rather than adding a new operation or making an existing one cheaper, the original text states that no separate security concerns were raised.

Current status

This EIP is classified as Stagnant, meaning it is not being actively discussed or decided for fork inclusion. The given core developer call records contain no decision or debate on this proposal.

Original EIP · ← EIPs