Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bouncy Cloud Mockingbird - Rounding Errors in Interest Rate Calculations #247

Open
sherlock-admin2 opened this issue Dec 31, 2024 · 0 comments

Comments

@sherlock-admin2
Copy link
Contributor

Bouncy Cloud Mockingbird

High

Rounding Errors in Interest Rate Calculations

Summary

Rounding errors compounded over time can lead to inaccurate interest accumulation, impacting both borrowers and lenders.

Root Cause

In https://github.com/sherlock-audit/2024-12-numa-audit/blob/ae1d7781efb4cb2c3a40c642887ddadeecabb97d/Numa/contracts/lending/CToken.sol#L459
The accrueInterest function calculates interest using Exp structs but can result in rounding errors due to the truncation of mantissas. This is because the Exp struct represents fixed-point numbers with a mantissa (the significant digits of the number) and an implicit exponent. When performing arithmetic operations with these fixed-point numbers, the results are often truncated to fit within the fixed-point representation.

Internal pre-conditions

Here's why this can lead to rounding errors:

1.Fixed-Point Arithmetic: The Exp struct uses fixed-point arithmetic, which involves scaling numbers by a factor (e.g., 1e18) to represent fractional values. When performing operations like multiplication or division, the results are scaled back down, which can lead to truncation of the least significant digits.

2.Truncation: During arithmetic operations, especially division, the result may not be an exact multiple of the scaling factor. This means that the result will be truncated to fit within the fixed-point representation, losing some precision in the process.

3.Accumulation of Errors: Over time, as interest is accrued repeatedly, these small truncation errors can accumulate, leading to noticeable discrepancies in the calculated interest.

External pre-conditions

No response

Attack Path

No response

Impact

Small rounding errors compounded over time can lead to inaccurate interest accumulation, impacting both borrowers and lenders causing financial discrepancies leading to unfair interest calculations and protocol instability.

PoC

No response

Mitigation

1.Increase Precision: Use a higher precision for the fixed-point representation. For example, instead of using 18 decimal places, you could use 27 or more. This reduces the impact of truncation errors but may increase gas costs.

2.Accumulate Interest in a Separate Variable: Instead of updating the principal directly, accumulate the interest in a separate variable and update the principal periodically. This reduces the frequency of truncation errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant