Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions bigint/bigint_nonjs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1470,12 +1470,13 @@ pub fn BigInt::from_octets(input : BytesView, signum? : Int = 1) -> BigInt {
limbs[limbs_len - 1] = (limbs[limbs_len - 1] << 8) | input[i].to_uint()
}
let byte_per_limb = RADIX_BIT_LEN / 8
// tail
// tail: at RADIX_BIT_LEN == 32 a limb is exactly one big-endian 32-bit word,
// so the shift-accumulate loop collapses to a single u32be read. That
// constant is pinned by a test in bigint_nonjs_wbtest.mbt, so narrowing it
// fails there rather than silently mis-decoding here.
for i in 0..<div {
for j in 0..<byte_per_limb {
let bytes_idx = len - byte_per_limb - i * byte_per_limb + j
limbs[i] = (limbs[i] << 8) | input[bytes_idx].to_uint()
}
guard! input[len - byte_per_limb - i * byte_per_limb:] is [u32be(word), ..]
limbs[i] = word
}
Comment on lines 1472 to 1480

Copy link
Copy Markdown
Contributor Author

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_limb is RADIX_BIT_LEN / 8, and the file only requires RADIX_BIT_LEN to 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-coded u32be would. 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.mbt gets a test asserting RADIX_BIT_LEN == 32, with a comment explaining that from_octets's u32be read is what depends on it.
  • The comment above the tail loop now names the assumption and points at that test.

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 < 4 the very first tail iteration slices fewer than four bytes, so guard! ... is [u32be(word), ..] fails and it aborts.

moon test --target all is green: wasm 7444, wasm-gc 7445, js 7388, native 7361.

Copy link
Copy Markdown
Contributor Author

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 than inspect(..., content="32") (ba03090).

inspect would have made the guard self-defeating. Its expected value is a snapshot, so a routine moon test --update after narrowing the constant would rewrite content="32" to the new value and the test would go green — retiring the guard at exactly the moment it is supposed to fire. assert_eq is not auto-updatable.

Verified both halves by temporarily setting the expected value to 16:

  • moon test fails with FAILED: 32 != 16, so the pin is live and not skipped by backend gating.
  • moon test --update leaves it failing and does not touch the source, confirming the durability claim.

{ limbs, sign: Positive, len: normalize_len(limbs, limbs_len), }
}
Expand Down
13 changes: 13 additions & 0 deletions bigint/bigint_nonjs_wbtest.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,16 @@ test {
content="{limbs : <FixedArray: [2, 2147483648, 0]>, sign : Negative, len : 2 }",
) // Int64.min_value - 2
}

///|
/// `BigInt::from_octets` reads each tail limb with a single `u32be` bits
/// pattern, which agrees with the shift-accumulate loop it replaced only when a
/// limb is exactly four bytes wide. Pin the constant here so narrowing it fails
/// this test instead of silently mis-decoding octets.
///
/// Deliberately `assert_eq` rather than `inspect`: a snapshot would be rewritten
/// by `moon test --update`, which would silently retire this guard at exactly
/// the moment it is supposed to fire.
test "from_octets assumes 32-bit limbs" {
@test.assert_eq(RADIX_BIT_LEN, 32)
}
30 changes: 30 additions & 0 deletions bigint/from_octets_bench_test.mbt
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)) })
}
Loading