Skip to content

solidity: use shifts instead of multiply in LEB128 length decoder#94

Closed
deuszx wants to merge 1 commit into
zefchain:mainfrom
deuszx:solidity-pr-6-leb128-shift
Closed

solidity: use shifts instead of multiply in LEB128 length decoder#94
deuszx wants to merge 1 commit into
zefchain:mainfrom
deuszx:solidity-pr-6-leb128-shift

Conversation

@deuszx
Copy link
Copy Markdown
Contributor

@deuszx deuszx commented May 14, 2026

Summary

* 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.

Test Plan

Functionally equivalent: existing multi-byte length round-trip coverage (vec i64 of length 140) exercises this path.

`* 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.
@deuszx deuszx requested a review from ma2bd as a code owner May 14, 2026 11:35
@deuszx
Copy link
Copy Markdown
Contributor Author

deuszx commented May 14, 2026

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.

@deuszx deuszx closed this May 14, 2026
@deuszx deuszx deleted the solidity-pr-6-leb128-shift branch May 14, 2026 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant