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