Overview
Opcode:0x09
Introduced: 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: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
MUL then MOD is that MULMOD avoids intermediate overflow when a * b >= 2^256.
Examples
Basic Modular Multiplication
Overflow-Safe Multiplication
Zero Modulus
Multiply by Zero
Large Modulus Operation
Gas Cost
Cost: 8 gas (GasMidStep) MULMOD shares the same gas cost as ADDMOD due to similar computational complexity: Comparison:- ADD/SUB: 3 gas
- MUL: 5 gas
- DIV/MOD: 5 gas
- ADDMOD/MULMOD: 8 gas
- EXP: 10 + 50 per byte
Edge Cases
Maximum Values
Modulus of 1
Result Equals Modulus Minus One
Stack Underflow
Out of Gas
Common Usage
Elliptic Curve Point Multiplication
Montgomery Reduction
Modular Exponentiation Building Block
RSA/Fermat Operations
Polynomial Evaluation
Implementation
- TypeScript
Testing
Test Coverage
Edge Cases Tested
- Basic modular multiplication (50 % 3 = 2)
- Zero modulus (returns 0)
- Modulus of 1 (always returns 0)
- Multiply by zero (always returns 0)
- Large values (MAX * MAX)
- Overflow-safe computation
- Very large intermediate products
- Stack underflow (< 3 items)
- Out of gas (< 8 gas)
Security
Cryptographic Operations
MULMOD is fundamental for implementing secure cryptographic primitives: secp256k1 Scalar Multiplication: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
UnlikeMUL then MOD, MULMOD prevents intermediate overflow:
Vulnerable pattern:
Constant-Time Guarantees
EVM implementations must ensure MULMOD executes in constant time to prevent leaking sensitive information through timing channels:References
- Yellow Paper - Section 9.1 (Arithmetic Operations)
- EVM Codes - MULMOD
- EIP-196 - alt_bn128 curve operations
- EIP-197 - Precompiled contracts for optimal ate pairing check
- Montgomery Arithmetic - Efficient modular multiplication

