The LibDibbler library uses a fixed interest rate known as the temperature to convert Beans into Pods. The conversion formula, pods = beans * (1 + temperature / 100%), involves multiplication and division operations that can introduce rounding errors. These errors occur because Solidity uses integer arithmetic, which truncates fractional parts. This results in users receiving a slightly different number of Pods than expected, affecting fairness and trust in the protocol.
Boundary Conditions and mulDiv Limitations
The mulDiv function is designed to mitigate rounding errors by performing multiplication and division in a single step, thus avoiding intermediate precision loss. However, it does not completely eliminate rounding errors, especially at boundary conditions where values are right at the edge of a rounding threshold.
i: Boundary Condition Example:
If a calculation yields a result like 4.99999, it will be rounded down to 4, causing a loss of precision.
This is a significant boundary condition issue, as users at the edge of the rounding threshold may receive fewer Pods than they should.
Relevant Code Snippets
beansToPods Function:
scaleSoilUp Function:
scaleSoilDown Function:
Consider a scenario where a user sows Beans under specific temperature conditions. Due to rounding errors, the number of Pods received can be slightly less or more than expected. This scenario demonstrates a boundary condition where the rounding errors are most noticeable.
Example Scenario:
Beans: 999
Temperature: 5000000 (representing 5% with precision 1e6)
Expected Pods:
pods = 999 * (1 + 5000000/100000000) = 999 * 1.05 = 1048.95
Due to integer arithmetic, the result in Solidity will be truncated:
Actual Pods: 1048 (truncated from 1048.95)
This shows a boundary condition where the value 1048.95 is rounded down to 1048, leading to a slight under-allocation of Pods.
Users may receive slightly fewer or more Pods than they should, leading to fairness issues.
Over time, these discrepancies can accumulate, potentially leading to significant imbalances within the protocol.
Manual review
Explicitly specify rounding directions in critical functions to avoid unintended rounding down or up.
Invalid as per docs https://docs.codehawks.com/hawks-auditors/how-to-determine-a-finding-validity
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.