RapBattle::_battle
hashes msg.sender
, block.timestamp
, and block.prevrandao
to create a supposedly random number that is eventually used to determine the winner of a rap battle. However, hashing these values does not create a truly random number. Malicious users can manipulate these values or know them ahead of time to choose the winner of the battle.
Validators can know ahead of time the block.timestamp
.
prevrandao
suffers from biasibility, miners can know its value ahead of time
User can mine/manipulate their msg.sender
value to result in their address being used to generate the winner.
Blockchains are deterministic systems by nature, designed to achieve consensus across all nodes. Using on-chain values as a randomness seed is a well-documented attack vector in the blockchain space.
Users can influence / predict the winner of battles.
Insert the following piece of code to OneShotTest.t.sol
:
and run it by executing forge test --mt test_weakRandomness -vvv
.
This gives the following output:
According to the test, when the same challenger and defender battle for 100 times, the challenger wins in all 100 cases. Since the RapperSkill
of the challenger's and the defender's NFTs are the same, each party should have a 50% chance of winning a battle. Winning 100 battles after each other has as less chance as 7.8886091e-29 %, so by random chance we certainly cannot expect one party to win all 100 battles, indicating weakness in the PRNG used in the protocol.
(Note: The testing environment provided by Foundry's Anvil is static, and block properties are not advanced except if specifically manipulated. If the randomness generation mechanism were truly random, we would expect to see variation in outcomes even in a controlled environment like Anvil. A truly random process should yield different outcomes under identical conditions because it does not depend solely on deterministic inputs.)
Manual review, Foundry.
Consider using a cryptographically provable random number generator, such as Chainlink VRF.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.