Skip to content
Smart contracts Audit finding Intermediate

Modulo bias in winner selection in raffle

The Raffle contract selects a winner by taking a random number modulo the ticket count without correcting for modulo bias, so when the ticket count does not divide the random number space evenly some ticket indices are more likely to win. An attacker could time or manipulate ticket purchases to obtain favourable indices and increase their chance of winning.

Opzteam Follow

Originally disclosed on Immunefi

Informational View original
  • Disclosed after under an hour

49626 sc insight modulo bias in winner selection in raffle

Submitted on Jul 17th 2025 at 19:12:26 UTC by @Opzteam for Attackathon | Plume Network

Description

Vulnerability Description

The Raffle contract contains a modulo bias vulnerability in the winner selection mechanism. The contract uses a simple modulo operation without bias protection, which can favor certain ticket indices when the total number of tickets doesn't evenly divide the VRF random number space (1,000,000), leading to unfair raffle outcomes.

Vulnerable Code Snippet

Solidity
// Line 247 in handleWinnerSelection function
uint256 winningTicketIndex = (rng[0] % totalTickets[prizeId]) + 1;

When totalTickets[prizeId] doesn't evenly divide 1,000,000, some ticket indices get more chances than others.

Example: If totalTickets = 7, then 1,000,000 % 7 = 6, meaning tickets 1-6 get 142,858 chances each while ticket 7 only gets 142,852 chances.

Proof of Concept

{% stepper %} {% step %} Create a raffle with 100,001 total tickets {% endstep %}

{% step %} Calculate bias: 1,000,000 % 100,001 = 999,999 (significant remainder) {% endstep %}

{% step %} Exploit: Tickets 1-999,999 get 10 chances each, ticket 1,000,000 gets 9 chances, ticket 100,001 gets 1 chance {% endstep %}

{% step %} Result: Users with tickets in positions 1-999,999 have higher winning probability than those in positions 1,000,000 and 100,001 {% endstep %}

{% step %} Manipulation: Attacker can strategically time entries or manipulate ticket purchases to favor advantageous positions {% endstep %} {% endstepper %}

Remediation

{% hint style="info" %} Implement rejection sampling to ensure uniform distribution. {% endhint %}

Recommended code pattern:

Solidity
uint256 maxValidValue = (type(uint256).max / totalTickets[prizeId]) * totalTickets[prizeId];
uint256 randomValue;
do {
    randomValue = rng[0];
} while (randomValue >= maxValidValue);
uint256 winningTicketIndex = (randomValue % totalTickets[prizeId]) + 1;

This ensures all ticket indices have exactly equal probability of being selected.

The original report as published on Immunefi, by Opzteam. 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

The winner index is computed with a plain modulo of the random value by the ticket count, without rejection sampling to remove the uneven remainder.

Impact

The raffle may not deliver fair promised odds, and an attacker could gain a higher chance of winning by choosing favourable ticket positions, though the contract does not lose funds directly.

What the attacker needed

Ability to create or enter a raffle; control over ticket purchase timing or positions

The fix

The winner selection should use rejection sampling so that every ticket index has exactly equal probability.

How it was found

  1. Create a raffle with 100,001 total tickets
  2. Calculate the remainder of 1,000,000 divided by 100,001
  3. Observe that some ticket positions receive more chances than others
  4. Strategically time entries or manipulate ticket purchases to favour advantageous positions

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.

A raffle picks a winner by turning a random number into a ticket number using division remainder. If the range of random numbers does not split evenly by the number of tickets, some ticket numbers come up slightly more often. This report says that small unfairness could be exploited by choosing when to buy tickets.

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 Opzteam

View all

6 more disclosed reports, best first.

More from Plume Network

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.