Erroneous probability calculation in physical attributes can lead to significant issues
The attribute generation code used an inclusive comparison when walking cumulative probabilities, so the boundary value was counted in the wrong bucket. This skewed the odds of each trait by one percent, making rare traits more common or even impossible to obtain, which affects fighter valuations.
Submitted by haxatron, also found by juancito, BARW, fnanni, and DanielArmstrong
To determine what physical attributes a user gets first we obtain a rarityRank which is computed from the DNA.
} else {
uint256 rarityRank = (dna / attributeToDnaDivisor[attributes[i]]) % 100;
uint256 attributeIndex = dnaToIndex(generation, rarityRank, attributes[i]);
finalAttributeProbabilityIndexes[i] = attributeIndex;
}
Here since we use % 100 operation is used, the range of rarityRank would be [0,99].
This rarityRank is used in the dnaToIndex to determine the final attribute of the part.
/// @dev Convert DNA and rarity rank into an attribute probability index.
/// @param attribute The attribute name.
/// @param rarityRank The rarity rank.
/// @return attributeProbabilityIndex attribute probability index.
function dnaToIndex(uint256 generation, uint256 rarityRank, string memory attribute)
public
view
returns (uint256 attributeProbabilityIndex)
{
uint8[] memory attrProbabilities = getAttributeProbabilities(generation, attribute);
uint256 cumProb = 0;
uint256 attrProbabilitiesLength = attrProbabilities.length;
for (uint8 i = 0; i < attrProbabilitiesLength; i++) {
cumProb += attrProbabilities[i];
if (cumProb >= rarityRank) {
attributeProbabilityIndex = i + 1;
break;
}
}
return attributeProbabilityIndex;
}
There is however, a very subtle bug in the calculation above due to the use of cumProb >= rarityRank as opposed to cumProb > rarityRank.
To explain the above, I will perform a calculation using a simple example. Let’s say we only have 2 possible attributes and the attrProbabilities is [50, 50].
- First iteration, when
i = 0, we havecumProb = 50, for theif (cumProb >= rarityRank)to be entered the range of valuesrarityRankcan be is [0, 50]. Therefore there is a 51% chance of obtaining this attribute - Next iteration, when
i = 1, we havecumProb = 100, for theif (cumProb >= rarityRank)to be entered the range of valuesrarityRankcan be is [51, 99]. Therefore there is a 49% chance of obtaining this attribute.
This means that for values in the first index, the probability is 1% greater than intended and for values in the last index the probability is 1% lesser than intended. This can be significant in certain cases, let us run through two of them.
Case 1: The first value in attrProbabilities is 1.
If the first value in attrProbabilities is 1. Let’s say [1, 99].
Then in reality if we perform the calculation above we get the following results:
- First iteration, when
i = 0, we havecumProb = 1, for theif (cumProb >= rarityRank)to be entered the range of valuesrarityRankcan be is [0, 1]. Therefore there is a 2% chance of obtaining this attribute - Next iteration, when
i = 1, we havecumProb = 100, for theif (cumProb >= rarityRank)to be entered the range of valuesrarityRankcan be is [2, 99]. Therefore there is a 98% chance of obtaining this attribute.
Then we have twice the chance of getting the rarer item, which would make it twice as common, thus devaluing it.
Case 2: The last value in attrProbabilities is 1.
If the last value in attrProbabilities is 1. Let’s say [99, 1].
Then in reality if we perform the calculation above we get the following results:
- First iteration, when
i = 0, we havecumProb = 99, for theif (cumProb >= rarityRank)to be entered the range of valuesrarityRankcan be is [0, 99]. Therefore we will always enter theif (cumProb >= rarityRank)block.
Then it would be impossible (0% chance) to obtain the 1% item.
Recommended Mitigation Steps
It should be cumProb > rarityRank. Going back to our example of [50, 50], if it were cumProb > rarityRank. Then we will get the following results:
- First iteration, when
i = 0, we havecumProb = 50, for theif (cumProb > rarityRank)to be entered the range of valuesrarityRankcan be is [0, 49]. Therefore there is a 50% chance of obtaining this attribute - Next iteration, when
i = 1, we havecumProb = 100, for theif (cumProb > rarityRank)to be entered the range of valuesrarityRankcan be is [50, 99]. Therefore there is a 50% chance of obtaining this attribute.
Thus the above recommended mitigation is correct.
Valid issue: wrong inclusion of boundary skews the probability to one-side by 1%. The shift in probabilities will affect trait generation, which affects fighter valuations.
Note: For full discussion, see here.
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 cumulative probability check used an inclusive comparison, so the boundary value was assigned to the earlier bucket instead of the next one.
Impact
Trait probabilities are shifted by one percent, so rare traits could become twice as common or impossible to obtain, affecting fighter valuations.
What the attacker needed
None
The fix
The comparison should be changed to a strict greater-than so each boundary value falls in the correct bucket.
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.
Each fighter gets traits picked by rolling a number and walking through a list of percentage weights. The code counted the edge number in the wrong group, so every trait's real chance was off by one percent. For rare traits that small error can double their frequency or make them impossible to get.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.