src/HOL/Number_Theory/Pocklington.thy
author paulson <lp15@cam.ac.uk>
Mon, 19 Feb 2018 16:44:45 +0000
changeset 67673 c8caefb20564
parent 67399 eab6ce8368fa
child 68157 057d5b4ce47e
permissions -rw-r--r--
lots of new material, ultimately related to measure theory
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
     1
(*  Title:      HOL/Number_Theory/Pocklington.thy
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
     2
    Author:     Amine Chaieb
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
     3
*)
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
     4
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 58889
diff changeset
     5
section \<open>Pocklington's Theorem for Primes\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
     6
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
     7
theory Pocklington
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
     8
imports Residues
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
     9
begin
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    10
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    11
subsection \<open>Lemmas about previously defined terms\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    12
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    13
lemma prime_nat_iff'': "prime (p::nat) \<longleftrightarrow> p \<noteq> 0 \<and> p \<noteq> 1 \<and> (\<forall>m. 0 < m \<and> m < p \<longrightarrow> coprime p m)"
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
    14
  apply (auto simp add: prime_nat_iff)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
    15
   apply (rule coprimeI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
    16
   apply (auto dest: nat_dvd_not_less simp add: ac_simps)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
    17
  apply (metis One_nat_def dvd_1_iff_1 dvd_pos_nat gcd_nat.order_iff is_unit_gcd linorder_neqE_nat nat_dvd_not_less)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
    18
  done
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    19
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    20
lemma finite_number_segment: "card { m. 0 < m \<and> m < n } = n - 1"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    21
proof -
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    22
  have "{ m. 0 < m \<and> m < n } = {1..<n}" by auto
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    23
  then show ?thesis by simp
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    24
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    25
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    26
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    27
subsection \<open>Some basic theorems about solving congruences\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    28
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    29
lemma cong_solve:
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    30
  fixes n :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    31
  assumes an: "coprime a n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    32
  shows "\<exists>x. [a * x = b] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    33
proof (cases "a = 0")
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    34
  case True
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    35
  with an show ?thesis
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
    36
    by (simp add: cong_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    37
next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    38
  case False
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    39
  from bezout_add_strong_nat [OF this]
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    40
  obtain d x y where dxy: "d dvd a" "d dvd n" "a * x = n * y + d" by blast
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    41
  from dxy(1,2) have d1: "d = 1"
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
    42
    using assms coprime_common_divisor [of a n d] by simp
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    43
  with dxy(3) have "a * x * b = (n * y + 1) * b"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    44
    by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    45
  then have "a * (x * b) = n * (y * b) + b"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    46
    by (auto simp: algebra_simps)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    47
  then have "a * (x * b) mod n = (n * (y * b) + b) mod n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    48
    by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    49
  then have "a * (x * b) mod n = b mod n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    50
    by (simp add: mod_add_left_eq)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    51
  then have "[a * (x * b) = b] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
    52
    by (simp only: cong_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    53
  then show ?thesis by blast
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    54
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    55
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    56
lemma cong_solve_unique:
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    57
  fixes n :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    58
  assumes an: "coprime a n" and nz: "n \<noteq> 0"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    59
  shows "\<exists>!x. x < n \<and> [a * x = b] (mod n)"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    60
proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    61
  from cong_solve[OF an] obtain x where x: "[a * x = b] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    62
    by blast
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    63
  let ?P = "\<lambda>x. x < n \<and> [a * x = b] (mod n)"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    64
  let ?x = "x mod n"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    65
  from x have *: "[a * ?x = b] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
    66
    by (simp add: cong_def mod_mult_right_eq[of a x n])
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    67
  from mod_less_divisor[ of n x] nz * have Px: "?P ?x" by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    68
  have "y = ?x" if Py: "y < n" "[a * y = b] (mod n)" for y
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    69
  proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    70
    from Py(2) * have "[a * y = a * ?x] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
    71
      by (simp add: cong_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    72
    then have "[y = ?x] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    73
      by (metis an cong_mult_lcancel_nat)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    74
    with mod_less[OF Py(1)] mod_less_divisor[ of n x] nz
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    75
    show ?thesis
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
    76
      by (simp add: cong_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    77
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    78
  with Px show ?thesis by blast
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    79
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    80
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    81
lemma cong_solve_unique_nontrivial:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    82
  fixes p :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    83
  assumes p: "prime p"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    84
    and pa: "coprime p a"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    85
    and x0: "0 < x"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    86
    and xp: "x < p"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    87
  shows "\<exists>!y. 0 < y \<and> y < p \<and> [x * y = a] (mod p)"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    88
proof -
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    89
  from pa have ap: "coprime a p"
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
    90
    by (simp add: ac_simps)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
    91
  from x0 xp p have px: "coprime x p"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
    92
    by (auto simp add: prime_nat_iff'' ac_simps)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    93
  obtain y where y: "y < p" "[x * y = a] (mod p)" "\<forall>z. z < p \<and> [x * z = a] (mod p) \<longrightarrow> z = y"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
    94
    by (metis cong_solve_unique neq0_conv p prime_gt_0_nat px)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    95
  have "y \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    96
  proof
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    97
    assume "y = 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
    98
    with y(2) have "p dvd a"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
    99
      using cong_dvd_iff by auto
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   100
    with not_prime_1 p pa show False
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   101
      by (auto simp add: gcd_nat.order_iff)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   102
  qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   103
  with y show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   104
    by blast
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   105
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   106
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   107
lemma cong_unique_inverse_prime:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   108
  fixes p :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   109
  assumes "prime p" and "0 < x" and "x < p"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   110
  shows "\<exists>!y. 0 < y \<and> y < p \<and> [x * y = 1] (mod p)"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   111
  by (rule cong_solve_unique_nontrivial) (use assms in simp_all)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   112
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   113
lemma chinese_remainder_coprime_unique:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   114
  fixes a :: nat
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   115
  assumes ab: "coprime a b" and az: "a \<noteq> 0" and bz: "b \<noteq> 0"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   116
    and ma: "coprime m a" and nb: "coprime n b"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   117
  shows "\<exists>!x. coprime x (a * b) \<and> x < a * b \<and> [x = m] (mod a) \<and> [x = n] (mod b)"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   118
proof -
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   119
  let ?P = "\<lambda>x. x < a * b \<and> [x = m] (mod a) \<and> [x = n] (mod b)"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   120
  from binary_chinese_remainder_unique_nat[OF ab az bz]
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   121
  obtain x where x: "x < a * b" "[x = m] (mod a)" "[x = n] (mod b)" "\<forall>y. ?P y \<longrightarrow> y = x"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   122
    by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   123
  from ma nb x have "coprime x a" "coprime x b"
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   124
    using cong_imp_coprime_nat cong_sym by blast+
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   125
  then have "coprime x (a*b)"
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   126
    by simp
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   127
  with x show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   128
    by blast
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   129
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   130
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   131
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   132
subsection \<open>Lucas's theorem\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   133
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   134
lemma lucas_coprime_lemma:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   135
  fixes n :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   136
  assumes m: "m \<noteq> 0" and am: "[a^m = 1] (mod n)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   137
  shows "coprime a n"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   138
proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   139
  consider "n = 1" | "n = 0" | "n > 1" by arith
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   140
  then show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   141
  proof cases
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   142
    case 1
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   143
    then show ?thesis by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   144
  next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   145
    case 2
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   146
    with am m show ?thesis
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   147
      by simp
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   148
  next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   149
    case 3
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   150
    from m obtain m' where m': "m = Suc m'" by (cases m) blast+
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   151
    have "d = 1" if d: "d dvd a" "d dvd n" for d
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   152
    proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   153
      from am mod_less[OF \<open>n > 1\<close>] have am1: "a^m mod n = 1"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   154
        by (simp add: cong_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   155
      from dvd_mult2[OF d(1), of "a^m'"] have dam: "d dvd a^m"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   156
        by (simp add: m')
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   157
      from dvd_mod_iff[OF d(2), of "a^m"] dam am1 show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   158
        by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   159
    qed
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   160
    then show ?thesis
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   161
      by (auto intro: coprimeI)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   162
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   163
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   164
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   165
lemma lucas_weak:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   166
  fixes n :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   167
  assumes n: "n \<ge> 2"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   168
    and an: "[a ^ (n - 1) = 1] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   169
    and nm: "\<forall>m. 0 < m \<and> m < n - 1 \<longrightarrow> \<not> [a ^ m = 1] (mod n)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   170
  shows "prime n"
65726
f5d64d094efe More material on totient function
eberlm <eberlm@in.tum.de>
parents: 65465
diff changeset
   171
proof (rule totient_imp_prime)
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   172
  show "totient n = n - 1"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   173
  proof (rule ccontr)
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   174
    have "[a ^ totient n = 1] (mod n)"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   175
      by (rule euler_theorem, rule lucas_coprime_lemma [of "n - 1"]) (use n an in auto)
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   176
    moreover assume "totient n \<noteq> n - 1"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   177
    then have "totient n > 0" "totient n < n - 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   178
      using \<open>n \<ge> 2\<close> and totient_less[of n] by simp_all
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   179
    ultimately show False
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   180
      using nm by auto
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   181
  qed
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   182
qed (use n in auto)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   183
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   184
lemma nat_exists_least_iff: "(\<exists>(n::nat). P n) \<longleftrightarrow> (\<exists>n. P n \<and> (\<forall>m < n. \<not> P m))"
55337
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   185
  by (metis ex_least_nat_le not_less0)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   186
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   187
lemma nat_exists_least_iff': "(\<exists>(n::nat). P n) \<longleftrightarrow> P (Least P) \<and> (\<forall>m < (Least P). \<not> P m)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   188
  (is "?lhs \<longleftrightarrow> ?rhs")
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   189
proof
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   190
  show ?lhs if ?rhs
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   191
    using that by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   192
  show ?rhs if ?lhs
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   193
  proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   194
    from \<open>?lhs\<close> obtain n where n: "P n" by blast
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   195
    let ?x = "Least P"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   196
    have "\<not> P m" if "m < ?x" for m
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   197
      by (rule not_less_Least[OF that])
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   198
    with LeastI_ex[OF \<open>?lhs\<close>] show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   199
      by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   200
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   201
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   202
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   203
theorem lucas:
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   204
  assumes n2: "n \<ge> 2" and an1: "[a^(n - 1) = 1] (mod n)"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   205
    and pn: "\<forall>p. prime p \<and> p dvd n - 1 \<longrightarrow> [a^((n - 1) div p) \<noteq> 1] (mod n)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   206
  shows "prime n"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   207
proof-
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   208
  from n2 have n01: "n \<noteq> 0" "n \<noteq> 1" "n - 1 \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   209
    by arith+
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   210
  from mod_less_divisor[of n 1] n01 have onen: "1 mod n = 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   211
    by simp
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   212
  from lucas_coprime_lemma[OF n01(3) an1] cong_imp_coprime_nat an1
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   213
  have an: "coprime a n" "coprime (a ^ (n - 1)) n"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   214
    using \<open>n \<ge> 2\<close> by simp_all
67091
1393c2340eec more symbols;
wenzelm
parents: 67051
diff changeset
   215
  have False if H0: "\<exists>m. 0 < m \<and> m < n - 1 \<and> [a ^ m = 1] (mod n)" (is "\<exists>m. ?P m")
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   216
  proof -
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   217
    from H0[unfolded nat_exists_least_iff[of ?P]] obtain m where
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   218
      m: "0 < m" "m < n - 1" "[a ^ m = 1] (mod n)" "\<forall>k <m. \<not>?P k"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   219
      by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   220
    have False if nm1: "(n - 1) mod m > 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   221
    proof -
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   222
      from mod_less_divisor[OF m(1)] have th0:"(n - 1) mod m < m" by blast
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   223
      let ?y = "a^ ((n - 1) div m * m)"
64242
93c6f0da5c70 more standardized theorem names for facts involving the div and mod identity
haftmann
parents: 63905
diff changeset
   224
      note mdeq = div_mult_mod_eq[of "(n - 1)" m]
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   225
      have yn: "coprime ?y n"
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   226
        using an(1) by (cases "(n - Suc 0) div m * m = 0") auto
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   227
      have "?y mod n = (a^m)^((n - 1) div m) mod n"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   228
        by (simp add: algebra_simps power_mult)
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   229
      also have "\<dots> = (a^m mod n)^((n - 1) div m) mod n"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   230
        using power_mod[of "a^m" n "(n - 1) div m"] by simp
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   231
      also have "\<dots> = 1" using m(3)[unfolded cong_def onen] onen
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   232
        by (metis power_one)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   233
      finally have *: "?y mod n = 1"  .
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   234
      have **: "[?y * a ^ ((n - 1) mod m) = ?y* 1] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   235
        using an1[unfolded cong_def onen] onen
64242
93c6f0da5c70 more standardized theorem names for facts involving the div and mod identity
haftmann
parents: 63905
diff changeset
   236
          div_mult_mod_eq[of "(n - 1)" m, symmetric]
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   237
        by (simp add:power_add[symmetric] cong_def * del: One_nat_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   238
      have "[a ^ ((n - 1) mod m) = 1] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   239
        by (metis cong_mult_rcancel_nat mult.commute ** yn)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   240
      with m(4)[rule_format, OF th0] nm1
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   241
        less_trans[OF mod_less_divisor[OF m(1), of "n - 1"] m(2)] show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   242
        by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   243
    qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   244
    then have "(n - 1) mod m = 0" by auto
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   245
    then have mn: "m dvd n - 1" by presburger
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   246
    then obtain r where r: "n - 1 = m * r"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   247
      unfolding dvd_def by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   248
    from n01 r m(2) have r01: "r \<noteq> 0" "r \<noteq> 1" by auto
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   249
    obtain p where p: "prime p" "p dvd r"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   250
      by (metis prime_factor_nat r01(2))
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   251
    then have th: "prime p \<and> p dvd n - 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   252
      unfolding r by (auto intro: dvd_mult)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   253
    from r have "(a ^ ((n - 1) div p)) mod n = (a^(m*r div p)) mod n"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   254
      by (simp add: power_mult)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   255
    also have "\<dots> = (a^(m*(r div p))) mod n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   256
      using div_mult1_eq[of m r p] p(2)[unfolded dvd_eq_mod_eq_0] by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   257
    also have "\<dots> = ((a^m)^(r div p)) mod n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   258
      by (simp add: power_mult)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   259
    also have "\<dots> = ((a^m mod n)^(r div p)) mod n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   260
      using power_mod ..
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   261
    also from m(3) onen have "\<dots> = 1"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   262
      by (simp add: cong_def)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   263
    finally have "[(a ^ ((n - 1) div p))= 1] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   264
      using onen by (simp add: cong_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   265
    with pn th show ?thesis by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   266
  qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   267
  then have "\<forall>m. 0 < m \<and> m < n - 1 \<longrightarrow> \<not> [a ^ m = 1] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   268
    by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   269
  then show ?thesis by (rule lucas_weak[OF n2 an1])
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   270
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   271
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   272
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   273
subsection \<open>Definition of the order of a number mod n (0 in non-coprime case)\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   274
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   275
definition "ord n a = (if coprime n a then Least (\<lambda>d. d > 0 \<and> [a ^d = 1] (mod n)) else 0)"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   276
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   277
text \<open>This has the expected properties.\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   278
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   279
lemma coprime_ord:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   280
  fixes n::nat
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   281
  assumes "coprime n a"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   282
  shows "ord n a > 0 \<and> [a ^(ord n a) = 1] (mod n) \<and> (\<forall>m. 0 < m \<and> m < ord n a \<longrightarrow> [a^ m \<noteq> 1] (mod n))"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   283
proof-
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   284
  let ?P = "\<lambda>d. 0 < d \<and> [a ^ d = 1] (mod n)"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   285
  from bigger_prime[of a] obtain p where p: "prime p" "a < p"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   286
    by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   287
  from assms have o: "ord n a = Least ?P"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   288
    by (simp add: ord_def)
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   289
  have ex: "\<exists>m>0. ?P m"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   290
  proof (cases "n \<ge> 2")
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   291
    case True
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   292
    moreover from assms have "coprime a n"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   293
      by (simp add: ac_simps)
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   294
    then have "[a ^ totient n = 1] (mod n)"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   295
      by (rule euler_theorem)
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   296
    ultimately show ?thesis
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   297
      by (auto intro: exI [where x = "totient n"])
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   298
  next
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   299
    case False
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   300
    then have "n = 0 \<or> n = 1"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   301
      by auto
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   302
    with assms show ?thesis
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   303
      by auto
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   304
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   305
  from nat_exists_least_iff'[of ?P] ex assms show ?thesis
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   306
    unfolding o[symmetric] by auto
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   307
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   308
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   309
text \<open>With the special value \<open>0\<close> for non-coprime case, it's more convenient.\<close>
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   310
lemma ord_works: "[a ^ (ord n a) = 1] (mod n) \<and> (\<forall>m. 0 < m \<and> m < ord n a \<longrightarrow> \<not> [a^ m = 1] (mod n))"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   311
  for n :: nat
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   312
  by (cases "coprime n a") (use coprime_ord[of n a] in \<open>auto simp add: ord_def cong_def\<close>)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   313
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   314
lemma ord: "[a^(ord n a) = 1] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   315
  for n :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   316
  using ord_works by blast
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   317
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   318
lemma ord_minimal: "0 < m \<Longrightarrow> m < ord n a \<Longrightarrow> \<not> [a^m = 1] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   319
  for n :: nat
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   320
  using ord_works by blast
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   321
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   322
lemma ord_eq_0: "ord n a = 0 \<longleftrightarrow> \<not> coprime n a"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   323
  for n :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   324
  by (cases "coprime n a") (simp add: coprime_ord, simp add: ord_def)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   325
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   326
lemma divides_rexp: "x dvd y \<Longrightarrow> x dvd (y ^ Suc n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   327
  for x y :: nat
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   328
  by (simp add: dvd_mult2[of x y])
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   329
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   330
lemma ord_divides:"[a ^ d = 1] (mod n) \<longleftrightarrow> ord n a dvd d"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   331
  (is "?lhs \<longleftrightarrow> ?rhs")
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   332
  for n :: nat
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   333
proof
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   334
  assume ?rhs
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   335
  then obtain k where "d = ord n a * k"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   336
    unfolding dvd_def by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   337
  then have "[a ^ d = (a ^ (ord n a) mod n)^k] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   338
    by (simp add : cong_def power_mult power_mod)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   339
  also have "[(a ^ (ord n a) mod n)^k = 1] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   340
    using ord[of a n, unfolded cong_def]
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   341
    by (simp add: cong_def power_mod)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   342
  finally show ?lhs .
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   343
next
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   344
  assume ?lhs
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   345
  show ?rhs
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   346
  proof (cases "coprime n a")
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   347
    case prem: False
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   348
    then have o: "ord n a = 0" by (simp add: ord_def)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   349
    show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   350
    proof (cases d)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   351
      case 0
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   352
      with o prem show ?thesis by (simp add: cong_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   353
    next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   354
      case (Suc d')
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   355
      then have d0: "d \<noteq> 0" by simp
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   356
      from prem obtain p where p: "p dvd n" "p dvd a" "p \<noteq> 1"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   357
        by (auto elim: not_coprimeE) 
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   358
      from \<open>?lhs\<close> obtain q1 q2 where q12: "a ^ d + n * q1 = 1 + n * q2"
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   359
        using prem d0 lucas_coprime_lemma
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   360
        by (auto elim: not_coprimeE simp add: ac_simps)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   361
      then have "a ^ d + n * q1 - n * q2 = 1" by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   362
      with dvd_diff_nat [OF dvd_add [OF divides_rexp]]  dvd_mult2 Suc p have "p dvd 1"
55337
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   363
        by metis
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   364
      with p(3) have False by simp
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   365
      then show ?thesis ..
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   366
    qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   367
  next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   368
    case H: True
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   369
    let ?o = "ord n a"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   370
    let ?q = "d div ord n a"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   371
    let ?r = "d mod ord n a"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   372
    have eqo: "[(a^?o)^?q = 1] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   373
      using cong_pow ord_works by fastforce
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   374
    from H have onz: "?o \<noteq> 0" by (simp add: ord_eq_0)
67345
debef21cbed6 tuned op's
nipkow
parents: 67091
diff changeset
   375
    then have opos: "?o > 0" by simp
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   376
    from div_mult_mod_eq[of d "ord n a"] \<open>?lhs\<close>
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   377
    have "[a^(?o*?q + ?r) = 1] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   378
      by (simp add: cong_def mult.commute)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   379
    then have "[(a^?o)^?q * (a^?r) = 1] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   380
      by (simp add: cong_def power_mult[symmetric] power_add[symmetric])
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   381
    then have th: "[a^?r = 1] (mod n)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   382
      using eqo mod_mult_left_eq[of "(a^?o)^?q" "a^?r" n]
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   383
      by (simp add: cong_def del: One_nat_def) (metis mod_mult_left_eq nat_mult_1)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   384
    show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   385
    proof (cases "?r = 0")
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   386
      case True
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   387
      then show ?thesis by (simp add: dvd_eq_mod_eq_0)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   388
    next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   389
      case False
67345
debef21cbed6 tuned op's
nipkow
parents: 67091
diff changeset
   390
      with mod_less_divisor[OF opos, of d] have r0o:"?r >0 \<and> ?r < ?o" by simp
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   391
      from conjunct2[OF ord_works[of a n], rule_format, OF r0o] th
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   392
      show ?thesis by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   393
    qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   394
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   395
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   396
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   397
lemma order_divides_totient:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   398
  "ord n a dvd totient n" if "coprime n a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   399
  using that euler_theorem [of a n]
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   400
  by (simp add: ord_divides [symmetric] ac_simps)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   401
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   402
lemma order_divides_expdiff:
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   403
  fixes n::nat and a::nat assumes na: "coprime n a"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   404
  shows "[a^d = a^e] (mod n) \<longleftrightarrow> [d = e] (mod (ord n a))"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   405
proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   406
  have th: "[a^d = a^e] (mod n) \<longleftrightarrow> [d = e] (mod (ord n a))"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   407
    if na: "coprime n a" and ed: "(e::nat) \<le> d"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   408
    for n a d e :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   409
  proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   410
    from na ed have "\<exists>c. d = e + c" by presburger
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   411
    then obtain c where c: "d = e + c" ..
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   412
    from na have an: "coprime a n"
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   413
      by (simp add: ac_simps)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   414
    then have aen: "coprime (a ^ e) n"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   415
      by (cases "e > 0") simp_all
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   416
    from an have acn: "coprime (a ^ c) n"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   417
      by (cases "c > 0") simp_all
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   418
    from c have "[a^d = a^e] (mod n) \<longleftrightarrow> [a^(e + c) = a^(e + 0)] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   419
      by simp
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   420
    also have "\<dots> \<longleftrightarrow> [a^e* a^c = a^e *a^0] (mod n)" by (simp add: power_add)
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   421
    also have  "\<dots> \<longleftrightarrow> [a ^ c = 1] (mod n)"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   422
      using cong_mult_lcancel_nat [OF aen, of "a^c" "a^0"] by simp
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   423
    also have "\<dots> \<longleftrightarrow> ord n a dvd c"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   424
      by (simp only: ord_divides)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   425
    also have "\<dots> \<longleftrightarrow> [e + c = e + 0] (mod ord n a)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   426
      by (auto simp add: cong_altdef_nat)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   427
    finally show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   428
      using c by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   429
  qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   430
  consider "e \<le> d" | "d \<le> e" by arith
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   431
  then show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   432
  proof cases
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   433
    case 1
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   434
    with na show ?thesis by (rule th)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   435
  next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   436
    case 2
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   437
    from th[OF na this] show ?thesis
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   438
      by (metis cong_sym)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   439
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   440
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   441
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   442
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   443
subsection \<open>Another trivial primality characterization\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   444
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   445
lemma prime_prime_factor: "prime n \<longleftrightarrow> n \<noteq> 1 \<and> (\<forall>p. prime p \<and> p dvd n \<longrightarrow> p = n)"
55337
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   446
  (is "?lhs \<longleftrightarrow> ?rhs")
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   447
  for n :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   448
proof (cases "n = 0 \<or> n = 1")
55337
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   449
  case True
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   450
  then show ?thesis
63633
2accfb71e33b is_prime -> prime
eberlm <eberlm@in.tum.de>
parents: 63534
diff changeset
   451
     by (metis bigger_prime dvd_0_right not_prime_1 not_prime_0)
55337
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   452
next
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   453
  case False
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   454
  show ?thesis
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   455
  proof
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   456
    assume "prime n"
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   457
    then show ?rhs
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   458
      by (metis not_prime_1 prime_nat_iff)
55337
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   459
  next
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   460
    assume ?rhs
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   461
    with False show "prime n"
63633
2accfb71e33b is_prime -> prime
eberlm <eberlm@in.tum.de>
parents: 63534
diff changeset
   462
      by (auto simp: prime_nat_iff) (metis One_nat_def prime_factor_nat prime_nat_iff)
55337
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   463
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   464
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   465
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   466
lemma prime_divisor_sqrt: "prime n \<longleftrightarrow> n \<noteq> 1 \<and> (\<forall>d. d dvd n \<and> d\<^sup>2 \<le> n \<longrightarrow> d = 1)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   467
  for n :: nat
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   468
proof -
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   469
  consider "n = 0" | "n = 1" | "n \<noteq> 0" "n \<noteq> 1" by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   470
  then show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   471
  proof cases
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   472
    case 1
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   473
    then show ?thesis by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   474
  next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   475
    case 2
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   476
    then show ?thesis by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   477
  next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   478
    case n: 3
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   479
    then have np: "n > 1" by arith
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   480
    {
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   481
      fix d
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   482
      assume d: "d dvd n" "d\<^sup>2 \<le> n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   483
        and H: "\<forall>m. m dvd n \<longrightarrow> m = 1 \<or> m = n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   484
      from H d have d1n: "d = 1 \<or> d = n" by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   485
      then have "d = 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   486
      proof
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   487
        assume dn: "d = n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   488
        from n have "n\<^sup>2 > n * 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   489
          by (simp add: power2_eq_square)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   490
        with dn d(2) show ?thesis by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   491
      qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   492
    }
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   493
    moreover
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   494
    {
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   495
      fix d assume d: "d dvd n" and H: "\<forall>d'. d' dvd n \<and> d'\<^sup>2 \<le> n \<longrightarrow> d' = 1"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   496
      from d n have "d \<noteq> 0"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   497
        by (metis dvd_0_left_iff)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   498
      then have dp: "d > 0" by simp
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   499
      from d[unfolded dvd_def] obtain e where e: "n= d*e" by blast
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   500
      from n dp e have ep:"e > 0" by simp
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   501
      from dp ep have "d\<^sup>2 \<le> n \<or> e\<^sup>2 \<le> n"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   502
        by (auto simp add: e power2_eq_square mult_le_cancel_left)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   503
      then have "d = 1 \<or> d = n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   504
      proof
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   505
        assume "d\<^sup>2 \<le> n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   506
        with H[rule_format, of d] d have "d = 1" by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   507
        then show ?thesis ..
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   508
      next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   509
        assume h: "e\<^sup>2 \<le> n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   510
        from e have "e dvd n" by (simp add: dvd_def mult.commute)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   511
        with H[rule_format, of e] h have "e = 1" by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   512
        with e have "d = n" by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   513
        then show ?thesis ..
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   514
      qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   515
    }
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   516
    ultimately show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   517
      unfolding prime_nat_iff using np n(2) by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   518
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   519
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   520
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   521
lemma prime_prime_factor_sqrt:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   522
  "prime (n::nat) \<longleftrightarrow> n \<noteq> 0 \<and> n \<noteq> 1 \<and> (\<nexists>p. prime p \<and> p dvd n \<and> p\<^sup>2 \<le> n)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   523
  (is "?lhs \<longleftrightarrow>?rhs")
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   524
proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   525
  consider "n = 0" | "n = 1" | "n \<noteq> 0" "n \<noteq> 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   526
    by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   527
  then show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   528
  proof cases
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   529
    case 1
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   530
    then show ?thesis by (metis not_prime_0)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   531
  next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   532
    case 2
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   533
    then show ?thesis by (metis not_prime_1)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   534
  next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   535
    case n: 3
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   536
    show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   537
    proof
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   538
      assume ?lhs
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   539
      from this[unfolded prime_divisor_sqrt] n show ?rhs
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   540
        by (metis prime_prime_factor)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   541
    next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   542
      assume ?rhs
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   543
      {
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   544
        fix d
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   545
        assume d: "d dvd n" "d\<^sup>2 \<le> n" "d \<noteq> 1"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   546
        then obtain p where p: "prime p" "p dvd d"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   547
          by (metis prime_factor_nat)
55337
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   548
        from d(1) n have dp: "d > 0"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   549
          by (metis dvd_0_left neq0_conv)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   550
        from mult_mono[OF dvd_imp_le[OF p(2) dp] dvd_imp_le[OF p(2) dp]] d(2)
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   551
        have "p\<^sup>2 \<le> n" unfolding power2_eq_square by arith
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   552
        with \<open>?rhs\<close> n p(1) dvd_trans[OF p(2) d(1)] have False
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   553
          by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   554
      }
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   555
      with n prime_divisor_sqrt show ?lhs by auto
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   556
    qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   557
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   558
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   559
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   560
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   561
subsection \<open>Pocklington theorem\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   562
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   563
lemma pocklington_lemma:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   564
  fixes p :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   565
  assumes n: "n \<ge> 2" and nqr: "n - 1 = q * r"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   566
    and an: "[a^ (n - 1) = 1] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   567
    and aq: "\<forall>p. prime p \<and> p dvd q \<longrightarrow> coprime (a ^ ((n - 1) div p) - 1) n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   568
    and pp: "prime p" and pn: "p dvd n"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   569
  shows "[p = 1] (mod q)"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   570
proof -
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   571
  have p01: "p \<noteq> 0" "p \<noteq> 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   572
    using pp by (auto intro: prime_gt_0_nat)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   573
  obtain k where k: "a ^ (q * r) - 1 = n * k"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   574
    by (metis an cong_to_1_nat dvd_def nqr)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   575
  from pn[unfolded dvd_def] obtain l where l: "n = p * l"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   576
    by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   577
  have a0: "a \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   578
  proof
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   579
    assume "a = 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   580
    with n have "a^ (n - 1) = 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   581
      by (simp add: power_0_left)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   582
    with n an mod_less[of 1 n] show False
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   583
      by (simp add: power_0_left cong_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   584
  qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   585
  with n nqr have aqr0: "a ^ (q * r) \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   586
    by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   587
  then have "(a ^ (q * r) - 1) + 1  = a ^ (q * r)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   588
    by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   589
  with k l have "a ^ (q * r) = p * l * k + 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   590
    by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   591
  then have "a ^ (r * q) + p * 0 = 1 + p * (l * k)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   592
    by (simp add: ac_simps)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   593
  then have odq: "ord p (a^r) dvd q"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   594
    unfolding ord_divides[symmetric] power_mult[symmetric]
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   595
    by (metis an cong_dvd_modulus_nat mult.commute nqr pn)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   596
  from odq[unfolded dvd_def] obtain d where d: "q = ord p (a^r) * d"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   597
    by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   598
  have d1: "d = 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   599
  proof (rule ccontr)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   600
    assume d1: "d \<noteq> 1"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   601
    obtain P where P: "prime P" "P dvd d"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   602
      by (metis d1 prime_factor_nat)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   603
    from d dvd_mult[OF P(2), of "ord p (a^r)"] have Pq: "P dvd q" by simp
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   604
    from aq P(1) Pq have caP:"coprime (a^ ((n - 1) div P) - 1) n" by blast
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   605
    from Pq obtain s where s: "q = P*s" unfolding dvd_def by blast
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   606
    from P(1) have P0: "P \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   607
      by (metis not_prime_0)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   608
    from P(2) obtain t where t: "d = P*t" unfolding dvd_def by blast
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   609
    from d s t P0  have s': "ord p (a^r) * t = s"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   610
      by (metis mult.commute mult_cancel1 mult.assoc)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   611
    have "ord p (a^r) * t*r = r * ord p (a^r) * t"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 55370
diff changeset
   612
      by (metis mult.assoc mult.commute)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   613
    then have exps: "a^(ord p (a^r) * t*r) = ((a ^ r) ^ ord p (a^r)) ^ t"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   614
      by (simp only: power_mult)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   615
    then have "[((a ^ r) ^ ord p (a^r)) ^ t= 1] (mod p)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   616
      by (metis cong_pow ord power_one)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   617
    then have pd0: "p dvd a^(ord p (a^r) * t*r) - 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   618
      by (metis cong_to_1_nat exps)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   619
    from nqr s s' have "(n - 1) div P = ord p (a^r) * t*r"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   620
      using P0 by simp
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   621
    with caP have "coprime (a ^ (ord p (a ^ r) * t * r) - 1) n"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   622
      by simp
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   623
    with p01 pn pd0 coprime_common_divisor [of _ n p] show False
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   624
      by auto
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   625
  qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   626
  with d have o: "ord p (a^r) = q" by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   627
  from pp totient_prime [of p] have totient_eq: "totient p = p - 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   628
    by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   629
  {
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   630
    fix d
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   631
    assume d: "d dvd p" "d dvd a" "d \<noteq> 1"
63633
2accfb71e33b is_prime -> prime
eberlm <eberlm@in.tum.de>
parents: 63534
diff changeset
   632
    from pp[unfolded prime_nat_iff] d have dp: "d = p" by blast
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   633
    from n have "n \<noteq> 0" by simp
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   634
    then have False using d dp pn an
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   635
      by auto (metis One_nat_def Suc_lessI
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   636
        \<open>1 < p \<and> (\<forall>m. m dvd p \<longrightarrow> m = 1 \<or> m = p)\<close> \<open>a ^ (q * r) = p * l * k + 1\<close> add_diff_cancel_left' dvd_diff_nat dvd_power dvd_triv_left gcd_nat.trans nat_dvd_not_less nqr zero_less_diff zero_less_one) 
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   637
  }
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   638
  then have cpa: "coprime p a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   639
    by (auto intro: coprimeI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   640
  then have arp: "coprime (a ^ r) p"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66888
diff changeset
   641
    by (cases "r > 0") (simp_all add: ac_simps)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   642
  from euler_theorem [OF arp, simplified ord_divides] o totient_eq have "q dvd (p - 1)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   643
    by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   644
  then obtain d where d:"p - 1 = q * d"
55337
5d45fb978d5a Number_Theory no longer introduces One_nat_def as a simprule. Tidied some proofs.
paulson <lp15@cam.ac.uk>
parents: 55321
diff changeset
   645
    unfolding dvd_def by blast
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   646
  have "p \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   647
    by (metis p01(1))
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   648
  with d have "p + q * 0 = 1 + q * d" by simp
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   649
  then show ?thesis
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 55370
diff changeset
   650
    by (metis cong_iff_lin_nat mult.commute)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   651
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   652
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   653
theorem pocklington:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   654
  assumes n: "n \<ge> 2" and nqr: "n - 1 = q * r" and sqr: "n \<le> q\<^sup>2"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   655
    and an: "[a^ (n - 1) = 1] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   656
    and aq: "\<forall>p. prime p \<and> p dvd q \<longrightarrow> coprime (a^ ((n - 1) div p) - 1) n"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   657
  shows "prime n"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   658
  unfolding prime_prime_factor_sqrt[of n]
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   659
proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   660
  let ?ths = "n \<noteq> 0 \<and> n \<noteq> 1 \<and> (\<nexists>p. prime p \<and> p dvd n \<and> p\<^sup>2 \<le> n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   661
  from n have n01: "n \<noteq> 0" "n \<noteq> 1" by arith+
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   662
  {
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   663
    fix p
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   664
    assume p: "prime p" "p dvd n" "p\<^sup>2 \<le> n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   665
    from p(3) sqr have "p^(Suc 1) \<le> q^(Suc 1)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   666
      by (simp add: power2_eq_square)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   667
    then have pq: "p \<le> q"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   668
      by (metis le0 power_le_imp_le_base)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   669
    from pocklington_lemma[OF n nqr an aq p(1,2)] have *: "q dvd p - 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   670
      by (metis cong_to_1_nat)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   671
    have "p - 1 \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   672
      using prime_ge_2_nat [OF p(1)] by arith
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   673
    with pq * have False
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   674
      by (simp add: nat_dvd_not_less)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   675
  }
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   676
  with n01 show ?ths by blast
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   677
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   678
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   679
text \<open>Variant for application, to separate the exponentiation.\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   680
lemma pocklington_alt:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   681
  assumes n: "n \<ge> 2" and nqr: "n - 1 = q * r" and sqr: "n \<le> q\<^sup>2"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   682
    and an: "[a^ (n - 1) = 1] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   683
    and aq: "\<forall>p. prime p \<and> p dvd q \<longrightarrow> (\<exists>b. [a^((n - 1) div p) = b] (mod n) \<and> coprime (b - 1) n)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   684
  shows "prime n"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   685
proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   686
  {
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   687
    fix p
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   688
    assume p: "prime p" "p dvd q"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   689
    from aq[rule_format] p obtain b where b: "[a^((n - 1) div p) = b] (mod n)" "coprime (b - 1) n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   690
      by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   691
    have a0: "a \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   692
    proof
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   693
      assume a0: "a = 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   694
      from n an have "[0 = 1] (mod n)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   695
        unfolding a0 power_0_left by auto
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   696
      then show False
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   697
        using n by (simp add: cong_def dvd_eq_mod_eq_0[symmetric])
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   698
    qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   699
    then have a1: "a \<ge> 1" by arith
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   700
    from one_le_power[OF a1] have ath: "1 \<le> a ^ ((n - 1) div p)" .
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   701
    have b0: "b \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   702
    proof
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   703
      assume b0: "b = 0"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   704
      from p(2) nqr have "(n - 1) mod p = 0"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   705
        by (metis mod_0 mod_mod_cancel mod_mult_self1_is_0)
64242
93c6f0da5c70 more standardized theorem names for facts involving the div and mod identity
haftmann
parents: 63905
diff changeset
   706
      with div_mult_mod_eq[of "n - 1" p]
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   707
      have "(n - 1) div p * p= n - 1" by auto
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   708
      then have eq: "(a^((n - 1) div p))^p = a^(n - 1)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   709
        by (simp only: power_mult[symmetric])
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   710
      have "p - 1 \<noteq> 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   711
        using prime_ge_2_nat [OF p(1)] by arith
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   712
      then have pS: "Suc (p - 1) = p" by arith
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   713
      from b have d: "n dvd a^((n - 1) div p)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   714
        unfolding b0 by auto
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   715
      from divides_rexp[OF d, of "p - 1"] pS eq cong_dvd_iff [OF an] n show False
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   716
        by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   717
    qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   718
    then have b1: "b \<ge> 1" by arith
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   719
    from cong_imp_coprime_nat[OF Cong.cong_diff_nat[OF cong_sym [OF b(1)] cong_refl [of 1] b1]]
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   720
      ath b1 b nqr
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   721
    have "coprime (a ^ ((n - 1) div p) - 1) n"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   722
      by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   723
  }
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   724
  then have "\<forall>p. prime p \<and> p dvd q \<longrightarrow> coprime (a ^ ((n - 1) div p) - 1) n "
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   725
    by blast
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   726
  then show ?thesis by (rule pocklington[OF n nqr sqr an])
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   727
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   728
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   729
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   730
subsection \<open>Prime factorizations\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   731
55370
e6be866b5f5b minimal document;
wenzelm
parents: 55346
diff changeset
   732
(* FIXME some overlap with material in UniqueFactorization, class unique_factorization *)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   733
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   734
definition "primefact ps n \<longleftrightarrow> foldr ( * ) ps 1 = n \<and> (\<forall>p\<in> set ps. prime p)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   735
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   736
lemma primefact:
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   737
  fixes n :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   738
  assumes n: "n \<noteq> 0"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   739
  shows "\<exists>ps. primefact ps n"
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 62429
diff changeset
   740
proof -
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   741
  obtain xs where xs: "mset xs = prime_factorization n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   742
    using ex_mset [of "prime_factorization n"] by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   743
  from assms have "n = prod_mset (prime_factorization n)"
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63633
diff changeset
   744
    by (simp add: prod_mset_prime_factorization)
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63633
diff changeset
   745
  also have "\<dots> = prod_mset (mset xs)" by (simp add: xs)
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   746
  also have "\<dots> = foldr ( * ) xs 1" by (induct xs) simp_all
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   747
  finally have "foldr ( * ) xs 1 = n" ..
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   748
  moreover from xs have "\<forall>p\<in>#mset xs. prime p" by auto
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 62429
diff changeset
   749
  ultimately have "primefact xs n" by (auto simp: primefact_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   750
  then show ?thesis ..
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   751
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   752
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   753
lemma primefact_contains:
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   754
  fixes p :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   755
  assumes pf: "primefact ps n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   756
    and p: "prime p"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   757
    and pn: "p dvd n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   758
  shows "p \<in> set ps"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   759
  using pf p pn
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   760
proof (induct ps arbitrary: p n)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   761
  case Nil
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   762
  then show ?case by (auto simp: primefact_def)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   763
next
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   764
  case (Cons q qs)
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   765
  from Cons.prems[unfolded primefact_def]
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   766
  have q: "prime q" "q * foldr ( * ) qs 1 = n" "\<forall>p \<in>set qs. prime p"
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   767
    and p: "prime p" "p dvd q * foldr ( * ) qs 1"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   768
    by simp_all
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   769
  consider "p dvd q" | "p dvd foldr ( * ) qs 1"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   770
    by (metis p prime_dvd_mult_eq_nat)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   771
  then show ?case
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   772
  proof cases
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   773
    case 1
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   774
    with p(1) q(1) have "p = q"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   775
      unfolding prime_nat_iff by auto
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   776
    then show ?thesis by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   777
  next
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   778
    case prem: 2
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   779
    from q(3) have pqs: "primefact qs (foldr ( * ) qs 1)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   780
      by (simp add: primefact_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   781
    from Cons.hyps[OF pqs p(1) prem] show ?thesis by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   782
  qed
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   783
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   784
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   785
lemma primefact_variant: "primefact ps n \<longleftrightarrow> foldr ( * ) ps 1 = n \<and> list_all prime ps"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   786
  by (auto simp add: primefact_def list_all_iff)
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   787
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   788
text \<open>Variant of Lucas theorem.\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   789
lemma lucas_primefact:
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   790
  assumes n: "n \<ge> 2" and an: "[a^(n - 1) = 1] (mod n)"
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   791
    and psn: "foldr ( * ) ps 1 = n - 1"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   792
    and psp: "list_all (\<lambda>p. prime p \<and> \<not> [a^((n - 1) div p) = 1] (mod n)) ps"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   793
  shows "prime n"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   794
proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   795
  {
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   796
    fix p
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   797
    assume p: "prime p" "p dvd n - 1" "[a ^ ((n - 1) div p) = 1] (mod n)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   798
    from psn psp have psn1: "primefact ps (n - 1)"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   799
      by (auto simp add: list_all_iff primefact_variant)
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   800
    from p(3) primefact_contains[OF psn1 p(1,2)] psp
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   801
    have False by (induct ps) auto
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   802
  }
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   803
  with lucas[OF n an] show ?thesis by blast
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   804
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   805
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   806
text \<open>Variant of Pocklington theorem.\<close>
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   807
lemma pocklington_primefact:
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   808
  assumes n: "n \<ge> 2" and qrn: "q*r = n - 1" and nq2: "n \<le> q\<^sup>2"
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67345
diff changeset
   809
    and arnb: "(a^r) mod n = b" and psq: "foldr ( * ) ps 1 = q"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   810
    and bqn: "(b^q) mod n = 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   811
    and psp: "list_all (\<lambda>p. prime p \<and> coprime ((b^(q div p)) mod n - 1) n) ps"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   812
  shows "prime n"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   813
proof -
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   814
  from bqn psp qrn
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   815
  have bqn: "a ^ (n - 1) mod n = 1"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   816
    and psp: "list_all (\<lambda>p. prime p \<and> coprime (a^(r *(q div p)) mod n - 1) n) ps"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   817
    unfolding arnb[symmetric] power_mod
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   818
    by (simp_all add: power_mult[symmetric] algebra_simps)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   819
  from n have n0: "n > 0" by arith
64242
93c6f0da5c70 more standardized theorem names for facts involving the div and mod identity
haftmann
parents: 63905
diff changeset
   820
  from div_mult_mod_eq[of "a^(n - 1)" n]
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   821
    mod_less_divisor[OF n0, of "a^(n - 1)"]
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   822
  have an1: "[a ^ (n - 1) = 1] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   823
    by (metis bqn cong_def mod_mod_trivial)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   824
  have "coprime (a ^ ((n - 1) div p) - 1) n" if p: "prime p" "p dvd q" for p
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   825
  proof -
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   826
    from psp psq have pfpsq: "primefact ps q"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   827
      by (auto simp add: primefact_variant list_all_iff)
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   828
    from psp primefact_contains[OF pfpsq p]
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   829
    have p': "coprime (a ^ (r * (q div p)) mod n - 1) n"
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   830
      by (simp add: list_all_iff)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   831
    from p prime_nat_iff have p01: "p \<noteq> 0" "p \<noteq> 1" "p = Suc (p - 1)"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   832
      by auto
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   833
    from div_mult1_eq[of r q p] p(2)
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   834
    have eq1: "r* (q div p) = (n - 1) div p"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 55370
diff changeset
   835
      unfolding qrn[symmetric] dvd_eq_mod_eq_0 by (simp add: mult.commute)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   836
    have ath: "a \<le> b \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> 1 \<le> a \<and> 1 \<le> b" for a b :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   837
      by arith
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   838
    {
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   839
      assume "a ^ ((n - 1) div p) mod n = 0"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   840
      then obtain s where s: "a ^ ((n - 1) div p) = n * s"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   841
        unfolding mod_eq_0_iff by blast
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   842
      then have eq0: "(a^((n - 1) div p))^p = (n*s)^p" by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   843
      from qrn[symmetric] have qn1: "q dvd n - 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   844
        by (auto simp: dvd_def)
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   845
      from dvd_trans[OF p(2) qn1] have npp: "(n - 1) div p * p = n - 1"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   846
        by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   847
      with eq0 have "a ^ (n - 1) = (n * s) ^ p"
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   848
        by (simp add: power_mult[symmetric])
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   849
      with bqn p01 have "1 = (n * s)^(Suc (p - 1)) mod n"
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   850
        by simp
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 55370
diff changeset
   851
      also have "\<dots> = 0" by (simp add: mult.assoc)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   852
      finally have False by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   853
    }
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   854
    then have *: "a ^ ((n - 1) div p) mod n \<noteq> 0" by auto
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   855
    have "[a ^ ((n - 1) div p) mod n = a ^ ((n - 1) div p)] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   856
      by (simp add: cong_def)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   857
    with ath[OF mod_less_eq_dividend *]
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   858
    have "[a ^ ((n - 1) div p) mod n - 1 = a ^ ((n - 1) div p) - 1] (mod n)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66305
diff changeset
   859
      by (simp add: cong_diff_nat)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   860
    then show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   861
      by (metis cong_imp_coprime_nat eq1 p')
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   862
  qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   863
  with pocklington[OF n qrn[symmetric] nq2 an1] show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 65726
diff changeset
   864
    by blast
55321
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   865
qed
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   866
eadea363deb6 Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff changeset
   867
end