perf(utils): build Yul storage mapping pointer generator for nonces - #901
Merged
mijinummi merged 1 commit intoAug 20, 2026
Merged
Conversation
2 tasks
Collaborator
|
LGTM! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements a gas-optimized Yul storage mapping pointer generator for message nonces (
mapping(uint256 chainId => mapping(uint256 nonce => bool))), resolving issue #849.Closes #849
Changes
1.
contracts/utils/NonceSlotCalculator.sol[NEW]Implements a Solidity library
NonceSlotCalculatorwith functions:slotForNonce(uint256 chainId, uint256 nonce): calculates storage slot assuming default mapping slot0.slotForNonceAt(uint256 chainId, uint256 nonce, uint256 mappingSlot): calculates storage slot for any arbitrary mapping storage slot.Key Optimizations & Characteristics:
0x00–0x3f) to stage keys and intermediate hashes.outerSlot = keccak256(chainId ‖ mappingSlot)at0x00–0x40, and subsequentlyinnerSlot = keccak256(nonce ‖ outerSlot)by reusing the same scratch space slots.0x40, eliminating memory expansion gas costs for mapping lookups.2.
test/utils/NonceSlotCalculatorHarness.sol[NEW]Test harness contract exposing
NonceSlotCalculatorfunctions and verifying calculated slots directly against on-chain storage viasload.3.
test/utils/NonceSlotCalculator.test.ts[NEW]Comprehensive unit tests using Hardhat, Mocha, and Chai:
nonces[chainId][nonce].(0, 0), largeuint128values, andtype(uint256).max.mappingSlotoffsets withslotForNonceAt.pure) guaranteeing zero memory expansion.Test Results