src/HOL/Old_Number_Theory/Pocklington.thy
 author haftmann Fri Nov 27 08:41:10 2009 +0100 (2009-11-27) changeset 33963 977b94b64905 parent 33657 a4179bf442d1 child 37602 501b0cae5aa8 permissions -rw-r--r--
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
```     1 (*  Title:      HOL/Library/Pocklington.thy
```
```     2     Author:     Amine Chaieb
```
```     3 *)
```
```     4
```
```     5 header {* Pocklington's Theorem for Primes *}
```
```     6
```
```     7 theory Pocklington
```
```     8 imports Main Primes
```
```     9 begin
```
```    10
```
```    11 definition modeq:: "nat => nat => nat => bool"    ("(1[_ = _] '(mod _'))")
```
```    12   where "[a = b] (mod p) == ((a mod p) = (b mod p))"
```
```    13
```
```    14 definition modneq:: "nat => nat => nat => bool"    ("(1[_ \<noteq> _] '(mod _'))")
```
```    15   where "[a \<noteq> b] (mod p) == ((a mod p) \<noteq> (b mod p))"
```
```    16
```
```    17 lemma modeq_trans:
```
```    18   "\<lbrakk> [a = b] (mod p); [b = c] (mod p) \<rbrakk> \<Longrightarrow> [a = c] (mod p)"
```
```    19   by (simp add:modeq_def)
```
```    20
```
```    21
```
```    22 lemma nat_mod_lemma: assumes xyn: "[x = y] (mod n)" and xy:"y \<le> x"
```
```    23   shows "\<exists>q. x = y + n * q"
```
```    24 using xyn xy unfolding modeq_def using nat_mod_eq_lemma by blast
```
```    25
```
```    26 lemma nat_mod[algebra]: "[x = y] (mod n) \<longleftrightarrow> (\<exists>q1 q2. x + n * q1 = y + n * q2)"
```
```    27 unfolding modeq_def nat_mod_eq_iff ..
```
```    28
```
```    29 (* Lemmas about previously defined terms.                                    *)
```
```    30
```
```    31 lemma prime: "prime p \<longleftrightarrow> p \<noteq> 0 \<and> p\<noteq>1 \<and> (\<forall>m. 0 < m \<and> m < p \<longrightarrow> coprime p m)"
```
```    32   (is "?lhs \<longleftrightarrow> ?rhs")
```
```    33 proof-
```
```    34   {assume "p=0 \<or> p=1" hence ?thesis using prime_0 prime_1 by (cases "p=0", simp_all)}
```
```    35   moreover
```
```    36   {assume p0: "p\<noteq>0" "p\<noteq>1"
```
```    37     {assume H: "?lhs"
```
```    38       {fix m assume m: "m > 0" "m < p"
```
```    39         {assume "m=1" hence "coprime p m" by simp}
```
```    40         moreover
```
```    41         {assume "p dvd m" hence "p \<le> m" using dvd_imp_le m by blast with m(2)
```
```    42           have "coprime p m" by simp}
```
```    43         ultimately have "coprime p m" using prime_coprime[OF H, of m] by blast}
```
```    44       hence ?rhs using p0 by auto}
```
```    45     moreover
```
```    46     { assume H: "\<forall>m. 0 < m \<and> m < p \<longrightarrow> coprime p m"
```
```    47       from prime_factor[OF p0(2)] obtain q where q: "prime q" "q dvd p" by blast
```
```    48       from prime_ge_2[OF q(1)] have q0: "q > 0" by arith
```
```    49       from dvd_imp_le[OF q(2)] p0 have qp: "q \<le> p" by arith
```
```    50       {assume "q = p" hence ?lhs using q(1) by blast}
```
```    51       moreover
```
```    52       {assume "q\<noteq>p" with qp have qplt: "q < p" by arith
```
```    53         from H[rule_format, of q] qplt q0 have "coprime p q" by arith
```
```    54         with coprime_prime[of p q q] q have False by simp hence ?lhs by blast}
```
```    55       ultimately have ?lhs by blast}
```
```    56     ultimately have ?thesis by blast}
```
```    57   ultimately show ?thesis  by (cases"p=0 \<or> p=1", auto)
```
```    58 qed
```
```    59
```
```    60 lemma finite_number_segment: "card { m. 0 < m \<and> m < n } = n - 1"
```
```    61 proof-
```
```    62   have "{ m. 0 < m \<and> m < n } = {1..<n}" by auto
```
```    63   thus ?thesis by simp
```
```    64 qed
```
```    65
```
```    66 lemma coprime_mod: assumes n: "n \<noteq> 0" shows "coprime (a mod n) n \<longleftrightarrow> coprime a n"
```
```    67   using n dvd_mod_iff[of _ n a] by (auto simp add: coprime)
```
```    68
```
```    69 (* Congruences.                                                              *)
```
```    70
```
```    71 lemma cong_mod_01[simp,presburger]:
```
```    72   "[x = y] (mod 0) \<longleftrightarrow> x = y" "[x = y] (mod 1)" "[x = 0] (mod n) \<longleftrightarrow> n dvd x"
```
```    73   by (simp_all add: modeq_def, presburger)
```
```    74
```
```    75 lemma cong_sub_cases:
```
```    76   "[x = y] (mod n) \<longleftrightarrow> (if x <= y then [y - x = 0] (mod n) else [x - y = 0] (mod n))"
```
```    77 apply (auto simp add: nat_mod)
```
```    78 apply (rule_tac x="q2" in exI)
```
```    79 apply (rule_tac x="q1" in exI, simp)
```
```    80 apply (rule_tac x="q2" in exI)
```
```    81 apply (rule_tac x="q1" in exI, simp)
```
```    82 apply (rule_tac x="q1" in exI)
```
```    83 apply (rule_tac x="q2" in exI, simp)
```
```    84 apply (rule_tac x="q1" in exI)
```
```    85 apply (rule_tac x="q2" in exI, simp)
```
```    86 done
```
```    87
```
```    88 lemma cong_mult_lcancel: assumes an: "coprime a n" and axy:"[a * x = a * y] (mod n)"
```
```    89   shows "[x = y] (mod n)"
```
```    90 proof-
```
```    91   {assume "a = 0" with an axy coprime_0'[of n] have ?thesis by (simp add: modeq_def) }
```
```    92   moreover
```
```    93   {assume az: "a\<noteq>0"
```
```    94     {assume xy: "x \<le> y" hence axy': "a*x \<le> a*y" by simp
```
```    95       with axy cong_sub_cases[of "a*x" "a*y" n]  have "[a*(y - x) = 0] (mod n)"
```
```    96         by (simp only: if_True diff_mult_distrib2)
```
```    97       hence th: "n dvd a*(y -x)" by simp
```
```    98       from coprime_divprod[OF th] an have "n dvd y - x"
```
```    99         by (simp add: coprime_commute)
```
```   100       hence ?thesis using xy cong_sub_cases[of x y n] by simp}
```
```   101     moreover
```
```   102     {assume H: "\<not>x \<le> y" hence xy: "y \<le> x"  by arith
```
```   103       from H az have axy': "\<not> a*x \<le> a*y" by auto
```
```   104       with axy H cong_sub_cases[of "a*x" "a*y" n]  have "[a*(x - y) = 0] (mod n)"
```
```   105         by (simp only: if_False diff_mult_distrib2)
```
```   106       hence th: "n dvd a*(x - y)" by simp
```
```   107       from coprime_divprod[OF th] an have "n dvd x - y"
```
```   108         by (simp add: coprime_commute)
```
```   109       hence ?thesis using xy cong_sub_cases[of x y n] by simp}
```
```   110     ultimately have ?thesis by blast}
```
```   111   ultimately show ?thesis by blast
```
```   112 qed
```
```   113
```
```   114 lemma cong_mult_rcancel: assumes an: "coprime a n" and axy:"[x*a = y*a] (mod n)"
```
```   115   shows "[x = y] (mod n)"
```
```   116   using cong_mult_lcancel[OF an axy[unfolded mult_commute[of _a]]] .
```
```   117
```
```   118 lemma cong_refl: "[x = x] (mod n)" by (simp add: modeq_def)
```
```   119
```
```   120 lemma eq_imp_cong: "a = b \<Longrightarrow> [a = b] (mod n)" by (simp add: cong_refl)
```
```   121
```
```   122 lemma cong_commute: "[x = y] (mod n) \<longleftrightarrow> [y = x] (mod n)"
```
```   123   by (auto simp add: modeq_def)
```
```   124
```
```   125 lemma cong_trans[trans]: "[x = y] (mod n) \<Longrightarrow> [y = z] (mod n) \<Longrightarrow> [x = z] (mod n)"
```
```   126   by (simp add: modeq_def)
```
```   127
```
```   128 lemma cong_add: assumes xx': "[x = x'] (mod n)" and yy':"[y = y'] (mod n)"
```
```   129   shows "[x + y = x' + y'] (mod n)"
```
```   130 proof-
```
```   131   have "(x + y) mod n = (x mod n + y mod n) mod n"
```
```   132     by (simp add: mod_add_left_eq[of x y n] mod_add_right_eq[of "x mod n" y n])
```
```   133   also have "\<dots> = (x' mod n + y' mod n) mod n" using xx' yy' modeq_def by simp
```
```   134   also have "\<dots> = (x' + y') mod n"
```
```   135     by (simp add: mod_add_left_eq[of x' y' n] mod_add_right_eq[of "x' mod n" y' n])
```
```   136   finally show ?thesis unfolding modeq_def .
```
```   137 qed
```
```   138
```
```   139 lemma cong_mult: assumes xx': "[x = x'] (mod n)" and yy':"[y = y'] (mod n)"
```
```   140   shows "[x * y = x' * y'] (mod n)"
```
```   141 proof-
```
```   142   have "(x * y) mod n = (x mod n) * (y mod n) mod n"
```
```   143     by (simp add: mod_mult_left_eq[of x y n] mod_mult_right_eq[of "x mod n" y n])
```
```   144   also have "\<dots> = (x' mod n) * (y' mod n) mod n" using xx'[unfolded modeq_def] yy'[unfolded modeq_def] by simp
```
```   145   also have "\<dots> = (x' * y') mod n"
```
```   146     by (simp add: mod_mult_left_eq[of x' y' n] mod_mult_right_eq[of "x' mod n" y' n])
```
```   147   finally show ?thesis unfolding modeq_def .
```
```   148 qed
```
```   149
```
```   150 lemma cong_exp: "[x = y] (mod n) \<Longrightarrow> [x^k = y^k] (mod n)"
```
```   151   by (induct k, auto simp add: cong_refl cong_mult)
```
```   152 lemma cong_sub: assumes xx': "[x = x'] (mod n)" and yy': "[y = y'] (mod n)"
```
```   153   and yx: "y <= x" and yx': "y' <= x'"
```
```   154   shows "[x - y = x' - y'] (mod n)"
```
```   155 proof-
```
```   156   { fix x a x' a' y b y' b'
```
```   157     have "(x::nat) + a = x' + a' \<Longrightarrow> y + b = y' + b' \<Longrightarrow> y <= x \<Longrightarrow> y' <= x'
```
```   158       \<Longrightarrow> (x - y) + (a + b') = (x' - y') + (a' + b)" by arith}
```
```   159   note th = this
```
```   160   from xx' yy' obtain q1 q2 q1' q2' where q12: "x + n*q1 = x'+n*q2"
```
```   161     and q12': "y + n*q1' = y'+n*q2'" unfolding nat_mod by blast+
```
```   162   from th[OF q12 q12' yx yx']
```
```   163   have "(x - y) + n*(q1 + q2') = (x' - y') + n*(q2 + q1')"
```
```   164     by (simp add: right_distrib)
```
```   165   thus ?thesis unfolding nat_mod by blast
```
```   166 qed
```
```   167
```
```   168 lemma cong_mult_lcancel_eq: assumes an: "coprime a n"
```
```   169   shows "[a * x = a * y] (mod n) \<longleftrightarrow> [x = y] (mod n)" (is "?lhs \<longleftrightarrow> ?rhs")
```
```   170 proof
```
```   171   assume H: "?rhs" from cong_mult[OF cong_refl[of a n] H] show ?lhs .
```
```   172 next
```
```   173   assume H: "?lhs" hence H': "[x*a = y*a] (mod n)" by (simp add: mult_commute)
```
```   174   from cong_mult_rcancel[OF an H'] show ?rhs  .
```
```   175 qed
```
```   176
```
```   177 lemma cong_mult_rcancel_eq: assumes an: "coprime a n"
```
```   178   shows "[x * a = y * a] (mod n) \<longleftrightarrow> [x = y] (mod n)"
```
```   179 using cong_mult_lcancel_eq[OF an, of x y] by (simp add: mult_commute)
```
```   180
```
```   181 lemma cong_add_lcancel_eq: "[a + x = a + y] (mod n) \<longleftrightarrow> [x = y] (mod n)"
```
```   182   by (simp add: nat_mod)
```
```   183
```
```   184 lemma cong_add_rcancel_eq: "[x + a = y + a] (mod n) \<longleftrightarrow> [x = y] (mod n)"
```
```   185   by (simp add: nat_mod)
```
```   186
```
```   187 lemma cong_add_rcancel: "[x + a = y + a] (mod n) \<Longrightarrow> [x = y] (mod n)"
```
```   188   by (simp add: nat_mod)
```
```   189
```
```   190 lemma cong_add_lcancel: "[a + x = a + y] (mod n) \<Longrightarrow> [x = y] (mod n)"
```
```   191   by (simp add: nat_mod)
```
```   192
```
```   193 lemma cong_add_lcancel_eq_0: "[a + x = a] (mod n) \<longleftrightarrow> [x = 0] (mod n)"
```
```   194   by (simp add: nat_mod)
```
```   195
```
```   196 lemma cong_add_rcancel_eq_0: "[x + a = a] (mod n) \<longleftrightarrow> [x = 0] (mod n)"
```
```   197   by (simp add: nat_mod)
```
```   198
```
```   199 lemma cong_imp_eq: assumes xn: "x < n" and yn: "y < n" and xy: "[x = y] (mod n)"
```
```   200   shows "x = y"
```
```   201   using xy[unfolded modeq_def mod_less[OF xn] mod_less[OF yn]] .
```
```   202
```
```   203 lemma cong_divides_modulus: "[x = y] (mod m) \<Longrightarrow> n dvd m ==> [x = y] (mod n)"
```
```   204   apply (auto simp add: nat_mod dvd_def)
```
```   205   apply (rule_tac x="k*q1" in exI)
```
```   206   apply (rule_tac x="k*q2" in exI)
```
```   207   by simp
```
```   208
```
```   209 lemma cong_0_divides: "[x = 0] (mod n) \<longleftrightarrow> n dvd x" by simp
```
```   210
```
```   211 lemma cong_1_divides:"[x = 1] (mod n) ==> n dvd x - 1"
```
```   212   apply (cases "x\<le>1", simp_all)
```
```   213   using cong_sub_cases[of x 1 n] by auto
```
```   214
```
```   215 lemma cong_divides: "[x = y] (mod n) \<Longrightarrow> n dvd x \<longleftrightarrow> n dvd y"
```
```   216 apply (auto simp add: nat_mod dvd_def)
```
```   217 apply (rule_tac x="k + q1 - q2" in exI, simp add: add_mult_distrib2 diff_mult_distrib2)
```
```   218 apply (rule_tac x="k + q2 - q1" in exI, simp add: add_mult_distrib2 diff_mult_distrib2)
```
```   219 done
```
```   220
```
```   221 lemma cong_coprime: assumes xy: "[x = y] (mod n)"
```
```   222   shows "coprime n x \<longleftrightarrow> coprime n y"
```
```   223 proof-
```
```   224   {assume "n=0" hence ?thesis using xy by simp}
```
```   225   moreover
```
```   226   {assume nz: "n \<noteq> 0"
```
```   227   have "coprime n x \<longleftrightarrow> coprime (x mod n) n"
```
```   228     by (simp add: coprime_mod[OF nz, of x] coprime_commute[of n x])
```
```   229   also have "\<dots> \<longleftrightarrow> coprime (y mod n) n" using xy[unfolded modeq_def] by simp
```
```   230   also have "\<dots> \<longleftrightarrow> coprime y n" by (simp add: coprime_mod[OF nz, of y])
```
```   231   finally have ?thesis by (simp add: coprime_commute) }
```
```   232 ultimately show ?thesis by blast
```
```   233 qed
```
```   234
```
```   235 lemma cong_mod: "~(n = 0) \<Longrightarrow> [a mod n = a] (mod n)" by (simp add: modeq_def)
```
```   236
```
```   237 lemma mod_mult_cong: "~(a = 0) \<Longrightarrow> ~(b = 0)
```
```   238   \<Longrightarrow> [x mod (a * b) = y] (mod a) \<longleftrightarrow> [x = y] (mod a)"
```
```   239   by (simp add: modeq_def mod_mult2_eq mod_add_left_eq)
```
```   240
```
```   241 lemma cong_mod_mult: "[x = y] (mod n) \<Longrightarrow> m dvd n \<Longrightarrow> [x = y] (mod m)"
```
```   242   apply (auto simp add: nat_mod dvd_def)
```
```   243   apply (rule_tac x="k*q1" in exI)
```
```   244   apply (rule_tac x="k*q2" in exI, simp)
```
```   245   done
```
```   246
```
```   247 (* Some things when we know more about the order.                            *)
```
```   248
```
```   249 lemma cong_le: "y <= x \<Longrightarrow> [x = y] (mod n) \<longleftrightarrow> (\<exists>q. x = q * n + y)"
```
```   250   using nat_mod_lemma[of x y n]
```
```   251   apply auto
```
```   252   apply (simp add: nat_mod)
```
```   253   apply (rule_tac x="q" in exI)
```
```   254   apply (rule_tac x="q + q" in exI)
```
```   255   by (auto simp: algebra_simps)
```
```   256
```
```   257 lemma cong_to_1: "[a = 1] (mod n) \<longleftrightarrow> a = 0 \<and> n = 1 \<or> (\<exists>m. a = 1 + m * n)"
```
```   258 proof-
```
```   259   {assume "n = 0 \<or> n = 1\<or> a = 0 \<or> a = 1" hence ?thesis
```
```   260       apply (cases "n=0", simp_all add: cong_commute)
```
```   261       apply (cases "n=1", simp_all add: cong_commute modeq_def)
```
```   262       apply arith
```
```   263       by (cases "a=1", simp_all add: modeq_def cong_commute)}
```
```   264   moreover
```
```   265   {assume n: "n\<noteq>0" "n\<noteq>1" and a:"a\<noteq>0" "a \<noteq> 1" hence a': "a \<ge> 1" by simp
```
```   266     hence ?thesis using cong_le[OF a', of n] by auto }
```
```   267   ultimately show ?thesis by auto
```
```   268 qed
```
```   269
```
```   270 (* Some basic theorems about solving congruences.                            *)
```
```   271
```
```   272
```
```   273 lemma cong_solve: assumes an: "coprime a n" shows "\<exists>x. [a * x = b] (mod n)"
```
```   274 proof-
```
```   275   {assume "a=0" hence ?thesis using an by (simp add: modeq_def)}
```
```   276   moreover
```
```   277   {assume az: "a\<noteq>0"
```
```   278   from bezout_add_strong[OF az, of n]
```
```   279   obtain d x y where dxy: "d dvd a" "d dvd n" "a*x = n*y + d" by blast
```
```   280   from an[unfolded coprime, rule_format, of d] dxy(1,2) have d1: "d = 1" by blast
```
```   281   hence "a*x*b = (n*y + 1)*b" using dxy(3) by simp
```
```   282   hence "a*(x*b) = n*(y*b) + b" by algebra
```
```   283   hence "a*(x*b) mod n = (n*(y*b) + b) mod n" by simp
```
```   284   hence "a*(x*b) mod n = b mod n" by (simp add: mod_add_left_eq)
```
```   285   hence "[a*(x*b) = b] (mod n)" unfolding modeq_def .
```
```   286   hence ?thesis by blast}
```
```   287 ultimately  show ?thesis by blast
```
```   288 qed
```
```   289
```
```   290 lemma cong_solve_unique: assumes an: "coprime a n" and nz: "n \<noteq> 0"
```
```   291   shows "\<exists>!x. x < n \<and> [a * x = b] (mod n)"
```
```   292 proof-
```
```   293   let ?P = "\<lambda>x. x < n \<and> [a * x = b] (mod n)"
```
```   294   from cong_solve[OF an] obtain x where x: "[a*x = b] (mod n)" by blast
```
```   295   let ?x = "x mod n"
```
```   296   from x have th: "[a * ?x = b] (mod n)"
```
```   297     by (simp add: modeq_def mod_mult_right_eq[of a x n])
```
```   298   from mod_less_divisor[ of n x] nz th have Px: "?P ?x" by simp
```
```   299   {fix y assume Py: "y < n" "[a * y = b] (mod n)"
```
```   300     from Py(2) th have "[a * y = a*?x] (mod n)" by (simp add: modeq_def)
```
```   301     hence "[y = ?x] (mod n)" by (simp add: cong_mult_lcancel_eq[OF an])
```
```   302     with mod_less[OF Py(1)] mod_less_divisor[ of n x] nz
```
```   303     have "y = ?x" by (simp add: modeq_def)}
```
```   304   with Px show ?thesis by blast
```
```   305 qed
```
```   306
```
```   307 lemma cong_solve_unique_nontrivial:
```
```   308   assumes p: "prime p" and pa: "coprime p a" and x0: "0 < x" and xp: "x < p"
```
```   309   shows "\<exists>!y. 0 < y \<and> y < p \<and> [x * y = a] (mod p)"
```
```   310 proof-
```
```   311   from p have p1: "p > 1" using prime_ge_2[OF p] by arith
```
```   312   hence p01: "p \<noteq> 0" "p \<noteq> 1" by arith+
```
```   313   from pa have ap: "coprime a p" by (simp add: coprime_commute)
```
```   314   from prime_coprime[OF p, of x] dvd_imp_le[of p x] x0 xp have px:"coprime x p"
```
```   315     by (auto simp add: coprime_commute)
```
```   316   from cong_solve_unique[OF px p01(1)]
```
```   317   obtain y where y: "y < p" "[x * y = a] (mod p)" "\<forall>z. z < p \<and> [x * z = a] (mod p) \<longrightarrow> z = y" by blast
```
```   318   {assume y0: "y = 0"
```
```   319     with y(2) have th: "p dvd a" by (simp add: cong_commute[of 0 a p])
```
```   320     with p coprime_prime[OF pa, of p] have False by simp}
```
```   321   with y show ?thesis unfolding Ex1_def using neq0_conv by blast
```
```   322 qed
```
```   323 lemma cong_unique_inverse_prime:
```
```   324   assumes p: "prime p" and x0: "0 < x" and xp: "x < p"
```
```   325   shows "\<exists>!y. 0 < y \<and> y < p \<and> [x * y = 1] (mod p)"
```
```   326   using cong_solve_unique_nontrivial[OF p coprime_1[of p] x0 xp] .
```
```   327
```
```   328 (* Forms of the Chinese remainder theorem.                                   *)
```
```   329
```
```   330 lemma cong_chinese:
```
```   331   assumes ab: "coprime a b" and  xya: "[x = y] (mod a)"
```
```   332   and xyb: "[x = y] (mod b)"
```
```   333   shows "[x = y] (mod a*b)"
```
```   334   using ab xya xyb
```
```   335   by (simp add: cong_sub_cases[of x y a] cong_sub_cases[of x y b]
```
```   336     cong_sub_cases[of x y "a*b"])
```
```   337 (cases "x \<le> y", simp_all add: divides_mul[of a _ b])
```
```   338
```
```   339 lemma chinese_remainder_unique:
```
```   340   assumes ab: "coprime a b" and az: "a \<noteq> 0" and bz: "b\<noteq>0"
```
```   341   shows "\<exists>!x. x < a * b \<and> [x = m] (mod a) \<and> [x = n] (mod b)"
```
```   342 proof-
```
```   343   from az bz have abpos: "a*b > 0" by simp
```
```   344   from chinese_remainder[OF ab az bz] obtain x q1 q2 where
```
```   345     xq12: "x = m + q1 * a" "x = n + q2 * b" by blast
```
```   346   let ?w = "x mod (a*b)"
```
```   347   have wab: "?w < a*b" by (simp add: mod_less_divisor[OF abpos])
```
```   348   from xq12(1) have "?w mod a = ((m + q1 * a) mod (a*b)) mod a" by simp
```
```   349   also have "\<dots> = m mod a" apply (simp add: mod_mult2_eq)
```
```   350     apply (subst mod_add_left_eq)
```
```   351     by simp
```
```   352   finally have th1: "[?w = m] (mod a)" by (simp add: modeq_def)
```
```   353   from xq12(2) have "?w mod b = ((n + q2 * b) mod (a*b)) mod b" by simp
```
```   354   also have "\<dots> = ((n + q2 * b) mod (b*a)) mod b" by (simp add: mult_commute)
```
```   355   also have "\<dots> = n mod b" apply (simp add: mod_mult2_eq)
```
```   356     apply (subst mod_add_left_eq)
```
```   357     by simp
```
```   358   finally have th2: "[?w = n] (mod b)" by (simp add: modeq_def)
```
```   359   {fix y assume H: "y < a*b" "[y = m] (mod a)" "[y = n] (mod b)"
```
```   360     with th1 th2 have H': "[y = ?w] (mod a)" "[y = ?w] (mod b)"
```
```   361       by (simp_all add: modeq_def)
```
```   362     from cong_chinese[OF ab H'] mod_less[OF H(1)] mod_less[OF wab]
```
```   363     have "y = ?w" by (simp add: modeq_def)}
```
```   364   with th1 th2 wab show ?thesis by blast
```
```   365 qed
```
```   366
```
```   367 lemma chinese_remainder_coprime_unique:
```
```   368   assumes ab: "coprime a b" and az: "a \<noteq> 0" and bz: "b \<noteq> 0"
```
```   369   and ma: "coprime m a" and nb: "coprime n b"
```
```   370   shows "\<exists>!x. coprime x (a * b) \<and> x < a * b \<and> [x = m] (mod a) \<and> [x = n] (mod b)"
```
```   371 proof-
```
```   372   let ?P = "\<lambda>x. x < a * b \<and> [x = m] (mod a) \<and> [x = n] (mod b)"
```
```   373   from chinese_remainder_unique[OF ab az bz]
```
```   374   obtain x where x: "x < a * b" "[x = m] (mod a)" "[x = n] (mod b)"
```
```   375     "\<forall>y. ?P y \<longrightarrow> y = x" by blast
```
```   376   from ma nb cong_coprime[OF x(2)] cong_coprime[OF x(3)]
```
```   377   have "coprime x a" "coprime x b" by (simp_all add: coprime_commute)
```
```   378   with coprime_mul[of x a b] have "coprime x (a*b)" by simp
```
```   379   with x show ?thesis by blast
```
```   380 qed
```
```   381
```
```   382 (* Euler totient function.                                                   *)
```
```   383
```
```   384 definition phi_def: "\<phi> n = card { m. 0 < m \<and> m <= n \<and> coprime m n }"
```
```   385
```
```   386 lemma phi_0[simp]: "\<phi> 0 = 0"
```
```   387   unfolding phi_def by auto
```
```   388
```
```   389 lemma phi_finite[simp]: "finite ({ m. 0 < m \<and> m <= n \<and> coprime m n })"
```
```   390 proof-
```
```   391   have "{ m. 0 < m \<and> m <= n \<and> coprime m n } \<subseteq> {0..n}" by auto
```
```   392   thus ?thesis by (auto intro: finite_subset)
```
```   393 qed
```
```   394
```
```   395 declare coprime_1[presburger]
```
```   396 lemma phi_1[simp]: "\<phi> 1 = 1"
```
```   397 proof-
```
```   398   {fix m
```
```   399     have "0 < m \<and> m <= 1 \<and> coprime m 1 \<longleftrightarrow> m = 1" by presburger }
```
```   400   thus ?thesis by (simp add: phi_def)
```
```   401 qed
```
```   402
```
```   403 lemma [simp]: "\<phi> (Suc 0) = Suc 0" using phi_1 by simp
```
```   404
```
```   405 lemma phi_alt: "\<phi>(n) = card { m. coprime m n \<and> m < n}"
```
```   406 proof-
```
```   407   {assume "n=0 \<or> n=1" hence ?thesis by (cases "n=0", simp_all)}
```
```   408   moreover
```
```   409   {assume n: "n\<noteq>0" "n\<noteq>1"
```
```   410     {fix m
```
```   411       from n have "0 < m \<and> m <= n \<and> coprime m n \<longleftrightarrow> coprime m n \<and> m < n"
```
```   412         apply (cases "m = 0", simp_all)
```
```   413         apply (cases "m = 1", simp_all)
```
```   414         apply (cases "m = n", auto)
```
```   415         done }
```
```   416     hence ?thesis unfolding phi_def by simp}
```
```   417   ultimately show ?thesis by auto
```
```   418 qed
```
```   419
```
```   420 lemma phi_finite_lemma[simp]: "finite {m. coprime m n \<and>  m < n}" (is "finite ?S")
```
```   421   by (rule finite_subset[of "?S" "{0..n}"], auto)
```
```   422
```
```   423 lemma phi_another: assumes n: "n\<noteq>1"
```
```   424   shows "\<phi> n = card {m. 0 < m \<and> m < n \<and> coprime m n }"
```
```   425 proof-
```
```   426   {fix m
```
```   427     from n have "0 < m \<and> m < n \<and> coprime m n \<longleftrightarrow> coprime m n \<and> m < n"
```
```   428       by (cases "m=0", auto)}
```
```   429   thus ?thesis unfolding phi_alt by auto
```
```   430 qed
```
```   431
```
```   432 lemma phi_limit: "\<phi> n \<le> n"
```
```   433 proof-
```
```   434   have "{ m. coprime m n \<and> m < n} \<subseteq> {0 ..<n}" by auto
```
```   435   with card_mono[of "{0 ..<n}" "{ m. coprime m n \<and> m < n}"]
```
```   436   show ?thesis unfolding phi_alt by auto
```
```   437 qed
```
```   438
```
```   439 lemma stupid[simp]: "{m. (0::nat) < m \<and> m < n} = {1..<n}"
```
```   440   by auto
```
```   441
```
```   442 lemma phi_limit_strong: assumes n: "n\<noteq>1"
```
```   443   shows "\<phi>(n) \<le> n - 1"
```
```   444 proof-
```
```   445   show ?thesis
```
```   446     unfolding phi_another[OF n] finite_number_segment[of n, symmetric]
```
```   447     by (rule card_mono[of "{m. 0 < m \<and> m < n}" "{m. 0 < m \<and> m < n \<and> coprime m n}"], auto)
```
```   448 qed
```
```   449
```
```   450 lemma phi_lowerbound_1_strong: assumes n: "n \<ge> 1"
```
```   451   shows "\<phi>(n) \<ge> 1"
```
```   452 proof-
```
```   453   let ?S = "{ m. 0 < m \<and> m <= n \<and> coprime m n }"
```
```   454   from card_0_eq[of ?S] n have "\<phi> n \<noteq> 0" unfolding phi_alt
```
```   455     apply auto
```
```   456     apply (cases "n=1", simp_all)
```
```   457     apply (rule exI[where x=1], simp)
```
```   458     done
```
```   459   thus ?thesis by arith
```
```   460 qed
```
```   461
```
```   462 lemma phi_lowerbound_1: "2 <= n ==> 1 <= \<phi>(n)"
```
```   463   using phi_lowerbound_1_strong[of n] by auto
```
```   464
```
```   465 lemma phi_lowerbound_2: assumes n: "3 <= n" shows "2 <= \<phi> (n)"
```
```   466 proof-
```
```   467   let ?S = "{ m. 0 < m \<and> m <= n \<and> coprime m n }"
```
```   468   have inS: "{1, n - 1} \<subseteq> ?S" using n coprime_plus1[of "n - 1"]
```
```   469     by (auto simp add: coprime_commute)
```
```   470   from n have c2: "card {1, n - 1} = 2" by (auto simp add: card_insert_if)
```
```   471   from card_mono[of ?S "{1, n - 1}", simplified inS c2] show ?thesis
```
```   472     unfolding phi_def by auto
```
```   473 qed
```
```   474
```
```   475 lemma phi_prime: "\<phi> n = n - 1 \<and> n\<noteq>0 \<and> n\<noteq>1 \<longleftrightarrow> prime n"
```
```   476 proof-
```
```   477   {assume "n=0 \<or> n=1" hence ?thesis by (cases "n=1", simp_all)}
```
```   478   moreover
```
```   479   {assume n: "n\<noteq>0" "n\<noteq>1"
```
```   480     let ?S = "{m. 0 < m \<and> m < n}"
```
```   481     have fS: "finite ?S" by simp
```
```   482     let ?S' = "{m. 0 < m \<and> m < n \<and> coprime m n}"
```
```   483     have fS':"finite ?S'" apply (rule finite_subset[of ?S' ?S]) by auto
```
```   484     {assume H: "\<phi> n = n - 1 \<and> n\<noteq>0 \<and> n\<noteq>1"
```
```   485       hence ceq: "card ?S' = card ?S"
```
```   486       using n finite_number_segment[of n] phi_another[OF n(2)] by simp
```
```   487       {fix m assume m: "0 < m" "m < n" "\<not> coprime m n"
```
```   488         hence mS': "m \<notin> ?S'" by auto
```
```   489         have "insert m ?S' \<le> ?S" using m by auto
```
```   490         from m have "card (insert m ?S') \<le> card ?S"
```
```   491           by - (rule card_mono[of ?S "insert m ?S'"], auto)
```
```   492         hence False
```
```   493           unfolding card_insert_disjoint[of "?S'" m, OF fS' mS'] ceq
```
```   494           by simp }
```
```   495       hence "\<forall>m. 0 <m \<and> m < n \<longrightarrow> coprime m n" by blast
```
```   496       hence "prime n" unfolding prime using n by (simp add: coprime_commute)}
```
```   497     moreover
```
```   498     {assume H: "prime n"
```
```   499       hence "?S = ?S'" unfolding prime using n
```
```   500         by (auto simp add: coprime_commute)
```
```   501       hence "card ?S = card ?S'" by simp
```
```   502       hence "\<phi> n = n - 1" unfolding phi_another[OF n(2)] by simp}
```
```   503     ultimately have ?thesis using n by blast}
```
```   504   ultimately show ?thesis by (cases "n=0") blast+
```
```   505 qed
```
```   506
```
```   507 (* Multiplicativity property.                                                *)
```
```   508
```
```   509 lemma phi_multiplicative: assumes ab: "coprime a b"
```
```   510   shows "\<phi> (a * b) = \<phi> a * \<phi> b"
```
```   511 proof-
```
```   512   {assume "a = 0 \<or> b = 0 \<or> a = 1 \<or> b = 1"
```
```   513     hence ?thesis
```
```   514       by (cases "a=0", simp, cases "b=0", simp, cases"a=1", simp_all) }
```
```   515   moreover
```
```   516   {assume a: "a\<noteq>0" "a\<noteq>1" and b: "b\<noteq>0" "b\<noteq>1"
```
```   517     hence ab0: "a*b \<noteq> 0" by simp
```
```   518     let ?S = "\<lambda>k. {m. coprime m k \<and> m < k}"
```
```   519     let ?f = "\<lambda>x. (x mod a, x mod b)"
```
```   520     have eq: "?f ` (?S (a*b)) = (?S a \<times> ?S b)"
```
```   521     proof-
```
```   522       {fix x assume x:"x \<in> ?S (a*b)"
```
```   523         hence x': "coprime x (a*b)" "x < a*b" by simp_all
```
```   524         hence xab: "coprime x a" "coprime x b" by (simp_all add: coprime_mul_eq)
```
```   525         from mod_less_divisor a b have xab':"x mod a < a" "x mod b < b" by auto
```
```   526         from xab xab' have "?f x \<in> (?S a \<times> ?S b)"
```
```   527           by (simp add: coprime_mod[OF a(1)] coprime_mod[OF b(1)])}
```
```   528       moreover
```
```   529       {fix x y assume x: "x \<in> ?S a" and y: "y \<in> ?S b"
```
```   530         hence x': "coprime x a" "x < a" and y': "coprime y b" "y < b" by simp_all
```
```   531         from chinese_remainder_coprime_unique[OF ab a(1) b(1) x'(1) y'(1)]
```
```   532         obtain z where z: "coprime z (a * b)" "z < a * b" "[z = x] (mod a)"
```
```   533           "[z = y] (mod b)" by blast
```
```   534         hence "(x,y) \<in> ?f ` (?S (a*b))"
```
```   535           using y'(2) mod_less_divisor[of b y] x'(2) mod_less_divisor[of a x]
```
```   536           by (auto simp add: image_iff modeq_def)}
```
```   537       ultimately show ?thesis by auto
```
```   538     qed
```
```   539     have finj: "inj_on ?f (?S (a*b))"
```
```   540       unfolding inj_on_def
```
```   541     proof(clarify)
```
```   542       fix x y assume H: "coprime x (a * b)" "x < a * b" "coprime y (a * b)"
```
```   543         "y < a * b" "x mod a = y mod a" "x mod b = y mod b"
```
```   544       hence cp: "coprime x a" "coprime x b" "coprime y a" "coprime y b"
```
```   545         by (simp_all add: coprime_mul_eq)
```
```   546       from chinese_remainder_coprime_unique[OF ab a(1) b(1) cp(3,4)] H
```
```   547       show "x = y" unfolding modeq_def by blast
```
```   548     qed
```
```   549     from card_image[OF finj, unfolded eq] have ?thesis
```
```   550       unfolding phi_alt by simp }
```
```   551   ultimately show ?thesis by auto
```
```   552 qed
```
```   553
```
```   554 (* Fermat's Little theorem / Fermat-Euler theorem.                           *)
```
```   555
```
```   556
```
```   557 lemma nproduct_mod:
```
```   558   assumes fS: "finite S" and n0: "n \<noteq> 0"
```
```   559   shows "[setprod (\<lambda>m. a(m) mod n) S = setprod a S] (mod n)"
```
```   560 proof-
```
```   561   have th1:"[1 = 1] (mod n)" by (simp add: modeq_def)
```
```   562   from cong_mult
```
```   563   have th3:"\<forall>x1 y1 x2 y2.
```
```   564     [x1 = x2] (mod n) \<and> [y1 = y2] (mod n) \<longrightarrow> [x1 * y1 = x2 * y2] (mod n)"
```
```   565     by blast
```
```   566   have th4:"\<forall>x\<in>S. [a x mod n = a x] (mod n)" by (simp add: modeq_def)
```
```   567   from fold_image_related[where h="(\<lambda>m. a(m) mod n)" and g=a, OF th1 th3 fS, OF th4] show ?thesis unfolding setprod_def by (simp add: fS)
```
```   568 qed
```
```   569
```
```   570 lemma nproduct_cmul:
```
```   571   assumes fS:"finite S"
```
```   572   shows "setprod (\<lambda>m. (c::'a::{comm_monoid_mult})* a(m)) S = c ^ (card S) * setprod a S"
```
```   573 unfolding setprod_timesf setprod_constant[OF fS, of c] ..
```
```   574
```
```   575 lemma coprime_nproduct:
```
```   576   assumes fS: "finite S" and Sn: "\<forall>x\<in>S. coprime n (a x)"
```
```   577   shows "coprime n (setprod a S)"
```
```   578   using fS unfolding setprod_def by (rule finite_subset_induct)
```
```   579     (insert Sn, auto simp add: coprime_mul)
```
```   580
```
```   581 lemma fermat_little: assumes an: "coprime a n"
```
```   582   shows "[a ^ (\<phi> n) = 1] (mod n)"
```
```   583 proof-
```
```   584   {assume "n=0" hence ?thesis by simp}
```
```   585   moreover
```
```   586   {assume "n=1" hence ?thesis by (simp add: modeq_def)}
```
```   587   moreover
```
```   588   {assume nz: "n \<noteq> 0" and n1: "n \<noteq> 1"
```
```   589     let ?S = "{m. coprime m n \<and> m < n}"
```
```   590     let ?P = "\<Prod> ?S"
```
```   591     have fS: "finite ?S" by simp
```
```   592     have cardfS: "\<phi> n = card ?S" unfolding phi_alt ..
```
```   593     {fix m assume m: "m \<in> ?S"
```
```   594       hence "coprime m n" by simp
```
```   595       with coprime_mul[of n a m] an have "coprime (a*m) n"
```
```   596         by (simp add: coprime_commute)}
```
```   597     hence Sn: "\<forall>m\<in> ?S. coprime (a*m) n " by blast
```
```   598     from coprime_nproduct[OF fS, of n "\<lambda>m. m"] have nP:"coprime ?P n"
```
```   599       by (simp add: coprime_commute)
```
```   600     have Paphi: "[?P*a^ (\<phi> n) = ?P*1] (mod n)"
```
```   601     proof-
```
```   602       let ?h = "\<lambda>m. m mod n"
```
```   603       {fix m assume mS: "m\<in> ?S"
```
```   604         hence "?h m \<in> ?S" by simp}
```
```   605       hence hS: "?h ` ?S = ?S"by (auto simp add: image_iff)
```
```   606       have "a\<noteq>0" using an n1 nz apply- apply (rule ccontr) by simp
```
```   607       hence inj: "inj_on (op * a) ?S" unfolding inj_on_def by simp
```
```   608
```
```   609       have eq0: "fold_image op * (?h \<circ> op * a) 1 {m. coprime m n \<and> m < n} =
```
```   610      fold_image op * (\<lambda>m. m) 1 {m. coprime m n \<and> m < n}"
```
```   611       proof (rule fold_image_eq_general[where h="?h o (op * a)"])
```
```   612         show "finite ?S" using fS .
```
```   613       next
```
```   614         {fix y assume yS: "y \<in> ?S" hence y: "coprime y n" "y < n" by simp_all
```
```   615           from cong_solve_unique[OF an nz, of y]
```
```   616           obtain x where x:"x < n" "[a * x = y] (mod n)" "\<forall>z. z < n \<and> [a * z = y] (mod n) \<longrightarrow> z=x" by blast
```
```   617           from cong_coprime[OF x(2)] y(1)
```
```   618           have xm: "coprime x n" by (simp add: coprime_mul_eq coprime_commute)
```
```   619           {fix z assume "z \<in> ?S" "(?h \<circ> op * a) z = y"
```
```   620             hence z: "coprime z n" "z < n" "(?h \<circ> op * a) z = y" by simp_all
```
```   621             from x(3)[rule_format, of z] z(2,3) have "z=x"
```
```   622               unfolding modeq_def mod_less[OF y(2)] by simp}
```
```   623           with xm x(1,2) have "\<exists>!x. x \<in> ?S \<and> (?h \<circ> op * a) x = y"
```
```   624             unfolding modeq_def mod_less[OF y(2)] by auto }
```
```   625         thus "\<forall>y\<in>{m. coprime m n \<and> m < n}.
```
```   626        \<exists>!x. x \<in> {m. coprime m n \<and> m < n} \<and> ((\<lambda>m. m mod n) \<circ> op * a) x = y" by blast
```
```   627       next
```
```   628         {fix x assume xS: "x\<in> ?S"
```
```   629           hence x: "coprime x n" "x < n" by simp_all
```
```   630           with an have "coprime (a*x) n"
```
```   631             by (simp add: coprime_mul_eq[of n a x] coprime_commute)
```
```   632           hence "?h (a*x) \<in> ?S" using nz
```
```   633             by (simp add: coprime_mod[OF nz] mod_less_divisor)}
```
```   634         thus " \<forall>x\<in>{m. coprime m n \<and> m < n}.
```
```   635        ((\<lambda>m. m mod n) \<circ> op * a) x \<in> {m. coprime m n \<and> m < n} \<and>
```
```   636        ((\<lambda>m. m mod n) \<circ> op * a) x = ((\<lambda>m. m mod n) \<circ> op * a) x" by simp
```
```   637       qed
```
```   638       from nproduct_mod[OF fS nz, of "op * a"]
```
```   639       have "[(setprod (op *a) ?S) = (setprod (?h o (op * a)) ?S)] (mod n)"
```
```   640         unfolding o_def
```
```   641         by (simp add: cong_commute)
```
```   642       also have "[setprod (?h o (op * a)) ?S = ?P ] (mod n)"
```
```   643         using eq0 fS an by (simp add: setprod_def modeq_def o_def)
```
```   644       finally show "[?P*a^ (\<phi> n) = ?P*1] (mod n)"
```
```   645         unfolding cardfS mult_commute[of ?P "a^ (card ?S)"]
```
```   646           nproduct_cmul[OF fS, symmetric] mult_1_right by simp
```
```   647     qed
```
```   648     from cong_mult_lcancel[OF nP Paphi] have ?thesis . }
```
```   649   ultimately show ?thesis by blast
```
```   650 qed
```
```   651
```
```   652 lemma fermat_little_prime: assumes p: "prime p" and ap: "coprime a p"
```
```   653   shows "[a^ (p - 1) = 1] (mod p)"
```
```   654   using fermat_little[OF ap] p[unfolded phi_prime[symmetric]]
```
```   655 by simp
```
```   656
```
```   657
```
```   658 (* Lucas's theorem.                                                          *)
```
```   659
```
```   660 lemma lucas_coprime_lemma:
```
```   661   assumes m: "m\<noteq>0" and am: "[a^m = 1] (mod n)"
```
```   662   shows "coprime a n"
```
```   663 proof-
```
```   664   {assume "n=1" hence ?thesis by simp}
```
```   665   moreover
```
```   666   {assume "n = 0" hence ?thesis using am m exp_eq_1[of a m] by simp}
```
```   667   moreover
```
```   668   {assume n: "n\<noteq>0" "n\<noteq>1"
```
```   669     from m obtain m' where m': "m = Suc m'" by (cases m, blast+)
```
```   670     {fix d
```
```   671       assume d: "d dvd a" "d dvd n"
```
```   672       from n have n1: "1 < n" by arith
```
```   673       from am mod_less[OF n1] have am1: "a^m mod n = 1" unfolding modeq_def by simp
```
```   674       from dvd_mult2[OF d(1), of "a^m'"] have dam:"d dvd a^m" by (simp add: m')
```
```   675       from dvd_mod_iff[OF d(2), of "a^m"] dam am1
```
```   676       have "d = 1" by simp }
```
```   677     hence ?thesis unfolding coprime by auto
```
```   678   }
```
```   679   ultimately show ?thesis by blast
```
```   680 qed
```
```   681
```
```   682 lemma lucas_weak:
```
```   683   assumes n: "n \<ge> 2" and an:"[a^(n - 1) = 1] (mod n)"
```
```   684   and nm: "\<forall>m. 0 <m \<and> m < n - 1 \<longrightarrow> \<not> [a^m = 1] (mod n)"
```
```   685   shows "prime n"
```
```   686 proof-
```
```   687   from n have n1: "n \<noteq> 1" "n\<noteq>0" "n - 1 \<noteq> 0" "n - 1 > 0" "n - 1 < n" by arith+
```
```   688   from lucas_coprime_lemma[OF n1(3) an] have can: "coprime a n" .
```
```   689   from fermat_little[OF can] have afn: "[a ^ \<phi> n = 1] (mod n)" .
```
```   690   {assume "\<phi> n \<noteq> n - 1"
```
```   691     with phi_limit_strong[OF n1(1)] phi_lowerbound_1[OF n]
```
```   692     have c:"\<phi> n > 0 \<and> \<phi> n < n - 1" by arith
```
```   693     from nm[rule_format, OF c] afn have False ..}
```
```   694   hence "\<phi> n = n - 1" by blast
```
```   695   with phi_prime[of n] n1(1,2) show ?thesis by simp
```
```   696 qed
```
```   697
```
```   698 lemma nat_exists_least_iff: "(\<exists>(n::nat). P n) \<longleftrightarrow> (\<exists>n. P n \<and> (\<forall>m < n. \<not> P m))"
```
```   699   (is "?lhs \<longleftrightarrow> ?rhs")
```
```   700 proof
```
```   701   assume ?rhs thus ?lhs by blast
```
```   702 next
```
```   703   assume H: ?lhs then obtain n where n: "P n" by blast
```
```   704   let ?x = "Least P"
```
```   705   {fix m assume m: "m < ?x"
```
```   706     from not_less_Least[OF m] have "\<not> P m" .}
```
```   707   with LeastI_ex[OF H] show ?rhs by blast
```
```   708 qed
```
```   709
```
```   710 lemma nat_exists_least_iff': "(\<exists>(n::nat). P n) \<longleftrightarrow> (P (Least P) \<and> (\<forall>m < (Least P). \<not> P m))"
```
```   711   (is "?lhs \<longleftrightarrow> ?rhs")
```
```   712 proof-
```
```   713   {assume ?rhs hence ?lhs by blast}
```
```   714   moreover
```
```   715   { assume H: ?lhs then obtain n where n: "P n" by blast
```
```   716     let ?x = "Least P"
```
```   717     {fix m assume m: "m < ?x"
```
```   718       from not_less_Least[OF m] have "\<not> P m" .}
```
```   719     with LeastI_ex[OF H] have ?rhs by blast}
```
```   720   ultimately show ?thesis by blast
```
```   721 qed
```
```   722
```
```   723 lemma power_mod: "((x::nat) mod m)^n mod m = x^n mod m"
```
```   724 proof(induct n)
```
```   725   case 0 thus ?case by simp
```
```   726 next
```
```   727   case (Suc n)
```
```   728   have "(x mod m)^(Suc n) mod m = ((x mod m) * (((x mod m) ^ n) mod m)) mod m"
```
```   729     by (simp add: mod_mult_right_eq[symmetric])
```
```   730   also have "\<dots> = ((x mod m) * (x^n mod m)) mod m" using Suc.hyps by simp
```
```   731   also have "\<dots> = x^(Suc n) mod m"
```
```   732     by (simp add: mod_mult_left_eq[symmetric] mod_mult_right_eq[symmetric])
```
```   733   finally show ?case .
```
```   734 qed
```
```   735
```
```   736 lemma lucas:
```
```   737   assumes n2: "n \<ge> 2" and an1: "[a^(n - 1) = 1] (mod n)"
```
```   738   and pn: "\<forall>p. prime p \<and> p dvd n - 1 \<longrightarrow> \<not> [a^((n - 1) div p) = 1] (mod n)"
```
```   739   shows "prime n"
```
```   740 proof-
```
```   741   from n2 have n01: "n\<noteq>0" "n\<noteq>1" "n - 1 \<noteq> 0" by arith+
```
```   742   from mod_less_divisor[of n 1] n01 have onen: "1 mod n = 1" by simp
```
```   743   from lucas_coprime_lemma[OF n01(3) an1] cong_coprime[OF an1]
```
```   744   have an: "coprime a n" "coprime (a^(n - 1)) n" by (simp_all add: coprime_commute)
```
```   745   {assume H0: "\<exists>m. 0 < m \<and> m < n - 1 \<and> [a ^ m = 1] (mod n)" (is "EX m. ?P m")
```
```   746     from H0[unfolded nat_exists_least_iff[of ?P]] obtain m where
```
```   747       m: "0 < m" "m < n - 1" "[a ^ m = 1] (mod n)" "\<forall>k <m. \<not>?P k" by blast
```
```   748     {assume nm1: "(n - 1) mod m > 0"
```
```   749       from mod_less_divisor[OF m(1)] have th0:"(n - 1) mod m < m" by blast
```
```   750       let ?y = "a^ ((n - 1) div m * m)"
```
```   751       note mdeq = mod_div_equality[of "(n - 1)" m]
```
```   752       from coprime_exp[OF an(1)[unfolded coprime_commute[of a n]],
```
```   753         of "(n - 1) div m * m"]
```
```   754       have yn: "coprime ?y n" by (simp add: coprime_commute)
```
```   755       have "?y mod n = (a^m)^((n - 1) div m) mod n"
```
```   756         by (simp add: algebra_simps power_mult)
```
```   757       also have "\<dots> = (a^m mod n)^((n - 1) div m) mod n"
```
```   758         using power_mod[of "a^m" n "(n - 1) div m"] by simp
```
```   759       also have "\<dots> = 1" using m(3)[unfolded modeq_def onen] onen
```
```   760         by (simp add: power_Suc0)
```
```   761       finally have th3: "?y mod n = 1"  .
```
```   762       have th2: "[?y * a ^ ((n - 1) mod m) = ?y* 1] (mod n)"
```
```   763         using an1[unfolded modeq_def onen] onen
```
```   764           mod_div_equality[of "(n - 1)" m, symmetric]
```
```   765         by (simp add:power_add[symmetric] modeq_def th3 del: One_nat_def)
```
```   766       from cong_mult_lcancel[of ?y n "a^((n - 1) mod m)" 1, OF yn th2]
```
```   767       have th1: "[a ^ ((n - 1) mod m) = 1] (mod n)"  .
```
```   768       from m(4)[rule_format, OF th0] nm1
```
```   769         less_trans[OF mod_less_divisor[OF m(1), of "n - 1"] m(2)] th1
```
```   770       have False by blast }
```
```   771     hence "(n - 1) mod m = 0" by auto
```
```   772     then have mn: "m dvd n - 1" by presburger
```
```   773     then obtain r where r: "n - 1 = m*r" unfolding dvd_def by blast
```
```   774     from n01 r m(2) have r01: "r\<noteq>0" "r\<noteq>1" by - (rule ccontr, simp)+
```
```   775     from prime_factor[OF r01(2)] obtain p where p: "prime p" "p dvd r" by blast
```
```   776     hence th: "prime p \<and> p dvd n - 1" unfolding r by (auto intro: dvd_mult)
```
```   777     have "(a ^ ((n - 1) div p)) mod n = (a^(m*r div p)) mod n" using r
```
```   778       by (simp add: power_mult)
```
```   779     also have "\<dots> = (a^(m*(r div p))) mod n" using div_mult1_eq[of m r p] p(2)[unfolded dvd_eq_mod_eq_0] by simp
```
```   780     also have "\<dots> = ((a^m)^(r div p)) mod n" by (simp add: power_mult)
```
```   781     also have "\<dots> = ((a^m mod n)^(r div p)) mod n" using power_mod[of "a^m" "n" "r div p" ] ..
```
```   782     also have "\<dots> = 1" using m(3) onen by (simp add: modeq_def power_Suc0)
```
```   783     finally have "[(a ^ ((n - 1) div p))= 1] (mod n)"
```
```   784       using onen by (simp add: modeq_def)
```
```   785     with pn[rule_format, OF th] have False by blast}
```
```   786   hence th: "\<forall>m. 0 < m \<and> m < n - 1 \<longrightarrow> \<not> [a ^ m = 1] (mod n)" by blast
```
```   787   from lucas_weak[OF n2 an1 th] show ?thesis .
```
```   788 qed
```
```   789
```
```   790 (* Definition of the order of a number mod n (0 in non-coprime case).        *)
```
```   791
```
```   792 definition "ord n a = (if coprime n a then Least (\<lambda>d. d > 0 \<and> [a ^d = 1] (mod n)) else 0)"
```
```   793
```
```   794 (* This has the expected properties.                                         *)
```
```   795
```
```   796 lemma coprime_ord:
```
```   797   assumes na: "coprime n a"
```
```   798   shows "ord n a > 0 \<and> [a ^(ord n a) = 1] (mod n) \<and> (\<forall>m. 0 < m \<and> m < ord n a \<longrightarrow> \<not> [a^ m = 1] (mod n))"
```
```   799 proof-
```
```   800   let ?P = "\<lambda>d. 0 < d \<and> [a ^ d = 1] (mod n)"
```
```   801   from euclid[of a] obtain p where p: "prime p" "a < p" by blast
```
```   802   from na have o: "ord n a = Least ?P" by (simp add: ord_def)
```
```   803   {assume "n=0 \<or> n=1" with na have "\<exists>m>0. ?P m" apply auto apply (rule exI[where x=1]) by (simp  add: modeq_def)}
```
```   804   moreover
```
```   805   {assume "n\<noteq>0 \<and> n\<noteq>1" hence n2:"n \<ge> 2" by arith
```
```   806     from na have na': "coprime a n" by (simp add: coprime_commute)
```
```   807     from phi_lowerbound_1[OF n2] fermat_little[OF na']
```
```   808     have ex: "\<exists>m>0. ?P m" by - (rule exI[where x="\<phi> n"], auto) }
```
```   809   ultimately have ex: "\<exists>m>0. ?P m" by blast
```
```   810   from nat_exists_least_iff'[of ?P] ex na show ?thesis
```
```   811     unfolding o[symmetric] by auto
```
```   812 qed
```
```   813 (* With the special value 0 for non-coprime case, it's more convenient.      *)
```
```   814 lemma ord_works:
```
```   815  "[a ^ (ord n a) = 1] (mod n) \<and> (\<forall>m. 0 < m \<and> m < ord n a \<longrightarrow> ~[a^ m = 1] (mod n))"
```
```   816 apply (cases "coprime n a")
```
```   817 using coprime_ord[of n a]
```
```   818 by (blast, simp add: ord_def modeq_def)
```
```   819
```
```   820 lemma ord: "[a^(ord n a) = 1] (mod n)" using ord_works by blast
```
```   821 lemma ord_minimal: "0 < m \<Longrightarrow> m < ord n a \<Longrightarrow> ~[a^m = 1] (mod n)"
```
```   822   using ord_works by blast
```
```   823 lemma ord_eq_0: "ord n a = 0 \<longleftrightarrow> ~coprime n a"
```
```   824 by (cases "coprime n a", simp add: neq0_conv coprime_ord, simp add: neq0_conv ord_def)
```
```   825
```
```   826 lemma ord_divides:
```
```   827  "[a ^ d = 1] (mod n) \<longleftrightarrow> ord n a dvd d" (is "?lhs \<longleftrightarrow> ?rhs")
```
```   828 proof
```
```   829   assume rh: ?rhs
```
```   830   then obtain k where "d = ord n a * k" unfolding dvd_def by blast
```
```   831   hence "[a ^ d = (a ^ (ord n a) mod n)^k] (mod n)"
```
```   832     by (simp add : modeq_def power_mult power_mod)
```
```   833   also have "[(a ^ (ord n a) mod n)^k = 1] (mod n)"
```
```   834     using ord[of a n, unfolded modeq_def]
```
```   835     by (simp add: modeq_def power_mod power_Suc0)
```
```   836   finally  show ?lhs .
```
```   837 next
```
```   838   assume lh: ?lhs
```
```   839   { assume H: "\<not> coprime n a"
```
```   840     hence o: "ord n a = 0" by (simp add: ord_def)
```
```   841     {assume d: "d=0" with o H have ?rhs by (simp add: modeq_def)}
```
```   842     moreover
```
```   843     {assume d0: "d\<noteq>0" then obtain d' where d': "d = Suc d'" by (cases d, auto)
```
```   844       from H[unfolded coprime]
```
```   845       obtain p where p: "p dvd n" "p dvd a" "p \<noteq> 1" by auto
```
```   846       from lh[unfolded nat_mod]
```
```   847       obtain q1 q2 where q12:"a ^ d + n * q1 = 1 + n * q2" by blast
```
```   848       hence "a ^ d + n * q1 - n * q2 = 1" by simp
```
```   849       with dvd_diff_nat [OF dvd_add [OF divides_rexp[OF p(2), of d'] dvd_mult2[OF p(1), of q1]] dvd_mult2[OF p(1), of q2]] d' have "p dvd 1" by simp
```
```   850       with p(3) have False by simp
```
```   851       hence ?rhs ..}
```
```   852     ultimately have ?rhs by blast}
```
```   853   moreover
```
```   854   {assume H: "coprime n a"
```
```   855     let ?o = "ord n a"
```
```   856     let ?q = "d div ord n a"
```
```   857     let ?r = "d mod ord n a"
```
```   858     from cong_exp[OF ord[of a n], of ?q]
```
```   859     have eqo: "[(a^?o)^?q = 1] (mod n)"  by (simp add: modeq_def power_Suc0)
```
```   860     from H have onz: "?o \<noteq> 0" by (simp add: ord_eq_0)
```
```   861     hence op: "?o > 0" by simp
```
```   862     from mod_div_equality[of d "ord n a"] lh
```
```   863     have "[a^(?o*?q + ?r) = 1] (mod n)" by (simp add: modeq_def mult_commute)
```
```   864     hence "[(a^?o)^?q * (a^?r) = 1] (mod n)"
```
```   865       by (simp add: modeq_def power_mult[symmetric] power_add[symmetric])
```
```   866     hence th: "[a^?r = 1] (mod n)"
```
```   867       using eqo mod_mult_left_eq[of "(a^?o)^?q" "a^?r" n]
```
```   868       apply (simp add: modeq_def del: One_nat_def)
```
```   869       by (simp add: mod_mult_left_eq[symmetric])
```
```   870     {assume r: "?r = 0" hence ?rhs by (simp add: dvd_eq_mod_eq_0)}
```
```   871     moreover
```
```   872     {assume r: "?r \<noteq> 0"
```
```   873       with mod_less_divisor[OF op, of d] have r0o:"?r >0 \<and> ?r < ?o" by simp
```
```   874       from conjunct2[OF ord_works[of a n], rule_format, OF r0o] th
```
```   875       have ?rhs by blast}
```
```   876     ultimately have ?rhs by blast}
```
```   877   ultimately  show ?rhs by blast
```
```   878 qed
```
```   879
```
```   880 lemma order_divides_phi: "coprime n a \<Longrightarrow> ord n a dvd \<phi> n"
```
```   881 using ord_divides fermat_little coprime_commute by simp
```
```   882 lemma order_divides_expdiff:
```
```   883   assumes na: "coprime n a"
```
```   884   shows "[a^d = a^e] (mod n) \<longleftrightarrow> [d = e] (mod (ord n a))"
```
```   885 proof-
```
```   886   {fix n a d e
```
```   887     assume na: "coprime n a" and ed: "(e::nat) \<le> d"
```
```   888     hence "\<exists>c. d = e + c" by arith
```
```   889     then obtain c where c: "d = e + c" by arith
```
```   890     from na have an: "coprime a n" by (simp add: coprime_commute)
```
```   891     from coprime_exp[OF na, of e]
```
```   892     have aen: "coprime (a^e) n" by (simp add: coprime_commute)
```
```   893     from coprime_exp[OF na, of c]
```
```   894     have acn: "coprime (a^c) n" by (simp add: coprime_commute)
```
```   895     have "[a^d = a^e] (mod n) \<longleftrightarrow> [a^(e + c) = a^(e + 0)] (mod n)"
```
```   896       using c by simp
```
```   897     also have "\<dots> \<longleftrightarrow> [a^e* a^c = a^e *a^0] (mod n)" by (simp add: power_add)
```
```   898     also have  "\<dots> \<longleftrightarrow> [a ^ c = 1] (mod n)"
```
```   899       using cong_mult_lcancel_eq[OF aen, of "a^c" "a^0"] by simp
```
```   900     also  have "\<dots> \<longleftrightarrow> ord n a dvd c" by (simp only: ord_divides)
```
```   901     also have "\<dots> \<longleftrightarrow> [e + c = e + 0] (mod ord n a)"
```
```   902       using cong_add_lcancel_eq[of e c 0 "ord n a", simplified cong_0_divides]
```
```   903       by simp
```
```   904     finally have "[a^d = a^e] (mod n) \<longleftrightarrow> [d = e] (mod (ord n a))"
```
```   905       using c by simp }
```
```   906   note th = this
```
```   907   have "e \<le> d \<or> d \<le> e" by arith
```
```   908   moreover
```
```   909   {assume ed: "e \<le> d" from th[OF na ed] have ?thesis .}
```
```   910   moreover
```
```   911   {assume de: "d \<le> e"
```
```   912     from th[OF na de] have ?thesis by (simp add: cong_commute) }
```
```   913   ultimately show ?thesis by blast
```
```   914 qed
```
```   915
```
```   916 (* Another trivial primality characterization.                               *)
```
```   917
```
```   918 lemma prime_prime_factor:
```
```   919   "prime n \<longleftrightarrow> n \<noteq> 1\<and> (\<forall>p. prime p \<and> p dvd n \<longrightarrow> p = n)"
```
```   920 proof-
```
```   921   {assume n: "n=0 \<or> n=1" hence ?thesis using prime_0 two_is_prime by auto}
```
```   922   moreover
```
```   923   {assume n: "n\<noteq>0" "n\<noteq>1"
```
```   924     {assume pn: "prime n"
```
```   925
```
```   926       from pn[unfolded prime_def] have "\<forall>p. prime p \<and> p dvd n \<longrightarrow> p = n"
```
```   927         using n
```
```   928         apply (cases "n = 0 \<or> n=1",simp)
```
```   929         by (clarsimp, erule_tac x="p" in allE, auto)}
```
```   930     moreover
```
```   931     {assume H: "\<forall>p. prime p \<and> p dvd n \<longrightarrow> p = n"
```
```   932       from n have n1: "n > 1" by arith
```
```   933       {fix m assume m: "m dvd n" "m\<noteq>1"
```
```   934         from prime_factor[OF m(2)] obtain p where
```
```   935           p: "prime p" "p dvd m" by blast
```
```   936         from dvd_trans[OF p(2) m(1)] p(1) H have "p = n" by blast
```
```   937         with p(2) have "n dvd m"  by simp
```
```   938         hence "m=n"  using dvd_antisym[OF m(1)] by simp }
```
```   939       with n1 have "prime n"  unfolding prime_def by auto }
```
```   940     ultimately have ?thesis using n by blast}
```
```   941   ultimately       show ?thesis by auto
```
```   942 qed
```
```   943
```
```   944 lemma prime_divisor_sqrt:
```
```   945   "prime n \<longleftrightarrow> n \<noteq> 1 \<and> (\<forall>d. d dvd n \<and> d^2 \<le> n \<longrightarrow> d = 1)"
```
```   946 proof-
```
```   947   {assume "n=0 \<or> n=1" hence ?thesis using prime_0 prime_1
```
```   948     by (auto simp add: nat_power_eq_0_iff)}
```
```   949   moreover
```
```   950   {assume n: "n\<noteq>0" "n\<noteq>1"
```
```   951     hence np: "n > 1" by arith
```
```   952     {fix d assume d: "d dvd n" "d^2 \<le> n" and H: "\<forall>m. m dvd n \<longrightarrow> m=1 \<or> m=n"
```
```   953       from H d have d1n: "d = 1 \<or> d=n" by blast
```
```   954       {assume dn: "d=n"
```
```   955         have "n^2 > n*1" using n
```
```   956           by (simp add: power2_eq_square mult_less_cancel1)
```
```   957         with dn d(2) have "d=1" by simp}
```
```   958       with d1n have "d = 1" by blast  }
```
```   959     moreover
```
```   960     {fix d assume d: "d dvd n" and H: "\<forall>d'. d' dvd n \<and> d'^2 \<le> n \<longrightarrow> d' = 1"
```
```   961       from d n have "d \<noteq> 0" apply - apply (rule ccontr) by simp
```
```   962       hence dp: "d > 0" by simp
```
```   963       from d[unfolded dvd_def] obtain e where e: "n= d*e" by blast
```
```   964       from n dp e have ep:"e > 0" by simp
```
```   965       have "d^2 \<le> n \<or> e^2 \<le> n" using dp ep
```
```   966         by (auto simp add: e power2_eq_square mult_le_cancel_left)
```
```   967       moreover
```
```   968       {assume h: "d^2 \<le> n"
```
```   969         from H[rule_format, of d] h d have "d = 1" by blast}
```
```   970       moreover
```
```   971       {assume h: "e^2 \<le> n"
```
```   972         from e have "e dvd n" unfolding dvd_def by (simp add: mult_commute)
```
```   973         with H[rule_format, of e] h have "e=1" by simp
```
```   974         with e have "d = n" by simp}
```
```   975       ultimately have "d=1 \<or> d=n"  by blast}
```
```   976     ultimately have ?thesis unfolding prime_def using np n(2) by blast}
```
```   977   ultimately show ?thesis by auto
```
```   978 qed
```
```   979 lemma prime_prime_factor_sqrt:
```
```   980   "prime n \<longleftrightarrow> n \<noteq> 0 \<and> n \<noteq> 1 \<and> \<not> (\<exists>p. prime p \<and> p dvd n \<and> p^2 \<le> n)"
```
```   981   (is "?lhs \<longleftrightarrow>?rhs")
```
```   982 proof-
```
```   983   {assume "n=0 \<or> n=1" hence ?thesis using prime_0 prime_1 by auto}
```
```   984   moreover
```
```   985   {assume n: "n\<noteq>0" "n\<noteq>1"
```
```   986     {assume H: ?lhs
```
```   987       from H[unfolded prime_divisor_sqrt] n
```
```   988       have ?rhs  apply clarsimp by (erule_tac x="p" in allE, simp add: prime_1)
```
```   989     }
```
```   990     moreover
```
```   991     {assume H: ?rhs
```
```   992       {fix d assume d: "d dvd n" "d^2 \<le> n" "d\<noteq>1"
```
```   993         from prime_factor[OF d(3)]
```
```   994         obtain p where p: "prime p" "p dvd d" by blast
```
```   995         from n have np: "n > 0" by arith
```
```   996         from d(1) n have "d \<noteq> 0" by - (rule ccontr, auto)
```
```   997         hence dp: "d > 0" by arith
```
```   998         from mult_mono[OF dvd_imp_le[OF p(2) dp] dvd_imp_le[OF p(2) dp]] d(2)
```
```   999         have "p^2 \<le> n" unfolding power2_eq_square by arith
```
```  1000         with H n p(1) dvd_trans[OF p(2) d(1)] have False  by blast}
```
```  1001       with n prime_divisor_sqrt  have ?lhs by auto}
```
```  1002     ultimately have ?thesis by blast }
```
```  1003   ultimately show ?thesis by (cases "n=0 \<or> n=1", auto)
```
```  1004 qed
```
```  1005 (* Pocklington theorem. *)
```
```  1006
```
```  1007 lemma pocklington_lemma:
```
```  1008   assumes n: "n \<ge> 2" and nqr: "n - 1 = q*r" and an: "[a^ (n - 1) = 1] (mod n)"
```
```  1009   and aq:"\<forall>p. prime p \<and> p dvd q \<longrightarrow> coprime (a^ ((n - 1) div p) - 1) n"
```
```  1010   and pp: "prime p" and pn: "p dvd n"
```
```  1011   shows "[p = 1] (mod q)"
```
```  1012 proof-
```
```  1013   from pp prime_0 prime_1 have p01: "p \<noteq> 0" "p \<noteq> 1" by - (rule ccontr, simp)+
```
```  1014   from cong_1_divides[OF an, unfolded nqr, unfolded dvd_def]
```
```  1015   obtain k where k: "a ^ (q * r) - 1 = n*k" by blast
```
```  1016   from pn[unfolded dvd_def] obtain l where l: "n = p*l" by blast
```
```  1017   {assume a0: "a = 0"
```
```  1018     hence "a^ (n - 1) = 0" using n by (simp add: power_0_left)
```
```  1019     with n an mod_less[of 1 n]  have False by (simp add: power_0_left modeq_def)}
```
```  1020   hence a0: "a\<noteq>0" ..
```
```  1021   from n nqr have aqr0: "a ^ (q * r) \<noteq> 0" using a0 by (simp add: neq0_conv)
```
```  1022   hence "(a ^ (q * r) - 1) + 1  = a ^ (q * r)" by simp
```
```  1023   with k l have "a ^ (q * r) = p*l*k + 1" by simp
```
```  1024   hence "a ^ (r * q) + p * 0 = 1 + p * (l*k)" by (simp add: mult_ac)
```
```  1025   hence odq: "ord p (a^r) dvd q"
```
```  1026     unfolding ord_divides[symmetric] power_mult[symmetric] nat_mod  by blast
```
```  1027   from odq[unfolded dvd_def] obtain d where d: "q = ord p (a^r) * d" by blast
```
```  1028   {assume d1: "d \<noteq> 1"
```
```  1029     from prime_factor[OF d1] obtain P where P: "prime P" "P dvd d" by blast
```
```  1030     from d dvd_mult[OF P(2), of "ord p (a^r)"] have Pq: "P dvd q" by simp
```
```  1031     from aq P(1) Pq have caP:"coprime (a^ ((n - 1) div P) - 1) n" by blast
```
```  1032     from Pq obtain s where s: "q = P*s" unfolding dvd_def by blast
```
```  1033     have P0: "P \<noteq> 0" using P(1) prime_0 by - (rule ccontr, simp)
```
```  1034     from P(2) obtain t where t: "d = P*t" unfolding dvd_def by blast
```
```  1035     from d s t P0  have s': "ord p (a^r) * t = s" by algebra
```
```  1036     have "ord p (a^r) * t*r = r * ord p (a^r) * t" by algebra
```
```  1037     hence exps: "a^(ord p (a^r) * t*r) = ((a ^ r) ^ ord p (a^r)) ^ t"
```
```  1038       by (simp only: power_mult)
```
```  1039     have "[((a ^ r) ^ ord p (a^r)) ^ t= 1^t] (mod p)"
```
```  1040       by (rule cong_exp, rule ord)
```
```  1041     then have th: "[((a ^ r) ^ ord p (a^r)) ^ t= 1] (mod p)"
```
```  1042       by (simp add: power_Suc0)
```
```  1043     from cong_1_divides[OF th] exps have pd0: "p dvd a^(ord p (a^r) * t*r) - 1" by simp
```
```  1044     from nqr s s' have "(n - 1) div P = ord p (a^r) * t*r" using P0 by simp
```
```  1045     with caP have "coprime (a^(ord p (a^r) * t*r) - 1) n" by simp
```
```  1046     with p01 pn pd0 have False unfolding coprime by auto}
```
```  1047   hence d1: "d = 1" by blast
```
```  1048   hence o: "ord p (a^r) = q" using d by simp
```
```  1049   from pp phi_prime[of p] have phip: " \<phi> p = p - 1" by simp
```
```  1050   {fix d assume d: "d dvd p" "d dvd a" "d \<noteq> 1"
```
```  1051     from pp[unfolded prime_def] d have dp: "d = p" by blast
```
```  1052     from n have n12:"Suc (n - 2) = n - 1" by arith
```
```  1053     with divides_rexp[OF d(2)[unfolded dp], of "n - 2"]
```
```  1054     have th0: "p dvd a ^ (n - 1)" by simp
```
```  1055     from n have n0: "n \<noteq> 0" by simp
```
```  1056     from d(2) an n12[symmetric] have a0: "a \<noteq> 0"
```
```  1057       by - (rule ccontr, simp add: modeq_def)
```
```  1058     have th1: "a^ (n - 1) \<noteq> 0" using n d(2) dp a0 by (auto simp add: neq0_conv)
```
```  1059     from coprime_minus1[OF th1, unfolded coprime]
```
```  1060       dvd_trans[OF pn cong_1_divides[OF an]] th0 d(3) dp
```
```  1061     have False by auto}
```
```  1062   hence cpa: "coprime p a" using coprime by auto
```
```  1063   from coprime_exp[OF cpa, of r] coprime_commute
```
```  1064   have arp: "coprime (a^r) p" by blast
```
```  1065   from fermat_little[OF arp, simplified ord_divides] o phip
```
```  1066   have "q dvd (p - 1)" by simp
```
```  1067   then obtain d where d:"p - 1 = q * d" unfolding dvd_def by blast
```
```  1068   from prime_0 pp have p0:"p \<noteq> 0" by -  (rule ccontr, auto)
```
```  1069   from p0 d have "p + q * 0 = 1 + q * d" by simp
```
```  1070   with nat_mod[of p 1 q, symmetric]
```
```  1071   show ?thesis by blast
```
```  1072 qed
```
```  1073
```
```  1074 lemma pocklington:
```
```  1075   assumes n: "n \<ge> 2" and nqr: "n - 1 = q*r" and sqr: "n \<le> q^2"
```
```  1076   and an: "[a^ (n - 1) = 1] (mod n)"
```
```  1077   and aq:"\<forall>p. prime p \<and> p dvd q \<longrightarrow> coprime (a^ ((n - 1) div p) - 1) n"
```
```  1078   shows "prime n"
```
```  1079 unfolding prime_prime_factor_sqrt[of n]
```
```  1080 proof-
```
```  1081   let ?ths = "n \<noteq> 0 \<and> n \<noteq> 1 \<and> \<not> (\<exists>p. prime p \<and> p dvd n \<and> p\<twosuperior> \<le> n)"
```
```  1082   from n have n01: "n\<noteq>0" "n\<noteq>1" by arith+
```
```  1083   {fix p assume p: "prime p" "p dvd n" "p^2 \<le> n"
```
```  1084     from p(3) sqr have "p^(Suc 1) \<le> q^(Suc 1)" by (simp add: power2_eq_square)
```
```  1085     hence pq: "p \<le> q" unfolding exp_mono_le .
```
```  1086     from pocklington_lemma[OF n nqr an aq p(1,2)]  cong_1_divides
```
```  1087     have th: "q dvd p - 1" by blast
```
```  1088     have "p - 1 \<noteq> 0"using prime_ge_2[OF p(1)] by arith
```
```  1089     with divides_ge[OF th] pq have False by arith }
```
```  1090   with n01 show ?ths by blast
```
```  1091 qed
```
```  1092
```
```  1093 (* Variant for application, to separate the exponentiation.                  *)
```
```  1094 lemma pocklington_alt:
```
```  1095   assumes n: "n \<ge> 2" and nqr: "n - 1 = q*r" and sqr: "n \<le> q^2"
```
```  1096   and an: "[a^ (n - 1) = 1] (mod n)"
```
```  1097   and aq:"\<forall>p. prime p \<and> p dvd q \<longrightarrow> (\<exists>b. [a^((n - 1) div p) = b] (mod n) \<and> coprime (b - 1) n)"
```
```  1098   shows "prime n"
```
```  1099 proof-
```
```  1100   {fix p assume p: "prime p" "p dvd q"
```
```  1101     from aq[rule_format] p obtain b where
```
```  1102       b: "[a^((n - 1) div p) = b] (mod n)" "coprime (b - 1) n" by blast
```
```  1103     {assume a0: "a=0"
```
```  1104       from n an have "[0 = 1] (mod n)" unfolding a0 power_0_left by auto
```
```  1105       hence False using n by (simp add: modeq_def dvd_eq_mod_eq_0[symmetric])}
```
```  1106     hence a0: "a\<noteq> 0" ..
```
```  1107     hence a1: "a \<ge> 1" by arith
```
```  1108     from one_le_power[OF a1] have ath: "1 \<le> a ^ ((n - 1) div p)" .
```
```  1109     {assume b0: "b = 0"
```
```  1110       from p(2) nqr have "(n - 1) mod p = 0"
```
```  1111         apply (simp only: dvd_eq_mod_eq_0[symmetric]) by (rule dvd_mult2, simp)
```
```  1112       with mod_div_equality[of "n - 1" p]
```
```  1113       have "(n - 1) div p * p= n - 1" by auto
```
```  1114       hence eq: "(a^((n - 1) div p))^p = a^(n - 1)"
```
```  1115         by (simp only: power_mult[symmetric])
```
```  1116       from prime_ge_2[OF p(1)] have pS: "Suc (p - 1) = p" by arith
```
```  1117       from b(1) have d: "n dvd a^((n - 1) div p)" unfolding b0 cong_0_divides .
```
```  1118       from divides_rexp[OF d, of "p - 1"] pS eq cong_divides[OF an] n
```
```  1119       have False by simp}
```
```  1120     then have b0: "b \<noteq> 0" ..
```
```  1121     hence b1: "b \<ge> 1" by arith
```
```  1122     from cong_coprime[OF cong_sub[OF b(1) cong_refl[of 1] ath b1]] b(2) nqr
```
```  1123     have "coprime (a ^ ((n - 1) div p) - 1) n" by (simp add: coprime_commute)}
```
```  1124   hence th: "\<forall>p. prime p \<and> p dvd q \<longrightarrow> coprime (a ^ ((n - 1) div p) - 1) n "
```
```  1125     by blast
```
```  1126   from pocklington[OF n nqr sqr an th] show ?thesis .
```
```  1127 qed
```
```  1128
```
```  1129 (* Prime factorizations.                                                     *)
```
```  1130
```
```  1131 definition "primefact ps n = (foldr op * ps  1 = n \<and> (\<forall>p\<in> set ps. prime p))"
```
```  1132
```
```  1133 lemma primefact: assumes n: "n \<noteq> 0"
```
```  1134   shows "\<exists>ps. primefact ps n"
```
```  1135 using n
```
```  1136 proof(induct n rule: nat_less_induct)
```
```  1137   fix n assume H: "\<forall>m<n. m \<noteq> 0 \<longrightarrow> (\<exists>ps. primefact ps m)" and n: "n\<noteq>0"
```
```  1138   let ?ths = "\<exists>ps. primefact ps n"
```
```  1139   {assume "n = 1"
```
```  1140     hence "primefact [] n" by (simp add: primefact_def)
```
```  1141     hence ?ths by blast }
```
```  1142   moreover
```
```  1143   {assume n1: "n \<noteq> 1"
```
```  1144     with n have n2: "n \<ge> 2" by arith
```
```  1145     from prime_factor[OF n1] obtain p where p: "prime p" "p dvd n" by blast
```
```  1146     from p(2) obtain m where m: "n = p*m" unfolding dvd_def by blast
```
```  1147     from n m have m0: "m > 0" "m\<noteq>0" by auto
```
```  1148     from prime_ge_2[OF p(1)] have "1 < p" by arith
```
```  1149     with m0 m have mn: "m < n" by auto
```
```  1150     from H[rule_format, OF mn m0(2)] obtain ps where ps: "primefact ps m" ..
```
```  1151     from ps m p(1) have "primefact (p#ps) n" by (simp add: primefact_def)
```
```  1152     hence ?ths by blast}
```
```  1153   ultimately show ?ths by blast
```
```  1154 qed
```
```  1155
```
```  1156 lemma primefact_contains:
```
```  1157   assumes pf: "primefact ps n" and p: "prime p" and pn: "p dvd n"
```
```  1158   shows "p \<in> set ps"
```
```  1159   using pf p pn
```
```  1160 proof(induct ps arbitrary: p n)
```
```  1161   case Nil thus ?case by (auto simp add: primefact_def)
```
```  1162 next
```
```  1163   case (Cons q qs p n)
```
```  1164   from Cons.prems[unfolded primefact_def]
```
```  1165   have q: "prime q" "q * foldr op * qs 1 = n" "\<forall>p \<in>set qs. prime p"  and p: "prime p" "p dvd q * foldr op * qs 1" by simp_all
```
```  1166   {assume "p dvd q"
```
```  1167     with p(1) q(1) have "p = q" unfolding prime_def by auto
```
```  1168     hence ?case by simp}
```
```  1169   moreover
```
```  1170   { assume h: "p dvd foldr op * qs 1"
```
```  1171     from q(3) have pqs: "primefact qs (foldr op * qs 1)"
```
```  1172       by (simp add: primefact_def)
```
```  1173     from Cons.hyps[OF pqs p(1) h] have ?case by simp}
```
```  1174   ultimately show ?case using prime_divprod[OF p] by blast
```
```  1175 qed
```
```  1176
```
```  1177 lemma primefact_variant: "primefact ps n \<longleftrightarrow> foldr op * ps 1 = n \<and> list_all prime ps" by (auto simp add: primefact_def list_all_iff)
```
```  1178
```
```  1179 (* Variant of Lucas theorem.                                                 *)
```
```  1180
```
```  1181 lemma lucas_primefact:
```
```  1182   assumes n: "n \<ge> 2" and an: "[a^(n - 1) = 1] (mod n)"
```
```  1183   and psn: "foldr op * ps 1 = n - 1"
```
```  1184   and psp: "list_all (\<lambda>p. prime p \<and> \<not> [a^((n - 1) div p) = 1] (mod n)) ps"
```
```  1185   shows "prime n"
```
```  1186 proof-
```
```  1187   {fix p assume p: "prime p" "p dvd n - 1" "[a ^ ((n - 1) div p) = 1] (mod n)"
```
```  1188     from psn psp have psn1: "primefact ps (n - 1)"
```
```  1189       by (auto simp add: list_all_iff primefact_variant)
```
```  1190     from p(3) primefact_contains[OF psn1 p(1,2)] psp
```
```  1191     have False by (induct ps, auto)}
```
```  1192   with lucas[OF n an] show ?thesis by blast
```
```  1193 qed
```
```  1194
```
```  1195 (* Variant of Pocklington theorem.                                           *)
```
```  1196
```
```  1197 lemma mod_le: assumes n: "n \<noteq> (0::nat)" shows "m mod n \<le> m"
```
```  1198 proof-
```
```  1199     from mod_div_equality[of m n]
```
```  1200     have "\<exists>x. x + m mod n = m" by blast
```
```  1201     then show ?thesis by auto
```
```  1202 qed
```
```  1203
```
```  1204
```
```  1205 lemma pocklington_primefact:
```
```  1206   assumes n: "n \<ge> 2" and qrn: "q*r = n - 1" and nq2: "n \<le> q^2"
```
```  1207   and arnb: "(a^r) mod n = b" and psq: "foldr op * ps 1 = q"
```
```  1208   and bqn: "(b^q) mod n = 1"
```
```  1209   and psp: "list_all (\<lambda>p. prime p \<and> coprime ((b^(q div p)) mod n - 1) n) ps"
```
```  1210   shows "prime n"
```
```  1211 proof-
```
```  1212   from bqn psp qrn
```
```  1213   have bqn: "a ^ (n - 1) mod n = 1"
```
```  1214     and psp: "list_all (\<lambda>p. prime p \<and> coprime (a^(r *(q div p)) mod n - 1) n) ps"  unfolding arnb[symmetric] power_mod
```
```  1215     by (simp_all add: power_mult[symmetric] algebra_simps)
```
```  1216   from n  have n0: "n > 0" by arith
```
```  1217   from mod_div_equality[of "a^(n - 1)" n]
```
```  1218     mod_less_divisor[OF n0, of "a^(n - 1)"]
```
```  1219   have an1: "[a ^ (n - 1) = 1] (mod n)"
```
```  1220     unfolding nat_mod bqn
```
```  1221     apply -
```
```  1222     apply (rule exI[where x="0"])
```
```  1223     apply (rule exI[where x="a^(n - 1) div n"])
```
```  1224     by (simp add: algebra_simps)
```
```  1225   {fix p assume p: "prime p" "p dvd q"
```
```  1226     from psp psq have pfpsq: "primefact ps q"
```
```  1227       by (auto simp add: primefact_variant list_all_iff)
```
```  1228     from psp primefact_contains[OF pfpsq p]
```
```  1229     have p': "coprime (a ^ (r * (q div p)) mod n - 1) n"
```
```  1230       by (simp add: list_all_iff)
```
```  1231     from prime_ge_2[OF p(1)] have p01: "p \<noteq> 0" "p \<noteq> 1" "p =Suc(p - 1)" by arith+
```
```  1232     from div_mult1_eq[of r q p] p(2)
```
```  1233     have eq1: "r* (q div p) = (n - 1) div p"
```
```  1234       unfolding qrn[symmetric] dvd_eq_mod_eq_0 by (simp add: mult_commute)
```
```  1235     have ath: "\<And>a (b::nat). a <= b \<Longrightarrow> a \<noteq> 0 ==> 1 <= a \<and> 1 <= b" by arith
```
```  1236     from n0 have n00: "n \<noteq> 0" by arith
```
```  1237     from mod_le[OF n00]
```
```  1238     have th10: "a ^ ((n - 1) div p) mod n \<le> a ^ ((n - 1) div p)" .
```
```  1239     {assume "a ^ ((n - 1) div p) mod n = 0"
```
```  1240       then obtain s where s: "a ^ ((n - 1) div p) = n*s"
```
```  1241         unfolding mod_eq_0_iff by blast
```
```  1242       hence eq0: "(a^((n - 1) div p))^p = (n*s)^p" by simp
```
```  1243       from qrn[symmetric] have qn1: "q dvd n - 1" unfolding dvd_def by auto
```
```  1244       from dvd_trans[OF p(2) qn1] div_mod_equality'[of "n - 1" p]
```
```  1245       have npp: "(n - 1) div p * p = n - 1" by (simp add: dvd_eq_mod_eq_0)
```
```  1246       with eq0 have "a^ (n - 1) = (n*s)^p"
```
```  1247         by (simp add: power_mult[symmetric])
```
```  1248       hence "1 = (n*s)^(Suc (p - 1)) mod n" using bqn p01 by simp
```
```  1249       also have "\<dots> = 0" by (simp add: mult_assoc)
```
```  1250       finally have False by simp }
```
```  1251       then have th11: "a ^ ((n - 1) div p) mod n \<noteq> 0" by auto
```
```  1252     have th1: "[a ^ ((n - 1) div p) mod n = a ^ ((n - 1) div p)] (mod n)"
```
```  1253       unfolding modeq_def by simp
```
```  1254     from cong_sub[OF th1 cong_refl[of 1]]  ath[OF th10 th11]
```
```  1255     have th: "[a ^ ((n - 1) div p) mod n - 1 = a ^ ((n - 1) div p) - 1] (mod n)"
```
```  1256       by blast
```
```  1257     from cong_coprime[OF th] p'[unfolded eq1]
```
```  1258     have "coprime (a ^ ((n - 1) div p) - 1) n" by (simp add: coprime_commute) }
```
```  1259   with pocklington[OF n qrn[symmetric] nq2 an1]
```
```  1260   show ?thesis by blast
```
```  1261 qed
```
```  1262
```
```  1263 end
```