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 #
powMod a k n— binary modular exponentiation returninga ^ k % n.Cert— a Pratt certificate tree;Cert.check— its Boolean checker.pratt_prime c— tactic closingNat.Prime pfrom an inline certificatec.
Main results #
powMod_eq—powMod a k n = a ^ k % nfor0 < n.powMod_cast—((powMod a k n : ℕ) : ZMod n) = (a : ZMod n) ^ k.lucas_of_cover—Nat.Prime pfrom a witnessa, a listqscontaining every prime factor ofp - 1, and fastpowModchecks.lucas_of_certificate— the same from a listqsof prime factors with productp - 1.Cert.sound—c.check = true → Nat.Prime c.prime.
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
- One or more equations did not get rendered due to their size.
- CatCrypt.PrattCertificate.powModGo m 0 x✝² x✝¹ x✝ = x✝
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
- CatCrypt.PrattCertificate.powMod a k n = CatCrypt.PrattCertificate.powModGo n k (a % n) k (1 % n)
Instances For
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.
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
stripFactor removes a power of q: n = stripFactor q fuel n * q ^ k.
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
Primality by trial division up to √q.
Equations
- CatCrypt.PrattCertificate.trialPrime q = (decide (2 ≤ q) && CatCrypt.PrattCertificate.trialGo q q 2)
Instances For
Soundness of trialPrime.
Equations
The prime a certificate is about.
Equations
- (CatCrypt.PrattCertificate.Cert.small p).prime = p
- (CatCrypt.PrattCertificate.Cert.node p a fs).prime = p
Instances For
The Boolean checker of a Pratt certificate tree.
Equations
Instances For
Soundness of Cert.check.
Soundness of Cert.checkAll.