Unsigned less than comparison for 256-bit 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:0x10Introduced: Frontier (EVM genesis)LT performs unsigned less than comparison on two 256-bit integers. Returns 1 if the first value is strictly less than the second, 0 otherwise. All values are treated as unsigned integers in the range 0 to 2^256 - 1.This is the fundamental comparison operation for implementing conditional logic and bounds checking in smart contracts.
Specification
Stack Input:
a (top)b
Stack Output:
a < b ? 1 : 0
Gas Cost: 3 (GasFastestStep)Operation:
result = (a < b) ? 1 : 0
Behavior
LT pops two values from the stack, compares them as unsigned 256-bit integers, and pushes 1 if a < b, otherwise 0:
If a < b: Result is 1 (true)
If a >= b: Result is 0 (false)
All comparisons are unsigned. Values with bit 255 set are treated as large positive numbers, not negative values.
// 2^255 is treated as large positive (not negative)const SIGN_BIT = 1n << 255n;// 1 < 2^255 = 1 (true, unsigned comparison)const frame = createFrame({ stack: [1n, SIGN_BIT] });lt(frame);console.log(frame.stack); // [1n]// In signed comparison (SLT), this would be 0 because 2^255 = -2^255 (negative)
Gas Cost
Cost: 3 gas (GasFastestStep)LT shares the lowest gas tier with other comparison and basic operations:
LT, GT, SLT, SGT, EQ (comparisons)
ISZERO, NOT
ADD, SUB
Comparison:
LT/GT/EQ: 3 gas
MUL/DIV: 5 gas
ADDMOD: 8 gas
Edge Cases
Boundary Values
const MAX = (1n << 256n) - 1n;// 0 < MAX = 1lt(createFrame({ stack: [0n, MAX] })); // [1n]// MAX < 0 = 0lt(createFrame({ stack: [MAX, 0n] })); // [0n]// MAX < MAX = 0lt(createFrame({ stack: [MAX, MAX] })); // [0n]
Sign Bit Set
// Values with bit 255 set are large positive (unsigned)const SIGN_BIT = 1n << 255n; // 2^255// SIGN_BIT is treated as 2^255, not -2^255// 2^255 < 1 = 0 (false, unsigned)lt(createFrame({ stack: [SIGN_BIT, 1n] })); // [0n]// Compare with SLT (signed):// SLT would return 1 because 2^255 = -2^255 < 1 (signed)
// Check if value < maxassembly { let valid := lt(value, max) if iszero(valid) { revert(0, 0) }}
Loop Conditions
// for (uint i = 0; i < n; i++)assembly { let i := 0 for {} lt(i, n) { i := add(i, 1) } { // Loop body }}
Minimum Value
// min(a, b)assembly { let minimum := a if lt(b, a) { minimum := b }}
Array Access Safety
// Safe array accessassembly { if lt(index, arrLength) { let value := sload(add(arrSlot, index)) // Use value }}
Implementation
TypeScript
/** * LT opcode (0x10) - Less than comparison (unsigned) */export function handle(frame: FrameType): EvmError | null { // Consume gas (GasFastestStep = 3) const gasErr = consumeGas(frame, FastestStep); if (gasErr) return gasErr; // Pop operands (b is top, a is second) const bResult = popStack(frame); if (bResult.error) return bResult.error; const b = bResult.value; const aResult = popStack(frame); if (aResult.error) return aResult.error; const a = aResult.value; // Compare: a < b (unsigned) const result = a < b ? 1n : 0n; // Push result const pushErr = pushStack(frame, result); if (pushErr) return pushErr; // Increment PC frame.pc += 1; return null;}
// VULNERABLE: Overflow corrupts comparisonuint256 sum = a + b; // May wrap to small valuerequire(sum > a); // Check may incorrectly pass// CORRECT: Check before operationrequire(a <= type(uint256).max - b);uint256 sum = a + b;
Type Width Issues
// VULNERABLE: Comparing different widthsuint256 large = type(uint256).max;uint128 small = type(uint128).max;// Implicit cast may truncaterequire(large < small); // Type confusion// CORRECT: Explicit same-width comparisonrequire(uint256(large) < uint256(small));
Optimizations
Inversion Patterns
// These are equivalent:// a < b === !(a >= b)assembly { // Direct let less := lt(a, b) // Inverted (sometimes useful in complex conditions) let less := iszero(or(gt(a, b), eq(a, b)))}
Short-Circuit Evaluation
// Evaluate cheapest condition firstassembly { if lt(index, length) { // Only check expensive condition if first passes if expensiveCheck() { // Execute } }}
Constant Comparison
// Compiler may optimize constant comparisonsassembly { if lt(value, 100) { // Constant 100 // Optimized by EVM implementations }}
Benchmarks
LT is one of the fastest EVM operations:Execution time (relative):
LT: 1.0x (baseline)
GT/EQ: 1.0x
ISZERO: 0.95x
ADD: 1.0x
MUL: 1.5x
Gas efficiency:
3 gas per comparison
~333,333 comparisons per million gas
Highly optimized in all EVM implementations
References
Yellow Paper - Section 9.1 (Comparison Operations)