Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Certora Audit] G-08. Use shift right/left instead of division/multiplication if possible #895

Merged
merged 3 commits into from
Jan 10, 2025

Conversation

remedcu
Copy link
Member

@remedcu remedcu commented Jan 9, 2025

This pull request includes optimizations to the gas usage in the Safe.sol and StorageAccessible.sol contracts by replacing multiplication operations with bitwise shift operations. While the DIV / MUL opcode uses 5 gas, the SHR / SHL opcode only uses 3 gas.

Gas optimization changes:

  • contracts/Safe.sol: Replaced the multiplication operation * 64 with the bitwise shift operation << 6 to reduce gas costs in the gasleft check.
  • contracts/common/StorageAccessible.sol: Replaced the multiplication operation * 32 with the bitwise shift operation << 5 to reduce gas costs when creating a new bytes array.

@remedcu remedcu self-assigned this Jan 9, 2025
@remedcu remedcu marked this pull request as ready for review January 9, 2025 14:24
@remedcu remedcu requested review from akshay-ap and mmv08 January 9, 2025 14:26
Copy link
Member

@mmv08 mmv08 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, just lacking comment in one file

contracts/common/StorageAccessible.sol Show resolved Hide resolved
@remedcu remedcu merged commit 7e760ef into main Jan 10, 2025
25 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants