Overview
Arithmetic operations provide integer math on 256-bit (32-byte) unsigned values. All operations use modular arithmetic (mod 2^256) with wrapping overflow/underflow semantics, matching the behavior of hardware integer registers. 11 opcodes enable:- Basic arithmetic: ADD, MUL, SUB, DIV, MOD
- Signed operations: SDIV, SMOD
- Modular arithmetic: ADDMOD, MULMOD
- Exponentiation: EXP
- Type extension: SIGNEXTEND
Opcodes
| Opcode | Name | Gas | Stack In → Out | Description |
|---|---|---|---|---|
| 0x01 | ADD | 3 | a, b → a+b | Addition with wrapping |
| 0x02 | MUL | 5 | a, b → a*b | Multiplication with wrapping |
| 0x03 | SUB | 3 | a, b → a-b | Subtraction with wrapping |
| 0x04 | DIV | 5 | a, b → a/b | Unsigned division (0 if b=0) |
| 0x05 | SDIV | 5 | a, b → a/b | Signed division (two’s complement) |
| 0x06 | MOD | 5 | a, b → a%b | Unsigned modulo (0 if b=0) |
| 0x07 | SMOD | 5 | a, b → a%b | Signed modulo (two’s complement) |
| 0x08 | ADDMOD | 8 | a, b, N → (a+b)%N | Addition modulo N (arbitrary precision) |
| 0x09 | MULMOD | 8 | a, b, N → (a*b)%N | Multiplication modulo N (arbitrary precision) |
| 0x0a | EXP | 10+50/byte | a, exp → a^exp | Exponentiation |
| 0x0b | SIGNEXTEND | 5 | b, x → y | Extend sign from byte b |
Overflow Semantics
Wrapping Operations
ADD, MUL, SUB use wrapping arithmetic:Division by Zero
DIV and MOD return 0 when dividing by zero (not an exception):Signed Arithmetic
Two’s Complement Representation
SDIV and SMOD interpret 256-bit values as signed integers:- Range: -2^255 to 2^255 - 1
- Negative flag: Bit 255 (most significant bit)
- Encoding: Two’s complement
Edge Case: MIN_INT / -1
Special handling for minimum signed integer divided by -1:Modular Arithmetic
ADDMOD and MULMOD
Perform operations in arbitrary precision before taking modulo:Modulo by Zero
Returns 0 when N = 0 (matches DIV/MOD behavior).Exponentiation
Dynamic Gas Cost
EXP charges 10 gas base + 50 gas per byte of exponent:Algorithm
Uses square-and-multiply for efficiency, but still constrained by gas limits.Sign Extension
SIGNEXTEND Operation
Extends the sign bit from a specified byte position:Gas Costs
| Category | Gas | Opcodes |
|---|---|---|
| Very Low (Fastest Step) | 3 | ADD, SUB |
| Low (Fast Step) | 5 | MUL, DIV, SDIV, MOD, SMOD, SIGNEXTEND |
| Mid Step | 8 | ADDMOD, MULMOD |
| EXP Step | 10 + 50/byte | EXP |
Common Patterns
Safe Math (Pre-Solidity 0.8.0)
Before built-in overflow checking:Efficient Modular Exponentiation
For large modular exponentiation, use MODEXP precompile (0x05) instead of combining EXP + MOD:Division with Rounding
EVM division truncates toward zero:Implementation
TypeScript
Zig
Edge Cases
Maximum Values
Zero Inputs
Security Considerations
Overflow Attacks
Pre-Solidity 0.8.0, unchecked arithmetic enabled overflow attacks:Division by Zero
Always returns 0 (not exception), can cause logic errors:Modular Arithmetic Precision
Use ADDMOD/MULMOD for cryptographic operations to avoid intermediate overflow:Benchmarks
Relative performance (gas costs reflect computational complexity):| Operation | Gas | Relative Speed |
|---|---|---|
| ADD/SUB | 3 | Fastest |
| MUL/DIV/MOD | 5 | Fast |
| ADDMOD/MULMOD | 8 | Medium |
| EXP | 10-1610 | Variable (exponent-dependent) |
References
- Yellow Paper - Section 9.4.1 (Arithmetic Operations)
- evm.codes - Interactive reference
- EIP-145 - Bitwise shifts (SHL/SHR/SAR)
- Solidity Docs - Type system and overflow semantics
Related Documentation
- Comparison Operations - LT, GT, EQ, ISZERO
- Bitwise Operations - AND, OR, XOR, shifts
- MODEXP Precompile - Efficient modular exponentiation
- Gas Constants - Gas cost definitions

