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: 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:
Stack Output:
Gas Cost: 8 (GasMidStep)
Operation:
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
The key advantage over 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
Despite higher cost, ADDMOD is more efficient than separate ADD + MOD operations when dealing with potential overflow.
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
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:
BLS12-381 Group Operations:
Timing Safety
ADDMOD operations complete in constant time regardless of operand values, preventing timing side-channel attacks in cryptographic implementations.
Overflow Protection
Unlike ADD then MOD, ADDMOD prevents intermediate overflow:
Vulnerable pattern:
Safe pattern:
References
- ADD - Basic addition with wrapping
- MULMOD - Modular multiplication
- MOD - Unsigned modulo operation