EIPs Explained

EIP-4399 · Final · Core · 2021-10-30

EIP-4399: Supplant DIFFICULTY opcode with PREVRANDAO

Renamed the DIFFICULTY opcode to PREVRANDAO at the Merge so it returns the beacon chain's RANDAO mix, giving contracts an on-chain (but biasable) randomness value.

What problem does it solve?

Under Proof-of-Work, opcode 0x44 (DIFFICULTY) returned the block's mining difficulty. Many contracts mixed that value into hashes as a cheap source of "randomness".

At the Merge (EIP-3675) the header's difficulty field became 0, so DIFFICULTY would have lost all meaning. Meanwhile the beacon chain produces its own randomness (the RANDAO mix) that the EVM could not see. EIP-4399 connects the two: the old opcode now returns beacon chain randomness, so existing contracts keep getting an unpredictable-looking value and new contracts gain access to RANDAO.

How it works

  • From the first PoS block, the header field mixHash (field 13) is set to the latest RANDAO mix from the beacon state of the previous block. The field is renamed prevRandao.
  • Opcode 0x44 returns that prevRandao value and is renamed PREVRANDAO. Gas cost is unchanged.
  • difficulty stays 0. The authors reused mixHash instead of difficulty to avoid hidden bugs in client code that still sums total difficulty, and to avoid adding a new header field.
  • A contract can tell whether it runs on PoS: RANDAO outputs are 256-bit values, almost always larger than 2**64, while PoW difficulty values were far below that.

The change was scheduled together with EIP-3675 and activated with the Merge (Paris, September 2022).

Sample code

Solidity 0.8.18+ exposes the value as block.prevrandao (block.difficulty is a deprecated alias):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract RandaoReader {
    function isPoS() external view returns (bool) {
        return block.prevrandao > 2**64;
    }

    // Illustration only: NOT safe for anything with real value at stake.
    function weakRandom(uint256 salt) external view returns (uint256) {
        return uint256(keccak256(abi.encode(block.prevrandao, salt)));
    }
}

Reading it off-chain with ethers v6:

const block = await provider.getBlock("latest");
console.log(block!.prevRandao); // 32-byte hex string

Things to know

  • Biasable. Each block proposer has roughly 1 bit of influence: they can skip their slot so the mix is not updated. A proposer can also delay a "dice roll" transaction into a block whose prevRandao they already know. Do not use it alone for lotteries or high-value games.
  • Partly predictable. Past values are fully known, and values a few slots ahead can be influenced or anticipated. The spec advises closing bets and then using a RANDAO mix from at least about four epochs later. For high-value use, commit-reveal schemes or VRF oracles are safer.
  • Contracts that assumed DIFFICULTY returns a small number may overflow; the spec considered this risk negligible.
  • Tooling and libraries may still show the field as mixHash or the opcode as DIFFICULTY.

#evm #opcode #randomness #core #merge

Details

Authors
Mikhail Kalinin (@mkalinin), Danny Ryan (@djrtwo)
Status
Final
Type
Standards Track / Core
Created
2021-10-30
Requires

Machine-readable: Markdown · JSON