Platform: Code4rena
Start Date: 01/04/2024
Pot Size: $120,000 USDC
Total HM: 11
Participants: 55
Period: 21 days
Judge: Picodes
Total Solo HM: 6
Id: 354
League: ETH
Rank: 44/55
Findings: 1
Award: $32.96
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: DadeKuma
Also found by: 0xStalin, 0xhacksmithh, 99Crits, Aymen0909, Bauchibred, CodeWasp, Dup1337, IllIllI, John_Femi, K42, KupiaSec, Naresh, Rhaydden, Rolezn, Sathish9098, Topmark, ZanyBonzy, albahaca, bareli, blockchainbuttonmasher, cheatc0d3, codeslide, crc32, d3e4, favelanky, grearlake, hihen, jasonxiale, jesjupyter, lanrebayode77, lirezArAzAvi, lsaudit, mining_mario, oualidpro, pfapostol, radin100, rbserver, sammy, satoshispeedrunner, slvDev, twcctop, zabihullahazadzoi
32.9585 USDC - $32.96
Math.sol
Unchecked Calls and Integer Overflows:
unchecked
keyword which skips overflow checks for arithmetic operations. This could potentially result in silent integer overflows or underflows that lead to incorrect calculations.unchecked
is often intentional in Solidity to save gas since Solidity 0.8.0 includes built-in overflow checks. In this case, one needs to ensure that the logic of each operation is correct, resists any overflow/underflow issues, and aligns with the expected mathematical properties. For instance, the functions mulDiv
, mulDiv64
, mulDiv96
, mulDiv128
, and mulDiv192
are structured to handle large number multiplications safely and don't typically require overflow checks.Unused or Missing Imports:
LiquidityChunkLibrary
is inferred but not explicitly seen, which means that either the import is missing or the code that relies on it is not provided.Type Conversion Checks:
toUint128
, toUint128Capped
, toInt128
, and toInt256
functions include checks to ensure safe typecasting between different numerical types. An overflow would result in a revert with a CastingError
. However, depending on the use cases of these functions, it might be better to include underflow checks too if any decrement operations are expected.abs
and absUint
Functions:
abs
function does not support type(int256).min
and will revert for this value as the negative of type(int256).min
cannot be represented in an int256. This could potentially be a bug depending on the context of where abs
is used.absUint
function comment says it supports type(int256).min
, but we don't see the explicit check here. The code does not seem to have a flaw, as the conversion will work because uint256(-type(int256).min)
is perfectly valid.Potential Roundup Errors:
mulDivRoundingUp
and other similar functions, there is a check for any remainder using mulmod
. If there is any remainder, the result is incremented by one to round up. However, this does not consider the edge case where the result could be exactly type(uint256).max
and incrementing would cause an overflow.Liquidity Chunk Math:
getAmount0ForLiquidity
and getAmount1ForLiquidity
functions assume particular return values from getSqrtRatioAtTick
but it's unclear without knowledge of that function's correctness if those assumptions always hold true.Documentation and Consistency:
getLiquidityForAmount0
and getLiquidityForAmount1
have the comment "Had to use a less optimal calculation to match Uniswap's implementation." This is not elaborated on, so future maintainers might not understand why the decisions were made, or what exactly is suboptimal.min
and max
functions for different types, but they all have identical descriptions in their comments. This may cause confusion for users of the library concerning which function applies to their use case.Comments and Code Consistency:
getSqrtRatioAtTick
. It's unclear if these are intended to be used or are vestigial remnants of an older version. This could be confusing or misleading and should be clarified or removed.This review is not exhaustive, but it covers several notable points that should be addressed for safer code or better maintainability. Remember that smart contract code is immutable once deployed on the blockchain, and even a small bug can lead to significant financial loss, so thorough testing and auditing are crucial.
#0 - c4-judge
2024-04-26T17:18:57Z
Picodes marked the issue as grade-b