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 feedback] _getPointer only needs one % 256 #124

Open
emizzle opened this issue Jul 30, 2024 · 1 comment
Open

[Certora feedback] _getPointer only needs one % 256 #124

emizzle opened this issue Jul 30, 2024 · 1 comment
Labels
Certora Issues discovered as part of Certora integration

Comments

@emizzle
Copy link
Collaborator

emizzle commented Jul 30, 2024

The _getPointer function can reduce the number of % 256's to reduce gas consumption:

Proofs.sol
- function _getPointer(SlotId id, Period period) internal view returns (uint8) {
-   uint256 blockNumber = block.number % 256;
-   // To ensure the pointer does not remain in downtime for many consecutive
-   // periods, for each period increase, move the pointer 67 blocks. We've
-   // chosen a prime number to ensure that we don't get cycles.
-   uint256 periodNumber = (Period.unwrap(period) * 67) % 256;
-   uint256 idOffset = uint256(SlotId.unwrap(id)) % 256;
-   uint256 pointer = (blockNumber + periodNumber + idOffset) % 256;
-   return uint8(pointer);
- }
+ function _getPointer(SlotId id, Period period) internal view returns (uint8) {
+   uint256 blockNumber = block.number;
+   // To ensure the pointer does not remain in downtime for many consecutive
+   // periods, for each period increase, move the pointer 67 blocks. We've
+   // chosen a prime number to ensure that we don't get cycles.
+   uint256 periodNumber = (Period.unwrap(period) * 67);
+   uint256 idOffset = uint256(SlotId.unwrap(id));
+   uint256 pointer = (blockNumber + periodNumber + idOffset) % 256;
+   return uint8(pointer);
+ }
@AuHau AuHau added the Certora Issues discovered as part of Certora integration label Aug 5, 2024
@markspanbroek
Copy link
Member

I would not do that, doing modulo twice was on purpose. A SlotId is the result of a hash. It can be anywhere in the uint256 range. Also at the very end. This could lead to an overflow when adding the blockNumber and periodNumber to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Certora Issues discovered as part of Certora integration
Projects
None yet
Development

No branches or pull requests

3 participants