lib/math+encoding: restore Hare loud preconditions (F-Q)

Three lib functions had lost their Hare loud-abort preconditions, so an
out-of-domain argument silently returned garbage instead of aborting:

  random.u32n / random.u64n  assert(n != 0)    ref/hare/math/random/random.ha:26,42
  base64.decodedsize         assert(sz%4 == 0) ref/hare/encoding/base64/base64.ha:597

Source-bundled lib change, identical on both stages (byte-id neutral).
989_libprecond_abort pins each precondition: n=0 / sz%4!=0 abort (rc!=0),
valid args return 0, run on cstage and wwstage.
This commit is contained in:
2026-06-14 23:18:28 +09:00
parent 7d4feac959
commit 46ed712352
4 changed files with 213 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ export fn next(r: *random) u64 = {
// fast unbiased mapping (mulhi-then-leftover-reject). Mirrors Hare's
// random::u32n.
export fn u32n(r: *random, n: u32) u32 = {
assert(n != 0u32); // ref/hare/math/random/random.ha:26
let x: u32 = next(r): u32;
let prod: u64 = (x: u64) * (n: u64);
let leftover: u32 = prod: u32;
@@ -48,6 +49,7 @@ export fn u32n(r: *random, n: u32) u32 = {
// path; otherwise rejection-sample to avoid modulo bias. Mirrors
// Hare's random::u64n.
export fn u64n(r: *random, n: u64) u64 = {
assert(n != 0u64); // ref/hare/math/random/random.ha:42
if ((n & (n - 1u64)) == 0u64) { return next(r) & (n - 1u64); };
// max = U64_MAX - (U64_MAX+1) % n = -1 - (-n % n)
let neg: u64 = (0u64 - n);