Overview
Opcode:0x08
Introduced: Frontier (EVM genesis)
ADDMOD performs modular addition (a + b) % N where all operands are 256-bit unsigned integers. Unlike standard ADD followed by MOD, ADDMOD computes the result using wider arithmetic to prevent intermediate overflow, making it essential for cryptographic operations.
Division by zero (N = 0) returns 0 rather than throwing an exception.
Specification
Stack Input:Behavior
ADDMOD 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
ADD then MOD is that ADDMOD avoids intermediate overflow when a + b >= 2^256.
Examples
Basic Modular Addition
Overflow-Safe Addition
Zero Modulus
Modulus of 1
Large Modulus
Gas Cost
Cost: 8 gas (GasMidStep) ADDMOD costs more than basic ADD due to wider arithmetic requirements: Comparison:- ADD/SUB: 3 gas
- MUL/DIV/MOD: 5 gas
- ADDMOD/MULMOD: 8 gas
- EXP: 10 + 50 per byte
Edge Cases
Maximum Values
Identity Elements
Stack Underflow
Out of Gas
Common Usage
Elliptic Curve Point Addition
Modular Ring Operations
Hash Computations
Schnorr/BLS Signature Math
Implementation
- TypeScript
Testing
Test Coverage
Edge Cases Tested
- Basic modular addition (15 % 3 = 0)
- Zero modulus (returns 0)
- Modulus of 1 (always returns 0)
- Large values (MAX + MAX)
- Overflow-safe computation
- Identity elements (a + 0, 0 + 0)
- Stack underflow (< 3 items)
- Out of gas (< 8 gas)
Security
Cryptographic Importance
ADDMOD is critical for implementing cryptographic operations that require modular arithmetic: Elliptic Curve Operations:Timing Safety
ADDMOD operations complete in constant time regardless of operand values, preventing timing side-channel attacks in cryptographic implementations.Overflow Protection
UnlikeADD then MOD, ADDMOD prevents intermediate overflow:
Vulnerable pattern:
References
- Yellow Paper - Section 9.1 (Arithmetic Operations)
- EVM Codes - ADDMOD
- EIP-196 - Precompiled contracts for addition and scalar multiplication on curve alt_bn128
- secp256k1 Parameters - SEC 2 specification
- BLS12-381 For The Rest Of Us - BLS curve reference

