Cheap Randomness
Coqnet offers three approaches to tackle random numbers
Backend-based random generation process
// Generating a 32-bytes length random number
import { randomBytes } from 'crypto';
export function generateRandomUint256(): bigint {
return BigInt('0x' + randomBytes(32).toString('hex'));
}
function _genPseudoRandom(uint256 randomWord) internal view virtual returns (uint256) {
// solhint-disable-next-line not-rely-on-block-hash
return uint256(keccak256(abi.encodePacked(blockhash(block.number - 1), block.prevrandao, randomWord)));
}In that snippet, the variable randomWord is the random number sent by the backend. Once the method _genPseudoRandom returns the backend-sourced pseudo random number, it is ready to be used.
randomWord is the random number sent by the backend. Once the method _genPseudoRandom returns the backend-sourced pseudo random number, it is ready to be used.Let's discuss its advantages and disadvantages:
Advantages
Disadvantages
Risks
Commit-Reveal Scheme for Secure Randomness
Native VRF Precompile contracts
Features
Usage
Chainlink VRF integration
Advantages
Disadvantages

Last updated