EIP-7883 · Final · Core · 2025-02-11
EIP-7883: ModExp Gas Cost Increase
Raises the gas price of the ModExp precompile (minimum 200 to 500, general cost roughly tripled, large inputs priced more steeply) so its worst cases no longer block a higher block gas limit.
What problem does it solve?
The ModExp precompile at address 0x05 computes base^exp mod modulus on arbitrarily large integers (EIP-198). Its gas formula was last tuned by EIP-2565. Benchmarks across execution clients later showed that some inputs were significantly underpriced: a block filled with such calls could take far longer to execute than its gas suggests.
That matters because the block gas limit can only be raised safely if every operation's worst case fits in the time budget. EIP-7883 reprices ModExp so it stops being one of the bottlenecks to scaling the gas limit.
How it works
The interface and arithmetic are unchanged; only the gas formula moves. Three adjustments to the EIP-2565 formula:
- Higher floor and no division by 3.
max(200, complexity * iterations / 3)becomesmax(500, complexity * iterations). The minimum goes from 200 to 500 gas and the general cost triples. - Large exponents cost more. For exponents longer than 32 bytes, each extra byte adds 16 iterations instead of 8.
- Minimum operand size and steeper scaling. Multiplication complexity is at least 16 (as if operands were 32 bytes), and above 32 bytes it is
2 * words²instead ofwords²(wherewords = ceil(max(base_len, mod_len) / 8)).
def multiplication_complexity(base_len, mod_len):
max_len = max(base_len, mod_len)
words = math.ceil(max_len / 8)
return 16 if max_len <= 32 else 2 * words**2
def iteration_count(exp_len, exp):
if exp_len <= 32 and exp == 0: n = 0
elif exp_len <= 32: n = exp.bit_length() - 1
else: n = 16 * (exp_len - 32) + ((exp & (2**256 - 1)).bit_length() - 1)
return max(n, 1)
def gas(base_len, mod_len, exp_len, exp):
return max(500, multiplication_complexity(base_len, mod_len) * iteration_count(exp_len, exp))
According to the spec's analysis, about 99.7 % of historical calls see either the 200 to 500 floor bump or a tripling. Some edge-case test vectors rise by 20x or more.
Sample code
Calling the precompile from Solidity; budget gas generously, since hard-coded gas stipends written for the old pricing may now be too low:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
library ModExp {
/// Returns base^exp % mod for 32-byte operands.
function modexp(uint256 base, uint256 exp, uint256 mod) internal view returns (uint256 r) {
bytes memory input = abi.encodePacked(
uint256(32), uint256(32), uint256(32), // lengths of base, exp, mod
base, exp, mod
);
(bool ok, bytes memory out) = address(0x05).staticcall(input);
require(ok, "modexp failed");
r = abi.decode(out, (uint256));
}
}
Things to know
- Status: Final. It is a backwards-incompatible gas change: contracts that call
0x05with a fixed gas amount tuned to EIP-2565 prices may now run out of gas. - Typical RSA verification and big-number cryptography contracts are the main users affected; re-measure gas for your inputs instead of assuming old numbers.
- It is often discussed together with EIP-7823, which caps
ModExpinput sizes, and EIP-7825, which caps per-transaction gas; together they bound worst-case work. - The spec notes the remaining risk is overpricing some cases, which it considers less harmful than underpricing.