Get block difficulty (pre-merge) or PREVRANDAO (post-merge)
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:0x44Introduced: 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.
Returns the computational difficulty required to mine the block:
Difficulty range: ~2 PH (petahash) averageAdjusts every block to maintain ~13.2 second block timeHigher difficulty = more computational work required
Post-Merge (PoS)
Returns PREVRANDAO - the beacon chain randomness output from the previous slot:
PREVRANDAO: 32-byte value from beacon chainMore unpredictable than PoW difficultyDetermined by beacon chain RANDAO mixCannot be manipulated by single validator
// Detect if post-merge based on DIFFICULTY valuedifficulty(frame);const value = frame.stack[0];// Pre-merge: Large difficulty value (billions+)// Post-merge: PREVRANDAO (unpredictable 256-bit value)const isPostMerge = /* check hardfork or use PREVRANDAO heuristics */;
Random Number Generation (Pre-Merge - Not Secure)
// PRE-MERGE ONLY: Weak randomnessdifficulty(frame);const diff = frame.stack[0];// Hash difficulty for pseudo-randomness (not secure!)const random = keccak256(diff);
Random Number Generation (Post-Merge)
// POST-MERGE: Better randomness (still not secure for high stakes)difficulty(frame); // Returns PREVRANDAOconst prevrandao = frame.stack[0];// More unpredictable than PoW difficulty// But still not suitable for high-value lotteries
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
// Pre-merge onlycontract DifficultyAware { uint256 public constant MIN_DIFFICULTY = 1_000_000_000_000; function checkDifficulty() public view returns (bool) { return block.difficulty >= MIN_DIFFICULTY; }}
Post-Merge: PREVRANDAO Usage
// Post-merge: Use PREVRANDAO for improved randomnesscontract RandomnessBetter { function randomNumber() public view returns (uint256) { // block.difficulty is now PREVRANDAO return uint256(keccak256(abi.encodePacked( block.prevrandao, // Same as block.difficulty post-merge block.timestamp, msg.sender ))); }}
Merge-Aware Contract
contract MergeAware { uint256 public constant MERGE_BLOCK = 15_537_394; function getRandomness() public view returns (uint256) { if (block.number >= MERGE_BLOCK) { // Post-merge: Use PREVRANDAO return uint256(block.prevrandao); } else { // Pre-merge: Use difficulty (less random) return uint256(block.difficulty); } }}
Solidity Compatibility
// Solidity 0.8.18+ has block.prevrandaocontract Modern { function getPrevrandao() public view returns (uint256) { return block.prevrandao; // Explicit name } function getDifficulty() public view returns (uint256) { return block.difficulty; // Still works, returns PREVRANDAO post-merge }}
On-Chain Randomness (Still Not Fully Secure)
contract ImprovedLottery { // Post-merge PREVRANDAO is better but still manipulable function drawWinner(address[] memory participants) public view returns (address) { // PREVRANDAO is more unpredictable than PoW difficulty // But validators can still influence it slightly uint256 randomness = uint256(keccak256(abi.encodePacked( block.prevrandao, block.timestamp, participants.length ))); uint256 index = randomness % participants.length; return participants[index]; }}
Security Considerations
Pre-Merge: Miner Manipulation
PoW miners could manipulate difficulty-based randomness:
// VULNERABLE (Pre-Merge)contract DifficultyLottery { function draw() external { uint256 winner = uint256(block.difficulty) % 100; // Miner can try different nonces to influence difficulty }}
Post-Merge: Validator Influence
PREVRANDAO is more secure but validators have limited influence:
// IMPROVED but not perfect (Post-Merge)contract PrevrandaoLottery { function draw() external { uint256 winner = uint256(block.prevrandao) % 100; // Validators can influence RANDAO reveal but: // - Must reveal in advance (can't see outcome first) // - Mixed with many other validators // - Still not suitable for high-stakes randomness }}
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:
// BROKEN POST-MERGEcontract DifficultyThreshold { function isHighDifficulty() public view returns (bool) { // Pre-merge: Returns true if mining difficulty high // Post-merge: Returns unpredictable value (PREVRANDAO) return block.difficulty > 10_000_000_000_000; }}
/** * DIFFICULTY opcode (0x44) * Pre-Merge: Block difficulty * Post-Merge: PREVRANDAO */export function difficulty(frame: FrameType): EvmError | null { // Consume gas (GasQuickStep = 2) frame.gasRemaining -= 2n; if (frame.gasRemaining < 0n) { frame.gasRemaining = 0n; return { type: "OutOfGas" }; } // Check hardfork to determine value const isPostMerge = frame.evm.hardfork.isAtLeast('MERGE'); const value = isPostMerge ? frame.evm.blockContext.block_prevrandao : frame.evm.blockContext.block_difficulty; // Push to stack if (frame.stack.length >= 1024) return { type: "StackOverflow" }; frame.stack.push(value); frame.pc += 1; return null;}
Edge Cases
Pre-Merge Zero Difficulty
// Genesis or test networkconst frame = createFrame({ hardfork: 'LONDON', blockContext: { block_difficulty: 0n }});difficulty(frame);console.log(frame.stack); // [0n]
Post-Merge PREVRANDAO
// Random 256-bit value from beacon chainconst frame = createFrame({ hardfork: 'PARIS', blockContext: { block_prevrandao: 0x9876543210abcdef...n }});difficulty(frame);console.log(frame.stack); // [PREVRANDAO value]
Maximum Values
// Pre-merge: Theoretical max difficultyconst maxDifficulty = (1n << 256n) - 1n;// Post-merge: Any 256-bit value possibleconst anyPrevrandao = 0xffffffffffffffffffffffffffffffffffffffffn;
Historical Context
Pre-Merge Difficulty Adjustment
// Pre-merge: Difficulty adjusted to maintain ~13.2s blocks// Difficulty Bomb: Exponentially increasing difficulty// Ice Age: Periods of increased difficulty to force upgrades