code-423n4 / 2022-01-trader-joe-findings

2 stars 0 forks source link

Gas Optimization: fmul optimization #290

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

gzeon

Vulnerability details

Impact

Can perform floating point multiplication like the following

https://github.com/code-423n4/2022-01-trader-joe/blob/a1579f6453bc4bf9fb0db9c627beaa41135438ed/contracts/RocketJoeStaking.sol#L108

    user.rewardDebt = (user.amount * accRJoePerShare) / PRECISION;

with this function to save gas

// out = x * y unchecked{/} z
function fmul(uint256 x, uint256 y, uint256 z) internal pure returns(uint256 out){
  assembly{
      if iszero(eq(div(mul(x,y),x),y)) {revert(0,0)}
      out := div(mul(x,y),z)
  }
}