Modular multiplication for 256-bit unsigned integers with arbitrary modulus
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:0x09Introduced: Frontier (EVM genesis)MULMOD performs modular multiplication (a * b) % N where all operands are 256-bit unsigned integers. Unlike standard MUL followed by MOD, MULMOD computes the result using wider arithmetic to prevent intermediate overflow, making it critical for cryptographic operations.Division by zero (N = 0) returns 0 rather than throwing an exception.
Specification
Stack Input:
a (top)bN (modulus)
Stack Output:
(a * b) % N
Gas Cost: 8 (GasMidStep)Operation:
if N == 0: result = 0else: result = (a * b) % N
Behavior
MULMOD pops three values from the stack (a, b, N), computes (a * b) mod N, and pushes the result back:
Normal case: Result is (a * b) % N
N = 0: Returns 0 (EVM convention)
No intermediate overflow: Uses 512-bit arithmetic internally
The key advantage over MUL then MOD is that MULMOD avoids intermediate overflow when a * b >= 2^256.
// secp256k1 field multiplicationassembly { let p := 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F // Multiply two field elements let x1 := mload(0x00) let x2 := mload(0x20) let product := mulmod(x1, x2, p)}
Montgomery Reduction
// Montgomery form multiplicationfunction montgomeryMul(uint256 a, uint256 b, uint256 N, uint256 R) pure returns (uint256){ assembly { let T := mulmod(a, b, N) let m := mulmod(T, R, N) let t := addmod(mulmod(m, N, N), T, N) mstore(0x00, mulmod(t, R, N)) return(0x00, 0x20) }}
Modular Exponentiation Building Block
// Square-and-multiply algorithmfunction 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) } }}
RSA/Fermat Operations
// Modular square for primality testingfunction fermatTest(uint256 a, uint256 p) pure returns (bool) { // Check if a^(p-1) ≡ 1 (mod p) uint256 exp = p - 1; uint256 result = 1; assembly { for {} gt(exp, 0) {} { if and(exp, 1) { result := mulmod(result, a, p) } a := mulmod(a, a, p) exp := shr(1, exp) } } return result == 1;}
Polynomial Evaluation
// Evaluate polynomial at point x mod pfunction polyEval(uint256[] memory coeffs, uint256 x, uint256 p) pure returns (uint256 result){ assembly { let len := mload(coeffs) result := 0 for { let i := 0 } lt(i, len) { i := add(i, 1) } { let coeff := mload(add(coeffs, mul(add(i, 1), 0x20))) result := addmod(mulmod(result, x, p), coeff, p) } }}
Implementation
TypeScript
/** * MULMOD opcode (0x09) - Multiplication modulo N */export function mulmod(frame: FrameType): EvmError | null { // Consume gas (GasMidStep = 8) frame.gasRemaining -= 8n; if (frame.gasRemaining < 0n) { frame.gasRemaining = 0n; return { type: "OutOfGas" }; } // Pop operands: a, b, N if (frame.stack.length < 3) return { type: "StackUnderflow" }; const a = frame.stack.pop(); const b = frame.stack.pop(); const n = frame.stack.pop(); // Compute result let result: bigint; if (n === 0n) { result = 0n; } else { // BigInt handles arbitrary precision - no overflow result = (a * b) % n; } // Push result if (frame.stack.length >= 1024) return { type: "StackOverflow" }; frame.stack.push(result); // Increment PC frame.pc += 1; return null;}
// BN254 field operationsuint256 constant BN254_P = 21888242871839275222246405745257275088696311157297823662689037894645226208583;function bn254FieldMul(uint256 a, uint256 b) pure returns (uint256) { return mulmod(a, b, BN254_P);}
Side-Channel Resistance
MULMOD completes in constant time regardless of operand values, preventing timing attacks in cryptographic implementations. This is critical for:
Private key operations
Signature generation
Zero-knowledge proof systems
Overflow Safety
Unlike MUL then MOD, MULMOD prevents intermediate overflow:Vulnerable pattern:
// Can overflow if a * b >= 2^256uint256 product = a * b;uint256 result = product % N; // Wrong result if overflow occurred
Safe pattern:
// Always correctuint256 result = mulmod(a, b, N);
Constant-Time Guarantees
EVM implementations must ensure MULMOD executes in constant time to prevent leaking sensitive information through timing channels:
// Safe for private key operationsfunction blindSignature(uint256 message, uint256 blindFactor, uint256 n) pure returns (uint256){ return mulmod(message, blindFactor, n);}
References
Yellow Paper - Section 9.1 (Arithmetic Operations)