Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
EIP-4399 proposes replacing the now-obsolete block difficulty value returned by the DIFFICULTY opcode after Ethereum's transition to Proof of Stake (PoS) with randomness supplied through PREVRANDAO.
This article translates, summarizes, and expands on the following specification.
For other Ethereum Improvement Proposals, see:
Overview
This Ethereum Improvement Proposal (EIP) changes the meaning of the return value of the existing DIFFICULTY (0x44) opcode and renames it PREVRANDAO (0x44).
An EIP is a proposal to improve the Ethereum protocol, including additions to its functionality and changes to existing specifications.
Renaming the opcode
The opcode named DIFFICULTY becomes PREVRANDAO. An opcode is an instruction executed by the Ethereum Virtual Machine (EVM). The new name reflects the change in the instruction's functionality.
For an introduction to opcodes, see the following resource.
Changing the return value
Previously, DIFFICULTY returned the difficulty of the current block. Under this EIP, PREVRANDAO (formerly DIFFICULTY) instead returns the output of the randomness beacon provided by the Beacon Chain.
The Beacon Chain was introduced as part of Ethereum 2.0, an upgrade intended to make network operation faster and more efficient. A randomness beacon is useful when decentralized applications (dApps) or smart contracts require fair and unpredictable random values.
With this change, developers can access a more reliable source of randomness through the same opcode that previously exposed block difficulty. This can improve the security and reliability of games, lotteries, and other applications that require randomness.
Motivation
The proposal addresses Ethereum's transition to Proof of Stake (PoS) and the resulting change in how the existing DIFFICULTY opcode is used.
Accessing Beacon Chain randomness
Applications can benefit from using the randomness accumulated by the Beacon Chain. The randomness it generates should therefore be accessible from the EVM.
The PoS upgrade and the difficulty field
As specified in EIP-3675, the block difficulty field must be set to 0 from TRANSITION_BLOCK onward during the transition to PoS. This is because block validation no longer relies on Proof of Work (PoW).
As a result, the DIFFICULTY opcode loses its previous meaning and no longer has a meaningful difficulty value to return.
Backward compatibility with existing smart contracts
Previous analyses have found that smart contracts commonly mix the value returned by DIFFICULTY with other values to obtain randomness.
Using the same opcode number to return the output of the Beacon Chain's RANDAO implementation makes the transition to PoS backward-compatible with existing contracts that use the opcode in this way.
Detecting the PoS upgrade
The change allows smart contracts to determine whether the transition to PoS has already taken place by examining the return value of the DIFFICULTY instruction.
A return value greater than 2**64 indicates that the transaction is executing in a PoS block.
Decompilers and similar tools can also use this technique to identify the new semantics of the instruction when the data of the block containing the transaction is available.
This EIP changes how randomness is obtained during Ethereum's transition to PoS while minimizing the impact on existing smart contracts. Developers can continue using the opcode as a source of randomness after the upgrade.
Specification
The changes supporting Ethereum's transition to Proof of Stake (PoS) concern the block structure, EVM behavior, and the names of a particular field and opcode.
Definition of TRANSITION_BLOCK
TRANSITION_BLOCK is the block defined in EIP-3675, marking the transition from Proof of Work (PoW) to PoS.
Changes to the block structure
Starting with TRANSITION_BLOCK, client software must set mixHash, the thirteenth block header field (counting from zero), to the latest RANDAO mix in the post-state of the previous Beacon Chain block.
This makes randomness information available in the block header.
Changes to the EVM
Starting with TRANSITION_BLOCK, the DIFFICULTY (0x44) instruction must return the value of the mixHash field.
After the transition to PoS, the instruction therefore supplies a randomness-related value rather than the difficulty of the block.
The gas cost of the DIFFICULTY instruction remains unchanged.
Renaming the field and opcode
The mixHash field should be renamed prevRandao to reflect more accurately that it contains a RANDAO output.
The DIFFICULTY (0x44) opcode should be renamed PREVRANDAO (0x44) to describe the instruction's new functionality and purpose.
These changes affect Ethereum's block structure and EVM behavior as part of the transition to PoS. They also rename a field and opcode to reflect their new role in providing randomness.
Rationale
Including the RANDAO output in the block header
Including the output of RANDAO in the Ethereum block header has several benefits.
Accessibility from the EVM
Storing the RANDAO output in the block header makes it readily accessible from the EVM. Because the EVM already has access to block header data, it can also use the RANDAO output directly.
Keeping the execution layer independent
Including the RANDAO output in the block header allows the execution layer, where smart contracts run, to execute a block completely without additional inputs from the PoS consensus layer.
In other words, the block itself contains all the information required for execution, without relying on external data.
Contributing to block hash uniqueness
Mixing RANDAO randomness into the block header can help distinguish block hashes even when the other header fields match those of another block.
The block hash is an important identifier, and its uniqueness matters for blockchain security.
The proposal aims to make RANDAO randomness easier and more efficient to access from the EVM, while keeping Ethereum blocks self-contained and secure. It is one step in Ethereum's transition to PoS.
Using the mixHash field instead of difficulty
The mixHash header field is used instead of the difficulty field to avoid a class of potentially hidden fork-choice bugs after the PoS upgrade.
Client software relies heavily on difficulty as a basis for PoW fork-choice rules. Setting the difficulty field to zero during the upgrade reduces the risk of bugs associated with the total difficulty value after the upgrade.
Reusing an existing field
The PoS upgrade would otherwise deprecate the mixHash field and set it to an array of zero bytes.
Reusing that field to store randomness saves 32 bytes per block and effectively removes the need to deprecate the field during the upgrade.
Reusing the DIFFICULTY opcode
See the Motivation section for the reasons to reuse the DIFFICULTY opcode rather than introduce a new one.
Renaming the field and opcode
The field and opcode should be renamed so that their names accurately describe their semantics.
Using TRANSITION_BLOCK
Using TRANSITION_BLOCK to trigger the changes in this EIP closely ties them to the PoS upgrade defined by EIP-3675.
This ensures continuity for applications using the opcode as a source of randomness.
Using the 2**64 threshold to identify PoS blocks
The probability of a RANDAO value falling between zero and 2**64 is extremely low, making it unlikely to be confused with a PoW difficulty value.
The proposed threshold might appear insufficiently distant from the Ethereum mainnet difficulty at the time of the proposal (approximately 2**54). However, a thousandfold increase in hashrate sufficient to undermine the threshold was considered impossible before the next consensus upgrade.
These technical decisions are intended to make Ethereum's transition to PoS smooth and secure. They retain compatibility with existing systems after the upgrade and efficiently meet the new requirements for randomness.
Backward Compatibility
This EIP introduces backward-incompatible changes to Ethereum execution and the validation of EVM state transitions.
Backward-incompatible changes
The changes are not backward-compatible with existing EVM execution and state transition validation because the EIP uses TRANSITION_BLOCK and is closely tied to the PoS upgrade introduced by EIP-3675.
Scheduling together with EIP-3675
If adopted, this EIP must be scheduled together with EIP-3675, since both proposals concern Ethereum's transition to PoS consensus.
Categories of affected applications
The proposed change is backward-incompatible with the following categories of applications.
Applications that use the value returned by the DIFFICULTY opcode as a PoW difficulty parameter.
Applications whose logic assumes that the DIFFICULTY opcode returns a relatively small number compared with the full 256-bit field size.
Scope of the impact
Applications in the first category are already affected by the switch to PoS consensus. This specification introduces no additional changes for them.
Applications in the second category may encounter overflow or underflow errors in operations that use the return value of DIFFICULTY.
In theory, an application could be constructed in which the change to the range of possible return values introduces a security vulnerability. The likelihood of such an application is considered negligible in the proposal.
The changes associated with Ethereum's transition to PoS may affect certain applications, but the security risk for the vast majority of applications is considered very low.
Tests
The following test scenarios concern the behavior of Ethereum blocks and smart contracts. They verify the behavior of the DIFFICULTY (0x44) opcode, later renamed PREVRANDAO (0x44), before and after the transition to PoS.
Deploying the contract
Deploy a smart contract in a block before TRANSITION_BLOCK that stores the return value of the DIFFICULTY (0x44) opcode in its state.
This allows the value returned before the transition to PoS to be recorded.
Verification in the parent of TRANSITION_BLOCK
Call DIFFICULTY (0x44) in a transaction executed in the parent block of TRANSITION_BLOCK, the block immediately before the transition, and verify that its return value equals the value in the block's difficulty field.
This confirms the opcode's expected behavior before the transition.
Verification in TRANSITION_BLOCK
Call PREVRANDAO (0x44), formerly DIFFICULTY, in a transaction executed in TRANSITION_BLOCK and verify that its return value equals the value of the block's prevRandao field.
This confirms that PREVRANDAO correctly performs its new function of returning randomness from the Beacon Chain after the transition to PoS.
These steps help ensure that the opcode changes associated with the transition to PoS are implemented correctly and behave as expected. In particular, they verify that the transition from DIFFICULTY to PREVRANDAO as a source of randomness takes place smoothly.
Biasability
The PREVRANDAO (0x44) opcode in PoS Ethereum is a source of randomness with different properties from the BLOCKHASH (0x40) and DIFFICULTY (0x44) opcodes in a PoW network.
Sources of randomness
In PoS Ethereum, PREVRANDAO provides randomness based on the Beacon Chain's RANDAO implementation.
RANDAO (Random Number Distributed Autonomous Organization) is a process in which participants contribute seed values that are combined to generate new random values for blocks. It provides a decentralized source of randomness intended to be difficult to predict.
The BLOCKHASH opcode
In PoW Ethereum, the BLOCKHASH opcode provides the hash of one of the previous 256 blocks.
A block hash is calculated from the contents of a block and provides a degree of randomness, but it can potentially be influenced by the miner producing the block.
The DIFFICULTY opcode
In PoW Ethereum, DIFFICULTY provides the difficulty of the current block.
The value measures mining difficulty and is adjusted dynamically in response to the network's hashrate and block production time. It can also be used as a source of randomness, but its dependence on network conditions can make it more predictable.
The PREVRANDAO opcode in PoS Ethereum provides a different source of randomness generated by the Beacon Chain's RANDAO process. It is intended to be less susceptible to manipulation by miners and less predictable than earlier sources, allowing smart contracts and decentralized applications to use more reliable randomness. The following sections explain the remaining predictability and bias considerations.
Predictability
The Beacon Chain is part of Ethereum's Proof of Stake (PoS) consensus mechanism and plays an important role in generating randomness.
Predictability of randomness
Past randomness is 100% predictable, even when it is provided by a decentralized oracle.
Future randomness that has not yet been revealed, by contrast, can only be predicted to a limited extent.
Factors affecting future randomness
Factors affecting future randomness in the Beacon Chain include, but are not limited to, the following.
Accumulated randomness
The RANDAO mix generated by the Beacon Chain in the last slot of epoch N is a major input to the function that determines the block proposer for each slot of epoch N + MIN_SEED_LOOKAHEAD + 1.
It is therefore a key factor in determining future RANDAO revealers, the participants who reveal randomness.
Number of active validators
The number of active validators in an epoch is another input to the block proposer function.
Effective balance
All else being equal, the lower a validator's effective balance, the less likely that validator is to be selected as a proposer for a slot.
Accidentally missed proposals
Proposals accidentally missed because of network conditions or other factors are a source of high-quality entropy affecting the RANDAO mix.
The usual rate of missed proposals on mainnet cited in the proposal is approximately 1%.
Although these inputs may be predictable or controllable over short ranges of slots, the entropy accumulated by the Beacon Chain increases as the prediction horizon grows longer.
This improves the reliability of the randomness generated by the Beacon Chain and makes it harder to predict.
Tips for application developers
The following tips help application developers reduce the predictability and biasability of the randomness output returned by PREVRANDAO (0x44).
Rely on future randomness
Applications should use future randomness with a reasonably high lookahead.
For example, an application stops accepting bids at the end of epoch K and rolls the dice using a RANDAO mix generated at slot K + N + ε, where N is the lookahead in epochs and ε represents a few slots into epoch N + 1.
Effects of lookahead
A lookahead of at least four epochs has the following effects.
- The proposer set for epoch
N + 1is not yet known at the end of epoch K, breaking the direct link between bidders and the participants who roll the dice. - The number of active validators is updated at the end of each epoch, affecting the next epoch's proposer set and consequently the RANDAO mix the application uses to roll the dice.
- According to the mainnet statistics cited in the proposal, the probability that the network accidentally misses a proposal during this period is approximately 100%, reducing the predictability of the RANDAO mix used to roll the dice.
Choosing ε
- Setting
εto a small value, such as two or four slots, gives third parties little time to exert influence over the future randomness used to roll the dice. - This interval is defined by the
MIN_SEED_LOOKAHEADparameter and is approximately six minutes on mainnet.
Separating the bids from the dice roll
A reasonably long interval between accepting bids and rolling the dice reduces the chance that a bidder who controls some validators can exercise direct influence.
Ultimately, that chance depends on the type of game and the number of validators controlled. For example, the likelihood of a single validator influencing a one-off game is negligible, but the probability grows in repeated game scenarios involving multiple validators.
These tips provide guidance for using randomness more securely and protecting applications against potential manipulation.
Citation
Mikhail Kalinin (@mkalinin), Danny Ryan (@djrtwo), "EIP-4399: Supplant DIFFICULTY opcode with PREVRANDAO," Ethereum Improvement Proposals, no. 4399, October 2021. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-4399.
Conclusion
EIP-4399 proposes replacing the difficulty value that loses its meaning after the transition to PoS with randomness provided by PREVRANDAO.
If you have questions, feel free to send me a direct message on Twitter.
I also publish material on other platforms:



