EIP-2046 · Stagnant · Core · 2019-05-17
EIP-2046: Reduced gas cost for static calls made to precompiles
Proposed cutting the base cost of a STATICCALL to a precompile from 700 to 40 gas, making cheap cryptographic precompiles practical to call.
What problem does it solve?
Precompiles are built-in contracts at fixed low addresses (e.g. ecrecover, SHA-256, elliptic-curve operations) that clients implement natively. Calling one still goes through the normal CALL-family opcodes and pays their base cost.
The Spurious Dragon fork raised that base cost to 700 gas to reflect loading another contract's code from disk. Precompiles have no code to load, yet paid the same price. For a precompile whose own work costs much less than 700 gas, the call overhead dominated, and some uses became impractical.
How it works
After the activating fork, the base cost of STATICCALL (0xfa, from EIP-214) depends on the target:
| Target | Base cost |
|---|---|
| Precompile (address range defined by EIP-1352) | 40 |
| Any other address | 700 (unchanged) |
Design choices from the spec:
- Only
STATICCALLis changed, to limit impact. Precompiles are stateless, so calling them statically loses nothing. Contracts deployed before Byzantium, which predateSTATICCALL, would not benefit. - Not zero. 40 gas is kept to represent the context switch. This also keeps the call from being free, so older proxy contracts cannot use it to sneak in state changes.
- An earlier, similar idea appeared in EIP-1109, which later took a different approach.
Sample code
Calling the SHA-256 precompile (address 0x02) with staticcall, the kind of call this EIP would make cheaper:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Hasher {
function sha(bytes memory data) external view returns (bytes32 out) {
(bool ok, bytes memory ret) = address(0x02).staticcall(data);
require(ok, "precompile failed");
out = abi.decode(ret, (bytes32));
}
}
Pseudocode of the pricing rule:
def staticcall_base_cost(target):
if is_precompile(target): # per EIP-1352 range
return 40
return 700
Things to know
- Status: Stagnant. It was never activated. The spec still has a "FIXME" in its motivation, and test cases and implementation are "TBA".
- Call pricing changed afterwards anyway: EIP-2929 (Berlin) replaced the flat 700 with warm/cold access costs, and precompile addresses are always treated as warm, so a call to one costs 100 gas base. That addressed much of this EIP's motivation.
- Some later EIPs repriced individual precompiles directly instead of the call overhead.