Debian Codegraph @

src/Util/ZUtil/Modulo.v

raw
1 From Coq Require Import ZArith Lia Znumtheory Zpow_facts.
2 Require Import Crypto.Util.ZUtil.Hints.Core.
3 Require Import Crypto.Util.ZUtil.ZSimplify.Core.
4 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
5 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
6 Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos.
7 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
8 Require Import Crypto.Util.ZUtil.Div.
9 Require Import Crypto.Util.ZUtil.Divide.
10 Require Import Crypto.Util.Tactics.BreakMatch.
11 Require Import Crypto.Util.Tactics.DestructHead.
12 Local Open Scope Z_scope.
13
14 Module Z.
15 Lemma elim_mod : forall a b m, a = b -> a mod m = b mod m.
16 Proof. intros; subst; auto. Qed.
17 #[global]
18 Hint Resolve elim_mod : zarith.
19
20 Lemma mod_add_full : forall a b c, (a + b * c) mod c = a mod c.
21 Proof. intros a b c; destruct (Z_zerop c); try subst; autorewrite with zsimplify; reflexivity. Qed.
22 #[global]
23 Hint Rewrite mod_add_full : zsimplify.
24
25 Lemma mod_add_l_full : forall a b c, (a * b + c) mod b = c mod b.
26 Proof. intros a b c; rewrite (Z.add_comm _ c); autorewrite with zsimplify; reflexivity. Qed.
27 #[global]
28 Hint Rewrite mod_add_l_full : zsimplify.
29
30 Lemma mod_add'_full : forall a b c, (a + b * c) mod b = a mod b.
31 Proof. intros a b c; rewrite (Z.mul_comm _ c); autorewrite with zsimplify; reflexivity. Qed.
32 Lemma mod_add_l'_full : forall a b c, (a * b + c) mod a = c mod a.
33 Proof. intros a b c; rewrite (Z.mul_comm _ b); autorewrite with zsimplify; reflexivity. Qed.
34 #[global]
35 Hint Rewrite mod_add'_full mod_add_l'_full : zsimplify.
36
37 Lemma mod_add_l : forall a b c, b <> 0 -> (a * b + c) mod b = c mod b.
38 Proof. intros a b c H; rewrite (Z.add_comm _ c); autorewrite with zsimplify; reflexivity. Qed.
39
40 Lemma mod_add' : forall a b c, b <> 0 -> (a + b * c) mod b = a mod b.
41 Proof. intros a b c H; rewrite (Z.mul_comm _ c); autorewrite with zsimplify; reflexivity. Qed.
42 Lemma mod_add_l' : forall a b c, a <> 0 -> (a * b + c) mod a = c mod a.
43 Proof. intros a b c H; rewrite (Z.mul_comm _ b); autorewrite with zsimplify; reflexivity. Qed.
44
45 Lemma add_pow_mod_l : forall a b c, a <> 0 -> 0 < b ->
46 ((a ^ b) + c) mod a = c mod a.
47 Proof.
48 intros a b c H H0; replace b with (b - 1 + 1) by ring;
49 rewrite Z.pow_add_r, Z.pow_1_r by lia; auto using Z.mod_add_l.
50 Qed.
51
52 Lemma mod_exp_0 : forall a x m, x > 0 -> m > 1 -> a mod m = 0 ->
53 a ^ x mod m = 0.
54 Proof.
55 intros a x m H H0 H1.
56 replace x with (Z.of_nat (Z.to_nat x)) in * by (apply Z2Nat.id; lia).
57 induction (Z.to_nat x). {
58 simpl in *; lia.
59 } {
60 rewrite Nat2Z.inj_succ in *.
61 rewrite Z.pow_succ_r by lia.
62 rewrite Z.mul_mod by lia.
63 case_eq n; intros. {
64 subst. simpl.
65 rewrite Zmod_1_l by lia.
66 rewrite H1.
67 apply Zmod_0_l.
68 } {
69 subst.
70 rewrite IHn by (rewrite Nat2Z.inj_succ in *; lia).
71 rewrite H1.
72 auto.
73 }
74 }
75 Qed.
76
77 Lemma mod_pow : forall (a m b : Z), (0 <= b) -> (m <> 0) ->
78 a ^ b mod m = (a mod m) ^ b mod m.
79 Proof.
80 intros a m b H H0; rewrite <- (Z2Nat.id b) by auto.
81 induction (Z.to_nat b) as [|n IHn]; auto.
82 rewrite Nat2Z.inj_succ.
83 do 2 rewrite Z.pow_succ_r by apply Nat2Z.is_nonneg.
84 rewrite Z.mul_mod by auto.
85 rewrite (Z.mul_mod (a mod m) ((a mod m) ^ Z.of_nat n) m) by auto.
86 rewrite <- IHn by auto.
87 rewrite Z.mod_mod by auto.
88 reflexivity.
89 Qed.
90
91 Lemma mod_to_nat x m (Hm:(0 < m)%Z) (Hx:(0 <= x)%Z) : (Z.to_nat x mod Z.to_nat m = Z.to_nat (x mod m))%nat.
92 pose proof Nat2Z.inj_mod (Z.to_nat x) (Z.to_nat m) as H;
93 rewrite !Z2Nat.id in H by lia.
94 rewrite <-H.
95 rewrite !Nat2Z.id; reflexivity.
96 Qed.
97
98 Lemma mul_div_eq_full : forall a m, m <> 0 -> m * (a / m) = (a - a mod m).
99 Proof.
100 intros a m H. rewrite (Z_div_mod_eq_full a m) at 2 by auto. ring.
101 Qed.
102
103 #[global]
104 Hint Rewrite mul_div_eq_full using zutil_arith : zdiv_to_mod.
105 #[global]
106 Hint Rewrite <-mul_div_eq_full using zutil_arith : zmod_to_div.
107
108 Lemma f_equal_mul_mod x y x' y' m : x mod m = x' mod m -> y mod m = y' mod m -> (x * y) mod m = (x' * y') mod m.
109 Proof.
110 intros H0 H1; rewrite Zmult_mod, H0, H1, <- Zmult_mod; reflexivity.
111 Qed.
112 #[global]
113 Hint Resolve f_equal_mul_mod : zarith.
114
115 Lemma f_equal_add_mod x y x' y' m : x mod m = x' mod m -> y mod m = y' mod m -> (x + y) mod m = (x' + y') mod m.
116 Proof.
117 intros H0 H1; rewrite Zplus_mod, H0, H1, <- Zplus_mod; reflexivity.
118 Qed.
119 #[global]
120 Hint Resolve f_equal_add_mod : zarith.
121
122 Lemma f_equal_opp_mod x x' m : x mod m = x' mod m -> (-x) mod m = (-x') mod m.
123 Proof.
124 intro H.
125 destruct (Z_zerop (x mod m)) as [H'|H'], (Z_zerop (x' mod m)) as [H''|H''];
126 try congruence.
127 { rewrite !Z_mod_zero_opp_full by assumption; reflexivity. }
128 { rewrite Z_mod_nz_opp_full, H, <- Z_mod_nz_opp_full by assumption; reflexivity. }
129 Qed.
130 #[global]
131 Hint Resolve f_equal_opp_mod : zarith.
132
133 Lemma f_equal_sub_mod x y x' y' m : x mod m = x' mod m -> y mod m = y' mod m -> (x - y) mod m = (x' - y') mod m.
134 Proof.
135 rewrite <- !Z.add_opp_r; auto with zarith.
136 Qed.
137 #[global]
138 Hint Resolve f_equal_sub_mod : zarith.
139
140 Lemma mul_div_eq : forall a m, m > 0 -> m * (a / m) = (a - a mod m).
141 Proof.
142 intros a m H.
143 rewrite (Z_div_mod_eq_full a m) at 2.
144 ring.
145 Qed.
146
147 Lemma mul_div_eq' : (forall a m, m > 0 -> (a / m) * m = (a - a mod m))%Z.
148 Proof.
149 intros a m H.
150 rewrite (Z_div_mod_eq_full a m) at 2.
151 ring.
152 Qed.
153
154 #[global]
155 Hint Rewrite mul_div_eq mul_div_eq' using zutil_arith : zdiv_to_mod.
156 #[global]
157 Hint Rewrite <- mul_div_eq' using zutil_arith : zmod_to_div.
158
159 Lemma mod_div_eq0 : forall a b, 0 < b -> (a mod b) / b = 0.
160 Proof.
161 intros.
162 apply Z.div_small.
163 auto using Z.mod_pos_bound.
164 Qed.
165 #[global]
166 Hint Rewrite mod_div_eq0 using zutil_arith : zsimplify.
167
168 Local Lemma mod_pull_div_helper a b c X
169 (HX : forall a b c d e f g,
170 X a b c d e f g = if a =? 0 then c else 0)
171 : 0 <> b
172 -> 0 <> c
173 -> (a / b) mod c
174 = (a mod (c * b)) / b
175 + if c <? 0 then - X ((a / b) mod c) (a mod (c * b)) ((a mod (c * b)) / b) a b c (a / b) else 0.
176 Proof.
177 intros; break_match; Z.ltb_to_lt; rewrite ?Z.sub_0_r, ?Z.add_0_r;
178 assert (0 <> c * b) by nia; Z.div_mod_to_quot_rem_in_goal; subst;
179 destruct_head'_or; destruct_head'_and;
180 try assert (b < 0) by lia;
181 try assert (c < 0) by lia;
182 Z.replace_all_neg_with_pos;
183 try match goal with
184 | [ H : ?c * ?b * ?q1 + ?r1 = ?b * (?c * ?q2 + _) + _ |- _ ]
185 => assert (q1 = q2) by nia; progress subst
186 end;
187 rewrite ?HX; clear HX X;
188 try nia;
189 repeat match goal with
190 | [ |- - ?x = ?y ] => is_var y; assert (y <= 0) by nia; Z.replace_all_neg_with_pos
191 | [ |- - ?x = ?y + -_ ] => is_var y; assert (y <= 0) by nia; Z.replace_all_neg_with_pos
192 | [ H : -?x + (-?y + ?z) = -?w + ?v |- _ ]
193 => assert (x + (y + -z) = w + -v) by lia; clear H
194 | [ H : ?c * ?b * ?q1 + (?b * ?q2 + ?r) = ?b * (?c * ?q1' + ?q2') + ?r' |- _ ]
195 => assert (c * q1 + q2 = c * q1' + q2') by nia;
196 assert (r = r') by nia;
197 clear H
198 | [ H : -?x < -?y + ?z |- _ ] => assert (y + -z < x) by lia; clear H
199 | [ H : -?x + ?y <= 0 |- _ ] => assert (0 <= x + -y) by lia; clear H
200 | _ => progress Z.clean_neg
201 | _ => progress subst
202 end.
203 all:match goal with
204 | [ H : ?c * ?q + ?r = ?c * ?q' + ?r' |- _ ]
205 => first [ constr_eq q q'; assert (r = r') by nia; clear H
206 | assert (q = q') by nia; assert (r = r') by nia; clear H
207 | lazymatch goal with
208 | [ H' : r' < c |- _ ]
209 => destruct (Z_dec' r c) as [[?|?]|?]
210 | [ H' : r < c |- _ ]
211 => destruct (Z_dec' r' c) as [[?|?]|?]
212 end;
213 subst;
214 [ assert (q = q') by nia; assert (r = r') by nia; clear H
215 | nia
216 | first [ assert (1 + q = q') by nia | assert (q = 1 + q') by nia ];
217 first [ assert (r' = 0) by nia | assert (r = 0) by nia ] ] ]
218 end.
219 all:try lia.
220 all:break_match; Z.ltb_to_lt; lia.
221 Qed.
222
223 Lemma mod_pull_div_full a b c
224 : (a / b) mod c
225 = if ((c <? 0) && ((a / b) mod c =? 0))%bool
226 then 0
227 else (a mod (c * b)) / b.
228 Proof.
229 destruct (Z_zerop b), (Z_zerop c); subst;
230 autorewrite with zsimplify; try reflexivity.
231 { break_match; Z.ltb_to_lt; lia. }
232 { erewrite mod_pull_div_helper at 1 by (lia || reflexivity); cbv beta.
233 destruct (c <? 0) eqn:?; simpl; [ | lia ].
234 break_innermost_match; lia. }
235 Qed.
236
237 Lemma mod_pull_div a b c
238 : 0 <= c -> (a / b) mod c = a mod (c * b) / b.
239 Proof. rewrite mod_pull_div_full; destruct (c <? 0) eqn:?; Z.ltb_to_lt; simpl; lia. Qed.
240
241 Lemma small_mod_eq a b n: a mod n = b mod n -> 0 <= a < n -> a = b mod n.
242 Proof. intros; rewrite <-(Z.mod_small a n); auto. Qed.
243
244 Lemma mod_bound_min_max l x u d (H : l <= x <= u)
245 : (if l / d =? u / d then Z.min (l mod d) (u mod d) else Z.min 0 (d + 1))
246 <= x mod d
247 <= if l / d =? u / d then Z.max (l mod d) (u mod d) else Z.max 0 (d - 1).
248 Proof.
249 destruct (Z_dec d 0) as [ [?|?] | ? ];
250 try solve [ subst; autorewrite with zsimplify; simpl; split; reflexivity
251 | repeat first [ progress Z.div_mod_to_quot_rem_in_goal
252 | progress subst
253 | progress break_innermost_match
254 | progress Z.ltb_to_lt
255 | progress destruct_head'_or
256 | progress destruct_head'_and
257 | progress apply Z.min_case_strong
258 | progress apply Z.max_case_strong
259 | progress intros
260 | lia
261 | match goal with
262 | [ H : ?x <= ?y, H' : ?y <= ?x |- _ ] => assert (x = y) by lia; clear H H'
263 | _ => progress subst
264 | [ H : ?d * ?q0 + ?r0 = ?d * ?q1 + ?r1 |- _ ]
265 => assert (q0 = q1) by nia; subst q0
266 | [ H : ?d * ?q0 + ?r0 <= ?d * ?q1 + ?r1 |- _ ]
267 => assert (q0 = q1) by nia; subst q0
268 end ] ].
269 Qed.
270
271 Lemma mod_mod_0_0_eq x y : x mod y = 0 -> y mod x = 0 -> x = y \/ x = - y \/ x = 0 \/ y = 0.
272 Proof.
273 destruct (Z_zerop x), (Z_zerop y); eauto.
274 Z.div_mod_to_quot_rem_in_goal; subst.
275 rewrite ?Z.add_0_r in *.
276 match goal with
277 | [ H : ?x = ?x * ?q * ?q' |- _ ]
278 => assert (q * q' = 1) by nia;
279 destruct_head'_or;
280 first [ assert (q < 0) by nia
281 | assert (0 < q) by nia ];
282 first [ assert (q' < 0) by nia
283 | assert (0 < q') by nia ]
284 end;
285 nia.
286 Qed.
287 Lemma mod_mod_0_0_eq_pos x y : 0 < x -> 0 < y -> x mod y = 0 -> y mod x = 0 -> x = y.
288 Proof. intros ?? H0 H1; pose proof (mod_mod_0_0_eq x y H0 H1); lia. Qed.
289 Lemma mod_mod_trans x y z : y <> 0 -> x mod y = 0 -> y mod z = 0 -> x mod z = 0.
290 Proof.
291 intros Hy. rewrite (Zmod_eq_full x y Hy). intros Hx Hyz.
292 replace x with (x / y * y) by lia. now rewrite Zmult_mod, Hyz, Z.mul_0_r.
293 Qed.
294
295 Lemma mod_opp_r a b : a mod (-b) = -((-a) mod b).
296 Proof. pose proof (Z.div_opp_r a b); Z.div_mod_to_quot_rem; nia. Qed.
297 #[global]
298 Hint Resolve mod_opp_r : zarith.
299
300 Lemma mod_same_pow : forall a b c, 0 <= c <= b -> a ^ b mod a ^ c = 0.
301 Proof.
302 intros a b c H.
303 replace b with (b - c + c) by ring.
304 rewrite Z.pow_add_r by lia.
305 apply Z_mod_mult.
306 Qed.
307 #[global]
308 Hint Rewrite mod_same_pow using zutil_arith : zsimplify.
309 #[global]
310 Hint Resolve mod_same_pow : zarith.
311
312 Lemma mod_opp_l_z_iff a b (H : b <> 0) : a mod b = 0 <-> (-a) mod b = 0.
313 Proof.
314 split; intro H'; apply Z.mod_opp_l_z in H'; rewrite ?Z.opp_involutive in H'; assumption.
315 Qed.
316 #[global]
317 Hint Rewrite <- mod_opp_l_z_iff using zutil_arith : zsimplify.
318
319 Lemma mod_small_sym a b : 0 <= a < b -> a = a mod b.
320 Proof. intros; symmetry; apply Z.mod_small; assumption. Qed.
321 #[global]
322 Hint Resolve mod_small_sym : zarith.
323
324 Lemma mod_eq_le_to_eq a b : 0 < a <= b -> a mod b = 0 -> a = b.
325 Proof. pose proof (Z.mod_eq_le_div_1 a b); intros; Z.div_mod_to_quot_rem; nia. Qed.
326 #[global]
327 Hint Resolve mod_eq_le_to_eq : zarith.
328
329 Lemma mod_neq_0_le_to_neq a b : a mod b <> 0 -> a <> b.
330 Proof. repeat intro; subst; autorewrite with zsimplify in *; lia. Qed.
331 #[global]
332 Hint Resolve mod_neq_0_le_to_neq : zarith.
333
334 Lemma div_mod' a b : b <> 0 -> a = (a / b) * b + a mod b.
335 Proof. intro; etransitivity; [ apply (Z.div_mod a b); assumption | lia ]. Qed.
336 #[global]
337 Hint Rewrite <- div_mod' using zutil_arith : zsimplify.
338
339 Lemma div_mod'' a b : b <> 0 -> a = a mod b + b * (a / b).
340 Proof. intro; etransitivity; [ apply (Z.div_mod a b); assumption | lia ]. Qed.
341 #[global]
342 Hint Rewrite <- div_mod'' using zutil_arith : zsimplify.
343
344 Lemma div_mod''' a b : b <> 0 -> a = a mod b + (a / b) * b.
345 Proof. intro; etransitivity; [ apply (Z.div_mod a b); assumption | lia ]. Qed.
346 #[global]
347 Hint Rewrite <- div_mod''' using zutil_arith : zsimplify.
348
349 Lemma sub_mod_mod_0 x d : (x - x mod d) mod d = 0.
350 Proof.
351 destruct (Z_zerop d); subst; push_Zmod; autorewrite with zsimplify; reflexivity.
352 Qed.
353 #[global]
354 Hint Resolve sub_mod_mod_0 : zarith.
355 #[global]
356 Hint Rewrite sub_mod_mod_0 : zsimplify.
357
358 Lemma mod_small_n n a b : 0 <= n -> b <> 0 -> n * b <= a < (1 + n) * b -> a mod b = a - n * b.
359 Proof. intros; erewrite Zmod_eq_full, Z.div_between by eassumption. reflexivity. Qed.
360 #[global]
361 Hint Rewrite mod_small_n using zutil_arith : zsimplify.
362
363 Lemma mod_small_1 a b : b <> 0 -> b <= a < 2 * b -> a mod b = a - b.
364 Proof. intros; rewrite (mod_small_n 1) by lia; lia. Qed.
365 #[global]
366 Hint Rewrite mod_small_1 using zutil_arith : zsimplify.
367
368 Lemma mod_opp_small a m : 0 < a <= m -> (-a) mod m = m - a.
369 Proof. intros; symmetry; apply Zmod_unique with (-1); lia. Qed.
370
371 Lemma mod_neg_small a m : -m <= a < 0 -> a mod m = m + a.
372 Proof. intros; symmetry; apply Zmod_unique with (-1); lia. Qed.
373
374 Lemma mod_small_n_if n a b : 0 <= n -> b <> 0 -> n * b <= a < (2 + n) * b -> a mod b = a - (if (1 + n) * b <=? a then (1 + n) else n) * b.
375 Proof. intros; erewrite Zmod_eq_full, Z.div_between_if by eassumption; autorewrite with zsimplify_const. reflexivity. Qed.
376
377 Lemma mod_small_0_if a b : b <> 0 -> 0 <= a < 2 * b -> a mod b = a - if b <=? a then b else 0.
378 Proof. intros; rewrite (mod_small_n_if 0) by lia; autorewrite with zsimplify_const. break_match; lia. Qed.
379
380 Lemma mul_mod_distr_r_full a b c : (a * c) mod (b * c) = (a mod b * c).
381 Proof.
382 destruct (Z_zerop b); [ | destruct (Z_zerop c) ]; subst;
383 autorewrite with zsimplify; auto using Z.mul_mod_distr_r.
384 Qed.
385
386 Lemma mul_mod_distr_l_full a b c : (c * a) mod (c * b) = c * (a mod b).
387 Proof.
388 destruct (Z_zerop b); [ | destruct (Z_zerop c) ]; subst;
389 autorewrite with zsimplify; auto using Z.mul_mod_distr_l.
390 Qed.
391
392 Lemma lt_mul_2_mod_sub : forall a b, b <> 0 -> b <= a < 2 * b -> a mod b = a - b.
393 Proof.
394 intros a b H H0.
395 replace (a mod b) with ((1 * b + (a - b)) mod b) by (f_equal; ring).
396 rewrite Z.mod_add_l by auto.
397 apply Z.mod_small.
398 lia.
399 Qed.
400
401 Lemma mod_pow_r_split x b e1 e2 : 0 <= b -> 0 <= e1 <= e2 -> x mod b^e2 = (x mod b^e1) + (b^e1) * ((x / b^e1) mod b^(e2-e1)).
402 Proof.
403 destruct (Z_zerop b).
404 { destruct (Z_zerop e1), (Z_zerop e2), (Z.eq_dec e1 e2); subst; intros; cbn; autorewrite with zsimplify_fast; destruct x; lia. }
405 intros.
406 replace (b^e2) with (b^e1 * b^(e2 - e1)) by (autorewrite with pull_Zpow; f_equal; lia).
407 rewrite Z.rem_mul_r by auto with zarith.
408 reflexivity.
409 Qed.
410
411 Lemma opp_mod2 a : a mod 2 = - a mod 2.
412 Proof. rewrite !Zmod_odd, Z.odd_opp; reflexivity. Qed.
413
414 Lemma mod_pow_same_base_larger a b n m :
415 0 <= n < m -> 0 < b ->
416 (a mod (b^n)) mod (b^m) = a mod b^n.
417 Proof.
418 intros.
419 pose proof Z.mod_pos_bound a (b^n) ltac:(auto with zarith).
420 assert (b^n <= b^m) by auto with zarith.
421 apply Z.mod_small. auto with zarith.
422 Qed.
423
424 Lemma mod_pow_same_base_smaller a b n m :
425 0 <= m <= n -> 0 < b ->
426 (a mod (b^n)) mod (b^m) = a mod b^m.
427 Proof.
428 intros. replace n with (m+(n-m)) by lia.
429 rewrite Z.pow_add_r, Z.rem_mul_r by auto with zarith.
430 push_Zmod; pull_Zmod.
431 autorewrite with zsimplify_fast; reflexivity.
432 Qed.
433
434 (* Useful lemma for add-get-carry patterns.
435 This expression performs a bitwise or. In terms of bit ranges,
436 b...a + a...c << a-b = b...c *)
437 Lemma add_div_pow2 x a b :
438 0 <= b <= a ->
439 (x mod (2 ^ a)) / 2 ^ b + x / 2 ^ a * 2 ^ (a - b)
440 = x / 2 ^ b.
441 Proof.
442 intros. rewrite Z.pow_sub_r by auto with zarith.
443 rewrite <-Z.divide_div_mul_exact
444 by auto using Z.divide_pow_le with zarith.
445 rewrite <-Z.div_add by auto with zarith.
446 rewrite Z.mul_div_eq', Z.mul_mod, Z.mod_same_pow by auto with zarith.
447 autorewrite with zsimplify.
448 Z.div_mod_to_quot_rem; nia.
449 Qed.
450
451 Lemma cong_iff_0 a b m : a mod m = b mod m <-> (a - b) mod m = 0.
452 Proof.
453 case (Z.eq_dec m 0) as [->|Hm].
454 { rewrite ?Zmod_0_r; rewrite Z.sub_move_0_r; reflexivity. }
455 split; intros H. { rewrite Zminus_mod, H, Z.sub_diag, Z.mod_0_l; trivial. }
456 apply Zmod_divides in H; trivial; case H as [c H].
457 assert (b = a + (-c) * m) as ->; rewrite ?Z.mod_add; trivial.
458 (* lia *) rewrite Z.mul_opp_l, Z.mul_comm, <-H; ring.
459 Qed.
460
461 Lemma cong_iff_ex a b m : a mod m = b mod m <-> exists n, a - b = n * m.
462 Proof.
463 destruct (Z.eq_dec m 0) as [->|].
464 { rewrite !Zmod_0_r. setoid_rewrite Z.mul_0_r. setoid_rewrite Z.sub_move_0_r.
465 firstorder idtac. }
466 { rewrite cong_iff_0, Z.mod_divide by trivial; reflexivity. }
467 Qed.
468
469 Lemma mod_mod_divide a b c : (c | b) -> (a mod b) mod c = a mod c.
470 Proof.
471 destruct (Z.eqb_spec b 0); subst. { rewrite Zmod_0_r; trivial. }
472 inversion_clear 1; subst.
473 destruct (Z.eqb_spec c 0); subst. { rewrite Z.mul_0_r, 2Zmod_0_r; trivial. }
474 apply cong_iff_ex; eexists (- x * (a/(x*c))); rewrite Z.mod_eq by auto.
475 ring_simplify; trivial.
476 Qed.
477 End Z.