src/HOL/Number_Theory/Residues.thy
 author eberlm Thu May 04 16:49:29 2017 +0200 (2017-05-04) changeset 65726 f5d64d094efe parent 65465 067210a08a22 child 65899 ab7d8c999531 permissions -rw-r--r--
More material on totient function
```     1 (*  Title:      HOL/Number_Theory/Residues.thy
```
```     2     Author:     Jeremy Avigad
```
```     3
```
```     4 An algebraic treatment of residue rings, and resulting proofs of
```
```     5 Euler's theorem and Wilson's theorem.
```
```     6 *)
```
```     7
```
```     8 section \<open>Residue rings\<close>
```
```     9
```
```    10 theory Residues
```
```    11 imports
```
```    12   Cong
```
```    13   "~~/src/HOL/Algebra/More_Group"
```
```    14   "~~/src/HOL/Algebra/More_Ring"
```
```    15   "~~/src/HOL/Algebra/More_Finite_Product"
```
```    16   "~~/src/HOL/Algebra/Multiplicative_Group"
```
```    17   Totient
```
```    18 begin
```
```    19
```
```    20 definition QuadRes :: "int \<Rightarrow> int \<Rightarrow> bool" where
```
```    21   "QuadRes p a = (\<exists>y. ([y^2 = a] (mod p)))"
```
```    22
```
```    23 definition Legendre :: "int \<Rightarrow> int \<Rightarrow> int" where
```
```    24   "Legendre a p = (if ([a = 0] (mod p)) then 0
```
```    25     else if QuadRes p a then 1
```
```    26     else -1)"
```
```    27
```
```    28 subsection \<open>A locale for residue rings\<close>
```
```    29
```
```    30 definition residue_ring :: "int \<Rightarrow> int ring"
```
```    31 where
```
```    32   "residue_ring m =
```
```    33     \<lparr>carrier = {0..m - 1},
```
```    34      monoid.mult = \<lambda>x y. (x * y) mod m,
```
```    35      one = 1,
```
```    36      zero = 0,
```
```    37      add = \<lambda>x y. (x + y) mod m\<rparr>"
```
```    38
```
```    39 locale residues =
```
```    40   fixes m :: int and R (structure)
```
```    41   assumes m_gt_one: "m > 1"
```
```    42   defines "R \<equiv> residue_ring m"
```
```    43 begin
```
```    44
```
```    45 lemma abelian_group: "abelian_group R"
```
```    46 proof -
```
```    47   have "\<exists>y\<in>{0..m - 1}. (x + y) mod m = 0" if "0 \<le> x" "x < m" for x
```
```    48   proof (cases "x = 0")
```
```    49     case True
```
```    50     with m_gt_one show ?thesis by simp
```
```    51   next
```
```    52     case False
```
```    53     then have "(x + (m - x)) mod m = 0"
```
```    54       by simp
```
```    55     with m_gt_one that show ?thesis
```
```    56       by (metis False atLeastAtMost_iff diff_ge_0_iff_ge diff_left_mono int_one_le_iff_zero_less less_le)
```
```    57   qed
```
```    58   with m_gt_one show ?thesis
```
```    59     by (fastforce simp add: R_def residue_ring_def mod_add_right_eq ac_simps  intro!: abelian_groupI)
```
```    60 qed
```
```    61
```
```    62 lemma comm_monoid: "comm_monoid R"
```
```    63   unfolding R_def residue_ring_def
```
```    64   apply (rule comm_monoidI)
```
```    65     using m_gt_one  apply auto
```
```    66   apply (metis mod_mult_right_eq mult.assoc mult.commute)
```
```    67   apply (metis mult.commute)
```
```    68   done
```
```    69
```
```    70 lemma cring: "cring R"
```
```    71   apply (intro cringI abelian_group comm_monoid)
```
```    72   unfolding R_def residue_ring_def
```
```    73   apply (auto simp add: comm_semiring_class.distrib mod_add_eq mod_mult_left_eq)
```
```    74   done
```
```    75
```
```    76 end
```
```    77
```
```    78 sublocale residues < cring
```
```    79   by (rule cring)
```
```    80
```
```    81
```
```    82 context residues
```
```    83 begin
```
```    84
```
```    85 text \<open>
```
```    86   These lemmas translate back and forth between internal and
```
```    87   external concepts.
```
```    88 \<close>
```
```    89
```
```    90 lemma res_carrier_eq: "carrier R = {0..m - 1}"
```
```    91   unfolding R_def residue_ring_def by auto
```
```    92
```
```    93 lemma res_add_eq: "x \<oplus> y = (x + y) mod m"
```
```    94   unfolding R_def residue_ring_def by auto
```
```    95
```
```    96 lemma res_mult_eq: "x \<otimes> y = (x * y) mod m"
```
```    97   unfolding R_def residue_ring_def by auto
```
```    98
```
```    99 lemma res_zero_eq: "\<zero> = 0"
```
```   100   unfolding R_def residue_ring_def by auto
```
```   101
```
```   102 lemma res_one_eq: "\<one> = 1"
```
```   103   unfolding R_def residue_ring_def units_of_def by auto
```
```   104
```
```   105 lemma res_units_eq: "Units R = {x. 0 < x \<and> x < m \<and> coprime x m}"
```
```   106   using m_gt_one
```
```   107   unfolding Units_def R_def residue_ring_def
```
```   108   apply auto
```
```   109   apply (subgoal_tac "x \<noteq> 0")
```
```   110   apply auto
```
```   111   apply (metis invertible_coprime_int)
```
```   112   apply (subst (asm) coprime_iff_invertible'_int)
```
```   113   apply (auto simp add: cong_int_def mult.commute)
```
```   114   done
```
```   115
```
```   116 lemma res_neg_eq: "\<ominus> x = (- x) mod m"
```
```   117   using m_gt_one unfolding R_def a_inv_def m_inv_def residue_ring_def
```
```   118   apply simp
```
```   119   apply (rule the_equality)
```
```   120   apply (simp add: mod_add_right_eq)
```
```   121   apply (simp add: add.commute mod_add_right_eq)
```
```   122   apply (metis add.right_neutral minus_add_cancel mod_add_right_eq mod_pos_pos_trivial)
```
```   123   done
```
```   124
```
```   125 lemma finite [iff]: "finite (carrier R)"
```
```   126   by (simp add: res_carrier_eq)
```
```   127
```
```   128 lemma finite_Units [iff]: "finite (Units R)"
```
```   129   by (simp add: finite_ring_finite_units)
```
```   130
```
```   131 text \<open>
```
```   132   The function \<open>a \<mapsto> a mod m\<close> maps the integers to the
```
```   133   residue classes. The following lemmas show that this mapping
```
```   134   respects addition and multiplication on the integers.
```
```   135 \<close>
```
```   136
```
```   137 lemma mod_in_carrier [iff]: "a mod m \<in> carrier R"
```
```   138   unfolding res_carrier_eq
```
```   139   using insert m_gt_one by auto
```
```   140
```
```   141 lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
```
```   142   unfolding R_def residue_ring_def
```
```   143   by (auto simp add: mod_simps)
```
```   144
```
```   145 lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
```
```   146   unfolding R_def residue_ring_def
```
```   147   by (auto simp add: mod_simps)
```
```   148
```
```   149 lemma zero_cong: "\<zero> = 0"
```
```   150   unfolding R_def residue_ring_def by auto
```
```   151
```
```   152 lemma one_cong: "\<one> = 1 mod m"
```
```   153   using m_gt_one unfolding R_def residue_ring_def by auto
```
```   154
```
```   155 (* FIXME revise algebra library to use 1? *)
```
```   156 lemma pow_cong: "(x mod m) (^) n = x^n mod m"
```
```   157   using m_gt_one
```
```   158   apply (induct n)
```
```   159   apply (auto simp add: nat_pow_def one_cong)
```
```   160   apply (metis mult.commute mult_cong)
```
```   161   done
```
```   162
```
```   163 lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
```
```   164   by (metis mod_minus_eq res_neg_eq)
```
```   165
```
```   166 lemma (in residues) prod_cong: "finite A \<Longrightarrow> (\<Otimes>i\<in>A. (f i) mod m) = (\<Prod>i\<in>A. f i) mod m"
```
```   167   by (induct set: finite) (auto simp: one_cong mult_cong)
```
```   168
```
```   169 lemma (in residues) sum_cong: "finite A \<Longrightarrow> (\<Oplus>i\<in>A. (f i) mod m) = (\<Sum>i\<in>A. f i) mod m"
```
```   170   by (induct set: finite) (auto simp: zero_cong add_cong)
```
```   171
```
```   172 lemma mod_in_res_units [simp]:
```
```   173   assumes "1 < m" and "coprime a m"
```
```   174   shows "a mod m \<in> Units R"
```
```   175 proof (cases "a mod m = 0")
```
```   176   case True with assms show ?thesis
```
```   177     by (auto simp add: res_units_eq gcd_red_int [symmetric])
```
```   178 next
```
```   179   case False
```
```   180   from assms have "0 < m" by simp
```
```   181   with pos_mod_sign [of m a] have "0 \<le> a mod m" .
```
```   182   with False have "0 < a mod m" by simp
```
```   183   with assms show ?thesis
```
```   184     by (auto simp add: res_units_eq gcd_red_int [symmetric] ac_simps)
```
```   185 qed
```
```   186
```
```   187 lemma res_eq_to_cong: "(a mod m) = (b mod m) \<longleftrightarrow> [a = b] (mod m)"
```
```   188   unfolding cong_int_def by auto
```
```   189
```
```   190
```
```   191 text \<open>Simplifying with these will translate a ring equation in R to a congruence.\<close>
```
```   192 lemmas res_to_cong_simps = add_cong mult_cong pow_cong one_cong
```
```   193     prod_cong sum_cong neg_cong res_eq_to_cong
```
```   194
```
```   195 text \<open>Other useful facts about the residue ring.\<close>
```
```   196 lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2"
```
```   197   apply (simp add: res_one_eq res_neg_eq)
```
```   198   apply (metis add.commute add_diff_cancel mod_mod_trivial one_add_one uminus_add_conv_diff
```
```   199     zero_neq_one zmod_zminus1_eq_if)
```
```   200   done
```
```   201
```
```   202 end
```
```   203
```
```   204
```
```   205 subsection \<open>Prime residues\<close>
```
```   206
```
```   207 locale residues_prime =
```
```   208   fixes p :: nat and R (structure)
```
```   209   assumes p_prime [intro]: "prime p"
```
```   210   defines "R \<equiv> residue_ring (int p)"
```
```   211
```
```   212 sublocale residues_prime < residues p
```
```   213   unfolding R_def residues_def
```
```   214   using p_prime apply auto
```
```   215   apply (metis (full_types) of_nat_1 of_nat_less_iff prime_gt_1_nat)
```
```   216   done
```
```   217
```
```   218 context residues_prime
```
```   219 begin
```
```   220
```
```   221 lemma is_field: "field R"
```
```   222 proof -
```
```   223   have "\<And>x. \<lbrakk>gcd x (int p) \<noteq> 1; 0 \<le> x; x < int p\<rbrakk> \<Longrightarrow> x = 0"
```
```   224     by (metis dual_order.order_iff_strict gcd.commute less_le_not_le p_prime prime_imp_coprime prime_nat_int_transfer zdvd_imp_le)
```
```   225   then show ?thesis
```
```   226   apply (intro cring.field_intro2 cring)
```
```   227   apply (auto simp add: res_carrier_eq res_one_eq res_zero_eq res_units_eq)
```
```   228     done
```
```   229 qed
```
```   230
```
```   231 lemma res_prime_units_eq: "Units R = {1..p - 1}"
```
```   232   apply (subst res_units_eq)
```
```   233   apply auto
```
```   234   apply (subst gcd.commute)
```
```   235   apply (auto simp add: p_prime prime_imp_coprime_int zdvd_not_zless)
```
```   236   done
```
```   237
```
```   238 end
```
```   239
```
```   240 sublocale residues_prime < field
```
```   241   by (rule is_field)
```
```   242
```
```   243
```
```   244 section \<open>Test cases: Euler's theorem and Wilson's theorem\<close>
```
```   245
```
```   246 subsection \<open>Euler's theorem\<close>
```
```   247
```
```   248 lemma (in residues) totient_eq:
```
```   249   "totient (nat m) = card (Units R)"
```
```   250   thm R_def
```
```   251 proof -
```
```   252   have *: "inj_on nat (Units R)"
```
```   253     by (rule inj_onI) (auto simp add: res_units_eq)
```
```   254   define m' where "m' = nat m"
```
```   255   from m_gt_one have m: "m = int m'" "m' > 1" by (simp_all add: m'_def)
```
```   256   from m have "x \<in> Units R \<longleftrightarrow> x \<in> int ` totatives m'" for x
```
```   257     unfolding res_units_eq
```
```   258     by (cases x; cases "x = m") (auto simp: totatives_def transfer_int_nat_gcd)
```
```   259   hence "Units R = int ` totatives m'" by blast
```
```   260   hence "totatives m' = nat ` Units R" by (simp add: image_image)
```
```   261   then have "card (totatives (nat m)) = card (nat ` Units R)"
```
```   262     by (simp add: m'_def)
```
```   263   also have "\<dots> = card (Units R)"
```
```   264     using * card_image [of nat "Units R"] by auto
```
```   265   finally show ?thesis by (simp add: totient_def)
```
```   266 qed
```
```   267
```
```   268 lemma (in residues_prime) totient_eq: "totient p = p - 1"
```
```   269   using totient_eq by (simp add: res_prime_units_eq)
```
```   270
```
```   271 lemma (in residues) euler_theorem:
```
```   272   assumes "coprime a m"
```
```   273   shows "[a ^ totient (nat m) = 1] (mod m)"
```
```   274 proof -
```
```   275   have "a ^ totient (nat m) mod m = 1 mod m"
```
```   276     by (metis assms finite_Units m_gt_one mod_in_res_units one_cong totient_eq pow_cong units_power_order_eq_one)
```
```   277   then show ?thesis
```
```   278     using res_eq_to_cong by blast
```
```   279 qed
```
```   280
```
```   281 lemma euler_theorem:
```
```   282   fixes a m :: nat
```
```   283   assumes "coprime a m"
```
```   284   shows "[a ^ totient m = 1] (mod m)"
```
```   285 proof (cases "m = 0 | m = 1")
```
```   286   case True
```
```   287   then show ?thesis by auto
```
```   288 next
```
```   289   case False
```
```   290   with assms show ?thesis
```
```   291     using residues.euler_theorem [of "int m" "int a"] transfer_int_nat_cong
```
```   292     by (auto simp add: residues_def transfer_int_nat_gcd(1)) force
```
```   293 qed
```
```   294
```
```   295 lemma fermat_theorem:
```
```   296   fixes p a :: nat
```
```   297   assumes "prime p" and "\<not> p dvd a"
```
```   298   shows "[a ^ (p - 1) = 1] (mod p)"
```
```   299 proof -
```
```   300   from assms prime_imp_coprime [of p a] have "coprime a p"
```
```   301     by (auto simp add: ac_simps)
```
```   302   then have "[a ^ totient p = 1] (mod p)"
```
```   303      by (rule euler_theorem)
```
```   304   also have "totient p = p - 1"
```
```   305     by (rule totient_prime) (rule assms)
```
```   306   finally show ?thesis .
```
```   307 qed
```
```   308
```
```   309
```
```   310 subsection \<open>Wilson's theorem\<close>
```
```   311
```
```   312 lemma (in field) inv_pair_lemma: "x \<in> Units R \<Longrightarrow> y \<in> Units R \<Longrightarrow>
```
```   313     {x, inv x} \<noteq> {y, inv y} \<Longrightarrow> {x, inv x} \<inter> {y, inv y} = {}"
```
```   314   apply auto
```
```   315   apply (metis Units_inv_inv)+
```
```   316   done
```
```   317
```
```   318 lemma (in residues_prime) wilson_theorem1:
```
```   319   assumes a: "p > 2"
```
```   320   shows "[fact (p - 1) = (-1::int)] (mod p)"
```
```   321 proof -
```
```   322   let ?Inverse_Pairs = "{{x, inv x}| x. x \<in> Units R - {\<one>, \<ominus> \<one>}}"
```
```   323   have UR: "Units R = {\<one>, \<ominus> \<one>} \<union> \<Union>?Inverse_Pairs"
```
```   324     by auto
```
```   325   have "(\<Otimes>i\<in>Units R. i) = (\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i\<in>\<Union>?Inverse_Pairs. i)"
```
```   326     apply (subst UR)
```
```   327     apply (subst finprod_Un_disjoint)
```
```   328     apply (auto intro: funcsetI)
```
```   329     using inv_one apply auto[1]
```
```   330     using inv_eq_neg_one_eq apply auto
```
```   331     done
```
```   332   also have "(\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
```
```   333     apply (subst finprod_insert)
```
```   334     apply auto
```
```   335     apply (frule one_eq_neg_one)
```
```   336     using a apply force
```
```   337     done
```
```   338   also have "(\<Otimes>i\<in>(\<Union>?Inverse_Pairs). i) = (\<Otimes>A\<in>?Inverse_Pairs. (\<Otimes>y\<in>A. y))"
```
```   339     apply (subst finprod_Union_disjoint)
```
```   340     apply auto
```
```   341     apply (metis Units_inv_inv)+
```
```   342     done
```
```   343   also have "\<dots> = \<one>"
```
```   344     apply (rule finprod_one)
```
```   345     apply auto
```
```   346     apply (subst finprod_insert)
```
```   347     apply auto
```
```   348     apply (metis inv_eq_self)
```
```   349     done
```
```   350   finally have "(\<Otimes>i\<in>Units R. i) = \<ominus> \<one>"
```
```   351     by simp
```
```   352   also have "(\<Otimes>i\<in>Units R. i) = (\<Otimes>i\<in>Units R. i mod p)"
```
```   353     by (rule finprod_cong') (auto simp: res_units_eq)
```
```   354   also have "\<dots> = (\<Prod>i\<in>Units R. i) mod p"
```
```   355     by (rule prod_cong) auto
```
```   356   also have "\<dots> = fact (p - 1) mod p"
```
```   357     apply (simp add: fact_prod)
```
```   358     using assms
```
```   359     apply (subst res_prime_units_eq)
```
```   360     apply (simp add: int_prod zmod_int prod_int_eq)
```
```   361     done
```
```   362   finally have "fact (p - 1) mod p = \<ominus> \<one>" .
```
```   363   then show ?thesis
```
```   364     by (metis of_nat_fact Divides.transfer_int_nat_functions(2)
```
```   365       cong_int_def res_neg_eq res_one_eq)
```
```   366 qed
```
```   367
```
```   368 lemma wilson_theorem:
```
```   369   assumes "prime p"
```
```   370   shows "[fact (p - 1) = - 1] (mod p)"
```
```   371 proof (cases "p = 2")
```
```   372   case True
```
```   373   then show ?thesis
```
```   374     by (simp add: cong_int_def fact_prod)
```
```   375 next
```
```   376   case False
```
```   377   then show ?thesis
```
```   378     using assms prime_ge_2_nat
```
```   379     by (metis residues_prime.wilson_theorem1 residues_prime.intro le_eq_less_or_eq)
```
```   380 qed
```
```   381
```
```   382 text {*
```
```   383   This result can be transferred to the multiplicative group of
```
```   384   \$\mathbb{Z}/p\mathbb{Z}\$ for \$p\$ prime. *}
```
```   385
```
```   386 lemma mod_nat_int_pow_eq:
```
```   387   fixes n :: nat and p a :: int
```
```   388   assumes "a \<ge> 0" "p \<ge> 0"
```
```   389   shows "(nat a ^ n) mod (nat p) = nat ((a ^ n) mod p)"
```
```   390   using assms
```
```   391   by (simp add: int_one_le_iff_zero_less nat_mod_distrib order_less_imp_le nat_power_eq[symmetric])
```
```   392
```
```   393 theorem residue_prime_mult_group_has_gen :
```
```   394  fixes p :: nat
```
```   395  assumes prime_p : "prime p"
```
```   396  shows "\<exists>a \<in> {1 .. p - 1}. {1 .. p - 1} = {a^i mod p|i . i \<in> UNIV}"
```
```   397 proof -
```
```   398   have "p\<ge>2" using prime_gt_1_nat[OF prime_p] by simp
```
```   399   interpret R:residues_prime "p" "residue_ring p" unfolding residues_prime_def
```
```   400     by (simp add: prime_p)
```
```   401   have car: "carrier (residue_ring (int p)) - {\<zero>\<^bsub>residue_ring (int p)\<^esub>} =  {1 .. int p - 1}"
```
```   402     by (auto simp add: R.zero_cong R.res_carrier_eq)
```
```   403   obtain a where a:"a \<in> {1 .. int p - 1}"
```
```   404          and a_gen:"{1 .. int p - 1} = {a(^)\<^bsub>residue_ring (int p)\<^esub>i|i::nat . i \<in> UNIV}"
```
```   405     apply atomize_elim using field.finite_field_mult_group_has_gen[OF R.is_field]
```
```   406     by (auto simp add: car[symmetric] carrier_mult_of)
```
```   407   { fix x fix i :: nat assume x: "x \<in> {1 .. int p - 1}"
```
```   408     hence "x (^)\<^bsub>residue_ring (int p)\<^esub> i = x ^ i mod (int p)" using R.pow_cong[of x i] by auto}
```
```   409   note * = this
```
```   410   have **:"nat ` {1 .. int p - 1} = {1 .. p - 1}" (is "?L = ?R")
```
```   411   proof
```
```   412     { fix n assume n: "n \<in> ?L"
```
```   413       then have "n \<in> ?R" using `p\<ge>2` by force
```
```   414     } thus "?L \<subseteq> ?R" by blast
```
```   415     { fix n assume n: "n \<in> ?R"
```
```   416       then have "n \<in> ?L" using `p\<ge>2` Set_Interval.transfer_nat_int_set_functions(2) by fastforce
```
```   417     } thus "?R \<subseteq> ?L" by blast
```
```   418   qed
```
```   419   have "nat ` {a^i mod (int p) | i::nat. i \<in> UNIV} = {nat a^i mod p | i . i \<in> UNIV}" (is "?L = ?R")
```
```   420   proof
```
```   421     { fix x assume x: "x \<in> ?L"
```
```   422       then obtain i where i:"x = nat (a^i mod (int p))" by blast
```
```   423       hence "x = nat a ^ i mod p" using mod_nat_int_pow_eq[of a "int p" i] a `p\<ge>2` by auto
```
```   424       hence "x \<in> ?R" using i by blast
```
```   425     } thus "?L \<subseteq> ?R" by blast
```
```   426     { fix x assume x: "x \<in> ?R"
```
```   427       then obtain i where i:"x = nat a^i mod p" by blast
```
```   428       hence "x \<in> ?L" using mod_nat_int_pow_eq[of a "int p" i] a `p\<ge>2` by auto
```
```   429     } thus "?R \<subseteq> ?L" by blast
```
```   430   qed
```
```   431   hence "{1 .. p - 1} = {nat a^i mod p | i. i \<in> UNIV}"
```
```   432     using * a a_gen ** by presburger
```
```   433   moreover
```
```   434   have "nat a \<in> {1 .. p - 1}" using a by force
```
```   435   ultimately show ?thesis ..
```
```   436 qed
```
```   437
```
```   438 end
```