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: 0x06
Introduced: Frontier (EVM genesis)
MOD computes the remainder of unsigned integer division (modulo operation) on two 256-bit values. Like DIV, modulo by zero returns 0 instead of throwing an exception.
This operation is essential for cyclic calculations, hash table indexing, and constraint checking in smart contracts.
Specification
Stack Input:
Stack Output:
Gas Cost: 5 (GasFastStep)
Operation:
Behavior
MOD pops two values from the stack and computes the remainder:
- If
b ≠ 0: Result is a - (a / b) * b where division is integer division
- If
b = 0: Result is 0 (no exception)
The result satisfies: a = (a / b) * b + (a % b) for all b ≠ 0.
Examples
Basic Modulo
Even/Odd Check
Modulo by Zero
Modulo by Power of Two
Identity Cases
Gas Cost
Cost: 5 gas (GasFastStep)
MOD has the same cost as DIV and MUL:
Comparison:
- ADD/SUB: 3 gas
- MUL/DIV/MOD/SDIV/SMOD/SIGNEXTEND: 5 gas
- ADDMOD/MULMOD: 8 gas
- EXP: 10 + 50 per byte
MOD and DIV are typically implemented together in hardware, hence identical cost.
Edge Cases
Zero Modulo
Modulo Greater Than Dividend
Large Modulus
Power of Two Modulus
Common Usage
Cyclic Indexing
Range Constraints
Even/Odd Checks
Divisibility Testing
Implementation
Testing
Test Coverage
Security
Modulo by Zero
Bias in Random Selection
Off-by-One Errors
Negative Results in Assembly
Benchmarks
MOD performance characteristics:
Execution time:
- ADD: 1.0x
- MUL: 1.2x
- MOD: 2.5x (same as DIV)
Gas efficiency:
- 5 gas per modulo operation
- ~200,000 modulo operations per million gas
- Often computed with DIV in single hardware instruction
Optimization for powers of 2:
References