EIP-2565 · Final · Core · 2020-03-20
EIP-2565: ModExp Gas Cost
Replaced the gas formula for the ModExp precompile (0x05) with a simpler, benchmark-based one that made big-number modular exponentiation much cheaper, with a 200 gas floor.
What problem does it solve?
EIP-198 added the ModExp precompile at address 0x05, which computes base^exp mod modulus on arbitrary-size integers. It is the building block for RSA signature checks, VDFs, accumulators and some SNARK verifiers.
Benchmarks showed ModExp was overpriced compared with other precompiles, and that the EIP-198 complexity formula (modelled on Karatsuba multiplication) did not track real execution time well. ERC-2565 changed the pricing so these operations became practical on-chain. It went live in the Berlin upgrade.
How it works
The interface and arithmetic are unchanged; only gas changes. Cost is "how hard is one multiplication" times "how many steps the exponentiation takes", divided by 3:
- Multiplication complexity: take the larger of the base and modulus lengths in bytes, convert to 8-byte words (rounding up), and square it.
- Iteration count: roughly the bit length of the exponent minus one. For exponents longer than 32 bytes, add 8 per extra byte beyond 32, plus the bit length of the low 256 bits. Minimum 1.
- Gas =
max(200, floor(complexity * iterations / 3)).
So the old divisor (GQUADDIVISOR, 20 in EIP-198) became 3, and a 200 gas minimum stops tiny inputs from being underpriced.
def gas(base_len, mod_len, exp_len, exponent):
words = math.ceil(max(base_len, mod_len) / 8)
complexity = words ** 2
if exp_len <= 32 and exponent == 0:
iters = 0
elif exp_len <= 32:
iters = exponent.bit_length() - 1
else:
iters = 8 * (exp_len - 32) + ((exponent & (2**256 - 1)).bit_length() - 1)
return max(200, complexity * max(iters, 1) // 3)
From the spec's test table: modexp_nagydani_1_pow0x10001 dropped from 3276 to 341 gas, and modexp_nagydani_5_pow0x10001 from 285900 to 87381.
Sample code
Calling the precompile from Solidity. Input is three 32-byte lengths followed by base, exponent and modulus:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
library ModExp {
function modexp(bytes memory base, bytes memory exp, bytes memory mod)
internal view returns (bytes memory result)
{
bytes memory input = abi.encodePacked(
uint256(base.length), uint256(exp.length), uint256(mod.length),
base, exp, mod
);
bool ok;
(ok, result) = address(0x05).staticcall(input);
require(ok, "modexp failed");
// result has the same length as `mod`
}
}
Things to know
- Status is Final and active since Berlin.
- The spec flagged the main risk as making ModExp too cheap and opening a DoS vector. Later work revisited this: EIP-7883 raises ModExp costs again and EIP-7823 caps input sizes. Check which pricing applies on your target fork before hard-coding gas.
- Input lengths, not values, drive most of the cost, so pass minimal-length byte strings.
- EIP-2666, a broader precompile repricing proposal, assumed this change was in place.