src/HOL/Number_Theory/Gauss.thy
author wenzelm
Fri Apr 07 21:17:18 2017 +0200 (2017-04-07)
changeset 65435 378175f44328
parent 65413 cb7f9d7d35e6
child 66817 0b12755ccbb2
permissions -rw-r--r--
tuned headers;
wenzelm@65435
     1
(*  Title:      HOL/Number_Theory/Gauss.thy
wenzelm@65413
     2
    Authors:    Jeremy Avigad, David Gray, and Adam Kramer
lp15@55730
     3
wenzelm@65413
     4
Ported by lcp but unfinished.
lp15@55730
     5
*)
lp15@55730
     6
wenzelm@60526
     7
section \<open>Gauss' Lemma\<close>
lp15@55730
     8
lp15@55730
     9
theory Gauss
wenzelm@65413
    10
  imports Euler_Criterion
lp15@55730
    11
begin
lp15@55730
    12
wenzelm@65413
    13
lemma cong_prime_prod_zero_nat:
wenzelm@65413
    14
  "[a * b = 0] (mod p) \<Longrightarrow> prime p \<Longrightarrow> [a = 0] (mod p) \<or> [b = 0] (mod p)"
wenzelm@65413
    15
  for a :: nat
haftmann@64631
    16
  by (auto simp add: cong_altdef_nat prime_dvd_mult_iff)
lp15@55730
    17
wenzelm@65413
    18
lemma cong_prime_prod_zero_int:
wenzelm@65413
    19
  "[a * b = 0] (mod p) \<Longrightarrow> prime p \<Longrightarrow> [a = 0] (mod p) \<or> [b = 0] (mod p)"
wenzelm@65413
    20
  for a :: int
haftmann@64631
    21
  by (auto simp add: cong_altdef_int prime_dvd_mult_iff)
lp15@55730
    22
lp15@55730
    23
lp15@55730
    24
locale GAUSS =
lp15@55730
    25
  fixes p :: "nat"
lp15@55730
    26
  fixes a :: "int"
lp15@55730
    27
  assumes p_prime: "prime p"
lp15@55730
    28
  assumes p_ge_2: "2 < p"
lp15@55730
    29
  assumes p_a_relprime: "[a \<noteq> 0](mod p)"
wenzelm@65413
    30
  assumes a_nonzero: "0 < a"
lp15@55730
    31
begin
lp15@55730
    32
lp15@55730
    33
definition "A = {0::int <.. ((int p - 1) div 2)}"
lp15@55730
    34
definition "B = (\<lambda>x. x * a) ` A"
lp15@55730
    35
definition "C = (\<lambda>x. x mod p) ` B"
lp15@55730
    36
definition "D = C \<inter> {.. (int p - 1) div 2}"
lp15@55730
    37
definition "E = C \<inter> {(int p - 1) div 2 <..}"
lp15@55730
    38
definition "F = (\<lambda>x. (int p - x)) ` E"
lp15@55730
    39
lp15@55730
    40
wenzelm@60526
    41
subsection \<open>Basic properties of p\<close>
lp15@55730
    42
lp15@55730
    43
lemma odd_p: "odd p"
wenzelm@65413
    44
  by (metis p_prime p_ge_2 prime_odd_nat)
lp15@55730
    45
lp15@55730
    46
lemma p_minus_one_l: "(int p - 1) div 2 < p"
lp15@55730
    47
proof -
lp15@55730
    48
  have "(p - 1) div 2 \<le> (p - 1) div 1"
lp15@55730
    49
    by (metis div_by_1 div_le_dividend)
lp15@55730
    50
  also have "\<dots> = p - 1" by simp
wenzelm@65413
    51
  finally show ?thesis
wenzelm@65413
    52
    using p_ge_2 by arith
lp15@55730
    53
qed
lp15@55730
    54
lp15@55730
    55
lemma p_eq2: "int p = (2 * ((int p - 1) div 2)) + 1"
wenzelm@65413
    56
  using odd_p p_ge_2 nonzero_mult_div_cancel_left [of 2 "p - 1"] by simp
lp15@55730
    57
wenzelm@65413
    58
lemma p_odd_int: obtains z :: int where "int p = 2 * z + 1" "0 < z"
wenzelm@65413
    59
proof
wenzelm@65413
    60
  let ?z = "(int p - 1) div 2"
wenzelm@65413
    61
  show "int p = 2 * ?z + 1" by (rule p_eq2)
wenzelm@65413
    62
  show "0 < ?z"
wenzelm@65413
    63
    using p_ge_2 by linarith
wenzelm@65413
    64
qed
lp15@55730
    65
lp15@55730
    66
wenzelm@60526
    67
subsection \<open>Basic Properties of the Gauss Sets\<close>
lp15@55730
    68
wenzelm@65413
    69
lemma finite_A: "finite A"
wenzelm@65413
    70
  by (auto simp add: A_def)
lp15@55730
    71
wenzelm@65413
    72
lemma finite_B: "finite B"
wenzelm@65413
    73
  by (auto simp add: B_def finite_A)
lp15@55730
    74
wenzelm@65413
    75
lemma finite_C: "finite C"
wenzelm@65413
    76
  by (auto simp add: C_def finite_B)
lp15@55730
    77
wenzelm@65413
    78
lemma finite_D: "finite D"
wenzelm@65413
    79
  by (auto simp add: D_def finite_C)
lp15@55730
    80
wenzelm@65413
    81
lemma finite_E: "finite E"
wenzelm@65413
    82
  by (auto simp add: E_def finite_C)
lp15@55730
    83
wenzelm@65413
    84
lemma finite_F: "finite F"
wenzelm@65413
    85
  by (auto simp add: F_def finite_E)
lp15@55730
    86
lp15@55730
    87
lemma C_eq: "C = D \<union> E"
wenzelm@65413
    88
  by (auto simp add: C_def D_def E_def)
lp15@55730
    89
lp15@55730
    90
lemma A_card_eq: "card A = nat ((int p - 1) div 2)"
lp15@55730
    91
  by (auto simp add: A_def)
lp15@55730
    92
lp15@55730
    93
lemma inj_on_xa_A: "inj_on (\<lambda>x. x * a) A"
lp15@55730
    94
  using a_nonzero by (simp add: A_def inj_on_def)
lp15@55730
    95
wenzelm@65413
    96
definition ResSet :: "int \<Rightarrow> int set \<Rightarrow> bool"
wenzelm@65413
    97
  where "ResSet m X \<longleftrightarrow> (\<forall>y1 y2. y1 \<in> X \<and> y2 \<in> X \<and> [y1 = y2] (mod m) \<longrightarrow> y1 = y2)"
lp15@55730
    98
lp15@55730
    99
lemma ResSet_image:
wenzelm@65413
   100
  "0 < m \<Longrightarrow> ResSet m A \<Longrightarrow> \<forall>x \<in> A. \<forall>y \<in> A. ([f x = f y](mod m) \<longrightarrow> x = y) \<Longrightarrow> ResSet m (f ` A)"
lp15@55730
   101
  by (auto simp add: ResSet_def)
lp15@55730
   102
lp15@55730
   103
lemma A_res: "ResSet p A"
wenzelm@65413
   104
  using p_ge_2 by (auto simp add: A_def ResSet_def intro!: cong_less_imp_eq_int)
lp15@55730
   105
lp15@55730
   106
lemma B_res: "ResSet p B"
lp15@55730
   107
proof -
wenzelm@65413
   108
  have *: "x = y"
wenzelm@65413
   109
    if a: "[x * a = y * a] (mod p)"
wenzelm@65413
   110
    and b: "0 < x"
wenzelm@65413
   111
    and c: "x \<le> (int p - 1) div 2"
wenzelm@65413
   112
    and d: "0 < y"
wenzelm@65413
   113
    and e: "y \<le> (int p - 1) div 2"
wenzelm@65413
   114
    for x y
wenzelm@65413
   115
  proof -
wenzelm@65413
   116
    from p_a_relprime have "\<not> p dvd a"
eberlm@63534
   117
      by (simp add: cong_altdef_int)
wenzelm@65413
   118
    with p_prime have "coprime a (int p)"
wenzelm@65413
   119
      by (subst gcd.commute, intro prime_imp_coprime) auto
wenzelm@65413
   120
    with a cong_mult_rcancel_int [of a "int p" x y] have "[x = y] (mod p)"
wenzelm@65413
   121
      by simp
lp15@55730
   122
    with cong_less_imp_eq_int [of x y p] p_minus_one_l
wenzelm@65413
   123
      order_le_less_trans [of x "(int p - 1) div 2" p]
wenzelm@65413
   124
      order_le_less_trans [of y "(int p - 1) div 2" p]
wenzelm@65413
   125
    show ?thesis
haftmann@62348
   126
      by (metis b c cong_less_imp_eq_int d e zero_less_imp_eq_int of_nat_0_le_iff)
wenzelm@65413
   127
  qed
lp15@55730
   128
  show ?thesis
lp15@55730
   129
    apply (insert p_ge_2 p_a_relprime p_minus_one_l)
lp15@55730
   130
    apply (auto simp add: B_def)
lp15@55730
   131
    apply (rule ResSet_image)
wenzelm@65413
   132
      apply (auto simp add: A_res)
wenzelm@65413
   133
    apply (auto simp add: A_def *)
lp15@55730
   134
    done
wenzelm@65413
   135
qed
lp15@55730
   136
lp15@55730
   137
lemma SR_B_inj: "inj_on (\<lambda>x. x mod p) B"
lp15@55730
   138
proof -
wenzelm@65413
   139
  have False
wenzelm@65413
   140
    if a: "x * a mod p = y * a mod p"
wenzelm@65413
   141
    and b: "0 < x"
wenzelm@65413
   142
    and c: "x \<le> (int p - 1) div 2"
wenzelm@65413
   143
    and d: "0 < y"
wenzelm@65413
   144
    and e: "y \<le> (int p - 1) div 2"
wenzelm@65413
   145
    and f: "x \<noteq> y"
wenzelm@65413
   146
    for x y
wenzelm@65413
   147
  proof -
wenzelm@65413
   148
    from a have a': "[x * a = y * a](mod p)"
wenzelm@65413
   149
      by (metis cong_int_def)
wenzelm@65413
   150
    from p_a_relprime have "\<not>p dvd a"
wenzelm@65413
   151
      by (simp add: cong_altdef_int)
wenzelm@65413
   152
    with p_prime have "coprime a (int p)"
wenzelm@65413
   153
      by (subst gcd.commute, intro prime_imp_coprime) auto
wenzelm@65413
   154
    with a' cong_mult_rcancel_int [of a "int p" x y]
eberlm@63534
   155
    have "[x = y] (mod p)" by simp
wenzelm@65413
   156
    with cong_less_imp_eq_int [of x y p] p_minus_one_l
wenzelm@65413
   157
      order_le_less_trans [of x "(int p - 1) div 2" p]
wenzelm@65413
   158
      order_le_less_trans [of y "(int p - 1) div 2" p]
wenzelm@65413
   159
    have "x = y"
wenzelm@65413
   160
      by (metis b c cong_less_imp_eq_int d e zero_less_imp_eq_int of_nat_0_le_iff)
wenzelm@65413
   161
    then show ?thesis
wenzelm@65413
   162
      by (simp add: f)
wenzelm@65413
   163
  qed
lp15@55730
   164
  then show ?thesis
lp15@55730
   165
    by (auto simp add: B_def inj_on_def A_def) metis
lp15@55730
   166
qed
lp15@55730
   167
lp15@55730
   168
lemma inj_on_pminusx_E: "inj_on (\<lambda>x. p - x) E"
lp15@55730
   169
  apply (auto simp add: E_def C_def B_def A_def)
wenzelm@65413
   170
  apply (rule inj_on_inverseI [where g = "op - (int p)"])
lp15@55730
   171
  apply auto
lp15@55730
   172
  done
lp15@55730
   173
wenzelm@65413
   174
lemma nonzero_mod_p: "0 < x \<Longrightarrow> x < int p \<Longrightarrow> [x \<noteq> 0](mod p)"
wenzelm@65413
   175
  for x :: int
haftmann@59545
   176
  by (simp add: cong_int_def)
lp15@55730
   177
lp15@55730
   178
lemma A_ncong_p: "x \<in> A \<Longrightarrow> [x \<noteq> 0](mod p)"
lp15@55730
   179
  by (rule nonzero_mod_p) (auto simp add: A_def)
lp15@55730
   180
lp15@55730
   181
lemma A_greater_zero: "x \<in> A \<Longrightarrow> 0 < x"
lp15@55730
   182
  by (auto simp add: A_def)
lp15@55730
   183
lp15@55730
   184
lemma B_ncong_p: "x \<in> B \<Longrightarrow> [x \<noteq> 0](mod p)"
wenzelm@65413
   185
  by (auto simp: B_def p_prime p_a_relprime A_ncong_p dest: cong_prime_prod_zero_int)
lp15@55730
   186
lp15@55730
   187
lemma B_greater_zero: "x \<in> B \<Longrightarrow> 0 < x"
nipkow@56544
   188
  using a_nonzero by (auto simp add: B_def A_greater_zero)
lp15@55730
   189
lp15@55730
   190
lemma C_greater_zero: "y \<in> C \<Longrightarrow> 0 < y"
lp15@55730
   191
proof (auto simp add: C_def)
lp15@55730
   192
  fix x :: int
wenzelm@65413
   193
  assume x: "x \<in> B"
wenzelm@65413
   194
  moreover from x have "x mod int p \<noteq> 0"
wenzelm@65413
   195
    using B_ncong_p cong_int_def by simp
wenzelm@65413
   196
  moreover have "int y = 0 \<or> 0 < int y" for y
wenzelm@65413
   197
    by linarith
wenzelm@65413
   198
  ultimately show "0 < x mod int p"
lp15@55730
   199
    by (metis (no_types) B_greater_zero Divides.transfer_int_nat_functions(2) zero_less_imp_eq_int)
lp15@55730
   200
qed
lp15@55730
   201
wenzelm@65413
   202
lemma F_subset: "F \<subseteq> {x. 0 < x \<and> x \<le> ((int p - 1) div 2)}"
lp15@55730
   203
  apply (auto simp add: F_def E_def C_def)
wenzelm@65413
   204
   apply (metis p_ge_2 Divides.pos_mod_bound nat_int zless_nat_conj)
lp15@55730
   205
  apply (auto intro: p_odd_int)
lp15@55730
   206
  done
lp15@55730
   207
wenzelm@65413
   208
lemma D_subset: "D \<subseteq> {x. 0 < x \<and> x \<le> ((p - 1) div 2)}"
lp15@55730
   209
  by (auto simp add: D_def C_greater_zero)
lp15@55730
   210
wenzelm@65413
   211
lemma F_eq: "F = {x. \<exists>y \<in> A. (x = p - ((y * a) mod p) \<and> (int p - 1) div 2 < (y * a) mod p)}"
lp15@55730
   212
  by (auto simp add: F_def E_def D_def C_def B_def A_def)
lp15@55730
   213
wenzelm@65413
   214
lemma D_eq: "D = {x. \<exists>y \<in> A. (x = (y * a) mod p \<and> (y * a) mod p \<le> (int p - 1) div 2)}"
lp15@55730
   215
  by (auto simp add: D_def C_def B_def A_def)
lp15@55730
   216
wenzelm@65413
   217
lemma all_A_relprime:
wenzelm@65413
   218
  assumes "x \<in> A"
wenzelm@65413
   219
  shows "gcd x p = 1"
lp15@55730
   220
  using p_prime A_ncong_p [OF assms]
eberlm@63633
   221
  by (auto simp: cong_altdef_int gcd.commute[of _ "int p"] intro!: prime_imp_coprime)
lp15@55730
   222
nipkow@64272
   223
lemma A_prod_relprime: "gcd (prod id A) p = 1"
nipkow@64272
   224
  by (metis id_def all_A_relprime prod_coprime)
lp15@55730
   225
lp15@55730
   226
wenzelm@60526
   227
subsection \<open>Relationships Between Gauss Sets\<close>
lp15@55730
   228
wenzelm@65413
   229
lemma StandardRes_inj_on_ResSet: "ResSet m X \<Longrightarrow> inj_on (\<lambda>b. b mod m) X"
lp15@55730
   230
  by (auto simp add: ResSet_def inj_on_def cong_int_def)
lp15@55730
   231
lp15@55730
   232
lemma B_card_eq_A: "card B = card A"
lp15@55730
   233
  using finite_A by (simp add: finite_A B_def inj_on_xa_A card_image)
lp15@55730
   234
lp15@55730
   235
lemma B_card_eq: "card B = nat ((int p - 1) div 2)"
lp15@55730
   236
  by (simp add: B_card_eq_A A_card_eq)
lp15@55730
   237
lp15@55730
   238
lemma F_card_eq_E: "card F = card E"
wenzelm@65413
   239
  using finite_E by (simp add: F_def inj_on_pminusx_E card_image)
lp15@55730
   240
lp15@55730
   241
lemma C_card_eq_B: "card C = card B"
lp15@55730
   242
proof -
lp15@55730
   243
  have "inj_on (\<lambda>x. x mod p) B"
wenzelm@65413
   244
    by (metis SR_B_inj)
lp15@55730
   245
  then show ?thesis
lp15@55730
   246
    by (metis C_def card_image)
lp15@55730
   247
qed
lp15@55730
   248
lp15@55730
   249
lemma D_E_disj: "D \<inter> E = {}"
lp15@55730
   250
  by (auto simp add: D_def E_def)
lp15@55730
   251
lp15@55730
   252
lemma C_card_eq_D_plus_E: "card C = card D + card E"
lp15@55730
   253
  by (auto simp add: C_eq card_Un_disjoint D_E_disj finite_D finite_E)
lp15@55730
   254
nipkow@64272
   255
lemma C_prod_eq_D_times_E: "prod id E * prod id D = prod id C"
nipkow@64272
   256
  by (metis C_eq D_E_disj finite_D finite_E inf_commute prod.union_disjoint sup_commute)
lp15@55730
   257
nipkow@64272
   258
lemma C_B_zcong_prod: "[prod id C = prod id B] (mod p)"
lp15@55730
   259
  apply (auto simp add: C_def)
lp15@55730
   260
  apply (insert finite_B SR_B_inj)
nipkow@64272
   261
  apply (drule prod.reindex [of "\<lambda>x. x mod int p" B id])
haftmann@57418
   262
  apply auto
nipkow@64272
   263
  apply (rule cong_prod_int)
lp15@55730
   264
  apply (auto simp add: cong_int_def)
lp15@55730
   265
  done
lp15@55730
   266
lp15@55730
   267
lemma F_Un_D_subset: "(F \<union> D) \<subseteq> A"
wenzelm@65413
   268
  by (intro Un_least subset_trans [OF F_subset] subset_trans [OF D_subset]) (auto simp: A_def)
lp15@55730
   269
lp15@55730
   270
lemma F_D_disj: "(F \<inter> D) = {}"
lp15@55730
   271
proof (auto simp add: F_eq D_eq)
wenzelm@65413
   272
  fix y z :: int
wenzelm@65413
   273
  assume "p - (y * a) mod p = (z * a) mod p"
wenzelm@65413
   274
  then have "[(y * a) mod p + (z * a) mod p = 0] (mod p)"
haftmann@57512
   275
    by (metis add.commute diff_eq_eq dvd_refl cong_int_def dvd_eq_mod_eq_0 mod_0)
wenzelm@65413
   276
  moreover have "[y * a = (y * a) mod p] (mod p)"
lp15@55730
   277
    by (metis cong_int_def mod_mod_trivial)
lp15@55730
   278
  ultimately have "[a * (y + z) = 0] (mod p)"
haftmann@57512
   279
    by (metis cong_int_def mod_add_left_eq mod_add_right_eq mult.commute ring_class.ring_distribs(1))
wenzelm@65413
   280
  with p_prime a_nonzero p_a_relprime have a: "[y + z = 0] (mod p)"
eberlm@63534
   281
    by (auto dest!: cong_prime_prod_zero_int)
lp15@55730
   282
  assume b: "y \<in> A" and c: "z \<in> A"
wenzelm@65413
   283
  then have "0 < y + z"
wenzelm@65413
   284
    by (auto simp: A_def)
wenzelm@65413
   285
  moreover from b c p_eq2 have "y + z < p"
wenzelm@65413
   286
    by (auto simp: A_def)
lp15@55730
   287
  ultimately show False
lp15@55730
   288
    by (metis a nonzero_mod_p)
lp15@55730
   289
qed
lp15@55730
   290
lp15@55730
   291
lemma F_Un_D_card: "card (F \<union> D) = nat ((p - 1) div 2)"
lp15@55730
   292
proof -
lp15@55730
   293
  have "card (F \<union> D) = card E + card D"
lp15@55730
   294
    by (auto simp add: finite_F finite_D F_D_disj card_Un_disjoint F_card_eq_E)
lp15@55730
   295
  then have "card (F \<union> D) = card C"
lp15@55730
   296
    by (simp add: C_card_eq_D_plus_E)
lp15@55730
   297
  then show "card (F \<union> D) = nat ((p - 1) div 2)"
lp15@55730
   298
    by (simp add: C_card_eq_B B_card_eq)
lp15@55730
   299
qed
lp15@55730
   300
lp15@55730
   301
lemma F_Un_D_eq_A: "F \<union> D = A"
wenzelm@65413
   302
  using finite_A F_Un_D_subset A_card_eq F_Un_D_card by (auto simp add: card_seteq)
lp15@55730
   303
wenzelm@65413
   304
lemma prod_D_F_eq_prod_A: "prod id D * prod id F = prod id A"
nipkow@64272
   305
  by (metis F_D_disj F_Un_D_eq_A Int_commute Un_commute finite_D finite_F prod.union_disjoint)
lp15@55730
   306
wenzelm@65413
   307
lemma prod_F_zcong: "[prod id F = ((-1) ^ (card E)) * prod id E] (mod p)"
lp15@55730
   308
proof -
nipkow@64272
   309
  have FE: "prod id F = prod (op - p) E"
lp15@55730
   310
    apply (auto simp add: F_def)
lp15@55730
   311
    apply (insert finite_E inj_on_pminusx_E)
wenzelm@65413
   312
    apply (drule prod.reindex)
wenzelm@65413
   313
    apply auto
lp15@55730
   314
    done
lp15@55730
   315
  then have "\<forall>x \<in> E. [(p-x) mod p = - x](mod p)"
lp15@55730
   316
    by (metis cong_int_def minus_mod_self1 mod_mod_trivial)
nipkow@64272
   317
  then have "[prod ((\<lambda>x. x mod p) o (op - p)) E = prod (uminus) E](mod p)"
wenzelm@65413
   318
    using finite_E p_ge_2 cong_prod_int [of E "(\<lambda>x. x mod p) o (op - p)" uminus p]
lp15@55730
   319
    by auto
nipkow@64272
   320
  then have two: "[prod id F = prod (uminus) E](mod p)"
nipkow@64272
   321
    by (metis FE cong_cong_mod_int cong_refl_int cong_prod_int minus_mod_self1)
wenzelm@65413
   322
  have "prod uminus E = (-1) ^ card E * prod id E"
lp15@55730
   323
    using finite_E by (induct set: finite) auto
lp15@55730
   324
  with two show ?thesis
lp15@55730
   325
    by simp
lp15@55730
   326
qed
lp15@55730
   327
lp15@55730
   328
wenzelm@60526
   329
subsection \<open>Gauss' Lemma\<close>
lp15@55730
   330
nipkow@64272
   331
lemma aux: "prod id A * (- 1) ^ card E * a ^ card A * (- 1) ^ card E = prod id A * a ^ card A"
wenzelm@65413
   332
  by (metis (no_types) minus_minus mult.commute mult.left_commute power_minus power_one)
lp15@55730
   333
wenzelm@65413
   334
theorem pre_gauss_lemma: "[a ^ nat((int p - 1) div 2) = (-1) ^ (card E)] (mod p)"
lp15@55730
   335
proof -
nipkow@64272
   336
  have "[prod id A = prod id F * prod id D](mod p)"
wenzelm@65413
   337
    by (auto simp: prod_D_F_eq_prod_A mult.commute cong del: prod.strong_cong)
nipkow@64272
   338
  then have "[prod id A = ((-1)^(card E) * prod id E) * prod id D] (mod p)"
wenzelm@65413
   339
    by (rule cong_trans_int) (metis cong_scalar_int prod_F_zcong)
nipkow@64272
   340
  then have "[prod id A = ((-1)^(card E) * prod id C)] (mod p)"
haftmann@57512
   341
    by (metis C_prod_eq_D_times_E mult.commute mult.left_commute)
nipkow@64272
   342
  then have "[prod id A = ((-1)^(card E) * prod id B)] (mod p)"
lp15@55730
   343
    by (rule cong_trans_int) (metis C_B_zcong_prod cong_scalar2_int)
wenzelm@65413
   344
  then have "[prod id A = ((-1)^(card E) * prod id ((\<lambda>x. x * a) ` A))] (mod p)"
lp15@55730
   345
    by (simp add: B_def)
wenzelm@65413
   346
  then have "[prod id A = ((-1)^(card E) * prod (\<lambda>x. x * a) A)] (mod p)"
nipkow@64272
   347
    by (simp add: inj_on_xa_A prod.reindex)
wenzelm@65413
   348
  moreover have "prod (\<lambda>x. x * a) A = prod (\<lambda>x. a) A * prod id A"
lp15@55730
   349
    using finite_A by (induct set: finite) auto
wenzelm@65413
   350
  ultimately have "[prod id A = ((-1)^(card E) * (prod (\<lambda>x. a) A * prod id A))] (mod p)"
lp15@55730
   351
    by simp
wenzelm@65413
   352
  then have "[prod id A = ((-1)^(card E) * a^(card A) * prod id A)](mod p)"
wenzelm@65413
   353
    by (rule cong_trans_int)
wenzelm@65413
   354
      (simp add: cong_scalar2_int cong_scalar_int finite_A prod_constant mult.assoc)
nipkow@64272
   355
  then have a: "[prod id A * (-1)^(card E) =
nipkow@64272
   356
      ((-1)^(card E) * a^(card A) * prod id A * (-1)^(card E))](mod p)"
lp15@55730
   357
    by (rule cong_scalar_int)
nipkow@64272
   358
  then have "[prod id A * (-1)^(card E) = prod id A *
lp15@55730
   359
      (-1)^(card E) * a^(card A) * (-1)^(card E)](mod p)"
wenzelm@65413
   360
    by (rule cong_trans_int) (simp add: a mult.commute mult.left_commute)
nipkow@64272
   361
  then have "[prod id A * (-1)^(card E) = prod id A * a^(card A)](mod p)"
wenzelm@65413
   362
    by (rule cong_trans_int) (simp add: aux cong del: prod.strong_cong)
haftmann@58410
   363
  with A_prod_relprime have "[(- 1) ^ card E = a ^ card A](mod p)"
lp15@55730
   364
    by (metis cong_mult_lcancel_int)
lp15@55730
   365
  then show ?thesis
lp15@55730
   366
    by (simp add: A_card_eq cong_sym_int)
lp15@55730
   367
qed
lp15@55730
   368
wenzelm@65413
   369
theorem gauss_lemma: "Legendre a p = (-1) ^ (card E)"
lp15@55730
   370
proof -
wenzelm@65413
   371
  from euler_criterion p_prime p_ge_2 have "[Legendre a p = a^(nat (((p) - 1) div 2))] (mod p)"
lp15@55730
   372
    by auto
wenzelm@65413
   373
  moreover have "int ((p - 1) div 2) = (int p - 1) div 2"
wenzelm@65413
   374
    using p_eq2 by linarith
wenzelm@65413
   375
  then have "[a ^ nat (int ((p - 1) div 2)) = a ^ nat ((int p - 1) div 2)] (mod int p)"
wenzelm@65413
   376
    by force
wenzelm@65413
   377
  ultimately have "[Legendre a p = (-1) ^ (card E)] (mod p)"
wenzelm@65413
   378
    using pre_gauss_lemma cong_trans_int by blast
wenzelm@65413
   379
  moreover from p_a_relprime have "Legendre a p = 1 \<or> Legendre a p = -1"
lp15@55730
   380
    by (auto simp add: Legendre_def)
wenzelm@65413
   381
  moreover have "(-1::int) ^ (card E) = 1 \<or> (-1::int) ^ (card E) = -1"
eberlm@64282
   382
    using neg_one_even_power neg_one_odd_power by blast
eberlm@64282
   383
  moreover have "[1 \<noteq> - 1] (mod int p)"
eberlm@64282
   384
    using cong_altdef_int nonzero_mod_p[of 2] p_odd_int by fastforce
lp15@55730
   385
  ultimately show ?thesis
eberlm@64282
   386
    by (auto simp add: cong_sym_int)
lp15@55730
   387
qed
lp15@55730
   388
lp15@55730
   389
end
lp15@55730
   390
lp15@55730
   391
end