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