Addition with wrapping overflow for 256-bit unsigned integers
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:0x01Introduced: Frontier (EVM genesis)ADD performs addition on two 256-bit unsigned integers with wrapping overflow semantics. When the result exceeds 2^256 - 1, it wraps around modulo 2^256, matching hardware integer register behavior.This is the most fundamental arithmetic operation in the EVM, used extensively in array indexing, counter increments, and numeric calculations.
Specification
Stack Input:
a (top)b
Stack Output:
(a + b) mod 2^256
Gas Cost: 3 (GasFastestStep)Operation:
result = (a + b) & ((1 << 256) - 1)
Behavior
ADD pops two values from the stack, adds them, and pushes the result back. Overflow wraps around without throwing exceptions:
If a + b < 2^256: Result is the mathematical sum
If a + b >= 2^256: Result is (a + b) mod 2^256
No exceptions are thrown for overflow. The result always fits in 256 bits.
// Manual overflow checkuint256 result = a + b;require(result >= a, "overflow");
Solidity 0.8.0+:
// Default: checked arithmetic (adds overflow checks)uint256 result = a + b; // Reverts on overflow// Explicit wrapping (uses raw ADD)unchecked { uint256 result = a + b; // Wraps on overflow}
Implementation
TypeScript
/** * ADD opcode (0x01) - Addition with overflow wrapping */export function add(frame: FrameType): EvmError | null { // Consume gas (GasFastestStep = 3) frame.gasRemaining -= 3n; if (frame.gasRemaining < 0n) { frame.gasRemaining = 0n; return { type: "OutOfGas" }; } // Pop operands if (frame.stack.length < 2) return { type: "StackUnderflow" }; const a = frame.stack.pop(); const b = frame.stack.pop(); // Compute result with wrapping (modulo 2^256) const result = (a + b) & ((1n << 256n) - 1n); // Push result if (frame.stack.length >= 1024) return { type: "StackOverflow" }; frame.stack.push(result); // Increment PC frame.pc += 1; return null;}
// VULNERABLE: No overflow protectionfunction transfer(address to, uint256 amount) public { balances[msg.sender] -= amount; // Can underflow! balances[to] += amount; // Can overflow!}
Attack scenario:
User with balance 5 transfers amount = 10
balances[msg.sender] -= 10 wraps to MAX_UINT256
Attacker now has infinite tokens
Mitigation (pre-0.8.0):
// Use SafeMath libraryfunction transfer(address to, uint256 amount) public { balances[msg.sender] = balances[msg.sender].sub(amount); // Reverts balances[to] = balances[to].add(amount); // Reverts}
Modern Solidity (0.8.0+):
// Automatic overflow checksfunction transfer(address to, uint256 amount) public { balances[msg.sender] -= amount; // Reverts on underflow balances[to] += amount; // Reverts on overflow}// Opt-in to wrapping when neededfunction unsafeIncrement(uint256 x) pure returns (uint256) { unchecked { return x + 1; // Uses raw ADD, wraps on overflow }}
Safe Patterns
Check-Effects-Interactions:
function withdraw(uint256 amount) public { require(balances[msg.sender] >= amount); // Check balances[msg.sender] -= amount; // Effect payable(msg.sender).transfer(amount); // Interaction}
Explicit Bounds Checking:
function add(uint256 a, uint256 b) pure returns (uint256) { uint256 c = a + b; require(c >= a, "addition overflow"); return c;}
Benchmarks
ADD is one of the fastest EVM operations:Execution time (relative):
ADD: 1.0x (baseline)
MUL: 1.2x
DIV: 2.5x
ADDMOD: 3.0x
EXP: 10x+
Gas efficiency:
3 gas per 256-bit addition
~0.33 million additions per million gas
Highly optimized in all EVM implementations
References
Yellow Paper - Section 9.1 (Arithmetic Operations)