Eggstravaganza

First Flight #37
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Severity: high
Valid

Both block.prevrandao and block.timestamp are not reliably source of randonness

Summary

Both block.prevrandao and block.timestamp are not reliably source of randomness

Vulnerability Details

In the

https://github.com/CodeHawks-Contests/2025-04-eggstravaganza/blob/main/src/EggHuntGame.sol#L65-L81

function searchForEgg() external {
require(gameActive, "Game not active");
require(block.timestamp >= startTime, "Game not started yet");
require(block.timestamp <= endTime, "Game ended");
// Pseudo-random number generation (for demonstration purposes only)
--> uint256 random = uint256(
--> keccak256(abi.encodePacked(block.timestamp, block.prevrandao, msg.sender, eggCounter))
--> ) % 100;
if (random < eggFindThreshold) {
eggCounter++;
eggsFound[msg.sender] += 1;
eggNFT.mintEgg(msg.sender, eggCounter);
emit EggFound(msg.sender, eggCounter, eggsFound[msg.sender]);
}
}

the code use block.prevrandao and block.timestamp as source of randoness to determine who is lucky to win the egg.

However, both op code are not good source of randonness.

https://eips.ethereum.org/EIPS/eip-4399

Security Considerations
The PREVRANDAO (0x44) opcode in PoS Ethereum (based on the beacon chain RANDAO implementation) is a source of randomness with different properties to the randomness supplied by BLOCKHASH (0x40) or DIFFICULTY (0x44) opcodes in the PoW network.

Biasability
The beacon chain RANDAO implementation gives every block proposer 1 bit of influence power per slot. Proposer may deliberately refuse to propose a block on the opportunity cost of proposer and transaction fees to prevent beacon chain randomness (a RANDAO mix) from being updated in a particular slot.

Impact

Miner can manipulate the block.prevrandao and block.timestamp to let specific address win the raffle

Tools Used

Recommendations

change randon generate method (can use chainlink VRF, etc...)

Updates

Lead Judging Commences

m3dython Lead Judge 8 months ago
Submission Judgement Published
Validated
Assigned finding tags:

Insecure Randomness

Insecure methods to generate pseudo-random numbers

Support

FAQs

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

Give us feedback!