| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import Znumtheory. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 5 |
Require Import Crypto.Util.ZUtil.Div. |
| 6 |
Require Import Crypto.Util.ZUtil.Tactics.DivideExistsMul. |
| 7 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 8 |
Local Open Scope Z_scope. |
| 9 |
|
| 10 |
Module Z. |
| 11 |
Lemma divide_mul_div: forall a b c (a_nonzero : a <> 0) (c_nonzero : c <> 0), |
| 12 |
(a | b * (a / c)) -> (c | a) -> (c | b). |
| 13 |
Proof. |
| 14 |
intros ? ? ? ? ? divide_a divide_c_a; do 2 Z.divide_exists_mul. |
| 15 |
rewrite divide_c_a in divide_a. |
| 16 |
rewrite Z.div_mul' in divide_a by auto. |
| 17 |
replace (b * k) with (k * b) in divide_a by ring. |
| 18 |
replace (c * k * k0) with (k * (k0 * c)) in divide_a by ring. |
| 19 |
rewrite Z.mul_cancel_l in divide_a by (intuition auto with nia; rewrite H in divide_c_a; ring_simplify in divide_a; intuition). |
| 20 |
eapply Zdivide_intro; eauto. |
| 21 |
Qed. |
| 22 |
|
| 23 |
Lemma divide2_even_iff : forall n, (2 | n) <-> Z.even n = true. |
| 24 |
Proof. |
| 25 |
intros n; split. { |
| 26 |
intro divide2_n. |
| 27 |
Z.divide_exists_mul; [ | pose proof (Z.mod_pos_bound n 2); lia]. |
| 28 |
rewrite divide2_n. |
| 29 |
apply Z.even_mul. |
| 30 |
} { |
| 31 |
intro n_even. |
| 32 |
pose proof (Zmod_even n) as H. |
| 33 |
rewrite n_even in H. |
| 34 |
apply Zmod_divide; lia || auto. |
| 35 |
} |
| 36 |
Qed. |
| 37 |
|
| 38 |
Lemma divide_pow_le b n m : 0 <= n <= m -> (b ^ n | b ^ m). |
| 39 |
Proof. |
| 40 |
intros. replace m with (n + (m - n)) by ring. |
| 41 |
rewrite Z.pow_add_r by lia. |
| 42 |
apply Z.divide_factor_l. |
| 43 |
Qed. |
| 44 |
|
| 45 |
Lemma mod_divide_full a b : (a mod b = 0) <-> (b | a). |
| 46 |
Proof. |
| 47 |
destruct (Z_zerop b); auto using Z.mod_divide; subst; []. |
| 48 |
rewrite Zmod_0_r; cbv [Z.divide]; intuition (destruct_head'_ex; subst; try exists 0; lia). |
| 49 |
Qed. |
| 50 |
|
| 51 |
Lemma mod_div_mod_full a m n : (n | m) -> a mod n = (a mod m) mod n. |
| 52 |
Proof. |
| 53 |
intros (p,Hp); rewrite (Z_div_mod_eq_full a m) at 1. |
| 54 |
rewrite Hp at 1. |
| 55 |
destruct (Z_zerop n); subst; try now autorewrite with zsimplify_const. |
| 56 |
rewrite Z.mul_shuffle0, Z.add_comm, Z.mod_add; auto. |
| 57 |
Qed. |
| 58 |
#[global] |
| 59 |
Hint Rewrite <- mod_div_mod_full using assumption : zsimplify push_Zmod. |
| 60 |
End Z. |