src/HOL/Number_Theory/Residues.thy
author haftmann
Sat Nov 11 18:41:08 2017 +0000 (19 months ago)
changeset 67051 e7e54a0b9197
parent 66954 0230af0f3c59
child 67091 1393c2340eec
permissions -rw-r--r--
dedicated definition for coprimality
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
wenzelm@66453
    13
  "HOL-Algebra.More_Group"
wenzelm@66453
    14
  "HOL-Algebra.More_Ring"
wenzelm@66453
    15
  "HOL-Algebra.More_Finite_Product"
wenzelm@66453
    16
  "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
haftmann@67051
   109
  apply (auto simp add: Units_def R_def residue_ring_def ac_simps invertible_coprime intro: ccontr)
nipkow@31952
   110
  apply (subst (asm) coprime_iff_invertible'_int)
haftmann@67051
   111
   apply (auto simp add: cong_def)
wenzelm@41541
   112
  done
nipkow@31719
   113
nipkow@31719
   114
lemma res_neg_eq: "\<ominus> x = (- x) mod m"
lp15@65066
   115
  using m_gt_one unfolding R_def a_inv_def m_inv_def residue_ring_def
lp15@65066
   116
  apply simp
nipkow@31719
   117
  apply (rule the_equality)
wenzelm@66305
   118
   apply (simp add: mod_add_right_eq)
wenzelm@66305
   119
   apply (simp add: add.commute mod_add_right_eq)
lp15@65066
   120
  apply (metis add.right_neutral minus_add_cancel mod_add_right_eq mod_pos_pos_trivial)
wenzelm@41541
   121
  done
nipkow@31719
   122
wenzelm@44872
   123
lemma finite [iff]: "finite (carrier R)"
haftmann@65416
   124
  by (simp add: res_carrier_eq)
nipkow@31719
   125
wenzelm@44872
   126
lemma finite_Units [iff]: "finite (Units R)"
haftmann@65416
   127
  by (simp add: finite_ring_finite_units)
nipkow@31719
   128
wenzelm@60527
   129
text \<open>
wenzelm@63167
   130
  The function \<open>a \<mapsto> a mod m\<close> maps the integers to the
wenzelm@60527
   131
  residue classes. The following lemmas show that this mapping
wenzelm@60527
   132
  respects addition and multiplication on the integers.
wenzelm@60527
   133
\<close>
nipkow@31719
   134
wenzelm@60527
   135
lemma mod_in_carrier [iff]: "a mod m \<in> carrier R"
wenzelm@60527
   136
  unfolding res_carrier_eq
wenzelm@60527
   137
  using insert m_gt_one by auto
nipkow@31719
   138
nipkow@31719
   139
lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
wenzelm@66305
   140
  by (auto simp: R_def residue_ring_def mod_simps)
nipkow@31719
   141
nipkow@31719
   142
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
wenzelm@66305
   143
  by (auto simp: R_def residue_ring_def mod_simps)
nipkow@31719
   144
nipkow@31719
   145
lemma zero_cong: "\<zero> = 0"
wenzelm@66305
   146
  by (auto simp: R_def residue_ring_def)
nipkow@31719
   147
nipkow@31719
   148
lemma one_cong: "\<one> = 1 mod m"
wenzelm@66305
   149
  using m_gt_one by (auto simp: R_def residue_ring_def)
nipkow@31719
   150
wenzelm@60527
   151
(* FIXME revise algebra library to use 1? *)
nipkow@31719
   152
lemma pow_cong: "(x mod m) (^) n = x^n mod m"
lp15@65066
   153
  using m_gt_one
nipkow@31719
   154
  apply (induct n)
wenzelm@41541
   155
  apply (auto simp add: nat_pow_def one_cong)
haftmann@57512
   156
  apply (metis mult.commute mult_cong)
wenzelm@41541
   157
  done
nipkow@31719
   158
nipkow@31719
   159
lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
lp15@55352
   160
  by (metis mod_minus_eq res_neg_eq)
nipkow@31719
   161
wenzelm@60528
   162
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
   163
  by (induct set: finite) (auto simp: one_cong mult_cong)
nipkow@31719
   164
wenzelm@60528
   165
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
   166
  by (induct set: finite) (auto simp: zero_cong add_cong)
nipkow@31719
   167
haftmann@60688
   168
lemma mod_in_res_units [simp]:
haftmann@60688
   169
  assumes "1 < m" and "coprime a m"
haftmann@60688
   170
  shows "a mod m \<in> Units R"
haftmann@60688
   171
proof (cases "a mod m = 0")
wenzelm@66305
   172
  case True
wenzelm@66305
   173
  with assms show ?thesis
haftmann@60688
   174
    by (auto simp add: res_units_eq gcd_red_int [symmetric])
haftmann@60688
   175
next
haftmann@60688
   176
  case False
haftmann@60688
   177
  from assms have "0 < m" by simp
wenzelm@66305
   178
  then have "0 \<le> a mod m" by (rule pos_mod_sign [of m a])
haftmann@60688
   179
  with False have "0 < a mod m" by simp
haftmann@60688
   180
  with assms show ?thesis
haftmann@60688
   181
    by (auto simp add: res_units_eq gcd_red_int [symmetric] ac_simps)
haftmann@60688
   182
qed
nipkow@31719
   183
wenzelm@60528
   184
lemma res_eq_to_cong: "(a mod m) = (b mod m) \<longleftrightarrow> [a = b] (mod m)"
haftmann@66888
   185
  by (auto simp: cong_def)
nipkow@31719
   186
nipkow@31719
   187
wenzelm@60528
   188
text \<open>Simplifying with these will translate a ring equation in R to a congruence.\<close>
wenzelm@66305
   189
lemmas res_to_cong_simps =
wenzelm@66305
   190
  add_cong mult_cong pow_cong one_cong
wenzelm@66305
   191
  prod_cong sum_cong neg_cong res_eq_to_cong
nipkow@31719
   192
wenzelm@60527
   193
text \<open>Other useful facts about the residue ring.\<close>
nipkow@31719
   194
lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2"
nipkow@31719
   195
  apply (simp add: res_one_eq res_neg_eq)
haftmann@57512
   196
  apply (metis add.commute add_diff_cancel mod_mod_trivial one_add_one uminus_add_conv_diff
wenzelm@60528
   197
    zero_neq_one zmod_zminus1_eq_if)
wenzelm@41541
   198
  done
nipkow@31719
   199
nipkow@31719
   200
end
nipkow@31719
   201
nipkow@31719
   202
wenzelm@60527
   203
subsection \<open>Prime residues\<close>
nipkow@31719
   204
nipkow@31719
   205
locale residues_prime =
eberlm@63534
   206
  fixes p :: nat and R (structure)
nipkow@31719
   207
  assumes p_prime [intro]: "prime p"
eberlm@63534
   208
  defines "R \<equiv> residue_ring (int p)"
nipkow@31719
   209
nipkow@31719
   210
sublocale residues_prime < residues p
lp15@65066
   211
  unfolding R_def residues_def
nipkow@31719
   212
  using p_prime apply auto
haftmann@62348
   213
  apply (metis (full_types) of_nat_1 of_nat_less_iff prime_gt_1_nat)
wenzelm@41541
   214
  done
nipkow@31719
   215
wenzelm@44872
   216
context residues_prime
wenzelm@44872
   217
begin
nipkow@31719
   218
haftmann@67051
   219
lemma p_coprime_left:
haftmann@67051
   220
  "coprime p a \<longleftrightarrow> \<not> p dvd a"
haftmann@67051
   221
  using p_prime by (auto intro: prime_imp_coprime dest: coprime_common_divisor)
haftmann@67051
   222
haftmann@67051
   223
lemma p_coprime_right:
haftmann@67051
   224
  "coprime a p  \<longleftrightarrow> \<not> p dvd a"
haftmann@67051
   225
  using p_coprime_left [of a] by (simp add: ac_simps)
haftmann@67051
   226
haftmann@67051
   227
lemma p_coprime_left_int:
haftmann@67051
   228
  "coprime (int p) a \<longleftrightarrow> \<not> int p dvd a"
haftmann@67051
   229
  using p_prime by (auto intro: prime_imp_coprime dest: coprime_common_divisor)
haftmann@67051
   230
haftmann@67051
   231
lemma p_coprime_right_int:
haftmann@67051
   232
  "coprime a (int p) \<longleftrightarrow> \<not> int p dvd a"
haftmann@67051
   233
  using p_coprime_left_int [of a] by (simp add: ac_simps)
haftmann@67051
   234
nipkow@31719
   235
lemma is_field: "field R"
lp15@65066
   236
proof -
haftmann@66837
   237
  have "0 < x \<Longrightarrow> x < int p \<Longrightarrow> coprime (int p) x" for x
haftmann@66837
   238
    by (rule prime_imp_coprime) (auto simp add: zdvd_not_zless)
lp15@65066
   239
  then show ?thesis
haftmann@66837
   240
    by (intro cring.field_intro2 cring)
haftmann@66837
   241
      (auto simp add: res_carrier_eq res_one_eq res_zero_eq res_units_eq ac_simps)
lp15@65066
   242
qed
nipkow@31719
   243
nipkow@31719
   244
lemma res_prime_units_eq: "Units R = {1..p - 1}"
nipkow@31719
   245
  apply (subst res_units_eq)
haftmann@67051
   246
  apply (auto simp add: p_coprime_right_int zdvd_not_zless)
wenzelm@41541
   247
  done
nipkow@31719
   248
nipkow@31719
   249
end
nipkow@31719
   250
nipkow@31719
   251
sublocale residues_prime < field
nipkow@31719
   252
  by (rule is_field)
nipkow@31719
   253
nipkow@31719
   254
wenzelm@60527
   255
section \<open>Test cases: Euler's theorem and Wilson's theorem\<close>
nipkow@31719
   256
wenzelm@60527
   257
subsection \<open>Euler's theorem\<close>
nipkow@31719
   258
haftmann@67051
   259
lemma (in residues) totatives_eq:
haftmann@67051
   260
  "totatives (nat m) = nat ` Units R"
lp15@55261
   261
proof -
haftmann@67051
   262
  from m_gt_one have "\<bar>m\<bar> > 1"
haftmann@67051
   263
    by simp
haftmann@67051
   264
  then have "totatives (nat \<bar>m\<bar>) = nat ` abs ` Units R"
haftmann@67051
   265
    by (auto simp add: totatives_def res_units_eq image_iff le_less)
haftmann@67051
   266
      (use m_gt_one zless_nat_eq_int_zless in force)
haftmann@67051
   267
  moreover have "\<bar>m\<bar> = m" "abs ` Units R = Units R"
haftmann@67051
   268
    using m_gt_one by (auto simp add: res_units_eq image_iff)
haftmann@67051
   269
  ultimately show ?thesis
haftmann@67051
   270
    by simp
haftmann@67051
   271
qed
haftmann@67051
   272
haftmann@67051
   273
lemma (in residues) totient_eq:
haftmann@67051
   274
  "totient (nat m) = card (Units R)"
haftmann@67051
   275
proof  -
haftmann@65465
   276
  have *: "inj_on nat (Units R)"
haftmann@65465
   277
    by (rule inj_onI) (auto simp add: res_units_eq)
haftmann@67051
   278
  then show ?thesis
haftmann@67051
   279
    by (simp add: totient_def totatives_eq card_image)
lp15@55261
   280
qed
lp15@55261
   281
haftmann@65465
   282
lemma (in residues_prime) totient_eq: "totient p = p - 1"
haftmann@65465
   283
  using totient_eq by (simp add: res_prime_units_eq)
nipkow@31719
   284
haftmann@65465
   285
lemma (in residues) euler_theorem:
haftmann@65465
   286
  assumes "coprime a m"
haftmann@65465
   287
  shows "[a ^ totient (nat m) = 1] (mod m)"
haftmann@65416
   288
proof -
haftmann@65465
   289
  have "a ^ totient (nat m) mod m = 1 mod m"
haftmann@65465
   290
    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
   291
  then show ?thesis
lp15@65066
   292
    using res_eq_to_cong by blast
nipkow@31719
   293
qed
nipkow@31719
   294
nipkow@31719
   295
lemma euler_theorem:
haftmann@65465
   296
  fixes a m :: nat
haftmann@65465
   297
  assumes "coprime a m"
haftmann@65465
   298
  shows "[a ^ totient m = 1] (mod m)"
wenzelm@60527
   299
proof (cases "m = 0 | m = 1")
wenzelm@60527
   300
  case True
wenzelm@44872
   301
  then show ?thesis by auto
nipkow@31719
   302
next
wenzelm@60527
   303
  case False
wenzelm@41541
   304
  with assms show ?thesis
haftmann@66954
   305
    using residues.euler_theorem [of "int m" "int a"] cong_int_iff
haftmann@66954
   306
    by (auto simp add: residues_def gcd_int_def) fastforce
nipkow@31719
   307
qed
nipkow@31719
   308
nipkow@31719
   309
lemma fermat_theorem:
haftmann@65465
   310
  fixes p a :: nat
haftmann@65465
   311
  assumes "prime p" and "\<not> p dvd a"
haftmann@65465
   312
  shows "[a ^ (p - 1) = 1] (mod p)"
nipkow@31719
   313
proof -
haftmann@65465
   314
  from assms prime_imp_coprime [of p a] have "coprime a p"
haftmann@65465
   315
    by (auto simp add: ac_simps)
haftmann@65465
   316
  then have "[a ^ totient p = 1] (mod p)"
haftmann@65465
   317
     by (rule euler_theorem)
haftmann@65465
   318
  also have "totient p = p - 1"
eberlm@65726
   319
    by (rule totient_prime) (rule assms)
haftmann@65465
   320
  finally show ?thesis .
nipkow@31719
   321
qed
nipkow@31719
   322
nipkow@31719
   323
wenzelm@60526
   324
subsection \<open>Wilson's theorem\<close>
nipkow@31719
   325
wenzelm@60527
   326
lemma (in field) inv_pair_lemma: "x \<in> Units R \<Longrightarrow> y \<in> Units R \<Longrightarrow>
wenzelm@60527
   327
    {x, inv x} \<noteq> {y, inv y} \<Longrightarrow> {x, inv x} \<inter> {y, inv y} = {}"
nipkow@31719
   328
  apply auto
lp15@55352
   329
  apply (metis Units_inv_inv)+
wenzelm@41541
   330
  done
nipkow@31719
   331
nipkow@31719
   332
lemma (in residues_prime) wilson_theorem1:
nipkow@31719
   333
  assumes a: "p > 2"
lp15@59730
   334
  shows "[fact (p - 1) = (-1::int)] (mod p)"
nipkow@31719
   335
proof -
wenzelm@60527
   336
  let ?Inverse_Pairs = "{{x, inv x}| x. x \<in> Units R - {\<one>, \<ominus> \<one>}}"
wenzelm@60527
   337
  have UR: "Units R = {\<one>, \<ominus> \<one>} \<union> \<Union>?Inverse_Pairs"
nipkow@31719
   338
    by auto
wenzelm@60527
   339
  have "(\<Otimes>i\<in>Units R. i) = (\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i\<in>\<Union>?Inverse_Pairs. i)"
nipkow@31732
   340
    apply (subst UR)
nipkow@31719
   341
    apply (subst finprod_Un_disjoint)
wenzelm@66305
   342
         apply (auto intro: funcsetI)
wenzelm@60527
   343
    using inv_one apply auto[1]
wenzelm@60527
   344
    using inv_eq_neg_one_eq apply auto
nipkow@31719
   345
    done
wenzelm@60527
   346
  also have "(\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
nipkow@31719
   347
    apply (subst finprod_insert)
wenzelm@66305
   348
        apply auto
nipkow@31719
   349
    apply (frule one_eq_neg_one)
wenzelm@60527
   350
    using a apply force
nipkow@31719
   351
    done
wenzelm@60527
   352
  also have "(\<Otimes>i\<in>(\<Union>?Inverse_Pairs). i) = (\<Otimes>A\<in>?Inverse_Pairs. (\<Otimes>y\<in>A. y))"
wenzelm@60527
   353
    apply (subst finprod_Union_disjoint)
wenzelm@66305
   354
       apply auto
wenzelm@66305
   355
     apply (metis Units_inv_inv)+
nipkow@31719
   356
    done
nipkow@31719
   357
  also have "\<dots> = \<one>"
wenzelm@60527
   358
    apply (rule finprod_one)
wenzelm@66305
   359
     apply auto
wenzelm@60527
   360
    apply (subst finprod_insert)
wenzelm@66305
   361
        apply auto
lp15@55352
   362
    apply (metis inv_eq_self)
nipkow@31719
   363
    done
wenzelm@60527
   364
  finally have "(\<Otimes>i\<in>Units R. i) = \<ominus> \<one>"
nipkow@31719
   365
    by simp
wenzelm@60527
   366
  also have "(\<Otimes>i\<in>Units R. i) = (\<Otimes>i\<in>Units R. i mod p)"
lp15@65066
   367
    by (rule finprod_cong') (auto simp: res_units_eq)
wenzelm@60527
   368
  also have "\<dots> = (\<Prod>i\<in>Units R. i) mod p"
lp15@65066
   369
    by (rule prod_cong) auto
nipkow@31719
   370
  also have "\<dots> = fact (p - 1) mod p"
nipkow@64272
   371
    apply (simp add: fact_prod)
lp15@65066
   372
    using assms
lp15@55242
   373
    apply (subst res_prime_units_eq)
nipkow@64272
   374
    apply (simp add: int_prod zmod_int prod_int_eq)
nipkow@31719
   375
    done
wenzelm@60527
   376
  finally have "fact (p - 1) mod p = \<ominus> \<one>" .
wenzelm@60527
   377
  then show ?thesis
haftmann@66888
   378
    by (simp add: cong_def res_neg_eq res_one_eq zmod_int)
nipkow@31719
   379
qed
nipkow@31719
   380
lp15@55352
   381
lemma wilson_theorem:
wenzelm@60527
   382
  assumes "prime p"
wenzelm@60527
   383
  shows "[fact (p - 1) = - 1] (mod p)"
lp15@55352
   384
proof (cases "p = 2")
lp15@59667
   385
  case True
lp15@55352
   386
  then show ?thesis
haftmann@66888
   387
    by (simp add: cong_def fact_prod)
lp15@55352
   388
next
lp15@55352
   389
  case False
lp15@55352
   390
  then show ?thesis
lp15@55352
   391
    using assms prime_ge_2_nat
lp15@55352
   392
    by (metis residues_prime.wilson_theorem1 residues_prime.intro le_eq_less_or_eq)
lp15@55352
   393
qed
nipkow@31719
   394
wenzelm@66304
   395
text \<open>
haftmann@65416
   396
  This result can be transferred to the multiplicative group of
wenzelm@66305
   397
  \<open>\<int>/p\<int>\<close> for \<open>p\<close> prime.\<close>
haftmann@65416
   398
haftmann@65416
   399
lemma mod_nat_int_pow_eq:
haftmann@65416
   400
  fixes n :: nat and p a :: int
wenzelm@66305
   401
  shows "a \<ge> 0 \<Longrightarrow> p \<ge> 0 \<Longrightarrow> (nat a ^ n) mod (nat p) = nat ((a ^ n) mod p)"
haftmann@65416
   402
  by (simp add: int_one_le_iff_zero_less nat_mod_distrib order_less_imp_le nat_power_eq[symmetric])
haftmann@65416
   403
haftmann@65416
   404
theorem residue_prime_mult_group_has_gen :
haftmann@65416
   405
 fixes p :: nat
haftmann@65416
   406
 assumes prime_p : "prime p"
haftmann@65416
   407
 shows "\<exists>a \<in> {1 .. p - 1}. {1 .. p - 1} = {a^i mod p|i . i \<in> UNIV}"
haftmann@65416
   408
proof -
wenzelm@66305
   409
  have "p \<ge> 2"
wenzelm@66305
   410
    using prime_gt_1_nat[OF prime_p] by simp
wenzelm@66305
   411
  interpret R: residues_prime p "residue_ring p"
wenzelm@66305
   412
    by (simp add: residues_prime_def prime_p)
wenzelm@66305
   413
  have car: "carrier (residue_ring (int p)) - {\<zero>\<^bsub>residue_ring (int p)\<^esub>} = {1 .. int p - 1}"
haftmann@65416
   414
    by (auto simp add: R.zero_cong R.res_carrier_eq)
wenzelm@66305
   415
wenzelm@66305
   416
  have "x (^)\<^bsub>residue_ring (int p)\<^esub> i = x ^ i mod (int p)"
wenzelm@66305
   417
    if "x \<in> {1 .. int p - 1}" for x and i :: nat
wenzelm@66305
   418
    using that R.pow_cong[of x i] by auto
wenzelm@66305
   419
  moreover
wenzelm@66305
   420
  obtain a where a: "a \<in> {1 .. int p - 1}"
wenzelm@66305
   421
    and a_gen: "{1 .. int p - 1} = {a(^)\<^bsub>residue_ring (int p)\<^esub>i|i::nat . i \<in> UNIV}"
wenzelm@66305
   422
    using field.finite_field_mult_group_has_gen[OF R.is_field]
haftmann@65416
   423
    by (auto simp add: car[symmetric] carrier_mult_of)
wenzelm@66305
   424
  moreover
wenzelm@66305
   425
  have "nat ` {1 .. int p - 1} = {1 .. p - 1}" (is "?L = ?R")
haftmann@65416
   426
  proof
wenzelm@66305
   427
    have "n \<in> ?R" if "n \<in> ?L" for n
wenzelm@66305
   428
      using that \<open>p\<ge>2\<close> by force
wenzelm@66305
   429
    then show "?L \<subseteq> ?R" by blast
wenzelm@66305
   430
    have "n \<in> ?L" if "n \<in> ?R" for n
haftmann@66837
   431
      using that \<open>p\<ge>2\<close> by (auto intro: rev_image_eqI [of "int n"])
wenzelm@66305
   432
    then show "?R \<subseteq> ?L" by blast
haftmann@65416
   433
  qed
wenzelm@66305
   434
  moreover
haftmann@65416
   435
  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
   436
  proof
wenzelm@66305
   437
    have "x \<in> ?R" if "x \<in> ?L" for x
wenzelm@66305
   438
    proof -
wenzelm@66305
   439
      from that obtain i where i: "x = nat (a^i mod (int p))"
wenzelm@66305
   440
        by blast
wenzelm@66305
   441
      then have "x = nat a ^ i mod p"
wenzelm@66305
   442
        using mod_nat_int_pow_eq[of a "int p" i] a \<open>p\<ge>2\<close> by auto
wenzelm@66305
   443
      with i show ?thesis by blast
wenzelm@66305
   444
    qed
wenzelm@66305
   445
    then show "?L \<subseteq> ?R" by blast
wenzelm@66305
   446
    have "x \<in> ?L" if "x \<in> ?R" for x
wenzelm@66305
   447
    proof -
wenzelm@66305
   448
      from that obtain i where i: "x = nat a^i mod p"
wenzelm@66305
   449
        by blast
wenzelm@66305
   450
      with mod_nat_int_pow_eq[of a "int p" i] a \<open>p\<ge>2\<close> show ?thesis
wenzelm@66305
   451
        by auto
wenzelm@66305
   452
    qed
wenzelm@66305
   453
    then show "?R \<subseteq> ?L" by blast
haftmann@65416
   454
  qed
wenzelm@66305
   455
  ultimately have "{1 .. p - 1} = {nat a^i mod p | i. i \<in> UNIV}"
wenzelm@66305
   456
    by presburger
wenzelm@66305
   457
  moreover from a have "nat a \<in> {1 .. p - 1}" by force
haftmann@65416
   458
  ultimately show ?thesis ..
haftmann@65416
   459
qed
haftmann@65416
   460
nipkow@31719
   461
end