Moving average precision is lost #483
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
old-submission-method
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Lines of code
https://github.com/code-423n4/2022-08-olympus/blob/2a0b515012b4a40076f6eac487f7816aafb8724a/src/modules/PRICE.sol#L134-L139
Vulnerability details
Now the precision is lost in moving average calculations as the difference is calculated separately and added each time, while it typically can be small enough to lose precision in the division involved.
For example,
10000
moves of990
size,numObservations = 1000
. This will yield0
on each update, while must yield9900
increase in the moving average.Proof of Concept
Moving average is calculated with the addition of the difference:
https://github.com/code-423n4/2022-08-olympus/blob/2a0b515012b4a40076f6eac487f7816aafb8724a/src/modules/PRICE.sol#L134-L139
/ numObs
can lose precision ascurrentPrice - earliestPrice
is usually small.It is returned on request as is:
https://github.com/code-423n4/2022-08-olympus/blob/2a0b515012b4a40076f6eac487f7816aafb8724a/src/modules/PRICE.sol#L189-L193
Recommended Mitigation Steps
Consider storing the cumulative
sum
, while returningsum / numObs
on request:https://github.com/code-423n4/2022-08-olympus/blob/2a0b515012b4a40076f6eac487f7816aafb8724a/src/modules/PRICE.sol#L189-L193
The text was updated successfully, but these errors were encountered: