Elo calculation error
The battleElo function passes the two player ratings to the Elo rating-change helper in the wrong order, so the change and its direction are computed for the opponent rather than the player. This produces incorrect rating updates for every battle processed.
Submitted by levi_104, also found by axelot, gmh5225, and io10
Finding description and impact
Here, the order of eloB and eloA is reversed, and subsequently, eloA and eloB are calculated with addition and subtraction based on the value of negative, leading to a final calculation error.
function battleElo(uint256 currentRating, uint8[] memory battles) public view returns (uint256) {
uint256 eloA = 1000;
uint256 eloB = 1000;
for (uint256 i = 0; i < battles.length; i++) {
uint256 result = mapBattleResultToGameResult(battles[i]);
(uint256 change, bool negative) = Elo.ratingChange(eloB, eloA, result, k);
change = _roundUp(change, 100);
if (negative) {
eloA -= change;
eloB += change;
} else {
eloA += change;
eloB -= change;
}
}
return currentRating + eloA - 1000;
}
In the ELO system:
ratingAusually represents Player A’s rating, and score is Player A’s score.ratingBis the opponent (Player B)’s rating.
But in this code:
eloAis regarded as Player A’s rating.eloBis regarded as Player B’s rating.
However, when calling Elo.ratingChange(eloB, eloA, result, k), it means:
ratingA = eloB(Player B’s rating).ratingB = eloA(Player A’s rating).- score is the score of
ratingA(eloB).
This is unexpected. Usually, Elo.ratingChange(eloA, eloB, result, k) should be used, because:
eloAis Player A’s rating, and result is Player A’s battle outcome.eloBis Player B’s rating.
Due to the wrong parameter order, Elo.ratingChange calculates the change and direction of eloB (as ratingA), not of eloA.
/// @notice Calculates the change in ELO rating, after a given outcome.
/// @param ratingA the ELO rating of the player A
/// @param ratingB the ELO rating of the player B
/// @param score the score of the player A, scaled by 100. 100 = win, 50 = draw, 0 = loss
/// @param kFactor the k-factor or development multiplier used to calculate the change in ELO rating. 20 is the typical value
/// @return change the change in ELO rating of player A, with 2 decimals of precision. 1501 = 15.01 ELO change
/// @return negative the directional change of player A's ELO. Opposite sign for player B
function ratingChange(
uint256 ratingA,
uint256 ratingB,
uint256 score,
uint256 kFactor
) internal pure returns (uint256 change, bool negative) {
Recommended mitigation steps
- Elo.ratingChange(eloB, eloA, result, k);
+ Elo.ratingChange(eloA, eloB, result, k);
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 arguments to the Elo rating-change helper are swapped, so the score is applied to the opponent's rating instead of the player's.
Impact
Every battle processed by this function yields an incorrect rating change, so player ratings drift from their true Elo values.
What the attacker needed
None
The fix
The call should pass the player's rating first and the opponent's second, i.e. swap the two arguments.
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 rating is a number that goes up or down after a game, like in chess. The helper function expects the player's rating first and the opponent's second, but the code swaps them, so the wrong player's rating is adjusted. The result is that ratings change incorrectly after battles.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.