Documentation

PrimeCert.PowMod

Proof-producing evaluation of a ^ b % n #

Note that Mathlib.Tactic.NormNum.PowMod contains a similar tactic, but that runs significantly slower and less efficiently than the one here.

def powMod (a b n : ) :

The pow-mod function, named explicitly to allow more precise control of reduction.

Equations
Instances For
    def powModAux (a b c n : ) :

    The pow-mod auxiliary function, named explicitly to allow more precise control of reduction.

    Equations
    Instances For
      def Nat.eager (k : ) (n : ) :
      Equations
      Instances For
        noncomputable def powModTR (a b n : ) :

        Kernel-reducible tail-recursive modular exponentiation: computes a ^ b % n. Uses Nat.rec with bounded fuel so the kernel can reduce it via eagerReduce.

        Equations
        Instances For
          noncomputable def powModTR.aux (n : ) :
          (a b c : ) →
          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            def powModTR' (a b n : ) :

            Computable version of powModTR using partial_fixpoint. Used at elaboration time (e.g. in mkPowModEq') where we need actual computation, not kernel reduction.

            Equations
            Instances For
              @[irreducible]
              def powModTR'.aux (n a b c : ) :
              Equations
              Instances For
                theorem Bool.rec_eq_ite {α : Type u_1} {b : Bool} {t f : α} :
                rec f t b = if b = true then t else f
                @[simp]
                theorem Nat.mod_eq_mod {a b : } :
                a.mod b = a % b
                @[simp]
                theorem Nat.div_eq_div {a b : } :
                a.div b = a / b
                @[simp]
                theorem Nat.land_eq_land {a b : } :
                a.land b = a &&& b
                @[simp]
                theorem powModTR_aux_zero_eq {n a b c : } :
                powModTR.aux n 0 a b c = 0
                theorem powModTR_aux_succ_eq {n a b c fuel : } :
                powModTR.aux n (fuel + 1) a b c = Bool.rec (Bool.rec (powModTR.aux n fuel (a * a % n) (b / 2) (a * c % n)) (powModTR.aux n fuel (a * a % n) (b / 2) c) ((b % 2).beq 0)) (c % n) (b.beq 0)
                theorem powModTR_aux_succ_eq' {n a b c fuel : } :
                powModTR.aux n (fuel + 1) a b c = if b = 0 then c % n else if b % 2 = 0 then powModTR.aux n fuel (a * a % n) (b / 2) c else powModTR.aux n fuel (a * a % n) (b / 2) (a * c % n)
                theorem powModTR_aux_eq (n a b c fuel : ) (hfuel : b < fuel) :
                powModTR.aux n fuel a b c = powModAux a b c n
                theorem powModTR_eq (a b n : ) :
                powModTR a b n = powMod a b n
                theorem powMod_eq_of_powModTR (a b n m : ) (h : (powModTR a b n).beq m = true) :
                powMod a b n = m
                theorem powMod_ne_of_powModTR (a b n m : ) (h : (powModTR a b n).beq m = false) :
                powMod a b n m

                Given a, b, n : ℕ, return (m, ⊢ powMod a b n = m).

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

                  Given a, b, n, m : ℕ, if powMod a b n = m then return a proof of that fact.

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

                    Given a, b, n, m : ℕ, if powMod a b n ≠ m then return a proof of that fact.

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

                        Tactic to close goals about modular exponentiation. Handles two goal shapes:

                        • powMod a b n = m — proves the equality by computing a ^ b % n at elaboration time
                        • powMod a b n ≠ m — proves the disequality similarly

                        All of a, b, n, m must be numeric literals. The computation uses powModTR' (the partial_fixpoint version) at elaboration time, then produces a kernel proof via powModTR and eagerReduce.

                        example : powMod 11 100002 100003 = 1 := by prove_pow_mod
                        example : powMod 2 100002 100003 ≠ 1 := by prove_pow_mod
                        
                        Equations
                        Instances For