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:0x14Introduced: Frontier (EVM genesis)EQ performs bitwise equality comparison on two 256-bit values. Returns 1 if the values are exactly equal, 0 otherwise. This is a fundamental operation for implementing conditional logic, address validation, and state checks.Unlike comparison operations (LT/GT), EQ works identically for both signed and unsigned interpretations since it performs exact bitwise equality.
Specification
Stack Input:
a (top)b
Stack Output:
a == b ? 1 : 0
Gas Cost: 3 (GasFastestStep)Operation:
result = (a == b) ? 1 : 0
Behavior
EQ pops two values from the stack, compares them bitwise, and pushes 1 if they are exactly equal, otherwise 0:
If a == b (all 256 bits identical): Result is 1 (true)
If a != b (any bit differs): Result is 0 (false)
Comparison is bitwise - no interpretation as signed/unsigned is needed.
// require(addr != address(0))assembly { if eq(addr, 0) { revert(0, 0) }}// Check specific addressassembly { if iszero(eq(caller(), expectedAddr)) { revert(0, 0) }}
State Checks
// Check if storage value matches expectedassembly { let value := sload(slot) if iszero(eq(value, expected)) { revert(0, 0) }}
Conditional Execution
// if (a == b)assembly { let equal := eq(a, b) if equal { // Execute if equal }}
Enum Comparison
// Check enum stateenum State { Pending, Active, Closed }assembly { let state := sload(stateSlot) if eq(state, 1) { // State.Active // Handle active state }}
Hash Verification
// Verify hash matchesassembly { let computed := keccak256(dataPtr, dataLen) if iszero(eq(computed, expectedHash)) { revert(0, 0) }}
Implementation
TypeScript
/** * EQ opcode (0x14) - Equality comparison */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 (bitwise equality) 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: Missing state checkfunction execute() { // Execute regardless of state doAction();}// CORRECT: State validationenum State { Pending, Active, Closed }State public state;function execute() { require(state == State.Active, "not active"); doAction();}
Hash Comparison
// VULNERABLE: Using incorrect hashbytes32 public secretHash;function reveal(bytes32 secret) { // Wrong: comparing unhashed value to hash require(secret == secretHash); // Never matches!}// CORRECT: Hash before comparisonfunction reveal(bytes32 secret) { require(keccak256(abi.encodePacked(secret)) == secretHash);}
Optimizations
Commutative Property
// EQ is commutative: a == b is same as b == aassembly { let equal := eq(a, b) // Same as: let equal := eq(b, a)}// Choose order to minimize stack operations
Zero Check Pattern
// Checking equality to zeroassembly { let isZero := eq(value, 0) // 3 gas}// Equivalent using ISZERO (same gas, clearer intent)assembly { let isZero := iszero(value) // 3 gas}// Prefer ISZERO for zero checks (more readable)
Inverted Logic
// Check inequality: a != bassembly { let notEqual := iszero(eq(a, b)) // 6 gas (EQ + ISZERO)}// Sometimes more efficient to structure logic around equalityassembly { if eq(a, b) { // Equal case } else { // Not equal case (no ISZERO needed) }}
Multiple Comparisons
// Check if value equals any of multiple optionsassembly { let match := or( eq(value, option1), or(eq(value, option2), eq(value, option3)) )}// More efficient with structured checksassembly { let match := 0 if eq(value, option1) { match := 1 } if eq(value, option2) { match := 1 } if eq(value, option3) { match := 1 }}
Benchmarks
EQ is one of the fastest EVM operations:Execution time (relative):
EQ: 1.0x (baseline)
LT/GT: 1.0x
ISZERO: 0.95x
ADD: 1.0x
MUL: 1.5x
Gas efficiency:
3 gas per equality check
~333,333 comparisons per million gas
Highly optimized in all EVM implementations
References
Yellow Paper - Section 9.1 (Comparison Operations)