Generating a verifiably random number means combining the random number generation process with a digital signature scheme like ECDSA or RSA.
A digital signature scheme generally has 3 functions:
Generate_KeyPair(seed) -> (sk, pk)
: genarates secret key and a public keySign(sk, msg) -> sig
: signs a message with the secret keyVerify(pk, sig, msg)-> boolean
: verification function returns true or false
A verifiable randomness scheme generates a pseudorandom number based on a given (publicly) known seed. The secret key is used to generate a random number and a proof. The owner of the corresponding public key can verify that the random number was generated by the owner of the secret key.
Note: This project is a learning exercise should not be used in production
Blockchains are notoriously deterministic. Chainlink provides a source of verifiable randomess on the blockchain.
Chainlink VRF (Verifiable Random Function) is a provably fair and verifiable source of randomness designed for smart contracts. Smart contract developers can use Chainlink VRF as a tamper-proof random number generator (RNG) to build reliable smart contracts for any applications which rely on unpredictable outcomes:
This project uses this Chainlink to get a random number to resolve a lottery. A user can participate in the lottery by sending a transaction to this contract with a value greater than a minimum required fee.
All the entrants are added to an array. Based on the random number provided by Chainlink, an index of the array is chosen as the winner. When the lottery ends, the winner will be transferred all the ether collected in the contract so far.
The owner is a privileged user. They can start and end the lottery.
Use hardhat to run tests:
npx hardhat test