Overview
Address:0x0000000000000000000000000000000000000005
Introduced: Byzantium (EIP-198)
EIP: EIP-198, EIP-2565
The ModExp precompile computes modular exponentiation: (base^exponent) mod modulus. This enables efficient RSA signature verification, Fermat primality testing, and other advanced cryptographic operations in smart contracts.
EIP-198 introduced ModExp in Byzantium. EIP-2565 (Berlin) reduced gas costs to make RSA verification practical.
Gas Cost
Complex formula that varies by hardfork: Pre-Berlin:max(200, complexity * iteration_count / GQUADDIVISOR)
Berlin+: max(200, complexity * iteration_count / GQUADDIVISOR_v2)
Where:
complexity = mult_complexity * max(length(base), length(modulus))mult_complexity = (max(length(base), length(modulus)) / 8)^2if max > 64, elsemult_complexity = max(length(base), length(modulus))^2 / 4iteration_count = max(exponent_bitlength - 1, 1)adjusted for exponent head- Minimum gas:
200
src/crypto/ModExp/calculateGas
Examples:
- Small inputs (1-byte each): ~200 gas
- 256-byte RSA (2048-bit): ~50,000+ gas
- 512-byte RSA (4096-bit): ~200,000+ gas
Input Format
Output Format
Output length equalsmodulus_length specified in input.
modulus_length = 0.
Usage Example
Error Conditions
- Input length < 96 bytes
- Out of gas (gas cost depends on input sizes)
- Modulus = 0 (returns error)
- Integer overflow in length values
Use Cases
- RSA signature verification: Verify RSA-2048, RSA-4096 signatures on-chain
- Zero-knowledge proofs: Perform modular arithmetic for zkSNARKs
- Cryptographic protocols: Diffie-Hellman key exchange, ElGamal encryption
- Primality testing: Fermat and Miller-Rabin primality tests
- Number theory: Modular inverses, Chinese remainder theorem
Implementation Details
- Zig: Uses multi-precision arithmetic from ModExp crypto module
- TypeScript: BigInt-based modular exponentiation with square-and-multiply
- Integration: Depends on ModExp.calculateGas for hardfork-specific gas calculation
- Optimization: Binary exponentiation (square-and-multiply algorithm)
RSA Verification Example
Gas Cost Reduction (EIP-2565)
Berlin hard fork (EIP-2565) reduced gas costs significantly:
This made RSA verification practical for many use cases.
Edge Cases
- Zero exponent: Returns 1 (any number to power 0 is 1)
- Modulus = 1: Returns 0 (anything mod 1 is 0)
- Base > modulus: Automatically reduced mod modulus
- Truncated input: Missing bytes treated as zero
- Zero modulus: Returns error (division by zero)
Test Vectors
References
Specifications
- Yellow Paper - Appendix E (Precompiled Contracts)
- EIP-198: Big Integer Modular Exponentiation
- EIP-2565: ModExp Gas Cost

