code-423n4 / 2022-07-swivel-findings

0 stars 1 forks source link

Gas Optimizations #182

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago
  1. useage of uint / int smaller than 32 bytes incurs overhead

When using elements that are smaller than 32 bytes, your contract’s gas usage may be higher. This is because the EVM operates on 32 bytes at a time. Therefore, if the element is smaller than that, the EVM must use more operations in order to reduce the size of the element from 32 bytes to the desired size.

resource : https://docs.soliditylang.org/en/v0.8.11/internals/layout_in_storage.html

Use a larger size then downcast where needed

POC : https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L15 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L578 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L600 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L620 https://github.com/code-423n4/2022-07-swivel/blob/main/Swivel/Swivel.sol#L634