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: 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
This semantic change occurred at The Merge (Paris hardfork) when Ethereum transitioned from Proof of Work to Proof of Stake.
Specification
Stack Input:
Stack Output:
Gas Cost: 2 (GasQuickStep)
Operation:
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 gas
NUMBER, TIMESTAMP, GASLIMIT: 2 gas
BLOCKHASH: 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:
Attack Vector:
- 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
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)
Gas efficiency:
- 2 gas per query
- ~500,000 queries per million gas
References