DEV Community 👩‍💻👨‍💻

Cover image for Why blockchain legacy randomness is not reliable...
Laurent Knauss
Laurent Knauss

Posted on • Updated on

Why blockchain legacy randomness is not reliable...

Broadly speaking, The EVM is a deterministic environment meaning, in layman terms , that it works with pre-defined algorithms that are very predictable which is the exact oppsite of randomness. Therefore, Vyper or Solidity language do not have a built-in source of entropy that can be used to generate random numbers. When a block is validated, it produces an unguessable hash.

Although the blockhash is guaranted to be lower than the block difficulty , the exact number is unknown until the block is validated.

Unfortunately, the blockhash of the current block is unavailable until after the block is validated , so we have to use the blockhash from the parent block.

Below snippet shows that we can access the most recent blockhash in our contracts with the block.blochash(block.number -1) expression :

Image description

This function hashes the parent blockhash with a user-generated seed and then takes the integer representation of the bytes. Changing the seed will change the output. This gives a number between 0 & 2^256 that can be used with a modulus to get a smaller range of numbers.

Image description

Although the blockhash is guaranteed to be lower than the block difficulty, the exact number is unknown until the block is validated in the context of POS.

As a simple attack, the random number can be maliciously guessed by an hacker by copying and pasting our random function into their contract and making sure it gets validated in the same block as a transaction that uses our random function.

Using block.difficulty,block.timestamp, blockHash, or anything associated with blocks to get a randomnumber into yout App opens up your code to exploits.

Knowing this flaw, if you need to secure large quantities of ether with a random number, a superior and far more secure way is to implement Chainlink Verifiable Random Function in your smart contract logic.

2022's web3 developers chose off-chain solutions, where a ramdomly verifiable number is generated off-chain and then re-introduced on-chain.
Nevertheless, without cryptographic proof of the impartiality of the off-chain value, the result can be manipulated by the transport layer who introduces back this value on-chain.
Moreover, users of any application engeneering this process are forced to believe that the random value is producted impartialy and that it has been re-introduced on-chain without being modified.

To face those potentiel risks, Chainlink VRF offers transparency & cryptographic truth that each result is generated in a fair manner.

Image description

In a nutshell, a smart contract ask for a random feature by providing a seed to Chainlink. The said seed is used to generate a random number, who is then sent to the smart contract.

Each oracle uses its own intrinsic secret key when the random number is created. When the result is broadcasted on-chain with a proof, it is verified with the help of the public key of the oracle and the seed of the app.

By relying on largely accepted abilities of signature verification & blockchain proofs, this enabled smart contracts to solely consume random datas that have been verified by the same environment on-chain that executes the contract itself.

To show concrete example, PoolTogether , no-loss lottery on Ethereum, implements VRF to provide warranty to theirs users about the random character of their app.

As a conclusion, I would suggest to use Chainlink VRF to build reliable smart contratcs for decentralized apps that rely on unpredictable outcomes such as :

  • Building blockchains games and NFTs.

  • Random assignment of duties and ressources in the real world: for example, randomly assigning judges to cases.

  • Choosing a representative sample for consensus mechanisms.

This blog post have been inspired by this book and this article

Shares and comments welcome.

Top comments (0)

Let's Get Wacky


Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category

Join the Hackathon <-