Expand description
Deterministic Miller-Rabin primality plus tabled fast paths for the power-of-two-aligned cases that dominate ruvector’s hot paths.
Designed for ADR-151 (PIAL — Prime-Indexed Acceleration Layer). Five consumers (shard router, HNSW buckets, sparsifier strides, mincut LSH, pi-brain witness chain) get one shared utility and zero new external dependencies.
§Determinism
| Range | Witnesses | Result |
|---|---|---|
n < 2^32 | {2, 7, 61} (Pomerance/Selfridge/Wagstaff) | Deterministic |
n < 2^64 | {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37} (Sinclair, 2011) | Deterministic |
n < 2^128 | 40 random rounds (unstable-u128 feature) | Pr[err] < 2⁻⁸⁰ |
Pinned-pseudoprime regressions in tests/primality_pseudoprimes.rs
protect the deterministic ranges from witness-set “optimizations”.
§Hot vs cold paths
Three of PIAL’s five sites request primes near fixed power-of-two
sizes. Those calls hit prev_prime_below_pow2 / next_prime_above_pow2
— a single L1-cached load, ~1 ns. The two unpredictable sites (LSH
universe, witness ephemeral primes) use the general MR descent at
~250 ns. Both are cold.
Crucially the table is generated at build time from this very module’s
is_prime_u64, so MR remains the source of truth.
Constants§
- PRIMES_
ABOVE_ 2K - Smallest prime strictly greater than 2^k for k in [8, 64], indexed by
k - 8. - PRIMES_
BELOW_ 2K - Largest prime strictly less than 2^k for k in [8, 64], indexed by
k - 8.
Functions§
- ephemeral_
prime - Derives a deterministic ephemeral prime from
seed, suitable for the pi-brain witness chain (ADR-151 §4.4). - is_
prime_ u32 - Returns
trueiffnis prime. Deterministic for allu32. - is_
prime_ u64 - Returns
trueiffnis prime. Deterministic for allu64. - next_
prime_ above_ pow2 - Smallest prime strictly greater than
2^k, fork ∈ [8, 63]. - next_
prime_ u64 - Smallest prime strictly greater than
n. Returns0ifnis at or above the largestu64prime (u64::MAX - 58). - prev_
prime_ below_ pow2 - Largest prime strictly less than
2^k, fork ∈ [8, 64]. - prev_
prime_ u64 - Largest prime strictly less than
n. Returns0if no suchu64prime exists (i.e.n <= 2).