author | paulson <lp15@cam.ac.uk> |
Thu, 24 Aug 2023 21:40:24 +0100 | |
changeset 78522 | 918a9ed06898 |
parent 75963 | 884dbbc8e1b3 |
child 80084 | 173548e4d5d0 |
permissions | -rw-r--r-- |
41959 | 1 |
(* Title: HOL/Number_Theory/Residues.thy |
31719 | 2 |
Author: Jeremy Avigad |
3 |
||
41541 | 4 |
An algebraic treatment of residue rings, and resulting proofs of |
41959 | 5 |
Euler's theorem and Wilson's theorem. |
31719 | 6 |
*) |
7 |
||
60526 | 8 |
section \<open>Residue rings\<close> |
31719 | 9 |
|
10 |
theory Residues |
|
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
11 |
imports |
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
12 |
Cong |
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
66305
diff
changeset
|
13 |
"HOL-Algebra.Multiplicative_Group" |
65465
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
14 |
Totient |
31719 | 15 |
begin |
16 |
||
66305 | 17 |
definition QuadRes :: "int \<Rightarrow> int \<Rightarrow> bool" |
18 |
where "QuadRes p a = (\<exists>y. ([y^2 = a] (mod p)))" |
|
64282
261d42f0bfac
Removed Old_Number_Theory; all theories ported (thanks to Jaime Mendizabal Roche)
eberlm <eberlm@in.tum.de>
parents:
64272
diff
changeset
|
19 |
|
66305 | 20 |
definition Legendre :: "int \<Rightarrow> int \<Rightarrow> int" |
21 |
where "Legendre a p = |
|
22 |
(if ([a = 0] (mod p)) then 0 |
|
23 |
else if QuadRes p a then 1 |
|
24 |
else -1)" |
|
25 |
||
64282
261d42f0bfac
Removed Old_Number_Theory; all theories ported (thanks to Jaime Mendizabal Roche)
eberlm <eberlm@in.tum.de>
parents:
64272
diff
changeset
|
26 |
|
60527 | 27 |
subsection \<open>A locale for residue rings\<close> |
31719 | 28 |
|
60527 | 29 |
definition residue_ring :: "int \<Rightarrow> int ring" |
66305 | 30 |
where |
31 |
"residue_ring m = |
|
32 |
\<lparr>carrier = {0..m - 1}, |
|
33 |
monoid.mult = \<lambda>x y. (x * y) mod m, |
|
34 |
one = 1, |
|
35 |
zero = 0, |
|
36 |
add = \<lambda>x y. (x + y) mod m\<rparr>" |
|
31719 | 37 |
|
38 |
locale residues = |
|
39 |
fixes m :: int and R (structure) |
|
40 |
assumes m_gt_one: "m > 1" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
41 |
defines R_m_def: "R \<equiv> residue_ring m" |
44872 | 42 |
begin |
31719 | 43 |
|
44 |
lemma abelian_group: "abelian_group R" |
|
65066 | 45 |
proof - |
46 |
have "\<exists>y\<in>{0..m - 1}. (x + y) mod m = 0" if "0 \<le> x" "x < m" for x |
|
47 |
proof (cases "x = 0") |
|
48 |
case True |
|
49 |
with m_gt_one show ?thesis by simp |
|
50 |
next |
|
51 |
case False |
|
52 |
then have "(x + (m - x)) mod m = 0" |
|
53 |
by simp |
|
54 |
with m_gt_one that show ?thesis |
|
55 |
by (metis False atLeastAtMost_iff diff_ge_0_iff_ge diff_left_mono int_one_le_iff_zero_less less_le) |
|
56 |
qed |
|
57 |
with m_gt_one show ?thesis |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
58 |
by (fastforce simp add: R_m_def residue_ring_def mod_add_right_eq ac_simps intro!: abelian_groupI) |
65899 | 59 |
qed |
31719 | 60 |
|
61 |
lemma comm_monoid: "comm_monoid R" |
|
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
62 |
proof - |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
63 |
have "\<And>x y z. \<lbrakk>x \<in> carrier R; y \<in> carrier R; z \<in> carrier R\<rbrakk> \<Longrightarrow> x \<otimes> y \<otimes> z = x \<otimes> (y \<otimes> z)" |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
64 |
"\<And>x y. \<lbrakk>x \<in> carrier R; y \<in> carrier R\<rbrakk> \<Longrightarrow> x \<otimes> y = y \<otimes> x" |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
65 |
unfolding R_m_def residue_ring_def |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
66 |
by (simp_all add: algebra_simps mod_mult_right_eq) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
67 |
then show ?thesis |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
68 |
unfolding R_m_def residue_ring_def |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
69 |
by unfold_locales (use m_gt_one in simp_all) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
70 |
qed |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
71 |
|
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
72 |
interpretation comm_monoid R |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
73 |
using comm_monoid by blast |
31719 | 74 |
|
75 |
lemma cring: "cring R" |
|
65066 | 76 |
apply (intro cringI abelian_group comm_monoid) |
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
77 |
unfolding R_m_def residue_ring_def |
65066 | 78 |
apply (auto simp add: comm_semiring_class.distrib mod_add_eq mod_mult_left_eq) |
41541 | 79 |
done |
31719 | 80 |
|
81 |
end |
|
82 |
||
83 |
sublocale residues < cring |
|
84 |
by (rule cring) |
|
85 |
||
86 |
||
41541 | 87 |
context residues |
88 |
begin |
|
31719 | 89 |
|
60527 | 90 |
text \<open> |
91 |
These lemmas translate back and forth between internal and |
|
92 |
external concepts. |
|
93 |
\<close> |
|
31719 | 94 |
|
95 |
lemma res_carrier_eq: "carrier R = {0..m - 1}" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
96 |
by (auto simp: R_m_def residue_ring_def) |
31719 | 97 |
|
98 |
lemma res_add_eq: "x \<oplus> y = (x + y) mod m" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
99 |
by (auto simp: R_m_def residue_ring_def) |
31719 | 100 |
|
101 |
lemma res_mult_eq: "x \<otimes> y = (x * y) mod m" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
102 |
by (auto simp: R_m_def residue_ring_def) |
31719 | 103 |
|
104 |
lemma res_zero_eq: "\<zero> = 0" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
105 |
by (auto simp: R_m_def residue_ring_def) |
31719 | 106 |
|
107 |
lemma res_one_eq: "\<one> = 1" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
108 |
by (auto simp: R_m_def residue_ring_def units_of_def) |
31719 | 109 |
|
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
110 |
lemma res_units_eq: "Units R = {x. 0 < x \<and> x < m \<and> coprime x m}" (is "_ = ?rhs") |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
111 |
proof |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
112 |
show "Units R \<subseteq> ?rhs" |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
113 |
using zero_less_mult_iff invertible_coprime |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
114 |
by (fastforce simp: Units_def R_m_def residue_ring_def) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
115 |
next |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
116 |
show "?rhs \<subseteq> Units R" |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
117 |
unfolding Units_def R_m_def residue_ring_def |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
118 |
by (force simp add: cong_def coprime_iff_invertible'_int mult.commute) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
119 |
qed |
31719 | 120 |
|
121 |
lemma res_neg_eq: "\<ominus> x = (- x) mod m" |
|
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
122 |
proof - |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
123 |
have "\<ominus> x = (THE y. 0 \<le> y \<and> y < m \<and> (x + y) mod m = 0 \<and> (y + x) mod m = 0)" |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
124 |
by (simp add: R_m_def a_inv_def m_inv_def residue_ring_def) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
125 |
also have "\<dots> = (- x) mod m" |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
126 |
proof - |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
127 |
have "\<And>y. 0 \<le> y \<and> y < m \<and> (x + y) mod m = 0 \<and> (y + x) mod m = 0 \<Longrightarrow> |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
128 |
y = - x mod m" |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
129 |
by (metis minus_add_cancel mod_add_eq plus_int_code(1) zmod_trivial_iff) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
130 |
then show ?thesis |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
131 |
by (intro the_equality) (use m_gt_one in \<open>simp add: add.commute mod_add_right_eq\<close>) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
132 |
qed |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
133 |
finally show ?thesis . |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
134 |
qed |
31719 | 135 |
|
44872 | 136 |
lemma finite [iff]: "finite (carrier R)" |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
137 |
by (simp add: res_carrier_eq) |
31719 | 138 |
|
44872 | 139 |
lemma finite_Units [iff]: "finite (Units R)" |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
140 |
by (simp add: finite_ring_finite_units) |
31719 | 141 |
|
60527 | 142 |
text \<open> |
63167 | 143 |
The function \<open>a \<mapsto> a mod m\<close> maps the integers to the |
60527 | 144 |
residue classes. The following lemmas show that this mapping |
145 |
respects addition and multiplication on the integers. |
|
146 |
\<close> |
|
31719 | 147 |
|
60527 | 148 |
lemma mod_in_carrier [iff]: "a mod m \<in> carrier R" |
149 |
unfolding res_carrier_eq |
|
150 |
using insert m_gt_one by auto |
|
31719 | 151 |
|
152 |
lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
153 |
by (auto simp: R_m_def residue_ring_def mod_simps) |
31719 | 154 |
|
155 |
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
156 |
by (auto simp: R_m_def residue_ring_def mod_simps) |
31719 | 157 |
|
158 |
lemma zero_cong: "\<zero> = 0" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
159 |
by (auto simp: R_m_def residue_ring_def) |
31719 | 160 |
|
161 |
lemma one_cong: "\<one> = 1 mod m" |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
162 |
using m_gt_one by (auto simp: R_m_def residue_ring_def) |
31719 | 163 |
|
60527 | 164 |
(* FIXME revise algebra library to use 1? *) |
67341
df79ef3b3a41
Renamed (^) to [^] in preparation of the move from "op X" to (X)
nipkow
parents:
67091
diff
changeset
|
165 |
lemma pow_cong: "(x mod m) [^] n = x^n mod m" |
65066 | 166 |
using m_gt_one |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
167 |
proof (induct n) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
168 |
case 0 |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
169 |
then show ?case |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
170 |
by (simp add: one_cong) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
171 |
next |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
172 |
case (Suc n) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
173 |
then show ?case |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
174 |
by (simp add: mult_cong power_commutes) |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
175 |
qed |
31719 | 176 |
|
177 |
lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m" |
|
55352 | 178 |
by (metis mod_minus_eq res_neg_eq) |
31719 | 179 |
|
60528 | 180 |
lemma (in residues) prod_cong: "finite A \<Longrightarrow> (\<Otimes>i\<in>A. (f i) mod m) = (\<Prod>i\<in>A. f i) mod m" |
55352 | 181 |
by (induct set: finite) (auto simp: one_cong mult_cong) |
31719 | 182 |
|
60528 | 183 |
lemma (in residues) sum_cong: "finite A \<Longrightarrow> (\<Oplus>i\<in>A. (f i) mod m) = (\<Sum>i\<in>A. f i) mod m" |
55352 | 184 |
by (induct set: finite) (auto simp: zero_cong add_cong) |
31719 | 185 |
|
60688
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
186 |
lemma mod_in_res_units [simp]: |
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
187 |
assumes "1 < m" and "coprime a m" |
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
188 |
shows "a mod m \<in> Units R" |
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
189 |
proof (cases "a mod m = 0") |
66305 | 190 |
case True |
191 |
with assms show ?thesis |
|
60688
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
192 |
by (auto simp add: res_units_eq gcd_red_int [symmetric]) |
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
193 |
next |
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
194 |
case False |
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
195 |
from assms have "0 < m" by simp |
66305 | 196 |
then have "0 \<le> a mod m" by (rule pos_mod_sign [of m a]) |
60688
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
197 |
with False have "0 < a mod m" by simp |
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
198 |
with assms show ?thesis |
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
199 |
by (auto simp add: res_units_eq gcd_red_int [symmetric] ac_simps) |
01488b559910
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents:
60528
diff
changeset
|
200 |
qed |
31719 | 201 |
|
60528 | 202 |
lemma res_eq_to_cong: "(a mod m) = (b mod m) \<longleftrightarrow> [a = b] (mod m)" |
66888 | 203 |
by (auto simp: cong_def) |
31719 | 204 |
|
205 |
||
60528 | 206 |
text \<open>Simplifying with these will translate a ring equation in R to a congruence.\<close> |
66305 | 207 |
lemmas res_to_cong_simps = |
208 |
add_cong mult_cong pow_cong one_cong |
|
209 |
prod_cong sum_cong neg_cong res_eq_to_cong |
|
31719 | 210 |
|
60527 | 211 |
text \<open>Other useful facts about the residue ring.\<close> |
31719 | 212 |
lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2" |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
213 |
using one_cong res_neg_eq res_one_eq zmod_zminus1_eq_if by fastforce |
31719 | 214 |
|
215 |
end |
|
216 |
||
217 |
||
60527 | 218 |
subsection \<open>Prime residues\<close> |
31719 | 219 |
|
220 |
locale residues_prime = |
|
63534
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents:
63417
diff
changeset
|
221 |
fixes p :: nat and R (structure) |
31719 | 222 |
assumes p_prime [intro]: "prime p" |
63534
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents:
63417
diff
changeset
|
223 |
defines "R \<equiv> residue_ring (int p)" |
31719 | 224 |
|
225 |
sublocale residues_prime < residues p |
|
65066 | 226 |
unfolding R_def residues_def |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
227 |
by (auto simp: p_prime prime_gt_1_int) |
31719 | 228 |
|
44872 | 229 |
context residues_prime |
230 |
begin |
|
31719 | 231 |
|
67051 | 232 |
lemma p_coprime_left: |
233 |
"coprime p a \<longleftrightarrow> \<not> p dvd a" |
|
234 |
using p_prime by (auto intro: prime_imp_coprime dest: coprime_common_divisor) |
|
235 |
||
236 |
lemma p_coprime_right: |
|
237 |
"coprime a p \<longleftrightarrow> \<not> p dvd a" |
|
238 |
using p_coprime_left [of a] by (simp add: ac_simps) |
|
239 |
||
240 |
lemma p_coprime_left_int: |
|
241 |
"coprime (int p) a \<longleftrightarrow> \<not> int p dvd a" |
|
242 |
using p_prime by (auto intro: prime_imp_coprime dest: coprime_common_divisor) |
|
243 |
||
244 |
lemma p_coprime_right_int: |
|
245 |
"coprime a (int p) \<longleftrightarrow> \<not> int p dvd a" |
|
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
246 |
using coprime_commute p_coprime_left_int by blast |
67051 | 247 |
|
31719 | 248 |
lemma is_field: "field R" |
65066 | 249 |
proof - |
66837 | 250 |
have "0 < x \<Longrightarrow> x < int p \<Longrightarrow> coprime (int p) x" for x |
251 |
by (rule prime_imp_coprime) (auto simp add: zdvd_not_zless) |
|
65066 | 252 |
then show ?thesis |
66837 | 253 |
by (intro cring.field_intro2 cring) |
254 |
(auto simp add: res_carrier_eq res_one_eq res_zero_eq res_units_eq ac_simps) |
|
65066 | 255 |
qed |
31719 | 256 |
|
257 |
lemma res_prime_units_eq: "Units R = {1..p - 1}" |
|
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
258 |
by (auto simp add: res_units_eq p_coprime_right_int zdvd_not_zless) |
31719 | 259 |
|
260 |
end |
|
261 |
||
262 |
sublocale residues_prime < field |
|
263 |
by (rule is_field) |
|
264 |
||
265 |
||
60527 | 266 |
section \<open>Test cases: Euler's theorem and Wilson's theorem\<close> |
31719 | 267 |
|
60527 | 268 |
subsection \<open>Euler's theorem\<close> |
31719 | 269 |
|
67051 | 270 |
lemma (in residues) totatives_eq: |
271 |
"totatives (nat m) = nat ` Units R" |
|
55261
ad3604df6bc6
new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents:
55242
diff
changeset
|
272 |
proof - |
67051 | 273 |
from m_gt_one have "\<bar>m\<bar> > 1" |
274 |
by simp |
|
275 |
then have "totatives (nat \<bar>m\<bar>) = nat ` abs ` Units R" |
|
276 |
by (auto simp add: totatives_def res_units_eq image_iff le_less) |
|
277 |
(use m_gt_one zless_nat_eq_int_zless in force) |
|
278 |
moreover have "\<bar>m\<bar> = m" "abs ` Units R = Units R" |
|
279 |
using m_gt_one by (auto simp add: res_units_eq image_iff) |
|
280 |
ultimately show ?thesis |
|
281 |
by simp |
|
282 |
qed |
|
283 |
||
284 |
lemma (in residues) totient_eq: |
|
285 |
"totient (nat m) = card (Units R)" |
|
286 |
proof - |
|
65465
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
287 |
have *: "inj_on nat (Units R)" |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
288 |
by (rule inj_onI) (auto simp add: res_units_eq) |
67051 | 289 |
then show ?thesis |
290 |
by (simp add: totient_def totatives_eq card_image) |
|
55261
ad3604df6bc6
new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents:
55242
diff
changeset
|
291 |
qed |
ad3604df6bc6
new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents:
55242
diff
changeset
|
292 |
|
75963
884dbbc8e1b3
avoid duplicate fact error on global_interpretation of residues
haftmann
parents:
71252
diff
changeset
|
293 |
lemma (in residues_prime) prime_totient_eq: "totient p = p - 1" |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
294 |
using p_prime totient_prime by blast |
31719 | 295 |
|
65465
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
296 |
lemma (in residues) euler_theorem: |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
297 |
assumes "coprime a m" |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
298 |
shows "[a ^ totient (nat m) = 1] (mod m)" |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
299 |
proof - |
65465
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
300 |
have "a ^ totient (nat m) mod m = 1 mod m" |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
301 |
by (metis assms finite_Units m_gt_one mod_in_res_units one_cong totient_eq pow_cong units_power_order_eq_one) |
65066 | 302 |
then show ?thesis |
303 |
using res_eq_to_cong by blast |
|
31719 | 304 |
qed |
305 |
||
306 |
lemma euler_theorem: |
|
65465
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
307 |
fixes a m :: nat |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
308 |
assumes "coprime a m" |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
309 |
shows "[a ^ totient m = 1] (mod m)" |
67091 | 310 |
proof (cases "m = 0 \<or> m = 1") |
60527 | 311 |
case True |
44872 | 312 |
then show ?thesis by auto |
31719 | 313 |
next |
60527 | 314 |
case False |
41541 | 315 |
with assms show ?thesis |
66954 | 316 |
using residues.euler_theorem [of "int m" "int a"] cong_int_iff |
317 |
by (auto simp add: residues_def gcd_int_def) fastforce |
|
31719 | 318 |
qed |
319 |
||
320 |
lemma fermat_theorem: |
|
65465
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
321 |
fixes p a :: nat |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
322 |
assumes "prime p" and "\<not> p dvd a" |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
323 |
shows "[a ^ (p - 1) = 1] (mod p)" |
31719 | 324 |
proof - |
65465
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
325 |
from assms prime_imp_coprime [of p a] have "coprime a p" |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
326 |
by (auto simp add: ac_simps) |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
327 |
then have "[a ^ totient p = 1] (mod p)" |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
328 |
by (rule euler_theorem) |
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
329 |
also have "totient p = p - 1" |
65726
f5d64d094efe
More material on totient function
eberlm <eberlm@in.tum.de>
parents:
65465
diff
changeset
|
330 |
by (rule totient_prime) (rule assms) |
65465
067210a08a22
more fundamental euler's totient function on nat rather than int;
haftmann
parents:
65416
diff
changeset
|
331 |
finally show ?thesis . |
31719 | 332 |
qed |
333 |
||
334 |
||
60526 | 335 |
subsection \<open>Wilson's theorem\<close> |
31719 | 336 |
|
60527 | 337 |
lemma (in field) inv_pair_lemma: "x \<in> Units R \<Longrightarrow> y \<in> Units R \<Longrightarrow> |
338 |
{x, inv x} \<noteq> {y, inv y} \<Longrightarrow> {x, inv x} \<inter> {y, inv y} = {}" |
|
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
339 |
by auto |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
340 |
|
31719 | 341 |
|
342 |
lemma (in residues_prime) wilson_theorem1: |
|
343 |
assumes a: "p > 2" |
|
59730
b7c394c7a619
The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents:
59667
diff
changeset
|
344 |
shows "[fact (p - 1) = (-1::int)] (mod p)" |
31719 | 345 |
proof - |
60527 | 346 |
let ?Inverse_Pairs = "{{x, inv x}| x. x \<in> Units R - {\<one>, \<ominus> \<one>}}" |
347 |
have UR: "Units R = {\<one>, \<ominus> \<one>} \<union> \<Union>?Inverse_Pairs" |
|
31719 | 348 |
by auto |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
349 |
have 11: "\<one> \<noteq> \<ominus> \<one>" |
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
350 |
using a one_eq_neg_one by force |
60527 | 351 |
have "(\<Otimes>i\<in>Units R. i) = (\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i\<in>\<Union>?Inverse_Pairs. i)" |
31732 | 352 |
apply (subst UR) |
31719 | 353 |
apply (subst finprod_Un_disjoint) |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
354 |
using inv_one inv_eq_neg_one_eq apply (auto intro!: funcsetI)+ |
31719 | 355 |
done |
60527 | 356 |
also have "(\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>" |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
357 |
by (simp add: 11) |
60527 | 358 |
also have "(\<Otimes>i\<in>(\<Union>?Inverse_Pairs). i) = (\<Otimes>A\<in>?Inverse_Pairs. (\<Otimes>y\<in>A. y))" |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
359 |
by (rule finprod_Union_disjoint) (auto simp: pairwise_def disjnt_def dest!: inv_eq_imp_eq) |
31719 | 360 |
also have "\<dots> = \<one>" |
68447
0beb927eed89
Adjusting Number_Theory for new Algebra
paulson <lp15@cam.ac.uk>
parents:
67341
diff
changeset
|
361 |
apply (rule finprod_one_eqI) |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
362 |
apply clarsimp |
60527 | 363 |
apply (subst finprod_insert) |
66305 | 364 |
apply auto |
55352 | 365 |
apply (metis inv_eq_self) |
31719 | 366 |
done |
60527 | 367 |
finally have "(\<Otimes>i\<in>Units R. i) = \<ominus> \<one>" |
31719 | 368 |
by simp |
60527 | 369 |
also have "(\<Otimes>i\<in>Units R. i) = (\<Otimes>i\<in>Units R. i mod p)" |
65066 | 370 |
by (rule finprod_cong') (auto simp: res_units_eq) |
60527 | 371 |
also have "\<dots> = (\<Prod>i\<in>Units R. i) mod p" |
65066 | 372 |
by (rule prod_cong) auto |
31719 | 373 |
also have "\<dots> = fact (p - 1) mod p" |
65066 | 374 |
using assms |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
375 |
by (simp add: res_prime_units_eq int_prod zmod_int prod_int_eq fact_prod) |
60527 | 376 |
finally have "fact (p - 1) mod p = \<ominus> \<one>" . |
377 |
then show ?thesis |
|
66888 | 378 |
by (simp add: cong_def res_neg_eq res_one_eq zmod_int) |
31719 | 379 |
qed |
380 |
||
55352 | 381 |
lemma wilson_theorem: |
60527 | 382 |
assumes "prime p" |
383 |
shows "[fact (p - 1) = - 1] (mod p)" |
|
55352 | 384 |
proof (cases "p = 2") |
59667
651ea265d568
Removal of the file HOL/Number_Theory/Binomial!! And class field_char_0 now declared in Int.thy
paulson <lp15@cam.ac.uk>
parents:
58889
diff
changeset
|
385 |
case True |
55352 | 386 |
then show ?thesis |
66888 | 387 |
by (simp add: cong_def fact_prod) |
55352 | 388 |
next |
389 |
case False |
|
390 |
then show ?thesis |
|
391 |
using assms prime_ge_2_nat |
|
392 |
by (metis residues_prime.wilson_theorem1 residues_prime.intro le_eq_less_or_eq) |
|
393 |
qed |
|
31719 | 394 |
|
66304 | 395 |
text \<open> |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
396 |
This result can be transferred to the multiplicative group of |
66305 | 397 |
\<open>\<int>/p\<int>\<close> for \<open>p\<close> prime.\<close> |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
398 |
|
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
399 |
lemma mod_nat_int_pow_eq: |
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
400 |
fixes n :: nat and p a :: int |
66305 | 401 |
shows "a \<ge> 0 \<Longrightarrow> p \<ge> 0 \<Longrightarrow> (nat a ^ n) mod (nat p) = nat ((a ^ n) mod p)" |
78522
918a9ed06898
some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents:
75963
diff
changeset
|
402 |
by (simp add: nat_mod_as_int) |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
403 |
|
69785
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
404 |
theorem residue_prime_mult_group_has_gen: |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
405 |
fixes p :: nat |
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
406 |
assumes prime_p : "prime p" |
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
407 |
shows "\<exists>a \<in> {1 .. p - 1}. {1 .. p - 1} = {a^i mod p|i . i \<in> UNIV}" |
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
408 |
proof - |
66305 | 409 |
have "p \<ge> 2" |
410 |
using prime_gt_1_nat[OF prime_p] by simp |
|
411 |
interpret R: residues_prime p "residue_ring p" |
|
412 |
by (simp add: residues_prime_def prime_p) |
|
413 |
have car: "carrier (residue_ring (int p)) - {\<zero>\<^bsub>residue_ring (int p)\<^esub>} = {1 .. int p - 1}" |
|
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
414 |
by (auto simp add: R.zero_cong R.res_carrier_eq) |
66305 | 415 |
|
67341
df79ef3b3a41
Renamed (^) to [^] in preparation of the move from "op X" to (X)
nipkow
parents:
67091
diff
changeset
|
416 |
have "x [^]\<^bsub>residue_ring (int p)\<^esub> i = x ^ i mod (int p)" |
66305 | 417 |
if "x \<in> {1 .. int p - 1}" for x and i :: nat |
418 |
using that R.pow_cong[of x i] by auto |
|
419 |
moreover |
|
420 |
obtain a where a: "a \<in> {1 .. int p - 1}" |
|
67341
df79ef3b3a41
Renamed (^) to [^] in preparation of the move from "op X" to (X)
nipkow
parents:
67091
diff
changeset
|
421 |
and a_gen: "{1 .. int p - 1} = {a[^]\<^bsub>residue_ring (int p)\<^esub>i|i::nat . i \<in> UNIV}" |
66305 | 422 |
using field.finite_field_mult_group_has_gen[OF R.is_field] |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
423 |
by (auto simp add: car[symmetric] carrier_mult_of) |
66305 | 424 |
moreover |
425 |
have "nat ` {1 .. int p - 1} = {1 .. p - 1}" (is "?L = ?R") |
|
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
426 |
proof |
66305 | 427 |
have "n \<in> ?R" if "n \<in> ?L" for n |
428 |
using that \<open>p\<ge>2\<close> by force |
|
429 |
then show "?L \<subseteq> ?R" by blast |
|
430 |
have "n \<in> ?L" if "n \<in> ?R" for n |
|
66837 | 431 |
using that \<open>p\<ge>2\<close> by (auto intro: rev_image_eqI [of "int n"]) |
66305 | 432 |
then show "?R \<subseteq> ?L" by blast |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
433 |
qed |
66305 | 434 |
moreover |
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
435 |
have "nat ` {a^i mod (int p) | i::nat. i \<in> UNIV} = {nat a^i mod p | i . i \<in> UNIV}" (is "?L = ?R") |
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
436 |
proof |
66305 | 437 |
have "x \<in> ?R" if "x \<in> ?L" for x |
438 |
proof - |
|
439 |
from that obtain i where i: "x = nat (a^i mod (int p))" |
|
440 |
by blast |
|
441 |
then have "x = nat a ^ i mod p" |
|
442 |
using mod_nat_int_pow_eq[of a "int p" i] a \<open>p\<ge>2\<close> by auto |
|
443 |
with i show ?thesis by blast |
|
444 |
qed |
|
445 |
then show "?L \<subseteq> ?R" by blast |
|
446 |
have "x \<in> ?L" if "x \<in> ?R" for x |
|
447 |
proof - |
|
448 |
from that obtain i where i: "x = nat a^i mod p" |
|
449 |
by blast |
|
450 |
with mod_nat_int_pow_eq[of a "int p" i] a \<open>p\<ge>2\<close> show ?thesis |
|
451 |
by auto |
|
452 |
qed |
|
453 |
then show "?R \<subseteq> ?L" by blast |
|
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
454 |
qed |
66305 | 455 |
ultimately have "{1 .. p - 1} = {nat a^i mod p | i. i \<in> UNIV}" |
456 |
by presburger |
|
457 |
moreover from a have "nat a \<in> {1 .. p - 1}" by force |
|
65416
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
458 |
ultimately show ?thesis .. |
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
459 |
qed |
f707dbcf11e3
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents:
65066
diff
changeset
|
460 |
|
69785
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
461 |
|
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
462 |
subsection \<open>Upper bound for the number of $n$-th roots\<close> |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
463 |
|
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
464 |
lemma roots_mod_prime_bound: |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
465 |
fixes n c p :: nat |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
466 |
assumes "prime p" "n > 0" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
467 |
defines "A \<equiv> {x\<in>{..<p}. [x ^ n = c] (mod p)}" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
468 |
shows "card A \<le> n" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
469 |
proof - |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
470 |
define R where "R = residue_ring (int p)" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
471 |
from assms(1) interpret residues_prime p R |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
472 |
by unfold_locales (simp_all add: R_def) |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
473 |
interpret R: UP_domain R "UP R" by (unfold_locales) |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
474 |
|
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
475 |
let ?f = "UnivPoly.monom (UP R) \<one>\<^bsub>R\<^esub> n \<ominus>\<^bsub>(UP R)\<^esub> UnivPoly.monom (UP R) (int (c mod p)) 0" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
476 |
have in_carrier: "int (c mod p) \<in> carrier R" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
477 |
using prime_gt_1_nat[OF assms(1)] by (simp add: R_def residue_ring_def) |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
478 |
|
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
479 |
have "deg R ?f = n" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
480 |
using assms in_carrier by (simp add: R.deg_minus_eq) |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
481 |
hence f_not_zero: "?f \<noteq> \<zero>\<^bsub>UP R\<^esub>" using assms by (auto simp add : R.deg_nzero_nzero) |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
482 |
have roots_bound: "finite {a \<in> carrier R. UnivPoly.eval R R id a ?f = \<zero>\<^bsub>R\<^esub>} \<and> |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
483 |
card {a \<in> carrier R. UnivPoly.eval R R id a ?f = \<zero>\<^bsub>R\<^esub>} \<le> deg R ?f" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
484 |
using finite in_carrier by (intro R.roots_bound[OF _ f_not_zero]) simp |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
485 |
have subs: "{x \<in> carrier R. x [^]\<^bsub>R\<^esub> n = int (c mod p)} \<subseteq> |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
486 |
{a \<in> carrier R. UnivPoly.eval R R id a ?f = \<zero>\<^bsub>R\<^esub>}" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
487 |
using in_carrier by (auto simp: R.evalRR_simps) |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
488 |
then have "card {x \<in> carrier R. x [^]\<^bsub>R\<^esub> n = int (c mod p)} \<le> |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
489 |
card {a \<in> carrier R. UnivPoly.eval R R id a ?f = \<zero>\<^bsub>R\<^esub>}" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
490 |
using finite by (intro card_mono) auto |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
491 |
also have "\<dots> \<le> n" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
492 |
using \<open>deg R ?f = n\<close> roots_bound by linarith |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
493 |
also { |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
494 |
fix x assume "x \<in> carrier R" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
495 |
hence "x [^]\<^bsub>R\<^esub> n = (x ^ n) mod (int p)" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
496 |
by (subst pow_cong [symmetric]) (auto simp: R_def residue_ring_def) |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
497 |
} |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
498 |
hence "{x \<in> carrier R. x [^]\<^bsub>R\<^esub> n = int (c mod p)} = {x \<in> carrier R. [x ^ n = int c] (mod p)}" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
499 |
by (fastforce simp: cong_def zmod_int) |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
500 |
also have "bij_betw int A {x \<in> carrier R. [x ^ n = int c] (mod p)}" |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
501 |
by (rule bij_betwI[of int _ _ nat]) |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
502 |
(use cong_int_iff in \<open>force simp: R_def residue_ring_def A_def\<close>)+ |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
503 |
from bij_betw_same_card[OF this] have "card {x \<in> carrier R. [x ^ n = int c] (mod p)} = card A" .. |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
504 |
finally show ?thesis . |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
505 |
qed |
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
506 |
|
9e326f6f8a24
More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents:
68458
diff
changeset
|
507 |
|
31719 | 508 |
end |