This report highlights a precision loss vulnerability in the variance
and stddev
functions of the Statistics
library. The issue arises from using integer arithmetic to perform calculations, resulting in loss of accuracy, especially when dealing with large numbers or datasets with minimal variation. This lack of precision could lead to inaccurate variance and standard deviation values, affecting downstream calculations that rely on these functions.
The precision loss in the variance
and stddev
functions results from integer division in Solidity. When calculating variance
, the squared difference of each element from the mean is divided by the dataset size, which can result in truncation. Similarly, the sqrt
function in stddev
applies integer-based square root calculation, which further reduces precision.
In the variance
function, the calculation divides the sum of squared differences by data.length
, which truncates any fractional component. Additionally, the stddev
function's use of sqrt
introduces additional rounding.
The precision loss could lead to inaccurate results for variance and standard deviation calculations, potentially impacting any contract logic that relies on these statistical calculations. This issue is particularly relevant for financial or data-analysis contracts that require accurate calculations to ensure fair outcomes or proper decision-making.
Manual review.
Use a fixed-point arithmetic library to maintain fractional precision in calculations. This involves scaling values by a large factor (e.g., 1e18) to allow fractional parts during division and square root operations, then dividing by the scale factor afterward.
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.