1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
// Helpers for verifying the scratch-length contracts of `limbs_*` functions (see
// DOC-CONVENTIONS.md).
//
// In safe Rust an under-sized scratch slice makes the function panic on a slice operation, so the
// primary check is simply running the function with a scratch of exactly the advertised length,
// swept densely across every algorithm-threshold boundary — that is where an incorrect formula
// hides, since the formulas are threshold-dependent and the bug precedent
// (`limbs_mul_high_same_length_scratch_len`, fixed 2026-08-02) lived exactly in a band between two
// algorithms. The sentinel fill measures the other direction: how much of the advertised length the
// function actually touches.
use crateLimb;
use random_primitive_ints;
use EXAMPLE_SEED;
// A limb pattern that arithmetic kernels are overwhelmingly unlikely to write. 0xa5a5... at any
// limb width.
pub const SCRATCH_SENTINEL: Limb = MAX / 0xff * 0xa5;
// Runs `f` on a scratch slice of exactly `scratch_len` limbs pre-filled with `SCRATCH_SENTINEL` and
// returns the high-water mark: the number of limbs up to and including the highest one that `f`
// overwrote. If `scratch_len` is smaller than `f` actually needs, `f` is expected to panic. A limb
// that `f` overwrites with the sentinel value itself goes undetected, which only shifts the
// measurement if it is the topmost touched limb (probability about $2^{-W}$ per call).
// Lengths that exercise every algorithm band: for each threshold, all lengths within `radius` of
// it, plus geometric filler (about 8 lengths per octave) so the interiors of the bands are sampled
// too. All returned lengths are within `min..=max`.
// Deterministic pseudorandom limbs for canary inputs. The values only need to vary enough to
// exercise value-dependent branches (evaluation signs, carry chains); `name` varies the sequence
// between call sites.