Multiplication 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:0x02Introduced: 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:
a (top)b
Stack Output:
(a * b) mod 2^256
Gas Cost: 5 (GasFastStep)Operation:
result = (a * b) & ((1 << 256) - 1)
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
import { mul } from '@tevm/voltaire/evm/arithmetic';import { createFrame } from '@tevm/voltaire/evm/Frame';// 5 * 10 = 50const frame = createFrame({ stack: [5n, 10n] });const err = mul(frame);console.log(frame.stack); // [50n]console.log(frame.gasRemaining); // Original - 5
Overflow Truncation
// Large multiplication overflowsconst MAX = (1n << 256n) - 1n;const frame = createFrame({ stack: [MAX, 2n] });const err = mul(frame);// Result: Only lower 256 bits kept// (MAX * 2) mod 2^256 = 2^256 - 2 = MAX - 1console.log(frame.stack); // [MAX - 1n]
Powers of Two
// Multiplying by 2 is left shiftconst frame = createFrame({ stack: [0x0Fn, 2n] });const err = mul(frame);console.log(frame.stack); // [0x1En] (15 * 2 = 30)
// VULNERABLE: No overflow protectionfunction calculateShares(uint256 price, uint256 quantity) returns (uint256) { return price * quantity; // Can overflow!}
Attack scenario:
// Attacker calls: calculateShares(2^200, 2^100)// Expected: Massive value// Actual: Overflows to small value, attacker pays less
Mitigation (SafeMath):
function calculateShares(uint256 price, uint256 quantity) returns (uint256) { uint256 result = price * quantity; require(price == 0 || result / price == quantity, "overflow"); return result;}
Safe Fixed-Point Arithmetic
Vulnerable pattern:
// WRONG: Intermediate overflowfunction wmul(uint256 x, uint256 y) pure returns (uint256) { return (x * y) / WAD; // x * y can overflow!}
Safe pattern (using mulmod for intermediate):
function wmul(uint256 x, uint256 y) pure returns (uint256 z) { // Use assembly to get full 512-bit intermediate result assembly { if iszero(or(iszero(x), eq(div(mul(x, y), x), y))) { revert(0, 0) // Overflow } z := div(mul(x, y), WAD) }}
Better: Use MULMOD opcode:
// Avoids overflow completelyfunction mulDivDown(uint256 x, uint256 y, uint256 denominator) pure returns (uint256 z) { assembly { // Equivalent to (x * y) / denominator with 512-bit intermediate z := div(mul(x, y), denominator) // Check for overflow: require(denominator > 0 && // (x == 0 || (x * y) / x == y)) if iszero(and( gt(denominator, 0), or(iszero(x), eq(div(mul(x, y), x), y)) )) { revert(0, 0) } }}
Modern Solidity (0.8.0+)
// Automatic overflow checksfunction multiply(uint256 a, uint256 b) pure returns (uint256) { return a * b; // Reverts on overflow}// Explicit wrapping when neededfunction unsafeMultiply(uint256 a, uint256 b) pure returns (uint256) { unchecked { return a * b; // Uses raw MUL, wraps on overflow }}
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:
// Prefer MUL over repeated ADDuint256 result = x * 10; // 5 gas// Instead of:uint256 result = x + x + x + x + x + x + x + x + x + x; // 30 gas
References
Yellow Paper - Section 9.1 (Arithmetic Operations)