Skip to content
Smart contracts Audit finding Intermediate

Modulo opation introduces bias during the winning yeet calculation

The contract selects winning yeets by taking a random number modulo the number of yeets, which introduces a slight bias when the count is not a power of two. An attacker could exploit this bias to gain a small advantage in winning, especially as the number of players grows.

RNemes Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

#42710 [SC-Medium] Modulo opation introduces bias during the winning yeet calculation

Submitted on Mar 25th 2025 at 13:03:42 UTC by @RNemes for Audit Comp | Yeet

Description

Brief/Intro

Randomness of game results decreases due to use of the Modulo operator in selecting the winning yeets.

Vulnerability Details

The draftWinners function uses the modulo operation to select the winning yeet. The Pyth documentation mentions:

Pyth Docs

Notice that using the modulo operator can distort the distribution of random numbers if it's not a power of 2. This is negligible for small and medium ranges, but it can be noticeable for large ranges. For example, if you want to generate a random number between 1 and 52, the probability of having value 5 is approximately 10^-77 higher than the probability of having value 50 which is infinitesimal.

This is not an issue for a small number of yeets ie 52 but as the number of yeets per game gets larger the effects are greater and will cause a bias in the results.

Impact Details

As the number of players increase the randomness of the results decreases, which causes unfairness in the game.

References

Pyth Docs

Proof of Concept

Proof of Concept

The following test shows how the randomness changes as the no of yeets increases

Solidity
function test_ModuloBias() public {
        // Test both a power of 2 and non-power of 2
        checkDistribution(1023);  // non-power of 2
        checkDistribution(1024);  // power of 2
        checkDistribution(52);  // non-power of 2
        checkDistribution(64);  // non-power of 2
    }

    function checkDistribution(uint256 nrOfYeets) private {
        uint256[] memory counts = new uint256[](nrOfYeets);
        uint256 iterations = 1_00_000;  // Use more iterations for better statistical significance
        
        for (uint256 i = 0; i < iterations; i++) {
            uint256 randomDataNumber = uint256(keccak256(abi.encodePacked(i)));
            uint256 index = randomDataNumber % nrOfYeets;
            counts[index]++;
        }

        uint256 expectedCount = iterations / nrOfYeets;
        uint256 sumDeviations = 0;
        uint256 maxDeviation = 0;
        uint256 minCount = type(uint256).max;
        uint256 maxCount = 0;

        for (uint256 i = 0; i < nrOfYeets; i++) {
            uint256 deviation;
            if (counts[i] > expectedCount) {
                deviation = counts[i] - expectedCount;
            } else {
                deviation = expectedCount - counts[i];
            }
            
            sumDeviations += deviation;
            maxDeviation = deviation > maxDeviation ? deviation : maxDeviation;
            minCount = counts[i] < minCount ? counts[i] : minCount;
            maxCount = counts[i] > maxCount ? counts[i] : maxCount;
        }

        uint256 avgDeviation = sumDeviations / nrOfYeets;
        
        emit log_named_uint("Number of possible values", nrOfYeets);
        emit log_named_uint("Expected count per value", expectedCount);
        emit log_named_uint("Minimum count observed", minCount);
        emit log_named_uint("Maximum count observed", maxCount);
        emit log_named_uint("Average deviation", avgDeviation);
        emit log_named_uint("Maximum deviation", maxDeviation);
        emit log_named_uint("Max deviation percentage", (maxDeviation * 100) / expectedCount);
        emit log_named_uint("Spread (max-min)", maxCount - minCount);
    }

The original report as published on Immunefi, by RNemes. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the use of the modulo operator on a random number without rejection sampling, which biases the distribution when the range is not a power of two.

Impact

As the number of players increases, the randomness of the results decreases, causing unfairness in the game.

What the attacker needed

Many players in a game

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When you pick a random winner by dividing a random number by the number of players and using the remainder, some players can have a slightly higher chance if the number of players is not a power of two. This report shows that the game's winner selection has this flaw, so the game is not perfectly fair.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by RNemes

View all

2 more disclosed reports, best first.

More from Yeet

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.