EIP-2666 Precompile and KECCAK256 gas repricing (Repricing of precompiles and Keccak256 function)
No call decision · Fees & gas · EIP status Stagnant · created 2020-05-22
A proposal to replace the gas costs charged for SHA256, RIPEMD, and KECCAK256 operations with new formulas that match their actual computational cost.
Why it is needed
Ethereum charges a fee called gas every time a smart contract performs an operation, and hash functions such as SHA256 and RIPEMD and the KECCAK256 operation have existing gas costs assigned to them. However, these costs do not properly reflect the time an actual client takes to process the operation. In particular, after EIP-2046 lowered the cost of STATICCALL(0xfa) calls, a correction factor implicitly baked into the old precompile prices no longer holds. As a result, some operations are priced more expensively than they should be, or in a way that does not match their structure. This EIP proposes more accurate formulas based on measured benchmarks from four clients: Geth, OpenEthereum, Besu, and Nethermind.
What changes
Today
- SHA256: 60 plus 12 gas per 32-byte word of input
- RIPEMD: 600 plus 120 gas per 32-byte word of input
- KECCAK256: 30 plus 6 gas per 32-byte word of input
- The implicit correction in the old prices no longer matches after the STATICCALL cost reduction (EIP-2046)
After EIP-2666
- SHA256: 10 + ((input length+8)/64+1) x 9 gas
- RIPEMD: 6 + ((input length+8)/64+1) x 12 gas
- KECCAK256: 13 + (input length/136+1) x 15 gas
- Replace with formulas fitted to the average of measured benchmarks across the four clients
Who is affected
- Smart contract developers: Gas consumption changes for functions that involve hash operations, so gas estimates and limit settings need to be rechecked.
- Security personnel: Contracts that used a fixed gas cost as a reentrancy defense may have that defense broken as the cost decreases.
- Client teams: Since only constant values need to change rather than the formula itself, this is a simple implementation that requires no separate reference implementation.
In depth
Repricing the SHA256 precompile (0x02)
SHA256 currently costs 60 gas plus 12 gas for each 32-byte word. This EIP changes the formula to the form A x ((input length+8)/64+1) + B, and proposes A=9, B=10 based on combining measurements across clients (Geth A=5,B=3 / OpenEthereum A=9,B=4 / Besu A=5,B=10 / Nethermind A=10,B=5). The final formula is 10 + ((len(input)+8)/64+1) x 9. It notes this precompile has no large one-time cost from the past that needs compensating, so it does not conflict with EIP-2046.
Repricing the RIPEMD precompile (0x03)
RIPEMD currently costs 600 gas plus 120 gas for each 32-byte word, a price that is relatively excessive. Using the same formula form A x ((input length+8)/64+1) + B with per-client measurements (Geth A=12,B=6 / OpenEthereum A=8,B=2 / Besu A=29,B=16 / Nethermind A=10,B=6), it proposes A=12, B=6. The final formula is 6 + ((len(input)+8)/64+1) x 12. The original text states that Besu expects performance improvements within the year.
Repricing the KECCAK256 operation (0x20)
KECCAK256 currently costs 30 gas plus 6 gas for each 32-byte word. The new formula is A x (input length/136+1) + B, and combining per-client measurements (Geth A=13,B=13 / OpenEthereum A=15,B=2 / Besu A=19,B=28 / Nethermind A=16,B=3) gives A=15, B=13. The final formula is 13 + (len(input)/136+1) x 15. According to the test cases in the original text, hashing 0 bytes changes from 30 to 28 gas, 64 bytes changes from 42 to 28 gas, and 160 bytes changes from 60 to 43 gas.
Relationship with EIP-2046
EIP-2046 lowers the cost of STATICCALL(0xfa) calls to precompiles. This EIP holds that some of the existing precompile prices were set to implicitly compensate for the old STATICCALL's large fixed cost of 700 gas. It explains that the new formulas are safe even without that correction. The original text also states that it would be ideal for EIP-2565, which covers MODEXP repricing, to be implemented at the same time.
The structural intent of the pricing formulas
In each formula, a term of the form A x 1 is kept separate rather than being merged into B. The original text explains that this design reflects the fact that even hashing an empty byte array actually requires performing one round of the hash operation, and the price should account for that.
Backwards compatibility, security, and EIP status
The original text states that repricing precompiles is a standard procedure that has happened before. However, it warns that as gas costs drop for many contracts, reentrancy defenses based on a fixed gas cost could break, while also noting that such a defense design was never a good or final design to begin with. This EIP's status is Stagnant, a proposal that has stalled with no further progress. It states that no reference implementation existed at the time of writing, and that only constant values in major clients would need to change.