Documentation

HexPrimality.Cert

A primality certificate. One inductive rather than two mutually recursive declarations, because a structure referring forward to PrimeCert while PrimeCert refers back to it does not elaborate.

Each factor entry is a base a, an exponent e (stored off by one, so the exponent e + 1 is positive by construction), and the child certificate for a prime q, read off as the child's subject. Factor lists in both Pocklington constructors must be in strictly ascending child-subject order for the checker to accept them.

  • small (n : Nat) : PrimeCert

    n is an entry of the stored table.

  • pock (n : Nat) (factors : List (Nat × Nat × PrimeCert)) : PrimeCert

    Pocklington: factors partially factors n - 1 past its square root.

  • pock3 (n r s w : Nat) (factors : List (Nat × Nat × PrimeCert)) : PrimeCert

    The cube-root Brillhart-Lehmer-Selfridge variant, with the cofactor decomposition R = 2 F s + r and the integer-square-root witness w for the discriminant test (Nat.sqrt is well-founded recursion and does not kernel-reduce, so the checker verifies w instead of computing a root).

  • pock3Sieve (n r s w m : Nat) (factors : List (Nat × Nat × PrimeCert)) : PrimeCert

    Cube-root Pocklington with divisors lF+1 excluded for 1 ≤ l < m.

Instances For

    The number a certificate is about.

    Equations
    Instances For

      acc * q ^ e, checking each nonzero multiplication by division before constructing it. A zero accumulator or base returns zero immediately; otherwise the computation aborts as soon as the next product would exceed bound, so an attacker-chosen enormous power is never constructed.

      Equations
      Instances For
        noncomputable def Hex.Nat.boundedPowMul.go (bound q acc e : Nat) :

        Kernel reduction uses raw recursors to avoid auxiliary match and Decidable reductions at each bounded multiplication.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          noncomputable def Hex.Nat.boundedPowMul (bound q acc e : Nat) :

          Powers of two use a checked shift: the right shift checks the final product bound before the left shift constructs that product. Other bases use bounded multiplication. Compiled evaluation retains the original loop.

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

            The compiler uses the verified runtime implementation.

            @[simp]
            theorem Hex.Nat.boundedPowMul_zero (bound q acc : Nat) :
            boundedPowMul bound q acc 0 = some acc

            Exponent zero preserves the accumulator, even when it exceeds the bound.

            theorem Hex.Nat.boundedPowMul_succ (bound q acc e : Nat) :
            boundedPowMul bound q acc (e + 1) = if acc = 0 then some 0 else if q = 0 then some 0 else if acc ≤ bound / q then boundedPowMul bound q (acc * q) e else none

            One checked multiplication, as an equation for symbolic proofs.

            The factored part F = ∏ qᵢ ^ (eᵢ + 1) of a factor list, aborting as soon as the running product exceeds bound.

            Equations
            Instances For
              noncomputable def Hex.Nat.pockProduct (bound : Nat) :

              Bounded product for positive factor subjects, with zero denoting failure. The caller checks positivity. Exponent-one entries multiply and check the result against the bound; larger exponents retain the bounded power routine. A multiplication has at most the sum of its operands’ bit lengths.

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

                Continue the canonical factor-subject check above a strict lower bound.

                Equations
                Instances For
                  noncomputable def Hex.Nat.subjectsAfter (lower : Nat) (fs : List (Nat × Nat × PrimeCert)) :

                  Primitive comparisons in a direct list fold for kernel replay.

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

                    The compiler uses the verified runtime implementation.

                    Structural check on the factor list: every claimed prime is at least 2, and the claimed primes are in strictly ascending order. The canonical order implies pairwise distinctness with one lower-bound subject comparison per entry.

                    Equations
                    Instances For

                      The per-entry witness conditions: Fermat at the base, and the gcd condition at the reduced exponent. The gcd argument is written modularly: the checker only holds the residue x, and (x + n - 1) % n is x - 1 modulo n at every residue, where the literal x - 1 would truncate at x = 0.

                      Equations
                      Instances For
                        noncomputable def Hex.Nat.checkWitness (n q a : Nat) :

                        The witness conditions with primitive Nat comparisons for kernel reduction.

                        Equations
                        Instances For
                          @[csimp]

                          The compiler uses the verified runtime implementation.

                          The ordinary per-entry witness traversal used by compiled search.

                          Equations
                          Instances For
                            noncomputable def Hex.Nat.checkWitnesses.go (n : Nat) (fs : List (Nat × Nat × PrimeCert)) (prev : Nat) (seen : Bool) :

                            Replay adjacent equal witness bases with one Fermat check. seen records that the preceding base passed its Fermat check; the initial state is false.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              noncomputable def Hex.Nat.checkWitnesses (n : Nat) (fs : List (Nat × Nat × PrimeCert)) :

                              Check every witness, reusing the Fermat leg for adjacent equal bases.

                              Equations
                              Instances For
                                @[csimp]

                                The compiler uses the verified runtime implementation.

                                The arithmetic side of the square-root Pocklington node: n odd and at least 2, canonical strictly ascending factor subjects, F ∣ n - 1 with n < F * F, and every per-entry witness condition. Child certificates are checked separately by checkPrime.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  noncomputable def Hex.Nat.checkPockArith (n : Nat) (factors : List (Nat × Nat × PrimeCert)) :

                                  Kernel comparisons use primitive Nat operations; compiled evaluation retains the original arithmetic check.

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

                                    The arithmetic side of the cube-root node: everything the square-root arm verifies except the n < F * F bound, which the Brillhart-Lehmer-Selfridge conditions replace: F even, the cofactor R = (n - 1) / F odd (the weaker form of the classical gcd(F, R) = 1 that the proof uses once F is even), the decomposition R = 2Fs + r with 1 ≤ r < 2F, the cube-root size bound, and the discriminant condition on r² - 8s, with the stored witness w verifying non-squareness by two multiplications. (When r² ≤ 8s the truncated subtraction makes the witness clause w * w < 0 unsatisfiable, so the disjunct is simply never taken; the middle disjunct covers that region.)

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      noncomputable def Hex.Nat.checkPock3Arith (n r s w : Nat) (factors : List (Nat × Nat × PrimeCert)) :

                                      Kernel replay uses the bounded positive product and primitive comparisons. Compiled checking retains the original arithmetic path.

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

                                        Maximum sieve bound in a checked certificate. This caps replay work for arbitrary untrusted literals, including rejected inputs.

                                        Equations
                                        Instances For

                                          Check the first k possible divisors lF+1.

                                          Equations
                                          Instances For

                                            The general cube-root arithmetic check; the legacy node retains its sieve-free path.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For
                                              noncomputable def Hex.Nat.checkPock3SieveArith (n r s w m : Nat) (factors : List (Nat × Nat × PrimeCert)) :

                                              Kernel replay uses the bounded positive product and primitive comparisons. Compiled checking retains the original arithmetic path.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                theorem Hex.Nat.boundedPowMul_eq {bound q : Nat} (e acc r : Nat) :
                                                boundedPowMul bound q acc e = some r → r = acc * q ^ e

                                                On success, the bounded accumulator computes the ordinary product.

                                                theorem Hex.Nat.boundedPowMul_le {bound q acc e r : Nat} (hacc : acc ≤ bound) (h : boundedPowMul bound q acc e = some r) :
                                                r ≤ bound

                                                A successful bounded multiplication preserves the accumulator bound. The incoming bound is needed only for the zero-exponent case; every positive step establishes it before constructing the next accumulator.

                                                theorem Hex.Nat.certProduct_le {bound : Nat} (hbound : 1 ≤ bound) (l : List (Nat × Nat × PrimeCert)) (F : Nat) :
                                                certProduct bound l = some F → F ≤ bound

                                                A successful certificate product is bounded when its initial accumulator 1 is bounded.

                                                @[csimp]

                                                The compiler uses the verified arithmetic implementation.

                                                @[csimp]

                                                The compiler uses the verified runtime implementation.

                                                @[csimp]

                                                The compiler uses the verified runtime implementation.

                                                Accept or reject a primality certificate. Pocklington factor lists are accepted only in strictly ascending child-subject order. This is the ordinary compiled traversal; the public checkPrime supplies the kernel fold.

                                                Equations
                                                Instances For

                                                  Accept every child certificate of a factor list.

                                                  Equations
                                                  Instances For
                                                    noncomputable def Hex.Nat.checkPrime :

                                                    Direct structural fold for kernel replay. Compiled code retains the ordinary mutually recursive traversal.

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

                                                      The compiler uses the verified recursive implementation.

                                                      theorem Hex.Nat.prime_of_pocklington {n : Nat} {factors : List (Nat × Nat × PrimeCert)} (hcheck : checkPockArith n factors = true) (hprimes : ∀ (x : Nat × Nat × PrimeCert), x ∈ factors → Prime x.snd.snd.subject) :

                                                      Pocklington replay with separately proved child primes. This permits a certificate generator to share child proofs across many parent certificates. Only each child's subject is used; checkPockArith validates the parent arithmetic, factor ordering and witnesses. The PrimeCert payload is ignored apart from its subject: a .small q here is not checked against the table. The separate hprimes hypothesis must prove that q is prime.

                                                      Checker soundness: an accepted certificate proves its subject prime. The whole conclusion; no certificate-existence, checker-completeness, or search-completeness claim accompanies it.

                                                      Single-Bool-slot wrapper for the tactic reifier: the kernel verifies the subject match and the certificate replay in one reduction.

                                                      An accepted certificate tied to the number requested by its caller. The subject equality is load-bearing: checkPrime proves primality of c.subject, not of an unrelated input that happened to request c.

                                                      Instances For

                                                        The primality of the requested subject.