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: 0x07
Introduced: Frontier (EVM genesis)
SMOD performs signed modulo operation on two 256-bit values interpreted as two’s complement signed integers. The result has the same sign as the dividend (not the divisor, unlike some languages).
Like MOD, modulo by zero returns 0. Additionally, SMOD has special handling for the MIN_INT / -1 edge case.
Specification
Stack Input:
Stack Output:
Gas Cost: 5 (GasFastStep)
Operation:
Behavior
SMOD interprets 256-bit values as signed integers using two’s complement:
- If
b = 0: Returns 0 (no exception)
- If
a = MIN_INT and b = -1: Returns 0 (special case)
- Otherwise: Returns
a - (a / b) * b where division is signed
Sign of result:
- Result always has the same sign as dividend
a
-7 % 2 = -1 (not 1)
7 % -2 = 1 (not -1)
Examples
Basic Signed Modulo
Negative Dividend
Negative Modulus
Both Negative
MIN_INT % -1 Edge Case
Gas Cost
Cost: 5 gas (GasFastStep)
SMOD has the same gas cost as MOD and SDIV:
Comparison:
- ADD/SUB: 3 gas
- MUL/DIV/MOD/SDIV/SMOD/SIGNEXTEND: 5 gas
- ADDMOD/MULMOD: 8 gas
No gas overhead for sign handling.
Edge Cases
Modulo by Zero
MIN_INT Special Cases
Zero Dividend
Sign Comparison with Other Languages
Common Usage
Signed Range Wrapping
Signed Parity Check
Cyclic Signed Indexing
Implementation
Testing
Test Coverage
Security
Sign Interpretation
Cross-Language Differences
Negative Index Wrapping
Safe Signed Modulo
Benchmarks
SMOD performance identical to MOD:
Execution time:
- ADD: 1.0x
- MUL: 1.2x
- SMOD: 2.5x (same as MOD/DIV)
Gas cost:
- 5 gas per signed modulo
- No overhead for sign handling
- ~200,000 signed modulo operations per million gas
References