Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
EIP-2565 changes the gas formula for Ethereum's ModExp precompile so that the charge better reflects the computation it performs.
This article explains the proposal below.
I also write about other EIPs.
Overview
ModExp computes modular exponentiation. Ethereum measures the resources consumed by execution in units of gas. EIP-2565 improves the estimate for this operation by multiplying an estimate of multiplication complexity by an estimate of the number of exponentiation iterations.
Motivation
Modular exponentiation is important for cryptographic applications such as signing and verification, some SNARKs, cryptographic accumulators, and mechanisms such as verifiable delay functions (VDFs). A precompile is a built-in operation that replaces the need to implement the same computation directly in EVM bytecode. The previous ModExp price made these uses disproportionately expensive. Lowering the charge can make more cryptographic designs practical on-chain, including some approaches to generating randomness with VDFs.
Modular exponentiation
Modular exponentiation computes , where is the base, the exponent, and the modulus. The result is the remainder of divided by .
Example
- Choose base , exponent , and modulus .
- Calculate .
- Divide 64 by 5 and take the remainder. Thus .
The result of is 4.
Cryptographic use
In RSA, select large primes and and use as the modulus. The public key is and the private key is , with and chosen to satisfy the key-generation conditions.
- Encryption computes for a message representative .
- Decryption computes from the ciphertext representative .
These simplified equations illustrate why the performance and security of modular exponentiation matter to RSA.
VDF
A verifiable delay function takes time to evaluate but allows others to verify its output quickly. It can support distributed randomness generation, timestamping, and fair ordering.
Example
Consider a blockchain lottery that needs a random value.
- Publish an initial value , potentially derived from public data such as a previous block hash.
- Evaluate the VDF on to get . Choose a delay parameter so evaluation takes a target amount of time, such as ten minutes, under the assumed hardware capabilities.
- Publish once evaluation finishes.
- Other participants verify promptly that is the correct output for .
- Use the verified value in the lottery, for example to select a winning number.
The delay makes it harder to know the output before others can act. The lottery still depends on how it selects the input and on its timing and hardware assumptions. Quick verification lets many participants check the result without repeating the long computation.
Specification
def calculate_multiplication_complexity(base_length, modulus_length):
max_length = max(base_length, modulus_length)
words = math.ceil(max_length / 8)
return words**2
def calculate_iteration_count(exponent_length, exponent):
iteration_count = 0
if exponent_length <= 32 and exponent == 0: iteration_count = 0
elif exponent_length <= 32: iteration_count = exponent.bit_length() - 1
elif exponent_length > 32: iteration_count = (8 * (exponent_length - 32)) + ((exponent & (2**256 - 1)).bit_length() - 1)
return max(iteration_count, 1)
def calculate_gas_cost(base_length, modulus_length, exponent_length, exponent):
multiplication_complexity = calculate_multiplication_complexity(base_length, modulus_length)
iteration_count = calculate_iteration_count(exponent_length, exponent)
return max(200, math.floor(multiplication_complexity * iteration_count / 3))Starting at the configured fork block, FORK_BLOCK_NUMBER, this calculation prices a call to the precompile at 0x0000000000000000000000000000000000000005.
1. Calculate multiplication complexity
calculate_multiplication_complexity takes the larger of the base and modulus lengths, divides it by eight, and rounds up to the number of words. Squaring that count gives an estimate of multiplication complexity.
2. Calculate the iteration count
calculate_iteration_count uses the exponent's length and value to estimate how many iterations exponentiation requires. For exponents at most 32 bytes long, it uses the bit length minus one for a nonzero exponent or a provisional count of zero when the exponent is zero. The final count is always at least one. For longer exponents, the displayed helper adds eight iterations per byte beyond the first 32 and uses up to 256 bits for the remaining term.
The helper preserves the original code, which masks the low 256 bits of its
exponentargument. For an exponent longer than 32 bytes, the relevant input to the adjusted exponent calculation is the first 32 bytes of the encoded exponent. Avoid interpreting the mask as a rule to use the trailing bytes of a full-length exponent.
3. Calculate the gas cost
calculate_gas_cost multiplies the complexity by the iteration count, divides by three, rounds down, and applies a minimum of 200 gas.
Together these changes align the charge with the work done and make it cheaper to use ModExp in cryptographic and other applications for many inputs.
Additional Notes
Benchmarks found that ModExp was overpriced relative to other precompiles. They also showed that a revised pricing formula could estimate the cost of different inputs more accurately.
ModExp is Ethereum's built-in modular exponentiation function. It is frequently useful in cryptographic computation, but still consumes node resources and therefore costs gas. Calling it overpriced means its assigned cost exceeded the computational work it required relative to comparable operations. The following changes address that mismatch.
1. Revise the complexity formula
EIP-198 defined a function to approximate the complexity of ModExp. The new formula is intended to track observed runtime more closely.
EIP-198's complexity function
EIP-198's mult_complexity approximates Karatsuba multiplication using different formulas based on x, the larger of the modulus and base lengths.
Karatsuba multiplication
Schoolbook multiplication is quadratic in the number of digits: doubling the length multiplies the work by about four. Karatsuba splits the operands into smaller parts and reduces the number of multiplications required, improving the asymptotic cost of large multiplications.
EIP-198 uses a piecewise approximation for this work. Small inputs use x ** 2. Medium and large inputs use different formulas to reflect the behavior of faster multiplication at larger sizes.
- For
x <= 64, usex ** 2. - For
64 < x <= 1024, usex ** 2 // 4 + 96 * x - 3072. - For
x > 1024, usex ** 2 // 16 + 480 * x - 199680.
The revised formula
EIP-2565 divides x by eight and rounds up to account for the number of limbs used in multiprecision arithmetic.
Multiprecision arithmetic represents a number as smaller parts called limbs so machines can handle values larger than their native integer types. With 64-bit limbs, each limb holds eight bytes. The revised formula uses the number of limbs needed for the larger of the base and modulus lengths. Larger inputs need more limbs and generally more work to process.
Multiprecision arithmetic
Fixed-width types such as 32-bit or 64-bit integers have a maximum representable value. Multiprecision arithmetic combines multiple limbs, which can have those same widths, into a representation that grows as needed.
Applications
Scientific computing, high-precision arithmetic, and cryptography use multiprecision arithmetic. RSA operations, for instance, can involve numbers 1,024 bits or larger.
Operations
Addition, subtraction, multiplication, and division work across limbs, with carries or borrows passed between them. Multiplication and division can require specialized algorithms. Depending on the input size, libraries can use approaches such as Karatsuba or FFT-based multiplication.
Implementations
Libraries such as GNU MP (GMP), MPFR, and MPIR implement multiprecision operations and handle the low-level representation and optimizations.
The formula also takes into account the binary exponentiation approach used for large exponents by big-integer libraries. Its regression fit follows measured runtime more closely than EIP-198's formula. The fit splits the test data because execution time varies depending on whether the modulus is odd or even.
Complexity functions
A complexity function is a mathematical model of the resources needed by an operation, such as steps or elapsed time as inputs grow.
EIP-198 estimates ModExp complexity using parameters such as the base and modulus lengths. The revised estimate better reflects the behavior of multiprecision arithmetic and binary exponentiation in measured runs. A closer match helps Ethereum allocate gas more proportionately to computation.
The new estimate helps make ModExp pricing more representative of its execution cost.

2. Change GQUADDIVISOR
After revising the complexity function, EIP-2565 changes GQUADDIVISOR to 3 to calibrate the charge in gas per second relative to other precompiles such as ECRecover.
What GQUADDIVISOR does
It is the constant denominator applied to the product of the complexity estimate and iteration count. A smaller divisor increases the charge if the numerator stays the same.
Why it changes
- The revised multiplication complexity formula changes the scale of the estimate.
- Recalibrating the denominator brings the charge closer to benchmarked runtime and helps align its gas per second with that of other precompiles.
Effect
A divisor of three makes the new formula more expensive than the same formula would be with a larger divisor. This comparison holds the numerator fixed. The change to the entire formula still lowers the gas cost in the benchmark cases below relative to EIP-198. The comparison with ECRecover concerns gas per unit of computation rather than claiming that any given ModExp call costs more.

3. Set a minimum charge
The minimum charge prevents the formula from underpricing small inputs. Gas tracks computational resources, and precompiles have their own gas schedules. Even a cheap call incurs some work. A floor ensures the operation pays at least a fixed amount even for the smallest inputs, helping protect the network from repeated calls that would otherwise consume resources at little cost.
Tests
The pricing cases below compare EIP-198 with EIP-2565. The revised model lowers charges substantially for many benchmark inputs.
| Test Case | EIP-198 Pricing | EIP-2565 Pricing |
|---|---|---|
| modexp_nagydani_1_square | 204 | 200 |
| modexp_nagydani_1_qube | 204 | 200 |
| modexp_nagydani_1_pow0x10001 | 3276 | 341 |
| modexp_nagydani_2_square | 665 | 200 |
| modexp_nagydani_2_qube | 665 | 200 |
| modexp_nagydani_2_pow0x10001 | 10649 | 1365 |
| modexp_nagydani_3_square | 1894 | 341 |
| modexp_nagydani_3_qube | 1894 | 341 |
| modexp_nagydani_3_pow0x10001 | 30310 | 5461 |
| modexp_nagydani_4_square | 5580 | 1365 |
| modexp_nagydani_4_qube | 5580 | 1365 |
| modexp_nagydani_4_pow0x10001 | 89292 | 21845 |
| modexp_nagydani_5_square | 17868 | 5461 |
| modexp_nagydani_5_qube | 17868 | 5461 |
| modexp_nagydani_5_pow0x10001 | 285900 | 87381 |
The reductions are particularly clear for larger inputs with exponent 0x10001.
Security
Underpricing ModExp relative to measured runtime could create a denial-of-service risk. Nodes must perform the modular exponentiations included in blocks, so the gas schedule needs to account for their computational cost. The revised complexity formula and minimum charge aim to make the operation usable while keeping pricing aligned with the work validators perform.
Further Reading
References
Kelly Olson (@ineffectualproperty), Sean Gulley (@sean-sn), Simon Peffers (@simonatsn), Justin Drake (@justindrake), Dankrad Feist (@dankrad), "EIP-2565: ModExp Gas Cost," Ethereum Improvement Proposals, no. 2565, March 2020. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-2565.
Conclusion
EIP-2565 revises Ethereum's ModExp gas formula so the charge better matches benchmarked execution costs. It changes the estimate of multiplication complexity, recalibrates the divisor, and applies a minimum charge.
If you have questions, feel free to send me a direct message on Twitter.
I also publish on these sites.


