solidity: use shifts instead of multiply in LEB128 length decoder#94
Closed
deuszx wants to merge 1 commit into
Closed
solidity: use shifts instead of multiply in LEB128 length decoder#94deuszx wants to merge 1 commit into
deuszx wants to merge 1 commit into
Conversation
`* 128` and `* val` on uint256 each cost 5 gas where `<< 7` costs 3. The Yul optimizer probably already folds `* 128` to `<< 7`, but rewriting the inner loop in terms of shift/OR makes the intent explicit and removes the question. Functionally equivalent: existing multi-byte length round-trip coverage (vec i64 of length 140) exercises this path.
Contributor
Author
|
Under 0.8 checked arithmetic, the old power *= 128 / result += … would revert on a malformed length field with enough continuation bytes to overflow uint256 (~37+ bytes). The new code can't revert: once shift >= 256, val << shift is 0 in Solidity and result |= 0 is a no-op. So malformed input that previously reverted now silently decodes to a truncated length instead. |
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
* 128and* valon uint256 each cost 5 gas where<< 7costs 3. The Yul optimizer probably already folds* 128to<< 7, but rewriting the inner loop in terms of shift/OR makes the intent explicit and removes the question.Test Plan
Functionally equivalent: existing multi-byte length round-trip coverage (vec i64 of length 140) exercises this path.