Debian Codegraph @

src/Util/NumTheoryUtil.v

raw
1 From Coq Require Import Zpower Znumtheory ZArith Zdiv.
2 From Coq Require Import Lia Arith.
3 Require Import Crypto.Util.ZUtil.Divide.
4 Require Import Crypto.Util.ZUtil.Modulo.
5 Require Import Crypto.Util.ZUtil.Odd.
6 Require Import Crypto.Util.NatUtil.
7 Require Import Crypto.Util.ZUtil.Tactics.PrimeBound.
8 Require Export Crypto.Util.FixCoqMistakes.
9 Require Import Crypto.Util.Tactics.BreakMatch.
10 Local Open Scope Z.
11
12 Require Coqprime.PrimalityTest.Euler Coqprime.PrimalityTest.Zp Coqprime.PrimalityTest.IGroup Coqprime.PrimalityTest.EGroup Coqprime.PrimalityTest.FGroup Coqprime.List.UList.
13
14 (* TODO: move somewhere else for lemmas about Coqprime.PrimalityTest? *)
15 Lemma in_ZPGroup_range (n : Z) (n_pos : 1 < n) (p : Z) :
16 List.In p (Coqprime.PrimalityTest.FGroup.s (Coqprime.PrimalityTest.Zp.ZPGroup n n_pos)) -> 1 <= p < n.
17 Proof.
18 unfold Coqprime.PrimalityTest.Zp.ZPGroup; simpl; intros Hin.
19 pose proof (Coqprime.PrimalityTest.IGroup.isupport_incl Z (Coqprime.PrimalityTest.Zp.pmult n) (Coqprime.PrimalityTest.Zp.mkZp n) 1 Z.eq_dec) as H.
20 unfold List.incl in *.
21 specialize (H p Hin).
22 apply Coqprime.PrimalityTest.Zp.in_mkZp in H; auto.
23 destruct (Z.eq_dec p 0); try subst.
24 apply Coqprime.PrimalityTest.IGroup.isupport_is_inv_true in Hin.
25 rewrite Coqprime.PrimalityTest.Zp.rel_prime_is_inv in Hin by auto.
26 pose proof (not_rel_prime_0 n n_pos).
27 destruct (rel_prime_dec 0 n) in Hin; try congruence.
28 lia.
29 Qed.
30
31 Lemma generator_subgroup_is_group p (lt_1_p : 1 < p): forall y,
32 (List.In y (Coqprime.PrimalityTest.FGroup.s (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p))
33 /\ Coqprime.PrimalityTest.EGroup.e_order Z.eq_dec y (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p) = Coqprime.PrimalityTest.FGroup.g_order (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p))
34 -> forall a, List.In a (Coqprime.PrimalityTest.FGroup.s (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p)) ->
35 List.In a (Coqprime.PrimalityTest.EGroup.support Z.eq_dec y (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p)).
36 Proof.
37 intros y H a H0.
38 destruct H as [in_ZPGroup_y y_order].
39 pose proof (Coqprime.PrimalityTest.EGroup.support_incl_G Z Z.eq_dec (Coqprime.PrimalityTest.Zp.pmult p) y (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p) in_ZPGroup_y).
40 eapply Permutation.permutation_in; [|eauto].
41 apply Permutation.permutation_sym.
42 eapply Coqprime.List.UList.ulist_eq_permutation; try apply Coqprime.PrimalityTest.EGroup.support_ulist; eauto.
43 unfold Coqprime.PrimalityTest.EGroup.e_order, Coqprime.PrimalityTest.FGroup.g_order in y_order.
44 apply Nat2Z.inj in y_order.
45 auto.
46 Qed.
47
48 Section EulerCriterion.
49
50 Variable x p : Z.
51 Hypothesis prime_p : prime p.
52 Hypothesis neq_p_2 : p <> 2. (* Euler's Criterion is also provable with p = 2, but we do not need it and are lazy.*)
53 Hypothesis x_id : x * 2 + 1 = p.
54
55 Lemma lt_1_p : 1 < p. Proof using prime_p. clear -prime_p. Z.prime_bound. Qed.
56 Lemma x_pos: 0 < x. Proof using prime_p x_id. clear -prime_p x_id. Z.prime_bound. Qed.
57 Lemma x_nonneg: 0 <= x. Proof using prime_p x_id. clear -prime_p x_id. Z.prime_bound. Qed.
58
59 Lemma x_id_inv : x = (p - 1) / 2.
60 Proof using x_id.
61 clear neq_p_2.
62 intros; apply Zeq_plus_swap in x_id.
63 replace (p - 1) with (2 * ((p - 1) / 2)) in x_id by
64 (symmetry; apply Z_div_exact_2; [lia | rewrite <- x_id; apply Z_mod_mult]).
65 ring_simplify in x_id; apply Z.mul_cancel_l in x_id; lia.
66 Qed.
67
68 Lemma mod_p_order : Coqprime.PrimalityTest.FGroup.g_order (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p) = p - 1.
69 Proof using Type.
70 intros; rewrite <- Coqprime.PrimalityTest.Zp.phi_is_order.
71 apply Coqprime.PrimalityTest.Euler.prime_phi_n_minus_1; auto.
72 Qed.
73
74 Lemma p_odd : Z.odd p = true.
75 Proof using neq_p_2 prime_p.
76 pose proof (Z.prime_odd_or_2 p prime_p) as H.
77 destruct H; auto.
78 Qed.
79
80 Lemma prime_pred_even : Z.even (p - 1) = true.
81 Proof using neq_p_2 prime_p.
82 intros.
83 rewrite <- Z.odd_succ.
84 replace (Z.succ (p - 1)) with p by ring.
85 apply p_odd.
86 Qed.
87
88 Lemma fermat_little: forall a (a_nonzero : a mod p <> 0),
89 a ^ (p - 1) mod p = 1.
90 Proof using prime_p.
91 clear -prime_p.
92 intros a a_nonzero.
93 assert (rel_prime a p). {
94 apply rel_prime_mod_rev; try Z.prime_bound.
95 assert (0 < p) as p_pos by Z.prime_bound.
96 apply rel_prime_le_prime; auto; pose proof (Z.mod_pos_bound a p p_pos).
97 lia.
98 }
99 rewrite (Coqprime.PrimalityTest.Zp.Zpower_mod_is_gpow _ _ _ lt_1_p) by (auto || Z.prime_bound).
100 rewrite <- mod_p_order.
101 apply Coqprime.PrimalityTest.EGroup.fermat_gen; try apply Z.eq_dec.
102 apply Coqprime.PrimalityTest.Zp.in_mod_ZPGroup; auto.
103 Qed.
104
105 Lemma fermat_inv : forall a, a mod p <> 0 -> ((a^(p-2) mod p) * a) mod p = 1.
106 Proof using prime_p.
107 clear -prime_p.
108 intros a H.
109 pose proof (prime_ge_2 _ prime_p).
110 rewrite Zmult_mod_idemp_l.
111 replace (a ^ (p - 2) * a) with (a^(p-1)).
112 2:replace (a ^ (p - 2) * a) with (a^1 * a ^ (p - 2)) by ring.
113 2:rewrite <-Zpower_exp; try f_equal; lia.
114 auto using fermat_little.
115 Qed.
116
117 Lemma squared_fermat_little: forall a (a_nonzero : a mod p <> 0),
118 (a * a) ^ x mod p = 1.
119 Proof using prime_p x_id.
120 intros.
121 rewrite <- Z.pow_2_r.
122 rewrite <- Z.pow_mul_r by (apply x_nonneg || lia).
123 replace (2 * x) with (x * 2 + 1 - 1) by lia.
124 rewrite x_id.
125 apply fermat_little; auto.
126 Qed.
127
128 Lemma euler_criterion_square_reverse : forall a (a_nonzero : a mod p <> 0),
129 (exists b, b * b mod p = a) -> (a ^ x mod p = 1).
130 Proof using Type*.
131 intros a a_nonzero a_square.
132 destruct a_square as [b a_square].
133 assert (b mod p <> 0) as b_nonzero. {
134 intuition.
135 rewrite <- Z.pow_2_r in a_square.
136 rewrite Z.mod_exp_0 in a_square by Z.prime_bound.
137 rewrite <- a_square in a_nonzero.
138 auto.
139 }
140 pose proof (squared_fermat_little b b_nonzero).
141 rewrite Z.mod_pow in * by Z.prime_bound.
142 rewrite <- a_square.
143 rewrite Z.mod_mod; Z.prime_bound.
144 Qed.
145
146 Lemma exists_primitive_root_power :
147 (exists y, List.In y (Coqprime.PrimalityTest.FGroup.s (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p))
148 /\ Coqprime.PrimalityTest.EGroup.e_order Z.eq_dec y (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p) = Coqprime.PrimalityTest.FGroup.g_order (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p)
149 /\ (forall a (a_range : 1 <= a <= p - 1), exists j, 0 <= j <= p - 1 /\ y ^ j mod p = a)).
150 Proof using Type.
151 intros.
152 destruct (Coqprime.PrimalityTest.Zp.Zp_cyclic p lt_1_p prime_p) as [y [y_in_ZPGroup y_order]].
153 exists y; repeat split; auto.
154 intros.
155 pose proof (in_ZPGroup_range p lt_1_p y y_in_ZPGroup) as y_range1.
156 assert (0 <= y < p) as y_range0 by lia.
157 assert (rel_prime y p) as rel_prime_y_p by (apply rel_prime_le_prime; lia || auto).
158 assert (rel_prime a p) as rel_prime_a_p by (apply rel_prime_le_prime; lia || auto).
159 assert (List.In a (Coqprime.PrimalityTest.FGroup.s (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p))) as a_in_ZPGroup by (apply Coqprime.PrimalityTest.Zp.in_ZPGroup; lia || auto).
160 destruct (Coqprime.PrimalityTest.EGroup.support_gpow Z Z.eq_dec (Coqprime.PrimalityTest.Zp.pmult p) y (Coqprime.PrimalityTest.Zp.ZPGroup p lt_1_p) y_in_ZPGroup a)
161 as [k [k_range gpow_y_k]]; [apply generator_subgroup_is_group; auto |].
162 exists k; split. {
163 unfold Coqprime.PrimalityTest.EGroup.e_order in y_order.
164 rewrite y_order in k_range.
165 rewrite mod_p_order in k_range.
166 lia.
167 } {
168 assert (y mod p = y) as y_small by (apply Z.mod_small; lia).
169 rewrite <- y_small in gpow_y_k.
170 rewrite <- (Coqprime.PrimalityTest.Zp.Zpower_mod_is_gpow y k p lt_1_p) in gpow_y_k by (lia || auto).
171 auto.
172 }
173 Qed.
174
175 Ltac ereplace x := match type of x with ?t =>
176 let e := fresh "e" in evar (e:t); replace x with e; subst e end.
177
178 Lemma euler_criterion_square : forall a (a_range : 1 <= a <= p - 1)
179 (pow_a_x : a ^ x mod p = 1), exists b, b * b mod p = a.
180 Proof using Type*.
181 intros a a_range pow_a_x.
182 destruct (exists_primitive_root_power) as [y [in_ZPGroup_y [y_order gpow_y]]]; auto.
183 destruct (gpow_y a a_range) as [j [j_range pow_y_j]]; clear gpow_y.
184 rewrite Z.mod_pow in pow_a_x by Z.prime_bound.
185 replace a with (a mod p) in pow_y_j by (apply Z.mod_small; lia).
186 rewrite <- pow_y_j in pow_a_x.
187 rewrite <- Z.mod_pow in pow_a_x by Z.prime_bound.
188 rewrite <- Z.pow_mul_r in pow_a_x by lia.
189 assert (p - 1 | j * x) as divide_mul_j_x. {
190 rewrite <- Coqprime.PrimalityTest.Zp.phi_is_order in y_order.
191 rewrite Coqprime.PrimalityTest.Euler.prime_phi_n_minus_1 in y_order by auto.
192 ereplace (p-1); try eapply Coqprime.PrimalityTest.EGroup.e_order_divide_gpow; eauto with zarith.
193 simpl.
194 apply in_ZPGroup_range in in_ZPGroup_y.
195 replace y with (y mod p) by (apply Z.mod_small; lia).
196 erewrite <- Coqprime.PrimalityTest.Zp.Zpower_mod_is_gpow; eauto.
197 apply rel_prime_le_prime; (lia || auto).
198 apply Z.mul_nonneg_nonneg; intuition; lia.
199 }
200 exists (y ^ (j / 2)).
201 rewrite <- Z.pow_add_r by (apply Z.div_pos; lia).
202 rewrite <- Z_div_plus by lia.
203 rewrite Z.mul_comm.
204 rewrite x_id_inv in divide_mul_j_x; auto.
205 apply (Z.divide_mul_div _ j 2) in divide_mul_j_x;
206 try (apply prime_pred_divide2 || Z.prime_bound); auto.
207 rewrite <- Zdivide_Zdiv_eq by (auto || lia).
208 rewrite Zplus_diag_eq_mult_2.
209 replace (a mod p) with a in pow_y_j by (symmetry; apply Z.mod_small; lia).
210 rewrite Z_div_mult by lia; auto.
211 apply Z.divide2_even_iff.
212 apply prime_pred_even.
213 Qed.
214
215 Lemma euler_criterion : forall a (a_range : 1 <= a <= p - 1),
216 (a ^ x mod p = 1) <-> exists b, b * b mod p = a.
217 Proof using Type*.
218 intros a a_range; split. {
219 exact (euler_criterion_square _ a_range).
220 } {
221 apply euler_criterion_square_reverse; auto.
222 replace (a mod p) with a by (symmetry; apply Zmod_small; lia).
223 lia.
224 }
225 Qed.
226
227 Lemma euler_criterion_nonsquare : forall a (a_range : 1 <= a <= p - 1),
228 (a ^ x mod p <> 1) <-> ~ (exists b, b * b mod p = a).
229 Proof using Type*.
230 intros a a_range; split; intros A B; apply (euler_criterion a a_range) in B; congruence.
231 Qed.
232
233 End EulerCriterion.
234
235 Lemma divide2_1mod4 : forall x (x_1mod4 : x mod 4 = 1) (x_nonneg : 0 <= x), (2 | x / 2).
236 Proof.
237 intros x x_1mod4 x_nonneg0.
238 assert (Z.to_nat x mod 4 = 1)%nat as x_1mod4_nat. {
239 replace 1 with (Z.of_nat 1) in * by auto.
240 replace (x mod 4) with (Z.of_nat (Z.to_nat x mod 4)) in *
241 by (rewrite Nat2Z.inj_mod; rewrite Z2Nat.id; auto).
242 apply Nat2Z.inj in x_1mod4; auto.
243 }
244 remember (Z.to_nat x / 4)%nat as c eqn:Heqc.
245 destruct (divide2_1mod4_nat c (Z.to_nat x) Heqc x_1mod4_nat) as [k k_id].
246 replace 2%nat with (Z.to_nat 2) in * by auto.
247 apply inj_eq in k_id.
248 rewrite Nat2Z.inj_div in k_id.
249 rewrite Nat2Z.inj_mul in k_id.
250 do 2 rewrite Z2Nat.id in k_id by lia.
251 rewrite Z.mul_comm in k_id.
252 symmetry in k_id.
253 apply Zdivide_intro in k_id; auto.
254 Qed.
255
256 Lemma minus1_even_pow : forall x y, (2 | y) -> (1 < x) -> (0 <= y) -> ((x - 1) ^ y mod x = 1).
257 Proof.
258 intros x y divide_2_y lt_1_x y_nonneg.
259 apply Zdivide_Zdiv_eq in divide_2_y; try lia.
260 rewrite divide_2_y.
261 rewrite Z.pow_mul_r by lia.
262 assert ((x - 1) ^ 2 mod x = 1) as square_case. {
263 replace ((x - 1) ^ 2) with (x ^ 2 - 2 * x + 1)
264 by (do 2 rewrite Z.pow_2_r; rewrite Z.mul_sub_distr_l; do 2 rewrite Z.mul_sub_distr_r; lia).
265 rewrite Zplus_mod.
266 rewrite Z.pow_2_r.
267 rewrite <- Z.mul_sub_distr_r.
268 rewrite Z_mod_mult.
269 do 2 rewrite Zmod_1_l by auto; auto.
270 }
271 rewrite <- (Z2Nat.id (y / 2)) by lia.
272 induction (Z.to_nat (y / 2)) as [|n IHn]; try apply Zmod_1_l; auto.
273 rewrite Nat2Z.inj_succ.
274 rewrite Z.pow_succ_r by apply Zle_0_nat.
275 rewrite Zmult_mod.
276 rewrite IHn.
277 rewrite square_case.
278 simpl; apply Zmod_1_l; auto.
279 Qed.
280
281 Lemma prime_1mod4_neq2 : forall p (prime_p : prime p), (p mod 4 = 1) -> p <> 2.
282 Proof.
283 intros; intuition.
284 assert (4 <> 0)%Z as neq_4_0 by lia.
285 pose proof (Z.div_mod p 4 neq_4_0).
286 lia.
287 Qed.
288
289 Lemma div2_p_1mod4 : forall (p : Z) (prime_p : prime p) (neq_p_2: p <> 2),
290 (p / 2) * 2 + 1 = p.
291 Proof.
292 intros p prime_p neq_p_2.
293 destruct (Z.prime_odd_or_2 p prime_p); intuition.
294 rewrite <- Zdiv2_div.
295 pose proof (Zdiv2_odd_eqn p); break_match; break_match_hyps; congruence || lia.
296 Qed.
297
298 Lemma minus1_square_1mod4 : forall (p : Z) (prime_p : prime p),
299 (p mod 4 = 1)%Z -> exists b : Z, (b * b mod p = p - 1)%Z.
300 Proof.
301 intros p prime_p H.
302 assert (p <> 2) as neq_p_2 by (apply prime_1mod4_neq2; auto).
303 apply (euler_criterion (p / 2) p prime_p).
304 + auto.
305 + apply div2_p_1mod4; auto.
306 + Z.prime_bound.
307 + apply minus1_even_pow; [apply divide2_1mod4 | | apply Z_div_pos]; Z.prime_bound.
308 Qed.
309
310
311 Lemma odd_as_div a : Z.odd a = true -> a = (2*(a/2) + 1)%Z.
312 Proof.
313 rewrite Zodd_mod, <-Zeq_is_eq_bool; intro H_1; rewrite <-H_1.
314 apply Z_div_mod_eq_full.
315 Qed.