src/HOL/NumberTheory/EulerFermat.thy
author wenzelm
Thu Dec 08 12:50:04 2005 +0100 (2005-12-08)
changeset 18369 694ea14ab4f2
parent 16733 236dfafbeb63
child 19670 2e4a143c73c5
permissions -rw-r--r--
tuned sources and proofs
wenzelm@11049
     1
(*  Title:      HOL/NumberTheory/EulerFermat.thy
paulson@9508
     2
    ID:         $Id$
wenzelm@11049
     3
    Author:     Thomas M. Rasmussen
wenzelm@11049
     4
    Copyright   2000  University of Cambridge
paulson@9508
     5
*)
paulson@9508
     6
wenzelm@11049
     7
header {* Fermat's Little Theorem extended to Euler's Totient function *}
wenzelm@11049
     8
haftmann@16417
     9
theory EulerFermat imports BijectionRel IntFact begin
wenzelm@11049
    10
wenzelm@11049
    11
text {*
wenzelm@11049
    12
  Fermat's Little Theorem extended to Euler's Totient function. More
wenzelm@11049
    13
  abstract approach than Boyer-Moore (which seems necessary to achieve
wenzelm@11049
    14
  the extended version).
wenzelm@11049
    15
*}
wenzelm@11049
    16
wenzelm@11049
    17
wenzelm@11049
    18
subsection {* Definitions and lemmas *}
paulson@9508
    19
paulson@9508
    20
consts
wenzelm@11049
    21
  RsetR :: "int => int set set"
wenzelm@11049
    22
  BnorRset :: "int * int => int set"
wenzelm@11049
    23
  norRRset :: "int => int set"
wenzelm@11049
    24
  noXRRset :: "int => int => int set"
wenzelm@11049
    25
  phi :: "int => nat"
wenzelm@11049
    26
  is_RRset :: "int set => int => bool"
wenzelm@11049
    27
  RRset2norRR :: "int set => int => int => int"
paulson@9508
    28
paulson@9508
    29
inductive "RsetR m"
wenzelm@11049
    30
  intros
wenzelm@11049
    31
    empty [simp]: "{} \<in> RsetR m"
paulson@11868
    32
    insert: "A \<in> RsetR m ==> zgcd (a, m) = 1 ==>
wenzelm@11049
    33
      \<forall>a'. a' \<in> A --> \<not> zcong a a' m ==> insert a A \<in> RsetR m"
paulson@9508
    34
wenzelm@11049
    35
recdef BnorRset
wenzelm@11049
    36
  "measure ((\<lambda>(a, m). nat a) :: int * int => nat)"
wenzelm@11049
    37
  "BnorRset (a, m) =
paulson@11868
    38
   (if 0 < a then
paulson@11868
    39
    let na = BnorRset (a - 1, m)
paulson@11868
    40
    in (if zgcd (a, m) = 1 then insert a na else na)
wenzelm@11049
    41
    else {})"
paulson@9508
    42
paulson@9508
    43
defs
paulson@11868
    44
  norRRset_def: "norRRset m == BnorRset (m - 1, m)"
wenzelm@11049
    45
  noXRRset_def: "noXRRset m x == (\<lambda>a. a * x) ` norRRset m"
wenzelm@11049
    46
  phi_def: "phi m == card (norRRset m)"
wenzelm@11049
    47
  is_RRset_def: "is_RRset A m == A \<in> RsetR m \<and> card A = phi m"
wenzelm@11049
    48
  RRset2norRR_def:
wenzelm@11049
    49
    "RRset2norRR A m a ==
paulson@11868
    50
     (if 1 < m \<and> is_RRset A m \<and> a \<in> A then
wenzelm@11049
    51
        SOME b. zcong a b m \<and> b \<in> norRRset m
paulson@11868
    52
      else 0)"
wenzelm@11049
    53
wenzelm@11049
    54
constdefs
wenzelm@11049
    55
  zcongm :: "int => int => int => bool"
wenzelm@11049
    56
  "zcongm m == \<lambda>a b. zcong a b m"
wenzelm@11049
    57
paulson@11868
    58
lemma abs_eq_1_iff [iff]: "(abs z = (1::int)) = (z = 1 \<or> z = -1)"
wenzelm@11049
    59
  -- {* LCP: not sure why this lemma is needed now *}
wenzelm@18369
    60
  by (auto simp add: abs_if)
wenzelm@11049
    61
wenzelm@11049
    62
wenzelm@11049
    63
text {* \medskip @{text norRRset} *}
wenzelm@11049
    64
wenzelm@11049
    65
declare BnorRset.simps [simp del]
wenzelm@11049
    66
wenzelm@11049
    67
lemma BnorRset_induct:
wenzelm@18369
    68
  assumes "!!a m. P {} a m"
wenzelm@18369
    69
    and "!!a m. 0 < (a::int) ==> P (BnorRset (a - 1, m::int)) (a - 1) m
wenzelm@18369
    70
      ==> P (BnorRset(a,m)) a m"
wenzelm@18369
    71
  shows "P (BnorRset(u,v)) u v"
wenzelm@18369
    72
  apply (rule BnorRset.induct)
wenzelm@18369
    73
  apply safe
wenzelm@18369
    74
   apply (case_tac [2] "0 < a")
wenzelm@18369
    75
    apply (rule_tac [2] prems)
wenzelm@18369
    76
     apply simp_all
wenzelm@18369
    77
   apply (simp_all add: BnorRset.simps prems)
wenzelm@11049
    78
  done
wenzelm@11049
    79
wenzelm@18369
    80
lemma Bnor_mem_zle [rule_format]: "b \<in> BnorRset (a, m) \<longrightarrow> b \<le> a"
wenzelm@11049
    81
  apply (induct a m rule: BnorRset_induct)
wenzelm@18369
    82
   apply simp
wenzelm@18369
    83
  apply (subst BnorRset.simps)
paulson@13833
    84
   apply (unfold Let_def, auto)
wenzelm@11049
    85
  done
wenzelm@11049
    86
wenzelm@11049
    87
lemma Bnor_mem_zle_swap: "a < b ==> b \<notin> BnorRset (a, m)"
wenzelm@18369
    88
  by (auto dest: Bnor_mem_zle)
wenzelm@11049
    89
paulson@11868
    90
lemma Bnor_mem_zg [rule_format]: "b \<in> BnorRset (a, m) --> 0 < b"
wenzelm@11049
    91
  apply (induct a m rule: BnorRset_induct)
wenzelm@11049
    92
   prefer 2
wenzelm@11049
    93
   apply (subst BnorRset.simps)
paulson@13833
    94
   apply (unfold Let_def, auto)
wenzelm@11049
    95
  done
wenzelm@11049
    96
wenzelm@11049
    97
lemma Bnor_mem_if [rule_format]:
paulson@11868
    98
    "zgcd (b, m) = 1 --> 0 < b --> b \<le> a --> b \<in> BnorRset (a, m)"
paulson@13833
    99
  apply (induct a m rule: BnorRset.induct, auto)
wenzelm@11049
   100
   apply (subst BnorRset.simps)
wenzelm@11049
   101
   defer
wenzelm@11049
   102
   apply (subst BnorRset.simps)
paulson@13833
   103
   apply (unfold Let_def, auto)
wenzelm@11049
   104
  done
paulson@9508
   105
wenzelm@11049
   106
lemma Bnor_in_RsetR [rule_format]: "a < m --> BnorRset (a, m) \<in> RsetR m"
paulson@13833
   107
  apply (induct a m rule: BnorRset_induct, simp)
wenzelm@11049
   108
  apply (subst BnorRset.simps)
paulson@13833
   109
  apply (unfold Let_def, auto)
wenzelm@11049
   110
  apply (rule RsetR.insert)
wenzelm@11049
   111
    apply (rule_tac [3] allI)
wenzelm@11049
   112
    apply (rule_tac [3] impI)
wenzelm@11049
   113
    apply (rule_tac [3] zcong_not)
paulson@11868
   114
       apply (subgoal_tac [6] "a' \<le> a - 1")
wenzelm@11049
   115
        apply (rule_tac [7] Bnor_mem_zle)
paulson@13833
   116
        apply (rule_tac [5] Bnor_mem_zg, auto)
wenzelm@11049
   117
  done
wenzelm@11049
   118
wenzelm@11049
   119
lemma Bnor_fin: "finite (BnorRset (a, m))"
wenzelm@11049
   120
  apply (induct a m rule: BnorRset_induct)
wenzelm@11049
   121
   prefer 2
wenzelm@11049
   122
   apply (subst BnorRset.simps)
paulson@13833
   123
   apply (unfold Let_def, auto)
wenzelm@11049
   124
  done
wenzelm@11049
   125
wenzelm@13524
   126
lemma norR_mem_unique_aux: "a \<le> b - 1 ==> a < (b::int)"
wenzelm@11049
   127
  apply auto
wenzelm@11049
   128
  done
paulson@9508
   129
wenzelm@11049
   130
lemma norR_mem_unique:
paulson@11868
   131
  "1 < m ==>
paulson@11868
   132
    zgcd (a, m) = 1 ==> \<exists>!b. [a = b] (mod m) \<and> b \<in> norRRset m"
wenzelm@11049
   133
  apply (unfold norRRset_def)
paulson@13833
   134
  apply (cut_tac a = a and m = m in zcong_zless_unique, auto)
wenzelm@11049
   135
   apply (rule_tac [2] m = m in zcong_zless_imp_eq)
wenzelm@11049
   136
       apply (auto intro: Bnor_mem_zle Bnor_mem_zg zcong_trans
wenzelm@13524
   137
	 order_less_imp_le norR_mem_unique_aux simp add: zcong_sym)
ballarin@14174
   138
  apply (rule_tac x = b in exI, safe)
wenzelm@11049
   139
  apply (rule Bnor_mem_if)
paulson@11868
   140
    apply (case_tac [2] "b = 0")
wenzelm@11049
   141
     apply (auto intro: order_less_le [THEN iffD2])
wenzelm@11049
   142
   prefer 2
wenzelm@11049
   143
   apply (simp only: zcong_def)
wenzelm@11049
   144
   apply (subgoal_tac "zgcd (a, m) = m")
wenzelm@11049
   145
    prefer 2
wenzelm@11049
   146
    apply (subst zdvd_iff_zgcd [symmetric])
wenzelm@11049
   147
     apply (rule_tac [4] zgcd_zcong_zgcd)
wenzelm@11049
   148
       apply (simp_all add: zdvd_zminus_iff zcong_sym)
wenzelm@11049
   149
  done
wenzelm@11049
   150
wenzelm@11049
   151
wenzelm@11049
   152
text {* \medskip @{term noXRRset} *}
wenzelm@11049
   153
wenzelm@11049
   154
lemma RRset_gcd [rule_format]:
paulson@11868
   155
    "is_RRset A m ==> a \<in> A --> zgcd (a, m) = 1"
wenzelm@11049
   156
  apply (unfold is_RRset_def)
paulson@13833
   157
  apply (rule RsetR.induct, auto)
wenzelm@11049
   158
  done
wenzelm@11049
   159
wenzelm@11049
   160
lemma RsetR_zmult_mono:
wenzelm@11049
   161
  "A \<in> RsetR m ==>
paulson@11868
   162
    0 < m ==> zgcd (x, m) = 1 ==> (\<lambda>a. a * x) ` A \<in> RsetR m"
paulson@13833
   163
  apply (erule RsetR.induct, simp_all)
paulson@13833
   164
  apply (rule RsetR.insert, auto)
wenzelm@11049
   165
   apply (blast intro: zgcd_zgcd_zmult)
wenzelm@11049
   166
  apply (simp add: zcong_cancel)
wenzelm@11049
   167
  done
wenzelm@11049
   168
wenzelm@11049
   169
lemma card_nor_eq_noX:
paulson@11868
   170
  "0 < m ==>
paulson@11868
   171
    zgcd (x, m) = 1 ==> card (noXRRset m x) = card (norRRset m)"
wenzelm@11049
   172
  apply (unfold norRRset_def noXRRset_def)
wenzelm@11049
   173
  apply (rule card_image)
wenzelm@11049
   174
   apply (auto simp add: inj_on_def Bnor_fin)
wenzelm@11049
   175
  apply (simp add: BnorRset.simps)
wenzelm@11049
   176
  done
wenzelm@11049
   177
wenzelm@11049
   178
lemma noX_is_RRset:
paulson@11868
   179
    "0 < m ==> zgcd (x, m) = 1 ==> is_RRset (noXRRset m x) m"
wenzelm@11049
   180
  apply (unfold is_RRset_def phi_def)
wenzelm@11049
   181
  apply (auto simp add: card_nor_eq_noX)
wenzelm@11049
   182
  apply (unfold noXRRset_def norRRset_def)
wenzelm@11049
   183
  apply (rule RsetR_zmult_mono)
paulson@13833
   184
    apply (rule Bnor_in_RsetR, simp_all)
wenzelm@11049
   185
  done
paulson@9508
   186
wenzelm@11049
   187
lemma aux_some:
paulson@11868
   188
  "1 < m ==> is_RRset A m ==> a \<in> A
wenzelm@11049
   189
    ==> zcong a (SOME b. [a = b] (mod m) \<and> b \<in> norRRset m) m \<and>
wenzelm@11049
   190
      (SOME b. [a = b] (mod m) \<and> b \<in> norRRset m) \<in> norRRset m"
wenzelm@11049
   191
  apply (rule norR_mem_unique [THEN ex1_implies_ex, THEN someI_ex])
paulson@13833
   192
   apply (rule_tac [2] RRset_gcd, simp_all)
wenzelm@11049
   193
  done
wenzelm@11049
   194
wenzelm@11049
   195
lemma RRset2norRR_correct:
paulson@11868
   196
  "1 < m ==> is_RRset A m ==> a \<in> A ==>
wenzelm@11049
   197
    [a = RRset2norRR A m a] (mod m) \<and> RRset2norRR A m a \<in> norRRset m"
paulson@13833
   198
  apply (unfold RRset2norRR_def, simp)
paulson@13833
   199
  apply (rule aux_some, simp_all)
wenzelm@11049
   200
  done
wenzelm@11049
   201
wenzelm@11049
   202
lemmas RRset2norRR_correct1 =
wenzelm@11049
   203
  RRset2norRR_correct [THEN conjunct1, standard]
wenzelm@11049
   204
lemmas RRset2norRR_correct2 =
wenzelm@11049
   205
  RRset2norRR_correct [THEN conjunct2, standard]
wenzelm@11049
   206
wenzelm@11049
   207
lemma RsetR_fin: "A \<in> RsetR m ==> finite A"
wenzelm@18369
   208
  by (induct set: RsetR) auto
wenzelm@11049
   209
wenzelm@11049
   210
lemma RRset_zcong_eq [rule_format]:
paulson@11868
   211
  "1 < m ==>
wenzelm@11049
   212
    is_RRset A m ==> [a = b] (mod m) ==> a \<in> A --> b \<in> A --> a = b"
wenzelm@11049
   213
  apply (unfold is_RRset_def)
wenzelm@11049
   214
  apply (rule RsetR.induct)
wenzelm@11049
   215
    apply (auto simp add: zcong_sym)
wenzelm@11049
   216
  done
wenzelm@11049
   217
wenzelm@11049
   218
lemma aux:
wenzelm@11049
   219
  "P (SOME a. P a) ==> Q (SOME a. Q a) ==>
wenzelm@11049
   220
    (SOME a. P a) = (SOME a. Q a) ==> \<exists>a. P a \<and> Q a"
wenzelm@11049
   221
  apply auto
wenzelm@11049
   222
  done
wenzelm@11049
   223
wenzelm@11049
   224
lemma RRset2norRR_inj:
paulson@11868
   225
    "1 < m ==> is_RRset A m ==> inj_on (RRset2norRR A m) A"
paulson@13833
   226
  apply (unfold RRset2norRR_def inj_on_def, auto)
wenzelm@11049
   227
  apply (subgoal_tac "\<exists>b. ([x = b] (mod m) \<and> b \<in> norRRset m) \<and>
wenzelm@11049
   228
      ([y = b] (mod m) \<and> b \<in> norRRset m)")
wenzelm@11049
   229
   apply (rule_tac [2] aux)
wenzelm@11049
   230
     apply (rule_tac [3] aux_some)
wenzelm@11049
   231
       apply (rule_tac [2] aux_some)
paulson@13833
   232
         apply (rule RRset_zcong_eq, auto)
wenzelm@11049
   233
  apply (rule_tac b = b in zcong_trans)
wenzelm@11049
   234
   apply (simp_all add: zcong_sym)
wenzelm@11049
   235
  done
wenzelm@11049
   236
wenzelm@11049
   237
lemma RRset2norRR_eq_norR:
paulson@11868
   238
    "1 < m ==> is_RRset A m ==> RRset2norRR A m ` A = norRRset m"
wenzelm@11049
   239
  apply (rule card_seteq)
wenzelm@11049
   240
    prefer 3
wenzelm@11049
   241
    apply (subst card_image)
nipkow@15402
   242
      apply (rule_tac RRset2norRR_inj, auto)
nipkow@15402
   243
     apply (rule_tac [3] RRset2norRR_correct2, auto)
wenzelm@11049
   244
    apply (unfold is_RRset_def phi_def norRRset_def)
nipkow@15402
   245
    apply (auto simp add: Bnor_fin)
wenzelm@11049
   246
  done
wenzelm@11049
   247
wenzelm@11049
   248
wenzelm@13524
   249
lemma Bnor_prod_power_aux: "a \<notin> A ==> inj f ==> f a \<notin> f ` A"
paulson@13833
   250
by (unfold inj_on_def, auto)
paulson@9508
   251
wenzelm@11049
   252
lemma Bnor_prod_power [rule_format]:
nipkow@15392
   253
  "x \<noteq> 0 ==> a < m --> \<Prod>((\<lambda>a. a * x) ` BnorRset (a, m)) =
nipkow@15392
   254
      \<Prod>(BnorRset(a, m)) * x^card (BnorRset (a, m))"
wenzelm@11049
   255
  apply (induct a m rule: BnorRset_induct)
wenzelm@11049
   256
   prefer 2
paulson@15481
   257
   apply (simplesubst BnorRset.simps)  --{*multiple redexes*}
paulson@13833
   258
   apply (unfold Let_def, auto)
wenzelm@11049
   259
  apply (simp add: Bnor_fin Bnor_mem_zle_swap)
wenzelm@11049
   260
  apply (subst setprod_insert)
wenzelm@13524
   261
    apply (rule_tac [2] Bnor_prod_power_aux)
wenzelm@11049
   262
     apply (unfold inj_on_def)
wenzelm@11049
   263
     apply (simp_all add: zmult_ac Bnor_fin finite_imageI
wenzelm@11049
   264
       Bnor_mem_zle_swap)
wenzelm@11049
   265
  done
wenzelm@11049
   266
wenzelm@11049
   267
wenzelm@11049
   268
subsection {* Fermat *}
wenzelm@11049
   269
wenzelm@11049
   270
lemma bijzcong_zcong_prod:
nipkow@15392
   271
    "(A, B) \<in> bijR (zcongm m) ==> [\<Prod>A = \<Prod>B] (mod m)"
wenzelm@11049
   272
  apply (unfold zcongm_def)
wenzelm@11049
   273
  apply (erule bijR.induct)
wenzelm@11049
   274
   apply (subgoal_tac [2] "a \<notin> A \<and> b \<notin> B \<and> finite A \<and> finite B")
wenzelm@11049
   275
    apply (auto intro: fin_bijRl fin_bijRr zcong_zmult)
wenzelm@11049
   276
  done
wenzelm@11049
   277
wenzelm@11049
   278
lemma Bnor_prod_zgcd [rule_format]:
nipkow@15392
   279
    "a < m --> zgcd (\<Prod>(BnorRset(a, m)), m) = 1"
wenzelm@11049
   280
  apply (induct a m rule: BnorRset_induct)
wenzelm@11049
   281
   prefer 2
wenzelm@11049
   282
   apply (subst BnorRset.simps)
paulson@13833
   283
   apply (unfold Let_def, auto)
wenzelm@11049
   284
  apply (simp add: Bnor_fin Bnor_mem_zle_swap)
wenzelm@11049
   285
  apply (blast intro: zgcd_zgcd_zmult)
wenzelm@11049
   286
  done
paulson@9508
   287
wenzelm@11049
   288
theorem Euler_Fermat:
paulson@11868
   289
    "0 < m ==> zgcd (x, m) = 1 ==> [x^(phi m) = 1] (mod m)"
wenzelm@11049
   290
  apply (unfold norRRset_def phi_def)
paulson@11868
   291
  apply (case_tac "x = 0")
paulson@11868
   292
   apply (case_tac [2] "m = 1")
wenzelm@11049
   293
    apply (rule_tac [3] iffD1)
nipkow@15392
   294
     apply (rule_tac [3] k = "\<Prod>(BnorRset(m - 1, m))"
wenzelm@11049
   295
       in zcong_cancel2)
wenzelm@11049
   296
      prefer 5
wenzelm@11049
   297
      apply (subst Bnor_prod_power [symmetric])
paulson@13833
   298
        apply (rule_tac [7] Bnor_prod_zgcd, simp_all)
wenzelm@11049
   299
  apply (rule bijzcong_zcong_prod)
wenzelm@11049
   300
  apply (fold norRRset_def noXRRset_def)
wenzelm@11049
   301
  apply (subst RRset2norRR_eq_norR [symmetric])
paulson@13833
   302
    apply (rule_tac [3] inj_func_bijR, auto)
nipkow@13187
   303
     apply (unfold zcongm_def)
nipkow@13187
   304
     apply (rule_tac [2] RRset2norRR_correct1)
nipkow@13187
   305
       apply (rule_tac [5] RRset2norRR_inj)
nipkow@13187
   306
        apply (auto intro: order_less_le [THEN iffD2]
wenzelm@11049
   307
	   simp add: noX_is_RRset)
wenzelm@11049
   308
  apply (unfold noXRRset_def norRRset_def)
wenzelm@11049
   309
  apply (rule finite_imageI)
wenzelm@11049
   310
  apply (rule Bnor_fin)
wenzelm@11049
   311
  done
wenzelm@11049
   312
nipkow@16733
   313
lemma Bnor_prime:
nipkow@16733
   314
  "\<lbrakk> zprime p; a < p \<rbrakk> \<Longrightarrow> card (BnorRset (a, p)) = nat a"
wenzelm@11049
   315
  apply (induct a p rule: BnorRset.induct)
wenzelm@11049
   316
  apply (subst BnorRset.simps)
nipkow@16733
   317
  apply (unfold Let_def, auto simp add:zless_zprime_imp_zrelprime)
paulson@13833
   318
  apply (subgoal_tac "finite (BnorRset (a - 1,m))")
paulson@13833
   319
   apply (subgoal_tac "a ~: BnorRset (a - 1,m)")
paulson@13833
   320
    apply (auto simp add: card_insert_disjoint Suc_nat_eq_nat_zadd1)
paulson@13833
   321
   apply (frule Bnor_mem_zle, arith)
paulson@13833
   322
  apply (frule Bnor_fin)
wenzelm@11049
   323
  done
wenzelm@11049
   324
nipkow@16663
   325
lemma phi_prime: "zprime p ==> phi p = nat (p - 1)"
wenzelm@11049
   326
  apply (unfold phi_def norRRset_def)
paulson@13833
   327
  apply (rule Bnor_prime, auto)
wenzelm@11049
   328
  done
wenzelm@11049
   329
wenzelm@11049
   330
theorem Little_Fermat:
nipkow@16663
   331
    "zprime p ==> \<not> p dvd x ==> [x^(nat (p - 1)) = 1] (mod p)"
wenzelm@11049
   332
  apply (subst phi_prime [symmetric])
wenzelm@11049
   333
   apply (rule_tac [2] Euler_Fermat)
wenzelm@11049
   334
    apply (erule_tac [3] zprime_imp_zrelprime)
paulson@13833
   335
    apply (unfold zprime_def, auto)
wenzelm@11049
   336
  done
paulson@9508
   337
paulson@9508
   338
end