Dria

Swan
NFTHardhat
21,000 USDC
View results
Submission Details
Severity: low
Invalid

`Statistics::sqrt()` will fail if the value is `type(uint256).max`

Description

The sqrt() function designed by the protocol, is not handling all possible values. it uses Babylonian method, but it don't take into consideration the maximum value type(uint256).max.

libraries/Statistics.sol#L41

function sqrt(uint256 x) internal pure returns (uint256 y) {
>> uint256 z = (x + 1) / 2;
y = x;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
}

Making the addition first x + 1 will make the function revert because of overflow, if the x is max.

Although this can be ignored, but for libraries and Mathematical functions we believe the mathematical function should result in correct results what ever the value is.

Recommendations

You can modify The function, to handle all possible values, using this new implementation.

function sqrt(uint y) internal pure returns (uint z) {
if (y > 3) {
z = y;
uint x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
} else if (y != 0) {
z = 1;
}
}

And you can use Soloday implementation for the function, which saves some Gas.

Updates

Lead Judging Commences

inallhonesty Lead Judge 8 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.