-
Notifications
You must be signed in to change notification settings - Fork 162
refactor(bigint): read each from_octets limb with a u32be pattern #4099
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright 2026 International Digital Economy Academy | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| ///| | ||
| fn from_octets_bench_data(len : Int) -> Bytes { | ||
| Bytes::makei(len, i => ((i * 37 + 11) & 0xff).to_byte()) | ||
| } | ||
|
|
||
| ///| | ||
| test "bench BigInt::from_octets n=64" (it : @bench.T) { | ||
| let data = from_octets_bench_data(64) | ||
| it.bench(fn() { it.keep(@bigint.BigInt::from_octets(data)) }) | ||
| } | ||
|
|
||
| ///| | ||
| test "bench BigInt::from_octets n=1024" (it : @bench.T) { | ||
| let data = from_octets_bench_data(1024) | ||
| it.bench(fn() { it.keep(@bigint.BigInt::from_octets(data)) }) | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch on the assumption, but I don't think the suggested remedy holds up — the fallback would not actually be generic.
byte_per_limbisRADIX_BIT_LEN / 8, and the file only requiresRADIX_BIT_LENto be a multiple of 4 and at most 32 (lines 79-80). So the legal widths are 4, 8, 12, ... 32, and for 4, 12, 20 and 28 that division truncates — the old shift-accumulate loop reads 0, 1, 2 or 3 bytes into a limb that is 4, 12, 20 or 28 bits wide and mis-decodes just as badly as a hard-codedu32bewould. Restoring it as the "safe" arm would convert a loud failure into a quiet one, and add a branch no test can reach (patch coverage here is currently 100%).So I pinned the assumption instead, in 09f2b3c:
bigint_nonjs_wbtest.mbtgets a test assertingRADIX_BIT_LEN == 32, with a comment explaining thatfrom_octets'su32beread is what depends on it.Narrowing the constant now fails a test rather than silently mis-decoding octets — which is the outcome you were after.
Worth adding: the new code cannot silently mis-decode even without the test. At any
byte_per_limb < 4the very first tail iteration slices fewer than four bytes, soguard! ... is [u32be(word), ..]fails and it aborts.moon test --target allis green: wasm 7444, wasm-gc 7445, js 7388, native 7361.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up: the pin now uses
@test.assert_eq(RADIX_BIT_LEN, 32)rather thaninspect(..., content="32")(ba03090).inspectwould have made the guard self-defeating. Its expected value is a snapshot, so a routinemoon test --updateafter narrowing the constant would rewritecontent="32"to the new value and the test would go green — retiring the guard at exactly the moment it is supposed to fire.assert_eqis not auto-updatable.Verified both halves by temporarily setting the expected value to 16:
moon testfails withFAILED: 32 != 16, so the pin is live and not skipped by backend gating.moon test --updateleaves it failing and does not touch the source, confirming the durability claim.