Skip to content

MODEXP

Arithmetic

Arbitrary-precision modular exponentiation: base^exponent mod modulus.

Address0x0000000000000000000000000000000000000005
NameMODEXP
Minimum gas500
Inputvariable, 96-byte header + operands
OutputmodLen bytes
Available sinceByzantium; repriced at Berlin (EIP-2565) and Osaka (EIP-7883)

Input

OffsetLengthFieldDescription
032baseLenByte length of `base`, big-endian.
3232expLenByte length of `exponent`, big-endian.
6432modLenByte length of `modulus`, big-endian.
96baseLenbaseThe base, big-endian.
expLenexponentThe exponent, big-endian.
modLenmodulusThe modulus, big-endian.

Output

base^exponent mod modulus, big-endian, left-padded to exactly `modLen` bytes.

Gas

max(500, mult_complexity(max(baseLen, modLen)) × iteration_count(expLen, expHead))

Frost prices MODEXP under Osaka rules (EIP-7883): the minimum is 500 gas, the exponent-length multiplier is 16, and multiplication complexity is 16 for operands up to 32 bytes and 2 × ceil(x/8)² above that.

Failure behaviour

  • Reverts with an error if any of baseLen, expLen, or modLen exceeds 1024 bytes (EIP-7823).
  • A zero modulus returns modLen zero bytes rather than failing.
  • Input shorter than the expected length is right-padded with zero bytes rather than rejected.

Calling it

Precompiles have no ABI and no function selector: the input is raw bytes at the exact offsets above. Call with staticcall.

// MODEXP at 0x05
(bool ok, bytes memory out) = address(0x05).staticcall(input);
require(ok, "MODEXP reverted");

Specification