Overview
Opcode:0x44
Introduced: Frontier (EVM genesis)
Repurposed: Paris (The Merge, EIP-4399)
DIFFICULTY returns different values depending on the network’s consensus mechanism:
- Pre-Merge (PoW): Block mining difficulty
- Post-Merge (PoS): PREVRANDAO - beacon chain randomness from previous slot
Specification
Stack Input:Behavior
Pre-Merge (PoW)
Returns the computational difficulty required to mine the block:Post-Merge (PoS)
Returns PREVRANDAO - the beacon chain randomness output from the previous slot:Examples
Basic Usage
Hardfork Detection
Random Number Generation (Pre-Merge - Not Secure)
Random Number Generation (Post-Merge)
Gas Cost
Cost: 2 gas (GasQuickStep) Same cost pre and post-merge despite different semantic meaning. Comparison:DIFFICULTY: 2 gasNUMBER,TIMESTAMP,GASLIMIT: 2 gasBLOCKHASH: 20 gas
Common Usage
Pre-Merge: Difficulty-Based Logic
Post-Merge: PREVRANDAO Usage
Merge-Aware Contract
Solidity Compatibility
On-Chain Randomness (Still Not Fully Secure)
Security Considerations
Pre-Merge: Miner Manipulation
PoW miners could manipulate difficulty-based randomness:Post-Merge: Validator Influence
PREVRANDAO is more secure but validators have limited influence:- Validator can choose to propose or skip slot
- Limited influence (not full control like PoW)
- Cost: Lost block rewards if skipping
Semantic Change at Merge
Contracts relying on difficulty semantics broke at The Merge:Recommended: Use Chainlink VRF
For secure randomness:Implementation
- TypeScript
Edge Cases
Pre-Merge Zero Difficulty
Post-Merge PREVRANDAO
Maximum Values
Historical Context
Pre-Merge Difficulty Adjustment
The Merge Transition
EIP-4399 Specification
Benchmarks
Performance:- Hardfork check: O(1)
- Stack push: O(1)
- 2 gas per query
- ~500,000 queries per million gas
Related Instructions
- BLOCKHASH (0x40) - Get block hash
- NUMBER (0x43) - Get block number
- TIMESTAMP (0x42) - Get block timestamp
References
- EIP-4399 - DIFFICULTY → PREVRANDAO
- Yellow Paper - Section 9.3
- EVM Codes - DIFFICULTY
- The Merge
- Beacon Chain RANDAO
- Chainlink VRF

