src/HOL/NumberTheory/Euler.thy
author wenzelm
Wed Aug 09 00:14:28 2006 +0200 (2006-08-09)
changeset 20369 7e03c3ed1a18
parent 19670 2e4a143c73c5
child 21404 eb85850d3eb7
permissions -rw-r--r--
tuned proofs;
paulson@13871
     1
(*  Title:      HOL/Quadratic_Reciprocity/Euler.thy
kleing@14981
     2
    ID:         $Id$
paulson@13871
     3
    Authors:    Jeremy Avigad, David Gray, and Adam Kramer
paulson@13871
     4
*)
paulson@13871
     5
paulson@13871
     6
header {* Euler's criterion *}
paulson@13871
     7
wenzelm@16974
     8
theory Euler imports Residues EvenOdd begin
paulson@13871
     9
wenzelm@19670
    10
definition
paulson@13871
    11
  MultInvPair :: "int => int => int => int set"
wenzelm@19670
    12
  "MultInvPair a p j = {StandardRes p j, StandardRes p (a * (MultInv p j))}"
wenzelm@19670
    13
paulson@13871
    14
  SetS        :: "int => int => int set set"
wenzelm@19670
    15
  "SetS        a p   =  (MultInvPair a p ` SRStar p)"
paulson@13871
    16
wenzelm@19670
    17
wenzelm@19670
    18
subsection {* Property for MultInvPair *}
paulson@13871
    19
wenzelm@19670
    20
lemma MultInvPair_prop1a:
wenzelm@19670
    21
  "[| zprime p; 2 < p; ~([a = 0](mod p));
wenzelm@19670
    22
      X \<in> (SetS a p); Y \<in> (SetS a p);
wenzelm@19670
    23
      ~((X \<inter> Y) = {}) |] ==> X = Y"
paulson@13871
    24
  apply (auto simp add: SetS_def)
wenzelm@16974
    25
  apply (drule StandardRes_SRStar_prop1a)+ defer 1
wenzelm@16974
    26
  apply (drule StandardRes_SRStar_prop1a)+
paulson@13871
    27
  apply (auto simp add: MultInvPair_def StandardRes_prop2 zcong_sym)
wenzelm@20369
    28
  apply (drule notE, rule MultInv_zcong_prop1, auto)[]
wenzelm@20369
    29
  apply (drule notE, rule MultInv_zcong_prop2, auto simp add: zcong_sym)[]
wenzelm@20369
    30
  apply (drule MultInv_zcong_prop2, auto simp add: zcong_sym)[]
wenzelm@20369
    31
  apply (drule MultInv_zcong_prop3, auto simp add: zcong_sym)[]
wenzelm@20369
    32
  apply (drule MultInv_zcong_prop1, auto)[]
wenzelm@20369
    33
  apply (drule MultInv_zcong_prop2, auto simp add: zcong_sym)[]
wenzelm@20369
    34
  apply (drule MultInv_zcong_prop2, auto simp add: zcong_sym)[]
wenzelm@20369
    35
  apply (drule MultInv_zcong_prop3, auto simp add: zcong_sym)[]
wenzelm@19670
    36
  done
paulson@13871
    37
wenzelm@19670
    38
lemma MultInvPair_prop1b:
wenzelm@19670
    39
  "[| zprime p; 2 < p; ~([a = 0](mod p));
wenzelm@19670
    40
      X \<in> (SetS a p); Y \<in> (SetS a p);
wenzelm@19670
    41
      X \<noteq> Y |] ==> X \<inter> Y = {}"
paulson@13871
    42
  apply (rule notnotD)
paulson@13871
    43
  apply (rule notI)
paulson@13871
    44
  apply (drule MultInvPair_prop1a, auto)
wenzelm@19670
    45
  done
paulson@13871
    46
nipkow@16663
    47
lemma MultInvPair_prop1c: "[| zprime p; 2 < p; ~([a = 0](mod p)) |] ==>  
paulson@13871
    48
    \<forall>X \<in> SetS a p. \<forall>Y \<in> SetS a p. X \<noteq> Y --> X\<inter>Y = {}"
paulson@13871
    49
  by (auto simp add: MultInvPair_prop1b)
paulson@13871
    50
nipkow@16663
    51
lemma MultInvPair_prop2: "[| zprime p; 2 < p; ~([a = 0](mod p)) |] ==> 
wenzelm@16974
    52
                          Union ( SetS a p) = SRStar p"
paulson@13871
    53
  apply (auto simp add: SetS_def MultInvPair_def StandardRes_SRStar_prop4 
paulson@13871
    54
    SRStar_mult_prop2)
paulson@13871
    55
  apply (frule StandardRes_SRStar_prop3)
paulson@13871
    56
  apply (rule bexI, auto)
wenzelm@19670
    57
  done
paulson@13871
    58
nipkow@16663
    59
lemma MultInvPair_distinct: "[| zprime p; 2 < p; ~([a = 0] (mod p)); 
paulson@13871
    60
                                ~([j = 0] (mod p)); 
paulson@13871
    61
                                ~(QuadRes p a) |]  ==> 
wenzelm@16974
    62
                             ~([j = a * MultInv p j] (mod p))"
wenzelm@20369
    63
proof
nipkow@16663
    64
  assume "zprime p" and "2 < p" and "~([a = 0] (mod p))" and 
wenzelm@16974
    65
    "~([j = 0] (mod p))" and "~(QuadRes p a)"
wenzelm@16974
    66
  assume "[j = a * MultInv p j] (mod p)"
wenzelm@16974
    67
  then have "[j * j = (a * MultInv p j) * j] (mod p)"
paulson@13871
    68
    by (auto simp add: zcong_scalar)
wenzelm@16974
    69
  then have a:"[j * j = a * (MultInv p j * j)] (mod p)"
paulson@13871
    70
    by (auto simp add: zmult_ac)
wenzelm@16974
    71
  have "[j * j = a] (mod p)"
wenzelm@16974
    72
    proof -
wenzelm@16974
    73
      from prems have b: "[MultInv p j * j = 1] (mod p)"
paulson@13871
    74
        by (simp add: MultInv_prop2a)
wenzelm@16974
    75
      from b a show ?thesis
paulson@13871
    76
        by (auto simp add: zcong_zmult_prop2)
wenzelm@16974
    77
    qed
wenzelm@16974
    78
  then have "[j^2 = a] (mod p)"
wenzelm@16974
    79
    apply(subgoal_tac "2 = Suc(Suc(0))")
paulson@13871
    80
    apply (erule ssubst)
paulson@13871
    81
    apply (auto simp only: power_Suc power_0)
paulson@13871
    82
    by auto
wenzelm@16974
    83
  with prems show False
paulson@13871
    84
    by (simp add: QuadRes_def)
wenzelm@16974
    85
qed
paulson@13871
    86
nipkow@16663
    87
lemma MultInvPair_card_two: "[| zprime p; 2 < p; ~([a = 0] (mod p)); 
paulson@13871
    88
                                ~(QuadRes p a); ~([j = 0] (mod p)) |]  ==> 
wenzelm@16974
    89
                             card (MultInvPair a p j) = 2"
paulson@13871
    90
  apply (auto simp add: MultInvPair_def)
wenzelm@16974
    91
  apply (subgoal_tac "~ (StandardRes p j = StandardRes p (a * MultInv p j))")
paulson@13871
    92
  apply auto
paulson@13871
    93
  apply (simp only: StandardRes_prop2)
paulson@13871
    94
  apply (drule MultInvPair_distinct)
wenzelm@20369
    95
  apply auto back
wenzelm@20369
    96
  done
paulson@13871
    97
wenzelm@19670
    98
wenzelm@19670
    99
subsection {* Properties of SetS *}
paulson@13871
   100
wenzelm@16974
   101
lemma SetS_finite: "2 < p ==> finite (SetS a p)"
paulson@13871
   102
  by (auto simp add: SetS_def SRStar_finite [of p] finite_imageI)
paulson@13871
   103
wenzelm@16974
   104
lemma SetS_elems_finite: "\<forall>X \<in> SetS a p. finite X"
paulson@13871
   105
  by (auto simp add: SetS_def MultInvPair_def)
paulson@13871
   106
nipkow@16663
   107
lemma SetS_elems_card: "[| zprime p; 2 < p; ~([a = 0] (mod p)); 
paulson@13871
   108
                        ~(QuadRes p a) |]  ==>
wenzelm@16974
   109
                        \<forall>X \<in> SetS a p. card X = 2"
paulson@13871
   110
  apply (auto simp add: SetS_def)
paulson@13871
   111
  apply (frule StandardRes_SRStar_prop1a)
paulson@13871
   112
  apply (rule MultInvPair_card_two, auto)
wenzelm@19670
   113
  done
paulson@13871
   114
wenzelm@16974
   115
lemma Union_SetS_finite: "2 < p ==> finite (Union (SetS a p))"
nipkow@15402
   116
  by (auto simp add: SetS_finite SetS_elems_finite finite_Union)
paulson@13871
   117
paulson@13871
   118
lemma card_setsum_aux: "[| finite S; \<forall>X \<in> S. finite (X::int set); 
wenzelm@16974
   119
    \<forall>X \<in> S. card X = n |] ==> setsum card S = setsum (%x. n) S"
wenzelm@18369
   120
  by (induct set: Finites) auto
paulson@13871
   121
nipkow@16663
   122
lemma SetS_card: "[| zprime p; 2 < p; ~([a = 0] (mod p)); ~(QuadRes p a) |] ==> 
wenzelm@16974
   123
                  int(card(SetS a p)) = (p - 1) div 2"
wenzelm@16974
   124
proof -
wenzelm@16974
   125
  assume "zprime p" and "2 < p" and  "~([a = 0] (mod p))" and "~(QuadRes p a)"
wenzelm@16974
   126
  then have "(p - 1) = 2 * int(card(SetS a p))"
wenzelm@16974
   127
  proof -
wenzelm@16974
   128
    have "p - 1 = int(card(Union (SetS a p)))"
paulson@13871
   129
      by (auto simp add: prems MultInvPair_prop2 SRStar_card)
wenzelm@16974
   130
    also have "... = int (setsum card (SetS a p))"
paulson@13871
   131
      by (auto simp add: prems SetS_finite SetS_elems_finite
nipkow@15402
   132
                         MultInvPair_prop1c [of p a] card_Union_disjoint)
wenzelm@16974
   133
    also have "... = int(setsum (%x.2) (SetS a p))"
wenzelm@19670
   134
      using prems
wenzelm@19670
   135
      by (auto simp add: SetS_elems_card SetS_finite SetS_elems_finite 
paulson@15047
   136
        card_setsum_aux simp del: setsum_constant)
wenzelm@16974
   137
    also have "... = 2 * int(card( SetS a p))"
paulson@13871
   138
      by (auto simp add: prems SetS_finite setsum_const2)
wenzelm@16974
   139
    finally show ?thesis .
wenzelm@16974
   140
  qed
wenzelm@16974
   141
  from this show ?thesis
paulson@13871
   142
    by auto
wenzelm@16974
   143
qed
paulson@13871
   144
nipkow@16663
   145
lemma SetS_setprod_prop: "[| zprime p; 2 < p; ~([a = 0] (mod p));
paulson@13871
   146
                              ~(QuadRes p a); x \<in> (SetS a p) |] ==> 
wenzelm@16974
   147
                          [\<Prod>x = a] (mod p)"
paulson@13871
   148
  apply (auto simp add: SetS_def MultInvPair_def)
paulson@13871
   149
  apply (frule StandardRes_SRStar_prop1a)
wenzelm@16974
   150
  apply (subgoal_tac "StandardRes p x \<noteq> StandardRes p (a * MultInv p x)")
paulson@13871
   151
  apply (auto simp add: StandardRes_prop2 MultInvPair_distinct)
paulson@13871
   152
  apply (frule_tac m = p and x = x and y = "(a * MultInv p x)" in 
wenzelm@16974
   153
    StandardRes_prop4)
wenzelm@16974
   154
  apply (subgoal_tac "[x * (a * MultInv p x) = a * (x * MultInv p x)] (mod p)")
paulson@13871
   155
  apply (drule_tac a = "StandardRes p x * StandardRes p (a * MultInv p x)" and
paulson@13871
   156
                   b = "x * (a * MultInv p x)" and
wenzelm@16974
   157
                   c = "a * (x * MultInv p x)" in  zcong_trans, force)
paulson@13871
   158
  apply (frule_tac p = p and x = x in MultInv_prop2, auto)
paulson@13871
   159
  apply (drule_tac a = "x * MultInv p x" and b = 1 in zcong_zmult_prop2)
paulson@13871
   160
  apply (auto simp add: zmult_ac)
wenzelm@19670
   161
  done
paulson@13871
   162
wenzelm@16974
   163
lemma aux1: "[| 0 < x; (x::int) < a; x \<noteq> (a - 1) |] ==> x < a - 1"
paulson@13871
   164
  by arith
paulson@13871
   165
wenzelm@16974
   166
lemma aux2: "[| (a::int) < c; b < c |] ==> (a \<le> b | b \<le> a)"
paulson@13871
   167
  by auto
paulson@13871
   168
wenzelm@18369
   169
lemma SRStar_d22set_prop: "2 < p \<Longrightarrow> (SRStar p) = {1} \<union> (d22set (p - 1))"
wenzelm@18369
   170
  apply (induct p rule: d22set.induct)
wenzelm@18369
   171
  apply auto
nipkow@16733
   172
  apply (simp add: SRStar_def d22set.simps)
paulson@13871
   173
  apply (simp add: SRStar_def d22set.simps, clarify)
paulson@13871
   174
  apply (frule aux1)
paulson@13871
   175
  apply (frule aux2, auto)
paulson@13871
   176
  apply (simp_all add: SRStar_def)
paulson@13871
   177
  apply (simp add: d22set.simps)
paulson@13871
   178
  apply (frule d22set_le)
paulson@13871
   179
  apply (frule d22set_g_1, auto)
wenzelm@18369
   180
  done
paulson@13871
   181
nipkow@16663
   182
lemma Union_SetS_setprod_prop1: "[| zprime p; 2 < p; ~([a = 0] (mod p)); ~(QuadRes p a) |] ==>
nipkow@15392
   183
                                 [\<Prod>(Union (SetS a p)) = a ^ nat ((p - 1) div 2)] (mod p)"
nipkow@15392
   184
proof -
nipkow@16663
   185
  assume "zprime p" and "2 < p" and  "~([a = 0] (mod p))" and "~(QuadRes p a)"
nipkow@15392
   186
  then have "[\<Prod>(Union (SetS a p)) = 
nipkow@15392
   187
      setprod (setprod (%x. x)) (SetS a p)] (mod p)"
paulson@13871
   188
    by (auto simp add: SetS_finite SetS_elems_finite
nipkow@15392
   189
                       MultInvPair_prop1c setprod_Union_disjoint)
nipkow@15392
   190
  also have "[setprod (setprod (%x. x)) (SetS a p) = 
nipkow@15392
   191
      setprod (%x. a) (SetS a p)] (mod p)"
wenzelm@18369
   192
    by (rule setprod_same_function_zcong)
wenzelm@18369
   193
      (auto simp add: prems SetS_setprod_prop SetS_finite)
nipkow@15392
   194
  also (zcong_trans) have "[setprod (%x. a) (SetS a p) = 
nipkow@15392
   195
      a^(card (SetS a p))] (mod p)"
nipkow@15392
   196
    by (auto simp add: prems SetS_finite setprod_constant)
nipkow@15392
   197
  finally (zcong_trans) show ?thesis
paulson@13871
   198
    apply (rule zcong_trans)
nipkow@15392
   199
    apply (subgoal_tac "card(SetS a p) = nat((p - 1) div 2)", auto)
nipkow@15392
   200
    apply (subgoal_tac "nat(int(card(SetS a p))) = nat((p - 1) div 2)", force)
paulson@13871
   201
    apply (auto simp add: prems SetS_card)
wenzelm@18369
   202
    done
nipkow@15392
   203
qed
paulson@13871
   204
nipkow@16663
   205
lemma Union_SetS_setprod_prop2: "[| zprime p; 2 < p; ~([a = 0](mod p)) |] ==> 
wenzelm@16974
   206
                                    \<Prod>(Union (SetS a p)) = zfact (p - 1)"
wenzelm@16974
   207
proof -
wenzelm@16974
   208
  assume "zprime p" and "2 < p" and "~([a = 0](mod p))"
nipkow@15392
   209
  then have "\<Prod>(Union (SetS a p)) = \<Prod>(SRStar p)"
paulson@13871
   210
    by (auto simp add: MultInvPair_prop2)
nipkow@15392
   211
  also have "... = \<Prod>({1} \<union> (d22set (p - 1)))"
paulson@13871
   212
    by (auto simp add: prems SRStar_d22set_prop)
nipkow@15392
   213
  also have "... = zfact(p - 1)"
nipkow@15392
   214
  proof -
wenzelm@18369
   215
    have "~(1 \<in> d22set (p - 1)) & finite( d22set (p - 1))"
paulson@13871
   216
      apply (insert prems, auto)
paulson@13871
   217
      apply (drule d22set_g_1)
paulson@13871
   218
      apply (auto simp add: d22set_fin)
wenzelm@18369
   219
      done
wenzelm@18369
   220
    then have "\<Prod>({1} \<union> (d22set (p - 1))) = \<Prod>(d22set (p - 1))"
wenzelm@18369
   221
      by auto
wenzelm@18369
   222
    then show ?thesis
wenzelm@18369
   223
      by (auto simp add: d22set_prod_zfact)
wenzelm@16974
   224
  qed
nipkow@15392
   225
  finally show ?thesis .
wenzelm@16974
   226
qed
paulson@13871
   227
nipkow@16663
   228
lemma zfact_prop: "[| zprime p; 2 < p; ~([a = 0] (mod p)); ~(QuadRes p a) |] ==>
wenzelm@16974
   229
                   [zfact (p - 1) = a ^ nat ((p - 1) div 2)] (mod p)"
paulson@13871
   230
  apply (frule Union_SetS_setprod_prop1) 
paulson@13871
   231
  apply (auto simp add: Union_SetS_setprod_prop2)
wenzelm@18369
   232
  done
paulson@13871
   233
wenzelm@19670
   234
text {* \medskip Prove the first part of Euler's Criterion: *}
paulson@13871
   235
nipkow@16663
   236
lemma Euler_part1: "[| 2 < p; zprime p; ~([x = 0](mod p)); 
paulson@13871
   237
    ~(QuadRes p x) |] ==> 
wenzelm@16974
   238
      [x^(nat (((p) - 1) div 2)) = -1](mod p)"
paulson@13871
   239
  apply (frule zfact_prop, auto)
paulson@13871
   240
  apply (frule Wilson_Russ)
paulson@13871
   241
  apply (auto simp add: zcong_sym)
paulson@13871
   242
  apply (rule zcong_trans, auto)
wenzelm@18369
   243
  done
paulson@13871
   244
wenzelm@19670
   245
text {* \medskip Prove another part of Euler Criterion: *}
paulson@13871
   246
wenzelm@16974
   247
lemma aux_1: "0 < p ==> (a::int) ^ nat (p) = a * a ^ (nat (p) - 1)"
wenzelm@16974
   248
proof -
wenzelm@16974
   249
  assume "0 < p"
wenzelm@16974
   250
  then have "a ^ (nat p) =  a ^ (1 + (nat p - 1))"
paulson@13871
   251
    by (auto simp add: diff_add_assoc)
wenzelm@16974
   252
  also have "... = (a ^ 1) * a ^ (nat(p) - 1)"
paulson@13871
   253
    by (simp only: zpower_zadd_distrib)
wenzelm@16974
   254
  also have "... = a * a ^ (nat(p) - 1)"
paulson@13871
   255
    by auto
wenzelm@16974
   256
  finally show ?thesis .
wenzelm@16974
   257
qed
paulson@13871
   258
wenzelm@16974
   259
lemma aux_2: "[| (2::int) < p; p \<in> zOdd |] ==> 0 < ((p - 1) div 2)"
wenzelm@16974
   260
proof -
wenzelm@16974
   261
  assume "2 < p" and "p \<in> zOdd"
wenzelm@16974
   262
  then have "(p - 1):zEven"
paulson@13871
   263
    by (auto simp add: zEven_def zOdd_def)
wenzelm@16974
   264
  then have aux_1: "2 * ((p - 1) div 2) = (p - 1)"
paulson@13871
   265
    by (auto simp add: even_div_2_prop2)
paulson@13871
   266
  then have "1 < (p - 1)"
paulson@13871
   267
    by auto
wenzelm@16974
   268
  then have " 1 < (2 * ((p - 1) div 2))"
paulson@13871
   269
    by (auto simp add: aux_1)
wenzelm@16974
   270
  then have "0 < (2 * ((p - 1) div 2)) div 2"
paulson@13871
   271
    by auto
paulson@13871
   272
  then show ?thesis by auto
wenzelm@16974
   273
qed
paulson@13871
   274
wenzelm@19670
   275
lemma Euler_part2:
wenzelm@19670
   276
    "[| 2 < p; zprime p; [a = 0] (mod p) |] ==> [0 = a ^ nat ((p - 1) div 2)] (mod p)"
paulson@13871
   277
  apply (frule zprime_zOdd_eq_grt_2)
paulson@13871
   278
  apply (frule aux_2, auto)
paulson@13871
   279
  apply (frule_tac a = a in aux_1, auto)
paulson@13871
   280
  apply (frule zcong_zmult_prop1, auto)
wenzelm@18369
   281
  done
paulson@13871
   282
wenzelm@19670
   283
text {* \medskip Prove the final part of Euler's Criterion: *}
paulson@13871
   284
wenzelm@16974
   285
lemma aux__1: "[| ~([x = 0] (mod p)); [y ^ 2 = x] (mod p)|] ==> ~(p dvd y)"
paulson@13871
   286
  apply (subgoal_tac "[| ~([x = 0] (mod p)); [y ^ 2 = x] (mod p)|] ==> 
wenzelm@16974
   287
    ~([y ^ 2 = 0] (mod p))")
paulson@13871
   288
  apply (auto simp add: zcong_sym [of "y^2" x p] intro: zcong_trans)
paulson@13871
   289
  apply (auto simp add: zcong_eq_zdvd_prop intro: zpower_zdvd_prop1)
wenzelm@18369
   290
  done
paulson@13871
   291
wenzelm@16974
   292
lemma aux__2: "2 * nat((p - 1) div 2) =  nat (2 * ((p - 1) div 2))"
paulson@13871
   293
  by (auto simp add: nat_mult_distrib)
paulson@13871
   294
nipkow@16663
   295
lemma Euler_part3: "[| 2 < p; zprime p; ~([x = 0](mod p)); QuadRes p x |] ==> 
wenzelm@16974
   296
                      [x^(nat (((p) - 1) div 2)) = 1](mod p)"
paulson@13871
   297
  apply (subgoal_tac "p \<in> zOdd")
paulson@13871
   298
  apply (auto simp add: QuadRes_def)
paulson@13871
   299
  apply (frule aux__1, auto)
wenzelm@16974
   300
  apply (drule_tac z = "nat ((p - 1) div 2)" in zcong_zpower)
paulson@13871
   301
  apply (auto simp add: zpower_zpower)
paulson@13871
   302
  apply (rule zcong_trans)
wenzelm@16974
   303
  apply (auto simp add: zcong_sym [of "x ^ nat ((p - 1) div 2)"])
paulson@13871
   304
  apply (simp add: aux__2)
paulson@13871
   305
  apply (frule odd_minus_one_even)
paulson@13871
   306
  apply (frule even_div_2_prop2)
paulson@13871
   307
  apply (auto intro: Little_Fermat simp add: zprime_zOdd_eq_grt_2)
wenzelm@18369
   308
  done
paulson@13871
   309
wenzelm@19670
   310
wenzelm@19670
   311
text {* \medskip Finally show Euler's Criterion: *}
paulson@13871
   312
nipkow@16663
   313
theorem Euler_Criterion: "[| 2 < p; zprime p |] ==> [(Legendre a p) =
wenzelm@16974
   314
    a^(nat (((p) - 1) div 2))] (mod p)"
paulson@13871
   315
  apply (auto simp add: Legendre_def Euler_part2)
wenzelm@20369
   316
  apply (frule Euler_part3, auto simp add: zcong_sym)[]
wenzelm@20369
   317
  apply (frule Euler_part1, auto simp add: zcong_sym)[]
wenzelm@18369
   318
  done
paulson@13871
   319
wenzelm@18369
   320
end