Debian Codegraph @

src/Arithmetic/ModularArithmeticTheorems.v

raw
1 From Coq Require Import Lia.
2 Require Import Crypto.Spec.ModularArithmetic.
3 Require Import Crypto.Arithmetic.ModularArithmeticPre.
4
5 From Coq Require Import ZArith Zdiv Znumtheory NArith NArithRing. (* import Zdiv before Znumtheory *)
6 From Coq Require Import Morphisms Setoid.
7 From Coq Require Export Ring_theory Ring_tac.
8
9 Require Import Crypto.Algebra.Hierarchy Crypto.Algebra.ScalarMult.
10 Require Crypto.Algebra.Ring Crypto.Algebra.Field.
11 Require Import Crypto.Util.Decidable.
12 Require Import Crypto.Util.ZUtil.Modulo.
13 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
14 Require Export Crypto.Util.FixCoqMistakes.
15
16 Module F.
17 Ltac unwrap_F :=
18 intros;
19 repeat match goal with [ x : F _ |- _ ] => destruct x end;
20 lazy iota beta delta [F.one F.zero F.add F.sub F.mul F.opp F.to_Z F.of_Z proj1_sig] in *;
21 try apply eqsig_eq;
22 pull_Zmod.
23
24 (* FIXME: remove the pose proof once [monoid] no longer contains decidable equality *)
25 Global Instance eq_dec {m} : DecidableRel (@eq (F m)). pose proof dec_eq_Z. exact _. Defined.
26
27 Global Instance commutative_ring_modulo m
28 : @Algebra.Hierarchy.commutative_ring (F m) Logic.eq 0%F 1%F F.opp F.add F.sub F.mul.
29 Proof.
30 repeat (split || intro); unwrap_F;
31 autorewrite with zsimplify; solve [ exact _ | auto with zarith | congruence].
32 Qed.
33
34 Lemma pow_spec {m} a : F.pow a 0%N = 1%F :> F m /\ forall x, F.pow a (1 + x)%N = F.mul a (F.pow a x).
35 Proof. change (@F.pow m) with (proj1_sig (@F.pow_with_spec m)); destruct (@F.pow_with_spec m); eauto. Qed.
36
37 Section FandZ.
38 Context {m:positive}.
39 Local Open Scope F_scope.
40
41 Theorem eq_to_Z_iff (x y : F m) : x = y <-> F.to_Z x = F.to_Z y.
42 Proof using Type. destruct x, y; intuition; simpl in *; try apply (eqsig_eq _ _); congruence. Qed.
43
44 Lemma eq_of_Z_iff : forall x y : Z, x mod m = y mod m <-> F.of_Z m x = F.of_Z m y.
45 Proof using Type. split; unwrap_F; congruence. Qed.
46
47
48 Lemma to_Z_of_Z : forall z, F.to_Z (F.of_Z m z) = z mod m.
49 Proof using Type. unwrap_F; trivial. Qed.
50
51 Lemma of_Z_to_Z x : F.of_Z m (F.to_Z x) = x :> F m.
52 Proof using Type. unwrap_F; congruence. Qed.
53
54
55 Lemma of_Z_mod : forall x, F.of_Z m x = F.of_Z m (x mod m).
56 Proof using Type. unwrap_F; trivial. Qed.
57
58 Lemma mod_to_Z : forall (x:F m), F.to_Z x mod m = F.to_Z x.
59 Proof using Type. unwrap_F. congruence. Qed.
60
61 Lemma to_Z_0 : F.to_Z (0:F m) = 0%Z.
62 Proof using Type. unwrap_F. apply Zmod_0_l. Qed.
63
64 Lemma of_Z_small_nonzero z : (0 < z < m)%Z -> F.of_Z m z <> 0.
65 Proof using Type. intros Hrange Hnz. inversion Hnz. rewrite Zmod_small, Zmod_0_l in *; lia. Qed.
66
67 Lemma to_Z_nonzero (x:F m) : x <> 0 -> F.to_Z x <> 0%Z.
68 Proof using Type. cbv [F.zero]. intros Hnz Hz. rewrite <- Hz, of_Z_to_Z in Hnz; auto. Qed.
69
70 Lemma to_Z_range (x : F m) : 0 < m -> 0 <= F.to_Z x < m.
71 Proof using Type. intros. rewrite <- mod_to_Z. apply Z.mod_pos_bound. trivial. Qed.
72
73 Lemma to_Z_nonzero_range (x : F m) : (x <> 0) -> 0 < m -> (1 <= F.to_Z x < m)%Z.
74 Proof using Type.
75 unfold not; intros Hnz Hlt.
76 rewrite eq_to_Z_iff, to_Z_0 in Hnz; pose proof (to_Z_range x Hlt).
77 lia.
78 Qed.
79
80 Lemma of_Z_add : forall (x y : Z),
81 F.of_Z m (x + y) = F.of_Z m x + F.of_Z m y.
82 Proof using Type. unwrap_F; trivial. Qed.
83
84 Lemma to_Z_add : forall x y : F m,
85 F.to_Z (x + y) = ((F.to_Z x + F.to_Z y) mod m)%Z.
86 Proof using Type. unwrap_F; trivial. Qed.
87
88 Lemma of_Z_mul x y : F.of_Z m (x * y) = F.of_Z _ x * F.of_Z _ y :> F m.
89 Proof using Type. unwrap_F. trivial. Qed.
90
91 Lemma to_Z_mul : forall x y : F m,
92 F.to_Z (x * y) = ((F.to_Z x * F.to_Z y) mod m)%Z.
93 Proof using Type. unwrap_F; trivial. Qed.
94
95 Lemma of_Z_sub x y : F.of_Z _ (x - y) = F.of_Z _ x - F.of_Z _ y :> F m.
96 Proof using Type. unwrap_F. trivial. Qed.
97
98 Lemma to_Z_opp : forall x : F m, F.to_Z (F.opp x) = (- F.to_Z x) mod m.
99 Proof using Type. unwrap_F; trivial. Qed.
100
101 Lemma of_Z_opp : forall x, F.of_Z m (Z.opp x) = F.opp (F.of_Z m x).
102 Proof using Type. unwrap_F; trivial. Qed.
103
104 Lemma of_Z_pow x n : F.of_Z _ x ^ n = F.of_Z _ (x ^ (Z.of_N n) mod m) :> F m.
105 Proof using Type.
106 induction n as [|n IHn] using N.peano_ind;
107 destruct (pow_spec (F.of_Z m x)) as [pow_0 pow_succ] . {
108 rewrite pow_0.
109 unwrap_F; trivial.
110 } {
111 rewrite N2Z.inj_succ.
112 rewrite Z.pow_succ_r by apply N2Z.is_nonneg.
113 rewrite <- N.add_1_l.
114 rewrite pow_succ.
115 rewrite IHn.
116 unwrap_F; trivial.
117 }
118 Qed.
119
120 Lemma to_Z_pow : forall (x : F m) n,
121 F.to_Z (x ^ n)%F = (F.to_Z x ^ Z.of_N n mod m)%Z.
122 Proof using Type.
123 intros x n.
124 symmetry.
125 induction n as [|n IHn] using N.peano_ind;
126 destruct (pow_spec x) as [pow_0 pow_succ] . {
127 rewrite pow_0, Z.pow_0_r; auto.
128 } {
129 rewrite N2Z.inj_succ.
130 rewrite Z.pow_succ_r by apply N2Z.is_nonneg.
131 rewrite <- N.add_1_l.
132 rewrite pow_succ.
133 rewrite <- Zmult_mod_idemp_r.
134 rewrite IHn.
135 apply to_Z_mul.
136 }
137 Qed.
138
139 Lemma square_iff (x:F m) :
140 (exists y : F m, y * y = x) <-> (exists y : Z, y * y mod m = F.to_Z x)%Z.
141 Proof using Type.
142 setoid_rewrite eq_to_Z_iff; setoid_rewrite to_Z_mul; split; intro H; destruct H as [x' H].
143 - eauto.
144 - exists (F.of_Z _ x'); rewrite !to_Z_of_Z; pull_Zmod; auto.
145 Qed.
146
147 Local Notation R_of_nat := (@Ring.of_nat (F m) 0%F 1%F F.add).
148 Lemma Ring_of_nat p : R_of_nat (Pos.to_nat p) = F.of_Z m (Z.pos p).
149 Proof.
150 induction p using Pos.peano_ind.
151 { simpl. rewrite left_identity. reflexivity. }
152 { rewrite Pos2Nat.inj_succ; simpl; rewrite IHp.
153 rewrite <-Pos.add_1_r, Pos2Z.inj_add, of_Z_add.
154 reflexivity. }
155 Qed.
156
157 Local Notation R_of_Z := (@Ring.of_Z (F m) 0%F 1%F F.opp F.add).
158 Lemma Ring_of_Z x : R_of_Z x = F.of_Z m x.
159 Proof.
160 destruct x; cbv [R_of_Z];
161 rewrite ?Ring_of_nat, <-?Pos2Z.opp_pos, ?of_Z_opp; reflexivity.
162 Qed.
163
164 Global Instance char_gt :
165 @Ring.char_ge
166 (F m) Logic.eq F.zero F.one F.opp F.add F.sub F.mul
167 m.
168 Proof.
169 cbv [Ring.char_ge Hierarchy.char_ge].
170 intros.
171 rewrite Ring_of_Z.
172 setoid_rewrite <-eq_of_Z_iff.
173 rewrite Z.mod_0_l by discriminate.
174 rewrite Z.mod_small; [discriminate|].
175 auto using Pos2Z.is_nonneg.
176 Qed.
177 End FandZ.
178 Section FandNat.
179 Import Nat.
180 Local Infix "mod" := modulo : nat_scope.
181 Local Open Scope nat_scope.
182
183 Context {m:BinPos.positive}.
184
185 Lemma to_nat_of_nat (n:nat) : F.to_nat (F.of_nat m n) = (n mod (Z.to_nat m))%nat.
186 Proof using Type.
187 unfold F.to_nat, F.of_nat.
188 rewrite F.to_Z_of_Z.
189 pose proof (Nat2Z.inj_mod n (Pos.to_nat m)) as Hmod.
190 rewrite positive_nat_Z in Hmod.
191 rewrite <- Hmod.
192 rewrite <-Nat2Z.id, Z2Nat.inj_pos; lia.
193 Qed.
194
195 Lemma of_nat_to_nat x : F.of_nat m (F.to_nat x) = x.
196 Proof using Type.
197
198 unfold F.to_nat, F.of_nat.
199 rewrite Z2Nat.id; [ eapply F.of_Z_to_Z | eapply F.to_Z_range; reflexivity].
200 Qed.
201
202 (* TODO: move *)
203 Lemma Pos_to_nat_nonzero p : Pos.to_nat p <> 0%nat.
204 Proof.
205 pose proof (Pos2Nat.is_pos p); lia.
206 Qed.
207
208 Lemma of_nat_mod (n:nat) : F.of_nat m (n mod (Z.to_nat m)) = F.of_nat m n.
209 Proof using Type.
210 unfold F.of_nat.
211 rewrite (F.of_Z_mod (Z.of_nat n)), ?Nat2Z.inj_mod, ?Z2Nat.id; [reflexivity|..].
212 apply Pos2Z.is_nonneg.
213 Qed.
214
215 Lemma to_nat_mod (x:F m) (Hm:(0 < m)%Z) : F.to_nat x mod (Z.to_nat m) = F.to_nat x.
216 Proof using Type.
217 unfold F.to_nat.
218 rewrite <-F.mod_to_Z at 2.
219 apply Z.mod_to_nat; [assumption|].
220 apply F.to_Z_range; assumption.
221 Qed.
222
223 Lemma of_nat_add x y :
224 F.of_nat m (x + y) = (F.of_nat m x + F.of_nat m y)%F.
225 Proof using Type. unfold F.of_nat; rewrite Nat2Z.inj_add, F.of_Z_add; reflexivity. Qed.
226
227 Lemma of_nat_mul x y :
228 F.of_nat m (x * y) = (F.of_nat m x * F.of_nat m y)%F.
229 Proof using Type. unfold F.of_nat; rewrite Nat2Z.inj_mul, F.of_Z_mul; reflexivity. Qed.
230 End FandNat.
231
232 Section RingTacticGadgets.
233 Context (m:positive).
234
235 Definition ring_theory : ring_theory 0%F 1%F (@F.add m) (@F.mul m) (@F.sub m) (@F.opp m) eq
236 := Algebra.Ring.ring_theory_for_stdlib_tactic.
237
238 Lemma pow_pow_N (x : F m) : forall (n : N), (x ^ id n)%F = pow_N 1%F F.mul x n.
239 Proof using Type.
240 destruct (pow_spec x) as [HO HS]; intros n.
241 destruct n as [|p]; auto; unfold id.
242 rewrite ModularArithmeticPre.N_pos_1plus at 1.
243 rewrite HS.
244 simpl.
245 induction p as [|p IHp] using Pos.peano_ind.
246 - simpl. rewrite HO. apply Algebra.Hierarchy.right_identity.
247 - rewrite (@pow_pos_succ (F m) (@F.mul m) eq _ _ associative x).
248 rewrite <-IHp, Pos.pred_N_succ, ModularArithmeticPre.N_pos_1plus, HS.
249 trivial.
250 Qed.
251
252 Lemma power_theory : power_theory 1%F (@F.mul m) eq id (@F.pow m).
253 Proof using Type. split; apply pow_pow_N. Qed.
254
255 (***** Division Theory *****)
256 Definition quotrem(a b: F m): F m * F m :=
257 let '(q, r) := (Z.quotrem (F.to_Z a) (F.to_Z b)) in (F.of_Z _ q , F.of_Z _ r).
258 Lemma div_theory : div_theory eq (@F.add m) (@F.mul m) (@id _) quotrem.
259 Proof using Type.
260 constructor; intros a b; unfold quotrem, id.
261
262 replace (Z.quotrem (F.to_Z a) (F.to_Z b)) with (Z.quot (F.to_Z a) (F.to_Z b), Z.rem (F.to_Z a) (F.to_Z b)) by
263 try (unfold Z.quot, Z.rem; rewrite <- surjective_pairing; trivial).
264
265 unwrap_F; rewrite <-Z.quot_rem'; trivial.
266 Qed.
267
268 (* Define a "ring morphism" between GF and Z, i.e. an equivalence
269 * between 'inject (ZFunction (X))' and 'GFFunction (inject (X))'.
270 *
271 * Doing this allows the [ring] tactic to do coefficient
272 * manipulations in Z rather than F, because we know it's equivalent
273 * to inject the result afterward. *)
274 Lemma ring_morph: ring_morph 0%F 1%F F.add F.mul F.sub F.opp eq
275 0%Z 1%Z Z.add Z.mul Z.sub Z.opp Z.eqb (F.of_Z m).
276 Proof using Type. split; try intro x; try intro y; unwrap_F; solve [ auto | rewrite (proj1 (Z.eqb_eq x y)); trivial]. Qed.
277
278 (* Redefine our division theory under the ring morphism *)
279 Lemma morph_div_theory:
280 Ring_theory.div_theory eq Zplus Zmult (F.of_Z m) Z.quotrem.
281 Proof using Type.
282 split; intros a b.
283 replace (Z.quotrem a b) with (Z.quot a b, Z.rem a b);
284 try (unfold Z.quot, Z.rem; rewrite <- surjective_pairing; trivial).
285 unwrap_F; rewrite <- (Z.quot_rem' a b); trivial.
286 Qed.
287
288 End RingTacticGadgets.
289
290 Ltac is_constant t := match t with F.of_Z _ ?x => x | _ => NotConstant end.
291 Ltac is_pow_constant t := Ncst t.
292
293 Section VariousModulo.
294 Context {m:positive}.
295 Local Open Scope F_scope.
296
297 Add Ring _theory : (ring_theory m)
298 (morphism (ring_morph m),
299 constants [is_constant],
300 div (morph_div_theory m),
301 power_tac (power_theory m) [is_pow_constant]).
302
303 Lemma mul_nonzero_l : forall a b : F m, a*b <> 0 -> a <> 0.
304 Proof using Type. intros a b Hnz Hz. rewrite Hz in Hnz; apply Hnz; ring. Qed.
305
306 Lemma mul_nonzero_r : forall a b : F m, a*b <> 0 -> b <> 0.
307 Proof using Type. intros a b Hnz Hz. rewrite Hz in Hnz; apply Hnz; ring. Qed.
308 End VariousModulo.
309
310 Section Pow.
311 Context {m:positive}.
312 Add Ring _theory' : (ring_theory m)
313 (morphism (ring_morph m),
314 constants [is_constant],
315 div (morph_div_theory m),
316 power_tac (power_theory m) [is_pow_constant]).
317 Local Open Scope F_scope.
318
319 (* TODO: move this somewhere? *)
320 Create HintDb nat2N discriminated.
321 Hint Rewrite Nat2N.inj_iff
322 (eq_refl _ : (0%N = N.of_nat 0))
323 (eq_refl _ : (1%N = N.of_nat 1))
324 (eq_refl _ : (2%N = N.of_nat 2))
325 (eq_refl _ : (3%N = N.of_nat 3))
326 : nat2N.
327 Hint Rewrite <- Nat2N.inj_double Nat2N.inj_succ_double Nat2N.inj_succ
328 Nat2N.inj_add Nat2N.inj_mul Nat2N.inj_sub Nat2N.inj_pred
329 Nat2N.inj_div2 Nat2N.inj_max Nat2N.inj_min Nat2N.id
330 : nat2N.
331
332 Lemma pow_0_r (x:F m) : x^0 = 1.
333 Proof using Type. destruct (F.pow_spec x); auto. Qed.
334
335 Lemma pow_succ_r (x:F m) n : x^(N.succ n) = x * x^n.
336 Proof using Type.
337 rewrite <-N.add_1_l;
338 destruct (F.pow_spec x); auto.
339 Qed.
340
341 Lemma pow_add_r (x:F m) (a b:N) : x^(a+b) = x^a * x^b.
342 Proof using Type.
343 destruct (F.pow_spec x) as [A B].
344 induction a as [|a IHa] using N.peano_ind;
345 rewrite ?N.add_succ_l, ?pow_0_r, ?pow_succ_r, ?N.add_0_l, ?IHa; try ring.
346 Qed.
347
348 Lemma pow_0_l (n:N) : n <> 0%N -> 0^n = 0 :> F m.
349 Proof using Type.
350 induction n as [|a IHa] using N.peano_ind; [contradiction|].
351 rewrite <-N.add_1_l, pow_add_r; intros; ring.
352 Qed.
353
354 Lemma pow_1_l (n:N) : 1^n = 1 :> F m.
355 Proof using Type.
356 induction n as [|n IHn] using N.peano_ind;
357 rewrite ?pow_0_r, ?pow_succ_r, ?pow_add_r, ?pow_1_l, ?IHn; ring.
358 Qed.
359
360 Lemma pow_mul_l (x y:F m) (n:N) : (x*y)^n = x^n * y^n.
361 Proof using Type.
362 induction n as [|n IHn] using N.peano_ind;
363 repeat (rewrite ?pow_0_r, ?pow_succ_r, ?pow_1_l, <-?N.add_1_l, ?N.mul_add_distr_r, ?pow_add_r, ?N.mul_1_l, ?IHn); try ring.
364 Qed.
365
366 Lemma pow_pow_l (x:F m) (a b:N) : (x^a)^b = x^(a*b).
367 Proof using Type.
368 induction a as [|a IHa] using N.peano_ind;
369 repeat (rewrite ?pow_0_r, ?pow_succ_r, ?pow_1_l, <-?N.add_1_l, ?N.mul_add_distr_r, ?pow_add_r, ?N.mul_1_l, ?pow_mul_l, ?IHa);
370 try ring.
371 Qed.
372
373 Lemma pow_1_r (x:F m) : x^1 = x.
374 Proof using Type.
375 change 1%N with (N.succ 0); repeat rewrite ?pow_succ_r, ?pow_0_r; ring.
376 Qed.
377
378 Lemma pow_2_r (x:F m) : x^2 = x*x.
379 Proof using Type.
380 change 1%N with (N.succ (N.succ 0)); repeat rewrite ?pow_succ_r, ?pow_0_r; ring.
381 Qed.
382
383 Lemma pow_3_r (x:F m) : x^3 = x*x*x.
384 Proof using Type.
385 change 1%N with (N.succ (N.succ (N.succ 0))); repeat rewrite ?pow_succ_r, ?pow_0_r; ring.
386 Qed.
387 End Pow.
388 End F.