Exponential operation for 256-bit unsigned integers with dynamic gas costs
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0x0aIntroduced: Frontier (EVM genesis)
Gas Update: EIP-160 (Spurious Dragon, 2016)EXP computes base^exponent where both operands are 256-bit unsigned integers. The result wraps modulo 2^256 on overflow. Unlike other arithmetic operations, EXP has dynamic gas costs based on the byte length of the exponent.This operation uses exponentiation by squaring for efficient computation, critical for cryptographic operations and mathematical calculations.
Specification
Stack Input:
base (top)exponent
Stack Output:
base^exponent mod 2^256
Gas Cost: 10 + (50 × byte_length(exponent))Operation:
result = (base^exponent) & ((1 << 256) - 1)
Behavior
EXP pops two values from the stack (base, exponent), computes base^exponent, and pushes the result back:
Normal case: Result is base^exponent mod 2^256
Exponent = 0: Result is 1 (even when base = 0)
Base = 0: Result is 0 (except when exponent = 0)
Overflow wrapping: Result wraps modulo 2^256
The implementation uses fast exponentiation by squaring (square-and-multiply algorithm) for O(log n) complexity.
// Combine with MULMOD for secure cryptofunction modExp(uint256 base, uint256 exp, uint256 mod) pure returns (uint256 result){ result = 1; assembly { for {} gt(exp, 0) {} { if and(exp, 1) { result := mulmod(result, base, mod) } base := mulmod(base, base, mod) exp := shr(1, exp) } }}
Fixed-Point Math
// Scale calculations with powers of 10uint256 constant PRECISION = 1e18;function multiply(uint256 a, uint256 b) pure returns (uint256) { return (a * b) / PRECISION;}assembly { let precision := exp(10, 18) let result := div(mul(a, b), precision)}
// Silent overflow - be carefuluint256 result = 2 ** 256; // result = 0, no revert// Safe pattern with bounds checkingfunction safePow(uint256 base, uint256 exp, uint256 max) pure returns (uint256){ uint256 result = base ** exp; require(result <= max, "overflow"); return result;}
Constant-Time Considerations
EXP implementation must avoid timing leaks in cryptographic contexts:
// Timing-safe modular exponentiationfunction modExpSafe(uint256 base, uint256 exp, uint256 mod) pure returns (uint256){ // Use constant-time square-and-multiply // Never branch on secret exponent bits}
Algorithm: Exponentiation by Squaring
EXP uses the efficient square-and-multiply algorithm:
Input: base, exponentOutput: base^exponent mod 2^256result = 1while exponent > 0: if exponent & 1: result = (result * base) mod 2^256 base = (base * base) mod 2^256 exponent = exponent >> 1return result
Complexity: O(log n) multiplications where n is exponent valueExample: 3^13
Binary of 13: 1101- bit 0 (1): result = 1 * 3 = 3- bit 1 (0): skip- bit 2 (1): result = 3 * 9 = 27- bit 3 (1): result = 27 * 729 = 19683 (wrong)Correct:13 = 1101₂ = 8 + 4 + 13^13 = 3^8 × 3^4 × 3^1 = 6561 × 81 × 3 = 1594323