author | haftmann |
Sat, 11 Nov 2017 18:41:08 +0000 | |
changeset 67051 | e7e54a0b9197 |
parent 66888 | 930abfdf8727 |
child 67091 | 1393c2340eec |
permissions | -rw-r--r-- |
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 | 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 | 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 | 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 | 14 |
apply (auto simp add: prime_nat_iff) |
15 |
apply (rule coprimeI) |
|
16 |
apply (auto dest: nat_dvd_not_less simp add: ac_simps) |
|
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) |
|
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 | 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 | 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 | 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 | 29 |
lemma cong_solve: |
30 |
fixes n :: nat |
|
31 |
assumes an: "coprime a n" |
|
32 |
shows "\<exists>x. [a * x = b] (mod n)" |
|
33 |
proof (cases "a = 0") |
|
34 |
case True |
|
35 |
with an show ?thesis |
|
66888 | 36 |
by (simp add: cong_def) |
66305 | 37 |
next |
38 |
case False |
|
39 |
from bezout_add_strong_nat [OF this] |
|
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 | 42 |
using assms coprime_common_divisor [of a n d] by simp |
66305 | 43 |
with dxy(3) have "a * x * b = (n * y + 1) * b" |
44 |
by simp |
|
45 |
then have "a * (x * b) = n * (y * b) + b" |
|
46 |
by (auto simp: algebra_simps) |
|
47 |
then have "a * (x * b) mod n = (n * (y * b) + b) mod n" |
|
48 |
by simp |
|
49 |
then have "a * (x * b) mod n = b mod n" |
|
50 |
by (simp add: mod_add_left_eq) |
|
51 |
then have "[a * (x * b) = b] (mod n)" |
|
66888 | 52 |
by (simp only: cong_def) |
66305 | 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 | 56 |
lemma cong_solve_unique: |
57 |
fixes n :: nat |
|
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 | 60 |
proof - |
61 |
from cong_solve[OF an] obtain x where x: "[a * x = b] (mod n)" |
|
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 | 65 |
from x have *: "[a * ?x = b] (mod n)" |
66888 | 66 |
by (simp add: cong_def mod_mult_right_eq[of a x n]) |
66305 | 67 |
from mod_less_divisor[ of n x] nz * have Px: "?P ?x" by simp |
68 |
have "y = ?x" if Py: "y < n" "[a * y = b] (mod n)" for y |
|
69 |
proof - |
|
70 |
from Py(2) * have "[a * y = a * ?x] (mod n)" |
|
66888 | 71 |
by (simp add: cong_def) |
66305 | 72 |
then have "[y = ?x] (mod n)" |
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 | 75 |
show ?thesis |
66888 | 76 |
by (simp add: cong_def) |
66305 | 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 | 82 |
fixes p :: nat |
83 |
assumes p: "prime p" |
|
84 |
and pa: "coprime p a" |
|
85 |
and x0: "0 < x" |
|
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 | 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 | 90 |
by (simp add: ac_simps) |
91 |
from x0 xp p have px: "coprime x p" |
|
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 | 95 |
have "y \<noteq> 0" |
96 |
proof |
|
97 |
assume "y = 0" |
|
98 |
with y(2) have "p dvd a" |
|
66888 | 99 |
using cong_dvd_iff by auto |
67051 | 100 |
with not_prime_1 p pa show False |
101 |
by (auto simp add: gcd_nat.order_iff) |
|
66305 | 102 |
qed |
103 |
with y show ?thesis |
|
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 | 108 |
fixes p :: nat |
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 | 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 | 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 | 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 | 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 | 121 |
obtain x where x: "x < a * b" "[x = m] (mod a)" "[x = n] (mod b)" "\<forall>y. ?P y \<longrightarrow> y = x" |
122 |
by blast |
|
123 |
from ma nb x have "coprime x a" "coprime x b" |
|
67051 | 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 | 126 |
by simp |
66305 | 127 |
with x show ?thesis |
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 | 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 | 135 |
fixes n :: nat |
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 | 138 |
proof - |
139 |
consider "n = 1" | "n = 0" | "n > 1" by arith |
|
140 |
then show ?thesis |
|
141 |
proof cases |
|
142 |
case 1 |
|
143 |
then show ?thesis by simp |
|
144 |
next |
|
145 |
case 2 |
|
146 |
with am m show ?thesis |
|
66888 | 147 |
by simp |
66305 | 148 |
next |
149 |
case 3 |
|
150 |
from m obtain m' where m': "m = Suc m'" by (cases m) blast+ |
|
151 |
have "d = 1" if d: "d dvd a" "d dvd n" for d |
|
152 |
proof - |
|
153 |
from am mod_less[OF \<open>n > 1\<close>] have am1: "a^m mod n = 1" |
|
66888 | 154 |
by (simp add: cong_def) |
66305 | 155 |
from dvd_mult2[OF d(1), of "a^m'"] have dam: "d dvd a^m" |
156 |
by (simp add: m') |
|
157 |
from dvd_mod_iff[OF d(2), of "a^m"] dam am1 show ?thesis |
|
158 |
by simp |
|
159 |
qed |
|
67051 | 160 |
then show ?thesis |
161 |
by (auto intro: coprimeI) |
|
66305 | 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 | 166 |
fixes n :: nat |
167 |
assumes n: "n \<ge> 2" |
|
168 |
and an: "[a ^ (n - 1) = 1] (mod n)" |
|
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 | 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 | 177 |
then have "totient n > 0" "totient n < n - 1" |
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 | 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 | 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 | 189 |
proof |
190 |
show ?lhs if ?rhs |
|
191 |
using that by blast |
|
192 |
show ?rhs if ?lhs |
|
193 |
proof - |
|
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 | 196 |
have "\<not> P m" if "m < ?x" for m |
197 |
by (rule not_less_Least[OF that]) |
|
198 |
with LeastI_ex[OF \<open>?lhs\<close>] show ?thesis |
|
199 |
by blast |
|
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 | 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 | 208 |
from n2 have n01: "n \<noteq> 0" "n \<noteq> 1" "n - 1 \<noteq> 0" |
209 |
by arith+ |
|
210 |
from mod_less_divisor[of n 1] n01 have onen: "1 mod n = 1" |
|
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 | 213 |
have an: "coprime a n" "coprime (a ^ (n - 1)) n" |
214 |
using \<open>n \<ge> 2\<close> by simp_all |
|
66305 | 215 |
have False if H0: "\<exists>m. 0 < m \<and> m < n - 1 \<and> [a ^ m = 1] (mod n)" (is "EX m. ?P m") |
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 | 218 |
m: "0 < m" "m < n - 1" "[a ^ m = 1] (mod n)" "\<forall>k <m. \<not>?P k" |
219 |
by blast |
|
220 |
have False if nm1: "(n - 1) mod m > 0" |
|
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 | 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 | 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 | 233 |
finally have *: "?y mod n = 1" . |
234 |
have **: "[?y * a ^ ((n - 1) mod m) = ?y* 1] (mod n)" |
|
66888 | 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 | 237 |
by (simp add:power_add[symmetric] cong_def * del: One_nat_def) |
66305 | 238 |
have "[a ^ ((n - 1) mod m) = 1] (mod n)" |
239 |
by (metis cong_mult_rcancel_nat mult.commute ** yn) |
|
240 |
with m(4)[rule_format, OF th0] nm1 |
|
241 |
less_trans[OF mod_less_divisor[OF m(1), of "n - 1"] m(2)] show ?thesis |
|
242 |
by blast |
|
243 |
qed |
|
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 | 246 |
then obtain r where r: "n - 1 = m * r" |
247 |
unfolding dvd_def by blast |
|
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 | 251 |
then have th: "prime p \<and> p dvd n - 1" |
252 |
unfolding r by (auto intro: dvd_mult) |
|
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 | 255 |
also have "\<dots> = (a^(m*(r div p))) mod n" |
256 |
using div_mult1_eq[of m r p] p(2)[unfolded dvd_eq_mod_eq_0] by simp |
|
257 |
also have "\<dots> = ((a^m)^(r div p)) mod n" |
|
258 |
by (simp add: power_mult) |
|
259 |
also have "\<dots> = ((a^m mod n)^(r div p)) mod n" |
|
260 |
using power_mod .. |
|
261 |
also from m(3) onen have "\<dots> = 1" |
|
66888 | 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 | 264 |
using onen by (simp add: cong_def) |
66305 | 265 |
with pn th show ?thesis by blast |
266 |
qed |
|
267 |
then have "\<forall>m. 0 < m \<and> m < n - 1 \<longrightarrow> \<not> [a ^ m = 1] (mod n)" |
|
268 |
by blast |
|
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 | 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 | 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 | 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 | 285 |
from bigger_prime[of a] obtain p where p: "prime p" "a < p" |
286 |
by blast |
|
287 |
from assms have o: "ord n a = Least ?P" |
|
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 | 309 |
text \<open>With the special value \<open>0\<close> for non-coprime case, it's more convenient.\<close> |
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))" |
|
311 |
for n :: nat |
|
66888 | 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 | 314 |
lemma ord: "[a^(ord n a) = 1] (mod n)" |
315 |
for n :: nat |
|
316 |
using ord_works by blast |
|
55321
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
317 |
|
66305 | 318 |
lemma ord_minimal: "0 < m \<Longrightarrow> m < ord n a \<Longrightarrow> \<not> [a^m = 1] (mod n)" |
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 | 322 |
lemma ord_eq_0: "ord n a = 0 \<longleftrightarrow> \<not> coprime n a" |
323 |
for n :: nat |
|
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 | 326 |
lemma divides_rexp: "x dvd y \<Longrightarrow> x dvd (y ^ Suc n)" |
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 | 330 |
lemma ord_divides:"[a ^ d = 1] (mod n) \<longleftrightarrow> ord n a dvd d" |
331 |
(is "?lhs \<longleftrightarrow> ?rhs") |
|
332 |
for n :: nat |
|
55321
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
333 |
proof |
66305 | 334 |
assume ?rhs |
335 |
then obtain k where "d = ord n a * k" |
|
336 |
unfolding dvd_def by blast |
|
337 |
then have "[a ^ d = (a ^ (ord n a) mod n)^k] (mod n)" |
|
66888 | 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 | 340 |
using ord[of a n, unfolded cong_def] |
341 |
by (simp add: cong_def power_mod) |
|
66305 | 342 |
finally show ?lhs . |
55321
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
343 |
next |
66305 | 344 |
assume ?lhs |
345 |
show ?rhs |
|
346 |
proof (cases "coprime n a") |
|
347 |
case prem: False |
|
348 |
then have o: "ord n a = 0" by (simp add: ord_def) |
|
349 |
show ?thesis |
|
350 |
proof (cases d) |
|
351 |
case 0 |
|
66888 | 352 |
with o prem show ?thesis by (simp add: cong_def) |
66305 | 353 |
next |
354 |
case (Suc d') |
|
355 |
then have d0: "d \<noteq> 0" by simp |
|
67051 | 356 |
from prem obtain p where p: "p dvd n" "p dvd a" "p \<noteq> 1" |
357 |
by (auto elim: not_coprimeE) |
|
66305 | 358 |
from \<open>?lhs\<close> obtain q1 q2 where q12: "a ^ d + n * q1 = 1 + n * q2" |
67051 | 359 |
using prem d0 lucas_coprime_lemma |
360 |
by (auto elim: not_coprimeE simp add: ac_simps) |
|
66305 | 361 |
then have "a ^ d + n * q1 - n * q2 = 1" by simp |
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 | 365 |
then show ?thesis .. |
366 |
qed |
|
367 |
next |
|
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 | 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) |
66305 | 375 |
then have op: "?o > 0" by simp |
376 |
from div_mult_mod_eq[of d "ord n a"] \<open>?lhs\<close> |
|
377 |
have "[a^(?o*?q + ?r) = 1] (mod n)" |
|
66888 | 378 |
by (simp add: cong_def mult.commute) |
66305 | 379 |
then have "[(a^?o)^?q * (a^?r) = 1] (mod n)" |
66888 | 380 |
by (simp add: cong_def power_mult[symmetric] power_add[symmetric]) |
66305 | 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 | 383 |
by (simp add: cong_def del: One_nat_def) (metis mod_mult_left_eq nat_mult_1) |
66305 | 384 |
show ?thesis |
385 |
proof (cases "?r = 0") |
|
386 |
case True |
|
387 |
then show ?thesis by (simp add: dvd_eq_mod_eq_0) |
|
388 |
next |
|
389 |
case False |
|
55321
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
390 |
with mod_less_divisor[OF op, of d] have r0o:"?r >0 \<and> ?r < ?o" by simp |
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 | 392 |
show ?thesis by blast |
393 |
qed |
|
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 | 397 |
lemma order_divides_totient: |
398 |
"ord n a dvd totient n" if "coprime n a" |
|
399 |
using that euler_theorem [of a n] |
|
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 | 405 |
proof - |
406 |
have th: "[a^d = a^e] (mod n) \<longleftrightarrow> [d = e] (mod (ord n a))" |
|
407 |
if na: "coprime n a" and ed: "(e::nat) \<le> d" |
|
408 |
for n a d e :: nat |
|
409 |
proof - |
|
410 |
from na ed have "\<exists>c. d = e + c" by presburger |
|
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 | 413 |
by (simp add: ac_simps) |
414 |
then have aen: "coprime (a ^ e) n" |
|
415 |
by (cases "e > 0") simp_all |
|
416 |
from an have acn: "coprime (a ^ c) n" |
|
417 |
by (cases "c > 0") simp_all |
|
66305 | 418 |
from c have "[a^d = a^e] (mod n) \<longleftrightarrow> [a^(e + c) = a^(e + 0)] (mod n)" |
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 | 423 |
also have "\<dots> \<longleftrightarrow> ord n a dvd c" |
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 | 426 |
by (auto simp add: cong_altdef_nat) |
66305 | 427 |
finally show ?thesis |
428 |
using c by simp |
|
429 |
qed |
|
430 |
consider "e \<le> d" | "d \<le> e" by arith |
|
431 |
then show ?thesis |
|
432 |
proof cases |
|
433 |
case 1 |
|
434 |
with na show ?thesis by (rule th) |
|
435 |
next |
|
436 |
case 2 |
|
437 |
from th[OF na this] show ?thesis |
|
66888 | 438 |
by (metis cong_sym) |
66305 | 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 | 442 |
|
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 | 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 | 447 |
for n :: nat |
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 | 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 | 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 | 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 | 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)" |
467 |
for n :: nat |
|
55321
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
468 |
proof - |
66305 | 469 |
consider "n = 0" | "n = 1" | "n \<noteq> 0" "n \<noteq> 1" by blast |
470 |
then show ?thesis |
|
471 |
proof cases |
|
472 |
case 1 |
|
473 |
then show ?thesis by simp |
|
474 |
next |
|
475 |
case 2 |
|
476 |
then show ?thesis by simp |
|
477 |
next |
|
478 |
case n: 3 |
|
479 |
then have np: "n > 1" by arith |
|
480 |
{ |
|
481 |
fix d |
|
482 |
assume d: "d dvd n" "d\<^sup>2 \<le> n" |
|
483 |
and H: "\<forall>m. m dvd n \<longrightarrow> m = 1 \<or> m = n" |
|
484 |
from H d have d1n: "d = 1 \<or> d = n" by blast |
|
485 |
then have "d = 1" |
|
486 |
proof |
|
487 |
assume dn: "d = n" |
|
488 |
from n have "n\<^sup>2 > n * 1" |
|
489 |
by (simp add: power2_eq_square) |
|
490 |
with dn d(2) show ?thesis by simp |
|
491 |
qed |
|
492 |
} |
|
55321
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
493 |
moreover |
66305 | 494 |
{ |
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 | 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 | 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 | 503 |
then have "d = 1 \<or> d = n" |
504 |
proof |
|
505 |
assume "d\<^sup>2 \<le> n" |
|
506 |
with H[rule_format, of d] d have "d = 1" by blast |
|
507 |
then show ?thesis .. |
|
508 |
next |
|
509 |
assume h: "e\<^sup>2 \<le> n" |
|
510 |
from e have "e dvd n" by (simp add: dvd_def mult.commute) |
|
511 |
with H[rule_format, of e] h have "e = 1" by simp |
|
512 |
with e have "d = n" by simp |
|
513 |
then show ?thesis .. |
|
514 |
qed |
|
515 |
} |
|
516 |
ultimately show ?thesis |
|
517 |
unfolding prime_nat_iff using np n(2) by blast |
|
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 | 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 | 524 |
proof - |
525 |
consider "n = 0" | "n = 1" | "n \<noteq> 0" "n \<noteq> 1" |
|
526 |
by blast |
|
527 |
then show ?thesis |
|
528 |
proof cases |
|
529 |
case 1 |
|
530 |
then show ?thesis by (metis not_prime_0) |
|
531 |
next |
|
532 |
case 2 |
|
533 |
then show ?thesis by (metis not_prime_1) |
|
534 |
next |
|
535 |
case n: 3 |
|
536 |
show ?thesis |
|
537 |
proof |
|
538 |
assume ?lhs |
|
539 |
from this[unfolded prime_divisor_sqrt] n show ?rhs |
|
540 |
by (metis prime_prime_factor) |
|
541 |
next |
|
542 |
assume ?rhs |
|
543 |
{ |
|
544 |
fix d |
|
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 | 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 | 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 | 552 |
with \<open>?rhs\<close> n p(1) dvd_trans[OF p(2) d(1)] have False |
553 |
by blast |
|
554 |
} |
|
555 |
with n prime_divisor_sqrt show ?lhs by auto |
|
556 |
qed |
|
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 | 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 | 564 |
fixes p :: nat |
565 |
assumes n: "n \<ge> 2" and nqr: "n - 1 = q * r" |
|
566 |
and an: "[a^ (n - 1) = 1] (mod n)" |
|
567 |
and aq: "\<forall>p. prime p \<and> p dvd q \<longrightarrow> coprime (a ^ ((n - 1) div p) - 1) n" |
|
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 | 571 |
have p01: "p \<noteq> 0" "p \<noteq> 1" |
572 |
using pp by (auto intro: prime_gt_0_nat) |
|
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 | 575 |
from pn[unfolded dvd_def] obtain l where l: "n = p * l" |
576 |
by blast |
|
577 |
have a0: "a \<noteq> 0" |
|
578 |
proof |
|
579 |
assume "a = 0" |
|
580 |
with n have "a^ (n - 1) = 0" |
|
581 |
by (simp add: power_0_left) |
|
582 |
with n an mod_less[of 1 n] show False |
|
66888 | 583 |
by (simp add: power_0_left cong_def) |
66305 | 584 |
qed |
585 |
with n nqr have aqr0: "a ^ (q * r) \<noteq> 0" |
|
586 |
by simp |
|
587 |
then have "(a ^ (q * r) - 1) + 1 = a ^ (q * r)" |
|
588 |
by simp |
|
589 |
with k l have "a ^ (q * r) = p * l * k + 1" |
|
590 |
by simp |
|
591 |
then have "a ^ (r * q) + p * 0 = 1 + p * (l * k)" |
|
592 |
by (simp add: ac_simps) |
|
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 | 595 |
by (metis an cong_dvd_modulus_nat mult.commute nqr pn) |
596 |
from odq[unfolded dvd_def] obtain d where d: "q = ord p (a^r) * d" |
|
597 |
by blast |
|
598 |
have d1: "d = 1" |
|
599 |
proof (rule ccontr) |
|
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 | 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 | 606 |
from P(1) have P0: "P \<noteq> 0" |
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 | 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 | 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 | 615 |
then have "[((a ^ r) ^ ord p (a^r)) ^ t= 1] (mod p)" |
66888 | 616 |
by (metis cong_pow ord power_one) |
66305 | 617 |
then have pd0: "p dvd a^(ord p (a^r) * t*r) - 1" |
618 |
by (metis cong_to_1_nat exps) |
|
619 |
from nqr s s' have "(n - 1) div P = ord p (a^r) * t*r" |
|
620 |
using P0 by simp |
|
67051 | 621 |
with caP have "coprime (a ^ (ord p (a ^ r) * t * r) - 1) n" |
622 |
by simp |
|
623 |
with p01 pn pd0 coprime_common_divisor [of _ n p] show False |
|
66305 | 624 |
by auto |
625 |
qed |
|
626 |
with d have o: "ord p (a^r) = q" by simp |
|
627 |
from pp totient_prime [of p] have totient_eq: "totient p = p - 1" |
|
628 |
by simp |
|
629 |
{ |
|
630 |
fix d |
|
631 |
assume d: "d dvd p" "d dvd a" "d \<noteq> 1" |
|
63633 | 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 | 634 |
then have False using d dp pn an |
635 |
by auto (metis One_nat_def Suc_lessI |
|
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 | 637 |
} |
67051 | 638 |
then have cpa: "coprime p a" |
639 |
by (auto intro: coprimeI) |
|
640 |
then have arp: "coprime (a ^ r) p" |
|
641 |
by (cases "r > 0") (simp_all add: ac_simps) |
|
66305 | 642 |
from euler_theorem [OF arp, simplified ord_divides] o totient_eq have "q dvd (p - 1)" |
643 |
by simp |
|
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 | 646 |
have "p \<noteq> 0" |
647 |
by (metis p01(1)) |
|
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 | 654 |
assumes n: "n \<ge> 2" and nqr: "n - 1 = q * r" and sqr: "n \<le> q\<^sup>2" |
655 |
and an: "[a^ (n - 1) = 1] (mod n)" |
|
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 | 658 |
unfolding prime_prime_factor_sqrt[of n] |
659 |
proof - |
|
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)" |
|
661 |
from n have n01: "n \<noteq> 0" "n \<noteq> 1" by arith+ |
|
662 |
{ |
|
663 |
fix p |
|
664 |
assume p: "prime p" "p dvd n" "p\<^sup>2 \<le> n" |
|
665 |
from p(3) sqr have "p^(Suc 1) \<le> q^(Suc 1)" |
|
666 |
by (simp add: power2_eq_square) |
|
667 |
then have pq: "p \<le> q" |
|
668 |
by (metis le0 power_le_imp_le_base) |
|
669 |
from pocklington_lemma[OF n nqr an aq p(1,2)] have *: "q dvd p - 1" |
|
670 |
by (metis cong_to_1_nat) |
|
671 |
have "p - 1 \<noteq> 0" |
|
672 |
using prime_ge_2_nat [OF p(1)] by arith |
|
673 |
with pq * have False |
|
674 |
by (simp add: nat_dvd_not_less) |
|
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 | 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 | 681 |
assumes n: "n \<ge> 2" and nqr: "n - 1 = q * r" and sqr: "n \<le> q\<^sup>2" |
682 |
and an: "[a^ (n - 1) = 1] (mod n)" |
|
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 | 685 |
proof - |
686 |
{ |
|
687 |
fix p |
|
688 |
assume p: "prime p" "p dvd q" |
|
689 |
from aq[rule_format] p obtain b where b: "[a^((n - 1) div p) = b] (mod n)" "coprime (b - 1) n" |
|
690 |
by blast |
|
691 |
have a0: "a \<noteq> 0" |
|
692 |
proof |
|
693 |
assume a0: "a = 0" |
|
694 |
from n an have "[0 = 1] (mod n)" |
|
695 |
unfolding a0 power_0_left by auto |
|
696 |
then show False |
|
66888 | 697 |
using n by (simp add: cong_def dvd_eq_mod_eq_0[symmetric]) |
66305 | 698 |
qed |
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 | 701 |
have b0: "b \<noteq> 0" |
702 |
proof |
|
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 | 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 | 710 |
have "p - 1 \<noteq> 0" |
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 | 713 |
from b have d: "n dvd a^((n - 1) div p)" |
714 |
unfolding b0 by auto |
|
66888 | 715 |
from divides_rexp[OF d, of "p - 1"] pS eq cong_dvd_iff [OF an] n show False |
66305 | 716 |
by simp |
717 |
qed |
|
718 |
then have b1: "b \<ge> 1" by arith |
|
66888 | 719 |
from cong_imp_coprime_nat[OF Cong.cong_diff_nat[OF cong_sym [OF b(1)] cong_refl [of 1] b1]] |
66305 | 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 | 722 |
by simp |
723 |
} |
|
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 | 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 | 730 |
subsection \<open>Prime factorizations\<close> |
55321
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
731 |
|
55370 | 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 |
|
66305 | 734 |
definition "primefact ps n \<longleftrightarrow> foldr op * 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 | 736 |
lemma primefact: |
737 |
fixes n :: nat |
|
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 | 741 |
obtain xs where xs: "mset xs = prime_factorization n" |
742 |
using ex_mset [of "prime_factorization n"] by blast |
|
743 |
from assms have "n = prod_mset (prime_factorization n)" |
|
63830 | 744 |
by (simp add: prod_mset_prime_factorization) |
745 |
also have "\<dots> = prod_mset (mset xs)" by (simp add: xs) |
|
66305 | 746 |
also have "\<dots> = foldr op * xs 1" by (induct xs) simp_all |
63534
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents:
62429
diff
changeset
|
747 |
finally have "foldr op * xs 1 = n" .. |
66305 | 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 | 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 | 754 |
fixes p :: nat |
755 |
assumes pf: "primefact ps n" |
|
756 |
and p: "prime p" |
|
757 |
and pn: "p dvd n" |
|
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 | 760 |
proof (induct ps arbitrary: p n) |
761 |
case Nil |
|
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 | 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] |
66305 | 766 |
have q: "prime q" "q * foldr op * qs 1 = n" "\<forall>p \<in>set qs. prime p" |
767 |
and p: "prime p" "p dvd q * foldr op * qs 1" |
|
768 |
by simp_all |
|
769 |
consider "p dvd q" | "p dvd foldr op * qs 1" |
|
770 |
by (metis p prime_dvd_mult_eq_nat) |
|
771 |
then show ?case |
|
772 |
proof cases |
|
773 |
case 1 |
|
774 |
with p(1) q(1) have "p = q" |
|
775 |
unfolding prime_nat_iff by auto |
|
776 |
then show ?thesis by simp |
|
777 |
next |
|
778 |
case prem: 2 |
|
55321
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
779 |
from q(3) have pqs: "primefact qs (foldr op * qs 1)" |
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
780 |
by (simp add: primefact_def) |
66305 | 781 |
from Cons.hyps[OF pqs p(1) prem] show ?thesis by simp |
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 |
|
eadea363deb6
Restoration of Pocklington.thy. Tidying.
paulson <lp15@cam.ac.uk>
parents:
diff
changeset
|
785 |
lemma primefact_variant: "primefact ps n \<longleftrightarrow> foldr op * ps 1 = n \<and> list_all prime ps" |
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 | 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)" |
66305 | 791 |
and psn: "foldr op * ps 1 = n - 1" |
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 | 794 |
proof - |
795 |
{ |
|
796 |
fix p |
|
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 | 801 |
have False by (induct ps) auto |
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 | 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" |
66305 | 809 |
and arnb: "(a^r) mod n = b" and psq: "foldr op * ps 1 = q" |
810 |
and bqn: "(b^q) mod n = 1" |
|
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 | 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 | 816 |
and psp: "list_all (\<lambda>p. prime p \<and> coprime (a^(r *(q div p)) mod n - 1) n) ps" |
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 | 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 | 823 |
by (metis bqn cong_def mod_mod_trivial) |
66305 | 824 |
have "coprime (a ^ ((n - 1) div p) - 1) n" if p: "prime p" "p dvd q" for p |
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 | 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 | 836 |
have ath: "a \<le> b \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> 1 \<le> a \<and> 1 \<le> b" for a b :: nat |
837 |
by arith |
|
838 |
{ |
|
839 |
assume "a ^ ((n - 1) div p) mod n = 0" |
|
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 | 842 |
then have eq0: "(a^((n - 1) div p))^p = (n*s)^p" by simp |
843 |
from qrn[symmetric] have qn1: "q dvd n - 1" |
|
844 |
by (auto simp: dvd_def) |
|
845 |
from dvd_trans[OF p(2) qn1] have npp: "(n - 1) div p * p = n - 1" |
|
846 |
by simp |
|
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 | 849 |
with bqn p01 have "1 = (n * s)^(Suc (p - 1)) mod n" |
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 | 852 |
finally have False by simp |
853 |
} |
|
854 |
then have *: "a ^ ((n - 1) div p) mod n \<noteq> 0" by auto |
|
855 |
have "[a ^ ((n - 1) div p) mod n = a ^ ((n - 1) div p)] (mod n)" |
|
66888 | 856 |
by (simp add: cong_def) |
66305 | 857 |
with ath[OF mod_less_eq_dividend *] |
858 |
have "[a ^ ((n - 1) div p) mod n - 1 = a ^ ((n - 1) div p) - 1] (mod n)" |
|
66888 | 859 |
by (simp add: cong_diff_nat) |
66305 | 860 |
then show ?thesis |
861 |
by (metis cong_imp_coprime_nat eq1 p') |
|
862 |
qed |
|
863 |
with pocklington[OF n qrn[symmetric] nq2 an1] show ?thesis |
|
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 |