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.
AiArenaHelper.sol#L106-L110
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.
AiArenaHelper.sol#L165-L186
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. Lets say we only have 2 possible attributes and the attrProbabilities is [50, 50].
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. Lets say [1, 99].
Then in reality if we perform the calculation above we get the following results:
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. Lets say [99, 1].
Then in reality if we perform the calculation above we get the following results:
Then it would be impossible (0% chance) to obtain the 1% item.
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:
Thus the above recommended mitigation is correct.
hickuphh3 (judge) commented:
Note: For full discussion, see here.
