<!-- canonical: https://0xsimao.com/findings/nftperp-i-nftp-scaling-1e18-underneath -->

# mul() and div() in NFTPMath are misleading due to scaling by 1e18 underneath

Low/Info · Three Sigma · NFT perpetuals · 2nd January, 2024

Finding 3S-NFTPerp-N09 of the Nftperp Exchange security review.

- Report: /reports/nftperp-i
- Source: https://cdn.sanity.io/files/qoqld077/production/c19530de75e234ad15694b4563edb1fc9d2a3fd8.pdf

---

### Description

Usually mul() and div() do multiplication and division, respectively, without scaling by
1e18 (commonly used by SafeMath). However, NFTPMath [`mul()`](https://github.com/nftperp/NFTPerp-V2-Contracts/blob/1c16f2010a851ac23aec73822d55388901bbe5e7/src/math/NFTPMath.sol#L22) and [`div()`](https://github.com/nftperp/NFTPerp-V2-Contracts/blob/1c16f2010a851ac23aec73822d55388901bbe5e7/src/math/NFTPMath.sol#L26) scale the
operations by 1e18, misleading code readers.
For example, when looking at ClearingHouse:openPosition(), it seems that it would round
down to 0 in [`_assertMaxLeverage()`](https://github.com/nftperp/NFTPerp-V2-Contracts/blob/1c16f2010a851ac23aec73822d55388901bbe5e7/src/ClearingHouse.sol#L776), but this is not the case as it correctly scales the
numbers.

### Recommendation

Consider renaming the functions to fullMulWad() and fullDivWad().

### Status

Acknowledged

---

Related findings:

- [`LenderCommitmentGroup_Smart` does not use `mulDiv` when converting between token and share amounts, possibly leading to DoS or loss of funds](https://0xsimao.com/findings/teller-finance-mul-div-converting-share): Teller Finance
- [ClampLib::mulDivDownInverse contains a dead `n == 0` check](https://0xsimao.com/findings/tenor-markets-clamp-inverse-contains-dead): Tenor Morpho Migrations
- [ClampLib::mulDivUpInverse overflows on large `target`, DoSing SELL offer reads](https://0xsimao.com/findings/tenor-markets-overflows-sell-offer-reads): Tenor Morpho Migrations
- [ClampLib::mulDivDownInverse reverts on `target == type(uint256).max` before the overflow guard runs](https://0xsimao.com/findings/tenor-markets-reverts-overflow-guard-runs): Tenor Morpho Migrations
