In the Statistics library, the variance function fails because the calculation data[i] - mean might produce a negative result, causing an underflow in the uint256 type. Consequently, LLMOracleCoordinator::validate will fail as it calls LLMOracleCoordinator::finalizeValidation, a private function that uses Statistics::stddev, which in turn calls Statistics::variance. As a result, all validation operations will fail.
In the Statistics library, the variance function is implemented as follows:
Consider a uint256 array with elements [1, 2, 3, 4, 5]. The function first calculates the average value (mean), which is (1 + 2 + 3 + 4 + 5) / 5 = 3.
The loop then iterates over each element to compute diff, but at i = 0, where data[0] = 1, the calculation diff = data[0] - mean results in 1 - 3, which should be -2. Since diff is defined as a uint256, this negative result causes an underflow, resulting in the revert of the transaction. This underflow leads to transaction revert, and any downstream calculations, such as those in the stddev function (which relies on variance), will also fail, ultimately causing the LLMOracleCoordinator::validate function to revert.
The following Foundry test serves as a PoC. Run forge test --mt testExploit, and the test will pass, indicating that the operation will indeed revert.
When a registered oracle calls LLMOracleCoordinator::validate, this function subsequently calls the private finalizeValidation function once the status is marked as completed. Within finalizeValidation, Statistics::stddev is invoked to obtain the (stddev, mean) pair. However, inside Statistics::stddev, the Statistics::variance function is called. If the elements in the array are not identical, the calculation may cause an underflow, leading to a transaction revert. Consequently, the status will never reach completion due to the recurring transaction revert.
Manual Review
To handle the underflow scenario, add a check before the subtraction in the variance function. This would ensure that the difference calculation does not cause an unintended underflow.
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.