Canto Dex Oracle contest - Bronicle's results

Execution layer for original work.

General Information

Platform: Code4rena

Start Date: 07/09/2022

Pot Size: $20,000 CANTO

Total HM: 7

Participants: 65

Period: 1 day

Judge: 0xean

Total Solo HM: 3

Id: 159

League: ETH

Canto

Findings Distribution

Researcher Performance

Rank: 40/65

Findings: 1

Award: $39.22

🌟 Selected for report: 0

πŸš€ Solo Findings: 0

Low issue

Multiplication after division

  1. Summary: Performing a multiplication after a division should be avoided as precision is lost in the division step.

  2. Details: In order to minimize rounding errors, when performing mathematical operations, it is encouraged to leave the division step at the end of the expression.

  3. Github Permalinks: https://github.com/code-423n4/2022-09-canto/blob/main/src/Swap/BaseV1-periphery.sol#L549-L593

  4. Mitigation:

    The current implementation of the computation at lines 581 to 585 can be mathematically written as follows:

    LpPricesCumulative=βˆ‘i(token0TVL+token1TVL)β‹…1018supplyi\text{LpPricesCumulative} = \sum_i \frac{\left(\text{token0TVL}+\text{token1TVL}\right)\cdot 10^{18}}{\text{supply}_i}

    which is equivalent to

    LpPricesCumulative=βˆ‘i(assetReservesiβ‹…pricesidecimals+token1TVL)β‹…1018supplyi\text{LpPricesCumulative} = \sum_i \frac{\left(\text{assetReserves}_i\cdot\frac{\text{prices}_i}{\text{decimals}}+\text{token1TVL}\right)\cdot 10^{18}}{\text{supply}_i}

    Note that we can extract $10^{18}$ from the summation because it is constant. Moreover, if multiplying both the denominator and the numerator by $\text{decimals}$, we can do the same. This results in:

    LpPricesCumulative=1018decimalsβˆ‘iassetReservesiβ‹…pricesi+token1TVLβ‹…decimalssupplyi\text{LpPricesCumulative} = \frac{10^{18}}{\text{decimals}} \sum_i \frac{\text{assetReserves}_i\cdot\text{prices}_i+\text{token1TVL}\cdot\text{decimals}}{\text{supply}_i}

    Moreover, if one checks that the number of decimals (let's call it $z$, so that $\text{decimals} = 10^{z}$) is less or equal than $18$, the above expression can be simplified to:

    LpPricesCumulative=1018βˆ’zβˆ‘iassetReservesiβ‹…pricesi+token1TVLβ‹…decimalssupplyi\text{LpPricesCumulative} = 10^{18 - z} \sum_i \frac{\text{assetReserves}_i\cdot\text{prices}_i+\text{token1TVL}\cdot\text{decimals}}{\text{supply}_i}

    With this expression, one can reduce the amount of divisions performed and will most likely have a better rounding precision.

    Moreover, at line 592, more simplifications can be done. The expression

    LpPriceβ‹…getPriceNote(address(wcanto),Β false)1018\text{LpPrice}\cdot\frac{\text{getPriceNote(address(wcanto), false)}}{10^{18}}

    can be rewritten as

    LpPricesCumulative8β‹…getPriceNote(address(wcanto),Β false)1018\frac{\text{LpPricesCumulative}}{8}\cdot\frac{\text{getPriceNote(address(wcanto), false)}}{10^{18}}

    which in turn equals

    1018βˆ’z8βˆ‘iassetReservesiβ‹…pricesi+token1TVLβ‹…decimalssupplyiβ‹…getPriceNote(address(wcanto),Β false)1018\frac{10^{18 - z}}{8} \sum_i \frac{\text{assetReserves}_i\cdot\text{prices}_i+\text{token1TVL}\cdot\text{decimals}}{\text{supply}_i}\cdot\frac{\text{getPriceNote(address(wcanto), false)}}{10^{18}}

    having thus

    18β‹…10Β΄zβˆ‘iassetReservesiβ‹…pricesi+token1TVLβ‹…decimalssupplyiβ‹…getPriceNote(address(wcanto),Β false)\frac{1}{8\cdot 10^Β΄z} \sum_i \frac{\text{assetReserves}_i\cdot\text{prices}_i+\text{token1TVL}\cdot\text{decimals}}{\text{supply}_i}\cdot\text{getPriceNote(address(wcanto), false)}

    which might be a better implementation of the computations.

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax Β© 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter