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: 0x02
Introduced: Frontier (EVM genesis)
MUL performs multiplication on two 256-bit unsigned integers with wrapping overflow semantics. When the result exceeds 2^256 - 1, only the lower 256 bits are kept, effectively computing (a * b) mod 2^256.
This operation is fundamental for scaling calculations, area/volume computations, and fixed-point arithmetic in smart contracts.
Specification
Stack Input:
Stack Output:
Gas Cost: 5 (GasFastStep)
Operation:
Behavior
MUL pops two values from the stack, multiplies them, and pushes the lower 256 bits of the result. The upper bits are discarded:
- If
a * b < 2^256: Result is the mathematical product
- If
a * b >= 2^256: Result is the lower 256 bits (truncated)
No exceptions are thrown for overflow. Information in the upper 256 bits is lost.
Examples
Basic Multiplication
Overflow Truncation
Powers of Two
Identity Element
Zero Element
Gas Cost
Cost: 5 gas (GasFastStep)
MUL costs slightly more than ADD/SUB due to increased computational complexity:
Comparison:
- ADD/SUB: 3 gas
- MUL/DIV/MOD/SDIV/SMOD/SIGNEXTEND: 5 gas
- ADDMOD/MULMOD: 8 gas
- EXP: 10 + 50 per byte
MUL is one of the most gas-efficient ways to multiply in the EVM, but still ~67% more expensive than addition.
Edge Cases
Maximum Overflow
Square Operations
Multiplication by Powers of Two
Stack Underflow
Common Usage
Fixed-Point Arithmetic
Percentage Calculations
Area/Volume Calculations
Scaling and Conversion
Implementation
Testing
Test Coverage
Security
Overflow Vulnerabilities
Pre-Solidity 0.8.0 vulnerability:
Attack scenario:
Mitigation (SafeMath):
Safe Fixed-Point Arithmetic
Vulnerable pattern:
Safe pattern (using mulmod for intermediate):
Better: Use MULMOD opcode:
Modern Solidity (0.8.0+)
Benchmarks
MUL performance characteristics:
Relative execution time:
- ADD: 1.0x
- MUL: 1.2x
- DIV: 2.5x
- ADDMOD: 3.0x
Gas efficiency:
- 5 gas per 256-bit multiplication
- ~200,000 multiplications per million gas
- Significantly faster than repeated addition
Optimization tip:
References