The formula of number of prizes for a degree is wrong
The smart contract used an incorrect formula to calculate the number of prizes for a given degree, making the count smaller than intended. As a result, the prize amount per degree became larger, so the protocol could distribute more funds than designed.
Submitted by WatchPug, also found by cmichel.
The formula of the number of prizes for a degree per the document: https://v4.docs.pooltogether.com/protocol/concepts/prize-distribution/#splitting-the-prizes is:
Number of prizes for a degree = (2^bit range)^degree - (2^bit range)^(degree-1) - (2^bit range)^(degree-2) - ...
Should be changed to:
Number of prizes for a degree = (2^bit range)^degree - (2^bit range)^(degree-1)
or
Number of prizes for a degree = 2^(bit range * degree) - 2^(bit range * (degree-1))
Impact
Per the document:
prize for a degree = total prize * degree percentage / number of prizes for a degree
Due to the miscalculation of number of prizes for a degree, it will be smaller than expected, as a result, prize for a degree will be larger than expected. Making the protocol giving out more prizes than designed.
Proof
We will use
f(bitRange, degree)to representnumberOfPrizesForDegree(bitRangeSize, degree).
Proof: (method 1)
2 ^ {bitRange \times n} = f(bitRange, n) + f(bitRange, n-1) + f(bitRange, n-2) + ... + f(bitRange, 1) + f(bitRange, 0)
f(bitRange, n) = 2 ^ {bitRange \times n} - ( f(bitRange, n-1) + f(bitRange, n-2) + ... + f(bitRange, 1) + f(bitRange, 0) )
f(bitRange, n) = 2 ^ {bitRange \times n} - f(bitRange, n-1) - ( f(bitRange, n-2) + ... + f(bitRange, 1) + f(bitRange, 0) )
Because:
2 ^ {bitRange \times (n-1)} = f(bitRange, n-1) + f(bitRange, n-2) + ... + f(bitRange, 1) + f(bitRange, 0)
2 ^ {bitRange \times (n-1)} - f(bitRange, n-1) = f(bitRange, n-2) + ... + f(bitRange, 1) + f(bitRange, 0)
Therefore:
f(bitRange, n) = 2 ^ {bitRange \times n} - f(bitRange, n-1) - ( 2 ^ {bitRange \times (n-1)} - f(bitRange, n-1) )
f(bitRange, n) = 2 ^ {bitRange \times n} - f(bitRange, n-1) - 2 ^ {bitRange \times (n-1)} + f(bitRange, n-1)
f(bitRange, n) = 2 ^ {bitRange \times n} - 2 ^ {bitRange \times (n-1)}
Because 2^x = 1 << x
Therefore, when n > 0:
f(bitRange, n) = ( 1 << bitRange * n ) - ( 1 << bitRange * (n - 1) )
QED.
Proof: (method 2)
By definition, degree n is constructed by 3 chunks:
- The first N numbers, must equal the matching numbers. Number of possible values:
1; - The N-th number, must not equal the N-th matching number. Number of possible values:
2^bitRange - 1 - From N (not include) until the end. Number of possible values:
2 ^ (bitRange * (n-1))
Therefore, total numberOfPrizesForDegree will be:
f(bitRange, n) = (2 ^ {bitRange} - 1) \times 2 ^ {bitRange \times (n - 1)}
f(bitRange, n) = 2 ^ {bitRange} \times 2 ^ {bitRange \times (n - 1)} - 2 ^ {bitRange \times (n - 1)}
f(bitRange, n) = 2 ^ {bitRange + bitRange \times (n - 1)} - 2 ^ {bitRange \times (n - 1)}
f(bitRange, n) = 2 ^ {bitRange + bitRange \times n - bitRange} - 2 ^ {bitRange \times (n - 1)}
f(bitRange, n) = 2 ^ {bitRange \times n} - 2 ^ {bitRange \times (n - 1)}
QED.
Recommendation
/**
* @notice Calculates the number of prizes for a given prizeDistributionIndex
* @param _bitRangeSize Bit range size for Draw
* @param _prizeTierIndex Index of the prize tier array to calculate
* @return returns the fraction of the total prize (base 1e18)
*/
function _numberOfPrizesForIndex(uint8 _bitRangeSize, uint256 _prizeTierIndex)
internal
pure
returns (uint256)
{
uint256 bitRangeDecimal = 2**uint256(_bitRangeSize);
uint256 numberOfPrizesForIndex = bitRangeDecimal**_prizeTierIndex;
while (_prizeTierIndex > 0) {
numberOfPrizesForIndex -= bitRangeDecimal**(_prizeTierIndex - 1);
_prizeTierIndex--;
}
return numberOfPrizesForIndex;
}
L423-431 should change to:
if (_prizeTierIndex > 0) {
return ( 1 << _bitRangeSize * _prizeTierIndex ) - ( 1 << _bitRangeSize * (_prizeTierIndex - 1) );
} else {
return 1;
}
BTW, the comment on L416 is wrong:
- seems like it’s copied from
\_calculatePrizeTierFraction() - plus, it’s not base 1e18 but base 1e9
PierrickGT (PoolTogether) confirmed and patched:
Alex the Entreprenerd (judge) commented:
The warden found the usage of an incorrect formula that would cause the protocol to give out larger prizes than expected, the sponsor has mitigated in a following PR
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 loop subtracted every lower degree's prize count instead of only the immediately preceding degree's count, which is mathematically equivalent to subtracting a single term.
Impact
The protocol could give out larger prizes than designed, potentially distributing more funds than intended.
What the attacker needed
None
The fix
The formula was corrected to subtract only the previous degree's count, and the sponsor patched it in a pull request.
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 prize pool splits its winnings across different tiers. The code that decides how many prizes each tier has used a wrong maths formula, so it thought there were fewer prizes in a tier than there really should be. Because the total prize for a tier is divided by that number, each winner got a bigger share, and the pool paid out more than planned.
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.