Bias in random word generation in XRandoms contract #1599
Labels
bug
Something isn't working
downgraded by judge
Judge downgraded the risk level of this issue
duplicate-1008
grade-c
QA (Quality Assurance)
Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax
unsatisfactory
does not satisfy C4 submission criteria; not eligible for awards
Lines of code
https://github.com/code-423n4/2023-10-nextgen/blob/58090c9fbc036c06bbaa9600ec326034f2181a17/hardhat/smart-contracts/XRandoms.sol#L40-L43
https://github.com/code-423n4/2023-10-nextgen/blob/58090c9fbc036c06bbaa9600ec326034f2181a17/hardhat/smart-contracts/XRandoms.sol#L15-L33
Vulnerability details
Impact
The
randomWord
function in theXRandoms
contract exhibits a bias in generating random words. Specifically, it never returns "Watermelon" and doubles the probability of generating "Acai," undermining the function's randomness and fairness.Proof of Concept
The
randomWord
function generates a number (randomNum
) between 0 and 99, which is then used by thegetWord
function to return a corresponding word. However, due to a logic error ingetWord
, the range of returned words is only from index 0 to 98:https://github.com/code-423n4/2023-10-nextgen/blob/58090c9fbc036c06bbaa9600ec326034f2181a17/hardhat/smart-contracts/XRandoms.sol#L40-L43
The
getWord
function incorrectly handles this range, causing it to return "Acai" for both 0 and 1, and never return "Watermelon" (which should correspond to 99):https://github.com/code-423n4/2023-10-nextgen/blob/58090c9fbc036c06bbaa9600ec326034f2181a17/hardhat/smart-contracts/XRandoms.sol#L15-L33
The function becomes not random since Watermelon has probability of 0, Acai has probability of 2, and other words have probability of 1.
Tools Used
Manual
Recommended Mitigation Steps
To resolve this bias, the
getWord
function should be modified to directly return the word corresponding to the generated randomNum, ensuring each word has an equal probability of being selected:Assessed type
Error
The text was updated successfully, but these errors were encountered: