det-prime
Deterministic, zero-entropy primality testing, transcript-to-prime hashing, primes in arithmetic progressions, and modular square roots for #![no_std] Rust and WebAssembly (wasm32-unknown-unknown).
Zero runtime dependencies. #![forbid(unsafe_code)]. Stack-allocated fixed-width limbs up to 4096 bits. Backed by Lean 4 proofs in lean/DetPrime/ and documented assumption boundaries in PROOF.md.
Why This Exists
Standard cryptographic and zero-knowledge libraries use probabilistic Miller-Rabin (k random rounds) and randomized Tonelli-Shanks (rejection-sampling a quadratic non-residue). That creates three recurring engineering problems:
- Entropy-free execution environments:
wasm32-unknown-unknownguests, smart-contract VMs, zkVMs, and consensus state transitions have no OS CSPRNG and require bit-identical execution across validators. - Adversarial parameter validation: Fixed small-base Miller-Rabin tables fail against constructed strong pseudoprimes (Arnault's 46-digit composite passes all 11 prime bases
2..=31; Sorenson-Webster's 82-bit composite passes2..=41) and Carmichael numbers. - Verifiable delay functions and Fiat-Shamir challenges: Wesolowski VDF verifiers and RSA accumulator transcripts must map a 256-bit hash digest deterministically to a prime challenge without interactive randomness.
det-prime replaces random witness sampling with deterministic prime-base bounds:
n < 2^64(is_prime_u64): Unconditional 7-base Sinclair witness set ([2, 325, 9375, 28178, 450775, 9780504, 1795265022]), exact across all 64-bit integers.2^64 <= n < 2^4096(is_prime,verify_miller_bach): Tests prime basesp <= floor(2 * ln_hi(n)^2)via 4-bit windowed CIOS Montgomery multiplication, returning either aPrimalityCertificateor an explicitCompositeWitness(Even,PrimeFactor(p), orStrongWitness(p)).sqrt_mod_primeandleast_nonresidue: Finds the least prime quadratic non-residueq <= floor(ln_hi(p)^2)deterministically and runs Tonelli-Shanks without randomness, returning the canonical rootr <= (p - 1) / 2after verifyingr^2 == a (mod p).hash_to_prime_256: Maps any 32-byte digest deterministically to a 255-bit odd prime along a bounded candidate sequence.prime_in_progression_u64: Finds the smallest primep == a (mod q)within(phi(q) * ln q)^2(for example, NTT-friendly primesp == 1 (mod 2^k)).
Usage
[]
= "0.1"
For #![no_std] or wasm32-unknown-unknown targets:
[]
= { = "0.1", = false }
Deterministic Primality and Adversarial Witness Extraction
use ;
// secp256k1 field prime: 2^256 - 2^32 - 977
let secp = U256from_hex.unwrap;
assert!;
let cert = verify_miller_bach.unwrap;
assert_eq!;
// Arnault's 46-digit strong pseudoprime to all 11 prime bases 2..=31:
let arnault = U256from_decimal.unwrap;
assert_eq!;
Deterministic VDF / Fiat-Shamir Hash-to-Prime
use ;
let transcript_digest = ;
let = hash_to_prime_256.unwrap;
assert_eq!;
assert!;
assert!;
Deterministic Tonelli-Shanks on High 2-Adicity ZK Fields
use ;
// Stark252 prime: p = 2^251 + 17 * 2^192 + 1 (2-adicity 192)
let stark = U256from_hex.unwrap;
assert_eq!;
let root = sqrt_mod_prime.unwrap.unwrap;
assert_eq!;
Measured Timings
Single-core cargo run --release --example timing on Apple Silicon (M-series, rustc 1.94.0):
| Operation | Input | Latency |
|---|---|---|
is_prime_u64 |
64-bit odd integers (10,000 calls) | 0.97 us / call |
is_prime (U256) |
256-bit odd composite | < 0.01 us / call |
is_prime (U256) |
256-bit secp256k1 prime (all 6,301 prime bases <= 62,973) |
36.3 ms |
hash_to_prime_256 |
32-byte digest -> 255-bit prime (108 odd steps) | 30.2 ms |
sqrt_mod_prime (U256) |
Stark252 (2^192 | p - 1, deterministic Tonelli-Shanks) |
0.72 ms / call |
prime_in_progression_u64 |
q = 2^20, a = 1 -> p = 7,340,033 |
1.88 us |
Security note: det-prime is built for deterministic verification of public parameters, consensus inputs, curve/field moduli, and public VDF/Fiat-Shamir transcripts. Branching and early exit on composite witnesses are data-dependent; do not use det-prime to generate secret RSA key factors where constant-time execution is required.
Soundness and Lean 4 Verification
det-prime splits its mathematical claims into three explicit layers (detailed in PROOF.md):
- Local Lean 4 proofs (
lean/DetPrime/, kernel-replayed vialeancheckerwith zerosorryAx):LogBounds.lean: Proves the pure-integeru128rational boundln_upper_scaledstrictly enclosesln(n)and thatbach_bound_from_kmandlls_bound_from_kmenclose2 * (ln n)^2and(ln p)^2.PrimeWitness.lean: Proves subgroup prime-factor witness reduction, Solovay-Strassen properness of the Euler-Jacobi subgroup for all odd composites, Monier-Rabin strong-liar inclusion in the Euler-Jacobi subgroup, and zero false negatives on primes.TonelliShanks.lean: Proves prime non-residue reduction, thep == 3 (mod 4)branch, and the Tonelli-Shanks loop step invariant.BridgeAudit.lean: Composes the local proofs with the analytic bound interfaces into the bidirectional equivalencemiller_bach_prime_iff_all_prime_basesandleast_nonresidue_has_prime_witness.
- Published explicit analytic bounds: Bach (1990, Theorem 2), Lamzouri, Li & Soundararajan (2015, Corollaries 1.1 & 1.2), and Carneiro, Milinovich & Soundararajan (2019, Theorem 5).
- Upstream zero-free half-plane hypotheses:
DirichletLNonvanishingRightOfHalfandRiemannZetaZerosOnLinematch Levent Alpoge'sStatements.lean(20 November 2026, kernel-replayed in Lean 4; human peer review ongoing) verbatim. Allu64operations (is_prime_u64) are unconditional.
Verification
RUSTFLAGS="-D warnings"
RUSTFLAGS="-D warnings"
RUSTFLAGS="-D warnings"
To replay the Lean 4 modules against a compiled Mathlib4 workspace:
MATHLIB_DIR=/path/to/mathlib4
License
Apache-2.0. See LICENSE.