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