Modular addition 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:0x08Introduced: 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:
a (top)bN (modulus)
Stack Output:
(a + b) % N
Gas Cost: 8 (GasMidStep)Operation:
if N == 0: result = 0else: result = (a + b) % N
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.
// secp256k1 field arithmetic (p = 2^256 - 2^32 - 977)assembly { let p := 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F // Add two field elements let x1 := mload(0x00) let x2 := mload(0x20) let sum := addmod(x1, x2, p)}
Modular Ring Operations
// Ring arithmetic mod Nfunction addInRing(uint256 a, uint256 b, uint256 N) pure returns (uint256){ assembly { mstore(0x00, addmod(a, b, N)) return(0x00, 0x20) }}
Hash Computations
// Polynomial rolling hashassembly { let hash := 0 let base := 31 let mod := 1000000007 // hash = (hash * base + char) % mod hash := addmod(mulmod(hash, base, mod), char, mod)}
Schnorr/BLS Signature Math
// s = (k + e*x) mod n (Schnorr signature)assembly { let n := 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 let k := mload(0x00) let e_x := mulmod(e, x, n) let s := addmod(k, e_x, n)}
Implementation
TypeScript
/** * ADDMOD opcode (0x08) - Addition modulo N */export function addmod(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;}
ADDMOD is critical for implementing cryptographic operations that require modular arithmetic:Elliptic Curve Operations:
// secp256k1 field additionuint256 constant FIELD_P = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;function fieldAdd(uint256 a, uint256 b) pure returns (uint256) { return addmod(a, b, FIELD_P);}
BLS12-381 Group Operations:
// BLS12-381 field modulusuint256 constant BLS_P = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab;function blsFieldAdd(uint256 a, uint256 b) pure returns (uint256) { return addmod(a, b, BLS_P);}
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:
// Can overflow if a + b >= 2^256uint256 sum = a + b;uint256 result = sum % N; // Wrong result if overflow occurred
Safe pattern:
// Always correctuint256 result = addmod(a, b, N);
References
Yellow Paper - Section 9.1 (Arithmetic Operations)