puremp 0.2.2

A pure-Rust arbitrary-precision arithmetic library — integers, rationals and MPFR-class floats — with a dependency-free clean-room core (optional serde/rand bridges), plus a C ABI and a CLI.
Documentation
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! Certificate-based primality **proving** — deterministic proofs, not just
//! probable-prime tests.
//!
//! A Baillie–PSW test (see [`Int::is_prime_bpsw`](crate::Int::is_prime_bpsw))
//! is fast and has no known counterexample, but for a value above `2^64` it is
//! only a *probable*-prime test: it can, in principle, be fooled. This module
//! instead produces a short, independently **checkable certificate** that a
//! number is prime, following the classical `n−1` methods.
//!
//! # Methods
//!
//! - **Pocklington–Lehmer (`n−1`).** Factor `n−1 = F·R` with `F` fully factored
//!   and `gcd(F, R) = 1`. If, for every prime `q | F`, there is a base `a_q`
//!   with `a_q^{n−1} ≡ 1 (mod n)` and `gcd(a_q^{(n−1)/q} − 1, n) = 1`, then every
//!   prime factor `p` of `n` satisfies `p ≡ 1 (mod F)`. When `F > √n` this forces
//!   `n` to be prime. (Brillhart, Lehmer & Selfridge, *Math. Comp.* **29** (1975);
//!   Crandall & Pomerance §4.1; HAC §4.6.)
//! - **BLS `n^{1/3}` refinement.** The same witnesses only need `F > n^{1/3}`:
//!   if `n` were composite it would be a product of exactly two primes
//!   `p_i = a_i·F + 1`, and the pair `(a_1, a_2)` would be the integer roots of
//!   `x² − t·x + s` where `s = ⌊R/F⌋`, `t = R mod F`. If that quadratic's
//!   discriminant `t² − 4s` is not a perfect square (or the implied factor does
//!   not divide `n`), no such factorization exists and `n` is prime. This lets a
//!   proof succeed with only a third of `n−1` factored.
//!
//! Both share one witness format, so a single [`PrimalityCertificate`] covers
//! them; the recorded [`Bound`] (√ or ∛) says which applied. Certificates are
//! **recursive**: each prime `q | F` that is itself larger than `2^64` carries
//! its own nested certificate, so [`PrimalityCertificate::verify`] can re-derive
//! the whole proof from the ground up, bottoming out at values below `2^64`
//! where Baillie–PSW is deterministic.
//!
//! Full [ECPP] and APR-CL — the general-purpose proofs that do not depend on
//! `n∓1` factoring nicely — are out of scope; when the `n−1` factoring cannot
//! reach the `n^{1/3}` bound, [`prove_prime`] reports [`Primality::Unproven`].
//!
//! [ECPP]: https://en.wikipedia.org/wiki/Elliptic_curve_primality
//!
//! # Example
//!
//! ```
//! use puremp::Int;
//! use puremp::primality::{prove_prime, Primality};
//!
//! // The Mersenne prime 2^89 − 1; n − 1 factors into small primes.
//! let n = Int::from(2).pow(89).sub(&Int::ONE);
//! match prove_prime(&n) {
//!     Primality::Prime(cert) => assert!(cert.verify(&n)),
//!     _ => panic!("2^89 − 1 is prime"),
//! }
//!
//! // A composite is rejected with certainty.
//! assert!(matches!(prove_prime(&Int::from(561)), Primality::Composite));
//! ```

use alloc::boxed::Box;
use alloc::vec::Vec;

use crate::int::Int;

/// Trial-division bound used to peel the small-prime part of `n−1`.
const TRIAL_BOUND: u64 = 1 << 16;

/// Largest base tried when searching for a Pocklington witness `a_q`. Real
/// primes almost always yield a witness at a tiny base, so this is generous.
const WITNESS_LIMIT: u64 = 4096;

/// A composite cofactor of `n−1` this many bits or smaller may be handed to the
/// full [`Int::factorize`] escalation (trial → rho → ECM/QS) to try to reach the
/// proving bound. Larger hard cofactors are left unfactored (→ `Unproven`) so a
/// proof attempt never runs away.
const FACTOR_CAP_BITS: u32 = 128;

/// The outcome of a primality **proof** attempt (see [`prove_prime`]).
#[derive(Clone, Debug)]
pub enum Primality {
    /// `n` is prime, witnessed by a checkable [`PrimalityCertificate`].
    Prime(PrimalityCertificate),
    /// `n` is *certainly* composite (a factor, a Fermat witness, or a
    /// deterministic Baillie–PSW rejection was found).
    Composite,
    /// `n` is a probable prime, but `n∓1` could not be factored far enough to
    /// build a certificate. A general-purpose proof (ECPP / APR-CL) would be
    /// needed; those are out of scope here.
    Unproven,
}

/// How much of `n−1` had to be factored for the proof to go through.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Bound {
    /// Classic Pocklington–Lehmer: the factored part `F` exceeds `√n`.
    Sqrt,
    /// BLS refinement: `F` only exceeds `n^{1/3}`, closed by the discriminant
    /// test on the residual cofactor.
    Cbrt,
}

/// A proof that one prime factor `q` of `F` is itself prime.
#[derive(Clone, Debug)]
enum PrimeProof {
    /// `q < 2^64`, where Baillie–PSW is a proven, deterministic test.
    Small,
    /// `q ≥ 2^64`, proved recursively by its own certificate.
    Recursive(Box<PrimalityCertificate>),
}

/// One prime power `q^e ‖ F`, its Pocklington witness, and the proof that `q`
/// is prime.
#[derive(Clone, Debug)]
struct FactorWitness {
    /// The prime `q`.
    prime: Int,
    /// Its multiplicity `e` in `F` (so `q^e` divides `n − 1`).
    exp: u32,
    /// A base `a` with `a^{n−1} ≡ 1` and `gcd(a^{(n−1)/q} − 1, n) = 1 (mod n)`.
    witness: Int,
    /// Proof that `prime` is prime.
    proof: PrimeProof,
}

/// The concrete proof carried by a [`PrimalityCertificate`].
#[derive(Clone, Debug)]
enum Kind {
    /// `n < 2^64`: settled directly by deterministic Baillie–PSW.
    SmallBpsw,
    /// An `n − 1` (Pocklington / BLS) proof.
    NMinusOne {
        /// The fully-factored part `F = ∏ prime^exp` of `n − 1`.
        factors: Vec<FactorWitness>,
        /// The unfactored cofactor `R = (n − 1) / F`, coprime to `F`.
        cofactor: Int,
        /// Which size bound on `F` the proof relied on.
        bound: Bound,
    },
}

/// A checkable proof that a specific number is prime.
///
/// Produced by [`prove_prime`] and re-checked, independently of how it was
/// built, by [`PrimalityCertificate::verify`]. The certificate is *recursive*:
/// verifying it re-derives every step, including the primality of each prime
/// factor of `F`, so a passing [`verify`](PrimalityCertificate::verify) is a
/// self-contained proof.
#[derive(Clone, Debug)]
pub struct PrimalityCertificate {
    /// The number this certificate is about.
    n: Int,
    /// The proof method and its data.
    kind: Kind,
}

impl PrimalityCertificate {
    /// The number this certificate proves prime.
    pub fn n(&self) -> &Int {
        &self.n
    }

    /// The size bound on the factored part `F` this proof relied on, or `None`
    /// for a small (`< 2^64`, Baillie–PSW) certificate.
    pub fn bound(&self) -> Option<Bound> {
        match &self.kind {
            Kind::SmallBpsw => None,
            Kind::NMinusOne { bound, .. } => Some(*bound),
        }
    }

    /// Re-checks the certificate from scratch, returning `true` only if it is a
    /// valid proof that `n` (which must equal the number the certificate was
    /// built for) is prime.
    ///
    /// This shares no state with [`prove_prime`]: it recomputes `F`, re-verifies
    /// every witness congruence, recursively re-checks each prime factor of `F`,
    /// and (for a BLS proof) re-runs the discriminant test. Tampering with any
    /// field makes it return `false`.
    pub fn verify(&self, n: &Int) -> bool {
        if &self.n != n {
            return false;
        }
        verify_cert(self)
    }
}

/// Attempts to **prove** whether `n` is prime.
///
/// Returns:
/// - [`Primality::Prime`] with a certificate whose
///   [`verify`](PrimalityCertificate::verify) succeeds, when a Pocklington / BLS
///   `n − 1` proof is found (values below `2^64` are settled directly by
///   deterministic Baillie–PSW);
/// - [`Primality::Composite`] — with certainty — for composites (including
///   Carmichael numbers and products of large primes);
/// - [`Primality::Unproven`] when `n` is a probable prime but `n − 1` could not
///   be factored past the `n^{1/3}` bound.
pub fn prove_prime(n: &Int) -> Primality {
    // Units and negatives are not prime; report them as (not-prime) composite.
    if n < &Int::from(2) {
        return Primality::Composite;
    }
    // Below 2^64 Baillie–PSW is a proven, deterministic test.
    if n.to_u64().is_some() {
        return if n.is_prime_bpsw() {
            Primality::Prime(PrimalityCertificate {
                n: n.clone(),
                kind: Kind::SmallBpsw,
            })
        } else {
            Primality::Composite
        };
    }
    // A Baillie–PSW *rejection* is always correct: n is certainly composite.
    if !n.is_prime_bpsw() {
        return Primality::Composite;
    }
    // n is a probable prime; try to build an n − 1 proof.
    match prove_n_minus_1(n) {
        Attempt::Proved(cert) => Primality::Prime(cert),
        Attempt::Composite => Primality::Composite,
        Attempt::Insufficient => Primality::Unproven,
    }
}

/// The internal result of the `n − 1` proving attempt.
enum Attempt {
    /// A finished certificate.
    Proved(PrimalityCertificate),
    /// `n` was shown composite along the way (a factor or Fermat witness).
    Composite,
    /// Could not factor `n − 1` far enough, or no witness was found.
    Insufficient,
}

/// Runs the Pocklington / BLS `n − 1` proof for a probable prime `n ≥ 2^64`.
fn prove_n_minus_1(n: &Int) -> Attempt {
    let m = n.sub(&Int::ONE); // n − 1
    let (mut primes, mut cofactor) = peel_smooth(&m);

    // If the residual cofactor is itself prime, it fully factors n − 1.
    if cofactor > Int::ONE && cofactor.is_prime_bpsw() {
        primes.push((cofactor.clone(), 1));
        cofactor = Int::ONE;
    }

    let mut factored = product_pow(&primes);

    // Not enough yet? If the leftover is a small-enough composite, spend the
    // full factoring escalation on it to try to reach the bound.
    if cube_le(&factored, n) && cofactor > Int::ONE && cofactor.bit_len() <= FACTOR_CAP_BITS {
        for p in cofactor.factorize() {
            match primes.iter_mut().find(|(q, _)| *q == p) {
                Some((_, e)) => *e += 1,
                None => primes.push((p, 1)),
            }
        }
        primes.sort_by(|a, b| a.0.cmp(&b.0));
        factored = product_pow(&primes);
        cofactor = m.div_trunc(&factored);
    }

    // Decide which bound (if any) F reaches.
    let bound = if factored.mul(&factored) > *n {
        Bound::Sqrt
    } else if !cube_le(&factored, n) {
        Bound::Cbrt
    } else {
        return Attempt::Insufficient;
    };

    assemble(n, &m, primes, cofactor, bound)
}

/// `true` iff `f³ ≤ n`.
fn cube_le(f: &Int, n: &Int) -> bool {
    f.mul(f).mul(f) <= *n
}

/// Finds a witness for each prime and, on success, builds the certificate.
fn assemble(n: &Int, m: &Int, primes: Vec<(Int, u32)>, cofactor: Int, bound: Bound) -> Attempt {
    let mut factors = Vec::with_capacity(primes.len());
    for (q, e) in primes {
        let witness = match find_witness(n, m, &q) {
            Witness::Found(a) => a,
            Witness::Composite => return Attempt::Composite,
            Witness::NotFound => return Attempt::Insufficient,
        };
        // Prove q itself prime (recursively for large q).
        let proof = if q.to_u64().is_some() {
            PrimeProof::Small
        } else {
            match prove_prime(&q) {
                Primality::Prime(sub) => PrimeProof::Recursive(Box::new(sub)),
                _ => return Attempt::Insufficient,
            }
        };
        factors.push(FactorWitness {
            prime: q,
            exp: e,
            witness,
            proof,
        });
    }
    // For a BLS (∛) proof, the discriminant test must actually close the case.
    if bound == Bound::Cbrt {
        let f = product_pow_from(&factors);
        if matches!(bls_discriminant(n, &f, &cofactor), Disc::Composite) {
            return Attempt::Composite;
        }
    }
    Attempt::Proved(PrimalityCertificate {
        n: n.clone(),
        kind: Kind::NMinusOne {
            factors,
            cofactor,
            bound,
        },
    })
}

/// The result of searching for a Pocklington witness for one prime `q`.
enum Witness {
    /// A base satisfying both congruences.
    Found(Int),
    /// A proof (Fermat failure or a proper factor) that `n` is composite.
    Composite,
    /// No base up to [`WITNESS_LIMIT`] worked.
    NotFound,
}

/// Searches bases `a = 2, 3, …` for one with `a^{n−1} ≡ 1 (mod n)` and
/// `gcd(a^{(n−1)/q} − 1, n) = 1`.
fn find_witness(n: &Int, m: &Int, q: &Int) -> Witness {
    let e = m.div_trunc(q); // (n − 1) / q, exact
    let mut a = 2u64;
    while a <= WITNESS_LIMIT {
        let base = Int::from(a);
        // a^{n−1} must be 1 mod n, otherwise n is composite (Fermat).
        if base.modpow(m, n) != Int::ONE {
            return Witness::Composite;
        }
        let x = base.modpow(&e, n);
        let g = x.sub(&Int::ONE).gcd(n);
        if g == Int::ONE {
            return Witness::Found(base);
        }
        if g != *n {
            // 1 < gcd < n is a proper factor: n is composite.
            return Witness::Composite;
        }
        // g == n means a^{(n−1)/q} ≡ 1; try the next base.
        a += 1;
    }
    Witness::NotFound
}

/// Peels the small-prime part of `m` by trial division up to [`TRIAL_BOUND`],
/// returning the prime powers found (each fully divided out) and the residual
/// cofactor coprime to every peeled prime.
fn peel_smooth(m: &Int) -> (Vec<(Int, u32)>, Int) {
    let mut primes: Vec<(Int, u32)> = Vec::new();
    let mut c = m.clone();

    // Factor of two.
    let two = Int::from(2);
    let mut e = 0u32;
    while c > Int::ZERO && c.is_even() {
        c = c.div_trunc(&two);
        e += 1;
    }
    if e > 0 {
        primes.push((two, e));
    }

    // Odd trial divisors.
    let mut d = 3u64;
    while d <= TRIAL_BOUND {
        let dn = Int::from(d);
        // Stop early once the divisor squared passes the cofactor.
        if dn.mul(&dn) > c {
            break;
        }
        let mut e = 0u32;
        loop {
            let (q, r) = c.div_rem_trunc(&dn);
            if r.is_zero() {
                c = q;
                e += 1;
            } else {
                break;
            }
        }
        if e > 0 {
            primes.push((dn, e));
        }
        d += 2;
    }

    (primes, c)
}

/// `∏ prime^exp` for a `(prime, exp)` list.
fn product_pow(primes: &[(Int, u32)]) -> Int {
    let mut f = Int::ONE;
    for (p, e) in primes {
        f = f.mul(&p.pow(*e));
    }
    f
}

/// `∏ prime^exp` for a witness list.
fn product_pow_from(factors: &[FactorWitness]) -> Int {
    let mut f = Int::ONE;
    for fw in factors {
        f = f.mul(&fw.prime.pow(fw.exp));
    }
    f
}

/// Outcome of the BLS discriminant test.
enum Disc {
    /// No composite factorization `n = (a₁F+1)(a₂F+1)` exists: `n` is prime.
    Prime,
    /// Such a factorization exists and its factor divides `n`: `n` is composite.
    Composite,
}

/// The BLS `n^{1/3}` discriminant test. Assumes every prime factor of `n` is
/// `≡ 1 (mod F)` (established by the Pocklington witnesses) and `F³ > n`, so any
/// composite `n` is a product of exactly two primes `a_i·F + 1`. The pair
/// `(a₁, a₂)` are the integer roots of `x² − t·x + s`, with `s = ⌊R/F⌋`,
/// `t = R mod F`; if the discriminant is not a perfect square (or the implied
/// factor does not divide `n`), no such factorization exists.
fn bls_discriminant(n: &Int, f: &Int, r: &Int) -> Disc {
    let (s, t) = r.div_rem_trunc(f); // R = s·F + t, 0 ≤ t < F
    let disc = t.mul(&t).sub(&s.mul(&Int::from(4)));
    if disc.is_negative() {
        return Disc::Prime; // no real roots
    }
    let root = match disc.sqrt_exact() {
        Some(root) => root,
        None => return Disc::Prime, // not a perfect square
    };
    let num = t.sub(&root); // 2·a₁
    if num.is_odd() || num.is_negative() {
        return Disc::Prime; // no non-negative integer root
    }
    let a1 = num.div_trunc(&Int::from(2));
    if a1.is_zero() {
        return Disc::Prime; // factor would be 1, not proper
    }
    let cand = a1.mul(f).add(&Int::ONE); // a₁·F + 1
    if cand > Int::ONE && &cand < n && cand.divides(n) {
        return Disc::Composite;
    }
    Disc::Prime
}

// --- verification (independent re-check) ---------------------------------

/// Independently re-checks a certificate. See [`PrimalityCertificate::verify`].
fn verify_cert(cert: &PrimalityCertificate) -> bool {
    let n = &cert.n;
    if n < &Int::from(2) {
        return false;
    }
    match &cert.kind {
        Kind::SmallBpsw => n.to_u64().is_some() && n.is_prime_bpsw(),
        Kind::NMinusOne {
            factors,
            cofactor,
            bound,
        } => verify_n_minus_1(n, factors, cofactor, *bound),
    }
}

/// Re-checks an `n − 1` (Pocklington / BLS) certificate from scratch.
fn verify_n_minus_1(n: &Int, factors: &[FactorWitness], cofactor: &Int, bound: Bound) -> bool {
    if factors.is_empty() || cofactor < &Int::ONE {
        return false;
    }
    let m = n.sub(&Int::ONE);
    let f = product_pow_from(factors);

    // F · R must equal n − 1, and F and R must be coprime.
    if f.mul(cofactor) != m {
        return false;
    }
    if f.gcd(cofactor) != Int::ONE {
        return false;
    }

    // The bound on F must hold, and match the recorded kind.
    let f2 = f.mul(&f);
    match bound {
        Bound::Sqrt => {
            if f2 <= *n {
                return false;
            }
        }
        Bound::Cbrt => {
            if f2.mul(&f) <= *n {
                return false;
            }
        }
    }

    // Every prime power and its witness.
    for fw in factors {
        // q must be prime (directly for small q, recursively otherwise).
        match &fw.proof {
            PrimeProof::Small => {
                if fw.prime.to_u64().is_none() || !fw.prime.is_prime_bpsw() {
                    return false;
                }
            }
            PrimeProof::Recursive(sub) => {
                if sub.n() != &fw.prime || !sub.verify(&fw.prime) {
                    return false;
                }
            }
        }
        // q^exp must actually divide n − 1 to the claimed multiplicity.
        let qe = fw.prime.pow(fw.exp);
        if !qe.divides(&m) {
            return false;
        }
        // Witness congruences: a^{n−1} ≡ 1 and gcd(a^{(n−1)/q} − 1, n) = 1.
        if fw.witness < Int::from(2) {
            return false;
        }
        if fw.witness.modpow(&m, n) != Int::ONE {
            return false;
        }
        let e = m.div_trunc(&fw.prime);
        let x = fw.witness.modpow(&e, n);
        if x.sub(&Int::ONE).gcd(n) != Int::ONE {
            return false;
        }
    }

    // A BLS proof additionally needs the discriminant test to close the case.
    if bound == Bound::Cbrt && matches!(bls_discriminant(n, &f, cofactor), Disc::Composite) {
        return false;
    }

    true
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A tampered certificate — any field mutated — must fail `verify`.
    #[test]
    fn tampered_certificate_is_rejected() {
        // 2^89 − 1 is a Pocklington-provable prime.
        let n = Int::from(2).pow(89).sub(&Int::ONE);
        let cert = match prove_prime(&n) {
            Primality::Prime(c) => c,
            other => panic!("expected a proof, got {other:?}"),
        };
        assert!(cert.verify(&n));

        // Corrupt a witness base.
        let mut bad = cert.clone();
        if let Kind::NMinusOne { factors, .. } = &mut bad.kind {
            factors[0].witness = factors[0].witness.add(&Int::ONE);
        }
        assert!(!bad.verify(&n), "bumped witness must fail");

        // Corrupt a prime-power exponent.
        let mut bad = cert.clone();
        if let Kind::NMinusOne { factors, .. } = &mut bad.kind {
            factors[0].exp += 1;
        }
        assert!(!bad.verify(&n), "wrong exponent must fail");

        // Corrupt the cofactor.
        let mut bad = cert.clone();
        if let Kind::NMinusOne { cofactor, .. } = &mut bad.kind {
            *cofactor = cofactor.add(&Int::ONE);
        }
        assert!(!bad.verify(&n), "wrong cofactor must fail");

        // Drop a factor from F (so the bound no longer holds).
        let mut bad = cert.clone();
        if let Kind::NMinusOne { factors, .. } = &mut bad.kind {
            factors.pop();
        }
        assert!(!bad.verify(&n), "shrunken F must fail");

        // Claim the certificate is about a different number.
        assert!(!cert.verify(&n.add(&Int::from(2))), "wrong n must fail");
    }

    /// Falsely relabelling a genuine BLS (∛) proof as a plain Pocklington (√)
    /// one must be rejected, since `F² ≤ n` there.
    #[test]
    fn forged_bound_label_is_rejected() {
        let n = Int::from(2).pow(127).sub(&Int::ONE); // needs the BLS bound
        let cert = match prove_prime(&n) {
            Primality::Prime(c) => c,
            other => panic!("expected a proof, got {other:?}"),
        };
        assert_eq!(cert.bound(), Some(Bound::Cbrt));
        let mut bad = cert.clone();
        if let Kind::NMinusOne { bound, .. } = &mut bad.kind {
            *bound = Bound::Sqrt;
        }
        assert!(!bad.verify(&n), "F² ≤ n, so the √ label is a lie");
    }
}