Documentation

CatCryptCore.ForMathlib.PrattCertificate

Pratt/Lucas prime certificates over fast modular exponentiation #

A reusable core for proving Nat.Prime p for large p via the Lucas primality criterion, using a binary (square-and-multiply) modular exponentiation whose result is checkable by kernel decide at cryptographic sizes.

The ZMod p power a ^ (p - 1) is unary npowRec, so it is not directly evaluable for a 256-bit p. powMod computes a ^ k % n in O(log k) multiplications, and powMod_cast transports a fast powMod … = 1 check into the a ^ (p - 1) = 1 hypothesis of lucas_primality.

Main definitions #

Main results #

Square-and-multiply worker for modular exponentiation. fuel bounds the number of squarings; base is the running square, e the remaining exponent bits, and acc the accumulated product, all kept reduced % m. The e = 0 guard halts the loop, so the recursion depth is the bit length of the initial exponent.

Equations
Instances For

    Binary (square-and-multiply) modular exponentiation: powMod a k n = a ^ k % n (for 0 < n), computed in O(log k) multiplications. The worker powModGo is tail-recursive with fuel k, so kernel decide reduces a fully applied powMod in O(log k) GMP Nat steps at cryptographic sizes without a Lean.ofReduceBool axiom.

    Equations
    Instances For
      theorem CatCrypt.PrattCertificate.powModGo_cast (m fuel base e acc : ℕ) :
      e < 2 ^ fuel → ↑(powModGo m fuel base e acc) = ↑acc * ↑base ^ e

      The worker invariant: cast into ZMod m, powModGo computes acc * base ^ e, provided the fuel covers the exponent (e < 2 ^ fuel).

      theorem CatCrypt.PrattCertificate.powModGo_lt (m : ℕ) (hm : 0 < m) (fuel base e acc : ℕ) :
      acc < m → powModGo m fuel base e acc < m

      The worker stays inside [0, m) when started from a reduced accumulator.

      theorem CatCrypt.PrattCertificate.powMod_lt (a k n : ℕ) (hn : 0 < n) :
      powMod a k n < n

      The output is a residue: powMod a k n < n for 0 < n.

      theorem CatCrypt.PrattCertificate.powMod_cast (a k n : ℕ) [NeZero n] :
      ↑(powMod a k n) = ↑a ^ k

      The fast powMod result cast into ZMod n equals the abstract power there, so a powMod … = 1 check discharges an a ^ k = 1 hypothesis over ZMod n.

      theorem CatCrypt.PrattCertificate.powMod_eq (a k n : ℕ) (hn : 0 < n) :
      powMod a k n = a ^ k % n

      Correctness of binary modular exponentiation.

      theorem CatCrypt.PrattCertificate.prime_mem_of_dvd_listProd {q : ℕ} (hq : Nat.Prime q) (l : List ℕ) :
      (∀ r ∈ l, Nat.Prime r) → q ∣ l.prod → q ∈ l

      A prime dividing the product of a list of primes is a member of the list.

      theorem CatCrypt.PrattCertificate.lucas_of_cover (p : ℕ) (hp1 : 1 < p) (a : ℕ) (qs : List ℕ) (hcover : ∀ (q : ℕ), Nat.Prime q → q ∣ p - 1 → q ∈ qs) (hfermat : powMod a (p - 1) p = 1) (hwit : ∀ q ∈ qs, powMod a ((p - 1) / q) p ≠ 1) :

      Lucas primality from a covering list of prime factors. Given a witness a, a list qs containing every prime factor of p - 1, the Fermat check powMod a (p - 1) p = 1, and for each q ∈ qs the check powMod a ((p - 1) / q) p ≠ 1, conclude Nat.Prime p.

      theorem CatCrypt.PrattCertificate.lucas_of_certificate (p : ℕ) (hp1 : 1 < p) (a : ℕ) (qs : List ℕ) (hprod : qs.prod = p - 1) (hqs_prime : ∀ q ∈ qs, Nat.Prime q) (hfermat : powMod a (p - 1) p = 1) (hwit : ∀ q ∈ qs, powMod a ((p - 1) / q) p ≠ 1) :

      Lucas primality via a certificate. Given a witness a, a list qs of the prime factors of p - 1 whose product is p - 1, the Fermat check powMod a (p - 1) p = 1, and for each factor q the check powMod a ((p - 1) / q) p ≠ 1, conclude Nat.Prime p.

      The Fermat prime 2 ^ 16 + 1 = 65537, proved via lucas_of_certificate. Its p - 1 = 2 ^ 16 has the single prime factor 2; 3 is a witness.

      Reflective Pratt certificates #

      A Cert is a Pratt certificate tree. Cert.check is a Boolean checker that reduces in the kernel by GMP-accelerated Nat arithmetic, and Cert.sound turns c.check = true into Nat.Prime c.prime. The tactic pratt_prime c closes a goal Nat.Prime p from an inline certificate c by kernel decide.

      stripFactor q fuel n divides n by q as long as q > 1 divides it, at most fuel times.

      Equations
      Instances For
        theorem CatCrypt.PrattCertificate.stripFactor_spec (q fuel n : ℕ) :
        ∃ (k : ℕ), n = stripFactor q fuel n * q ^ k

        stripFactor removes a power of q: n = stripFactor q fuel n * q ^ k.

        theorem CatCrypt.PrattCertificate.dvd_mem_of_stripAll_eq_one {r : ℕ} (hr : Nat.Prime r) (qs : List ℕ) (n : ℕ) :
        stripAll n qs = 1 → r ∣ n → ∃ q ∈ qs, r ∣ q

        If stripAll n qs = 1, every prime factor of n divides some q ∈ qs.

        Trial division loop: trialGo q fuel d is true when no m with d ≤ m and m * m ≤ q divides q, inspected for at most fuel candidates.

        Equations
        Instances For
          theorem CatCrypt.PrattCertificate.trialGo_spec (q fuel d : ℕ) :
          trialGo q fuel d = true → ∀ (m : ℕ), d ≤ m → m * m ≤ q → ¬m ∣ q

          Soundness of trialGo.

          Primality by trial division up to √q.

          Equations
          Instances For

            A Pratt certificate tree. small p is checked by trial division; node p a fs is a Lucas certificate for p with witness a whose list fs certifies the distinct prime factors of p - 1. A numeral n denotes small n.

            Instances For

              The local Lucas checks of one node: 1 < p, the Fermat check, the factors qs cover p - 1 (stripping them leaves 1), and the witness checks.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                theorem CatCrypt.PrattCertificate.prime_of_nodeOk (p a : ℕ) (qs : List ℕ) (hqs : ∀ q ∈ qs, Nat.Prime q) (h : nodeOk p a qs = true) :

                nodeOk and primality of the factors give primality of p.

                pratt_prime c closes a goal Nat.Prime p from a Pratt certificate tree c with c.prime = p, checking c.check = true by kernel reduction.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For