feat(math): implement fast Yul bit-search indexer and unit tests (#751) - #759
Merged
Merged
Conversation
Collaborator
|
Hello @Francis6-git , thank you for your contribution. Please kindly fix the ci failures. |
2 tasks
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.
Description
This PR implements a constant-time$O(1)$ bit-search library in Yul (
contracts/math/YulBitSearch.sol) to locate the Most Significant Bit (MSB) and Least Significant Bit (LSB) in auint256word.High-level Solidity while loops iterate bit-by-bit, causing substantial gas overhead on large numbers. This implementation uses an 8-stage binary search bitwise shift approach directly in Yul inline assembly, guaranteeing predictable constant gas execution regardless of the bit position.
Changes Included
contracts/math/YulBitSearch.sol: ImplementedmostSignificantBitandleastSignificantBitusing constant-time binary search shifts in Yul.test/math/YulBitSearch.test.ts: Added comprehensive Vitest suite covering singleton bits (MAX_UINT256), edge values, and zero-word handling.Test Results
Ran unit test suite via Vitest targeting the newly added test file:
Closes #751