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: 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:
Stack Output:
Gas Cost: 8 (GasMidStep)
Operation:
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.
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
MULMOD is more efficient than separate MUL + MOD when dealing with values that would overflow during multiplication.
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
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:
BN254 Pairing Operations:
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:
Safe pattern:
Constant-Time Guarantees
EVM implementations must ensure MULMOD executes in constant time to prevent leaking sensitive information through timing channels:
References
- MUL - Basic multiplication with wrapping
- ADDMOD - Modular addition
- MOD - Unsigned modulo operation
- EXP - Exponentiation (uses MULMOD internally)