TwentyOne

First Flight #29
Beginner FriendlyGameFiFoundrySolidity
100 EXP
View results
Submission Details
Severity: low
Invalid

Weak Randomness in Card Generation

Summary:

The TwentyOne contract uses predictable values (block.timestamp, msg.sender, block.prevrandao)
for random card generation which can be manipulated.

Vulnerability Details:

The following test demonstrates that card generation is predictable:

function test_PredictableRandomness() public {
// Setup
vm.startPrank(player1);
// Set fixed values that attacker can predict
uint256 fixedTimestamp = 1234;
uint256 fixedPrevrandao = 5678;
// Set block values before game starts
vm.warp(fixedTimestamp);
vm.prevrandao(fixedPrevrandao);
// Start game
twentyOne.startGame{value: 1 ether}();
// Attacker can calculate the exact card they'll receive
bytes32 randomHash = keccak256(
abi.encodePacked(fixedTimestamp, player1, fixedPrevrandao)
);
uint256 expectedCard = (uint256(randomHash) % 52) + 1;
uint256[] memory cards = twentyOne.getPlayerCards(player1);
uint256 actualCard = cards[0];
// Proves the randomness is predictable
assertEq(actualCard, expectedCard, "Random card should be predictable");
vm.stopPrank();
}

Impact:

  • Players can predict which cards they'll receive

  • Game integrity is compromised

  • Protocol could suffer financial losses

Tools Used

  • Manual code review

  • Forge testing framework

  • Gas analysis

Recommendation:

Use Chainlink VRF or commit-reveal scheme for true randomness.
Replace:

keccak256(abi.encodePacked(block.timestamp, msg.sender, block.prevrandao))

With:

requestRandomness() from Chainlink VRF
Updates

Lead Judging Commences

inallhonesty Lead Judge 11 months ago
Submission Judgement Published
Invalidated
Reason: Known issue
Assigned finding tags:

[INVALID] Known - Randomness

Randomness Manipulation: The randomness mechanism relies on block.timestamp, msg.sender, and block.prevrandao, which may be predictable in certain scenarios. Consider using Chainlink VRF or another oracle for more secure randomness.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.