Skip to content
Smart contracts Audit finding Intermediate

Incorrect mathematical logic

The ELO rating calculation in the contract miscomputes the expected score because it offsets the rating difference by 800 before taking a sixteenth root, so the exponent no longer matches the standard formula. This makes all rating changes wrong and could let an attacker obtain abnormally large rating gains.

djshan_eden Follow with Matin and Nexarion

Originally disclosed on Code4rena

Submitted by djshan_eden, also found by Matin and Nexarion

https://github.com/code-423n4/2025-04-virtuals-protocol/blob/21d5ffa77e769fa3c409b41e54656a0e3267beb5/contracts/libs/Elo.sol#L56

https://github.com/code-423n4/2025-04-virtuals-protocol/blob/21d5ffa77e769fa3c409b41e54656a0e3267beb5/contracts/libs/Elo.sol#L59

https://github.com/code-423n4/2025-04-virtuals-protocol/blob/21d5ffa77e769fa3c409b41e54656a0e3267beb5/contracts/libs/Elo.sol#L60

Finding description

There is a fundamental error in the ELO calculation formula. For example, when ratingA=1000 and ratingB=800, the code calculation result deviates greatly from the standard formula. The core problem lies in the exponent processing:

The correct calculation should be 10^(ratingDiff/400), but the code incorrectly converts the exponent through n = 800 ± ratingDiff and the fourth square root, resulting in an incorrect powered value.

Impact

All ELO rating changes are calculated incorrectly, the rating system is completely untrustworthy, and attackers can use this vulnerability to obtain abnormally high rating changes.

Recommended mitigation steps

  1. Remove offset: Handle the positive and negative rating differences directly.
  2. Accurate exponential calculation: Use a high-precision library (such as ABDKMath) to calculate 10^(ratingDiff/400).
  3. Avoid decomposition errors: Abandon multi-step square root decomposition and handle the raw exponential directly.

Proof of Concept

1. Differences between the original formula and the code implementation​:

Correct formula - the expected ELO score is:

ratingDiff = _negative ? ratingA - ratingB : ratingB - ratingA
expected score = 1 / (1 + 10 ^ (ratingDiff / 400))

Code error steps - the contract attempts to optimize the calculation by factoring the exponent, but introduces a fatal error:

n = _negative ? 800 - ratingDiff: 800 + ratingDiff;
_powered = fp.rpow(10, n / 25, 1); // calculate10^(n/25)
powered = sixteenthRoot(_powered); // The fourth square root → is equivalent to10^(n/(25 * 16)) = 10^(n/400)

On the surface, the math is equivalent to 10^(ratingDiff/400), but the ​​offset of 800​​ completely breaks the logic.

2. Sign error caused by offset

Example analysis - Assume that RA=1000, RB=800, then:

Correct case - The exponent is (1000−800)/400=0.5, and the denominator is 1+10^0.5≈4.1623.

Code calculation​​:

ratingDiff = 200, _negative = true
n = 800 - 200 = 600
n/25 = 24 → 10 ^24
Fourth square root → 10 ^(24/16)=10^1.5≈31.623
The denominator becomes 100+31.623=131.623, which is much smaller than the correct value 4.1623.

Results: expected score was incorrectly inflated, causing a complete distortion in the ELO change calculation.

3. Summary of the root causes of the error

  • Wrong offset: The introduction of 800 ± ratingDiff has no mathematical basis, resulting in double errors in the exponent sign and magnitude.
  • Decomposition steps are incompatible with integer operations: the correct decomposition should be 10^ratingDiff/400 = (10^ratingDiff/25)^1/16. But after n was incorrectly offset in the code, the actual calculation of 10^(800±ratingDiff)/(25×16) is equivalent to 10^(800±ratingDiff)/400, which is completely deviated from the original formula.
  • Sign processing is reversed: when RA>RB, the exponent should be positive, but the offset in the code makes it become (800−ratingDiff)/400, resulting in the reverse calculation of the denominator.

Virtuals marked as informative


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

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 code adds an unjustified offset of 800 to the rating difference before the root decomposition, so the exponent sign and magnitude no longer match 10^(ratingDiff/400).

Impact

Every ELO rating change is computed incorrectly, making the rating system untrustworthy, and an attacker could use this to obtain abnormally high rating changes.

What the attacker needed

Ability to trigger rating updates

The fix

The calculation should handle positive and negative rating differences directly and compute the exponential accurately, for example with a high-precision maths library, instead of the multi-step square-root decomposition.

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.

An ELO system estimates how likely one player is to beat another from the gap in their scores. The contract tried to compute a power of ten using a shortcut, but it added a fixed offset of 800 to the score gap, which changes the answer completely. As a result the expected win chance, and every rating update built on it, comes out wrong.

What is Smart contracts?

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

More from Virtuals Protocol

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.