40,000 USDC
View results
Submission Details
Severity: gas

Use assembly for math

Summary

Use assembly for math instead of Solidity. You can check for overflow/underflow in assembly to ensure safety. If using Solidity versions < 0.8.0 and you are using Safemath, you can gain significant gas savings by using assembly to calculate values and checking for overflow/underflow.

Recommendations

contract ThisContract {
//addition in assembly
function addFunction(uint256 a, uint256 b) public pure {
assembly {
let c := add(a, b)
if lt(c, a) {
mstore(0x00, "overflow")
revert(0x00, 0x20)
}
}
}
}

//Without assembly

src/gas.sol:Contract0 contract
Deployment Cost Deployment Size
40693 234
Function Name min avg median max # calls
addTest 308 308 308 308 1

// With assemebly

src/gas.sol:Contract1 contract
Deployment Cost Deployment Size
37087 216
Function Name min avg median max # calls
addAssemblyTest 263 263 263 263 1

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.