The scale factor 10 ** n
is used to increase the precision of the nthRoot
calculation by effectively shifting the decimal point of a
to the right n
times before computing the root
. However, this can lead to overflow
issues for large values of n
because the result of 10 ** n
grows exponentially.
Issue stems from the line:
Maximum value for a uint256
is 2**256 - 1
.
The maximum value that 10 ** n
can reach without overflowing a uint256
is 10 ** 77
because 10 ** 78 > 2**256
.
If n
is greater than 77
, 10 ** n
will overflow.
Let's assume n = 78
, which is just one more than the safe limit.
10 ** 78
is 10 times larger than 10 ** 77
.
2 ** 256
is approximately 1.1579209e+77
.
10 ** 78
is approximately 1e+78
.
Since 1e+78
is significantly larger than 1.1579209e+77
, it's clear that 10 ** 78
will overflow a uint256
.
In Solidity, an overflow
in such an operation would cause the result to wrap around modulo 2**256
, leading to an incorrect and very small number instead of the large expected value. This would completely distort the calculations that follow.
Since the code is written in Solidity version v0.8.x
, automatic revert will take place.
Manual Review
Implement a check to ensure that n
does not exceed a safe threshold.
Define a constant for the maximum allowed value of n
to prevent overflow.
Use a require statement
to enforce this limit before performing the calculation.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.