| 1 |
From Coq Require Import Morphisms. |
| 2 |
From Coq Require Import Equalities. |
| 3 |
From Coq Require Import ZArith Lia. |
| 4 |
Require Import Crypto.Util.Notations. |
| 5 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 6 |
Require Import Crypto.Util.ZUtil.Hints.ZArith. |
| 7 |
Require Import Crypto.Util.ZUtil.Modulo. |
| 8 |
Require Import Crypto.Util.ZUtil.Modulo.PullPush. |
| 9 |
|
| 10 |
Local Open Scope Z_scope. |
| 11 |
|
| 12 |
Module Z. |
| 13 |
Section equiv_modulo. |
| 14 |
Context (N : Z). |
| 15 |
Definition equiv_modulo x y := x mod N = y mod N. |
| 16 |
Local Infix "==" := equiv_modulo. |
| 17 |
|
| 18 |
Local Instance equiv_modulo_Reflexive : Reflexive equiv_modulo := fun _ => Logic.eq_refl. |
| 19 |
Local Instance equiv_modulo_Symmetric : Symmetric equiv_modulo := fun _ _ => @Logic.eq_sym _ _ _. |
| 20 |
Local Instance equiv_modulo_Transitive : Transitive equiv_modulo := fun _ _ _ => @Logic.eq_trans _ _ _ _. |
| 21 |
|
| 22 |
Local Instance mul_mod_Proper : Proper (equiv_modulo ==> equiv_modulo ==> equiv_modulo) Z.mul. |
| 23 |
Proof. unfold equiv_modulo, Proper, respectful; auto with zarith. Qed. |
| 24 |
|
| 25 |
Local Instance add_mod_Proper : Proper (equiv_modulo ==> equiv_modulo ==> equiv_modulo) Z.add. |
| 26 |
Proof. unfold equiv_modulo, Proper, respectful; auto with zarith. Qed. |
| 27 |
|
| 28 |
Local Instance sub_mod_Proper : Proper (equiv_modulo ==> equiv_modulo ==> equiv_modulo) Z.sub. |
| 29 |
Proof. unfold equiv_modulo, Proper, respectful; auto with zarith. Qed. |
| 30 |
|
| 31 |
Local Instance opp_mod_Proper : Proper (equiv_modulo ==> equiv_modulo) Z.opp. |
| 32 |
Proof. unfold equiv_modulo, Proper, respectful; auto with zarith. Qed. |
| 33 |
|
| 34 |
Local Instance pow_mod_Proper : Proper (equiv_modulo ==> eq ==> equiv_modulo) Z.pow. |
| 35 |
Proof. |
| 36 |
intros ?? H ???; subst; hnf in H |- *. |
| 37 |
rewrite Z.mod_pow_full, H, <- Z.mod_pow_full; reflexivity. |
| 38 |
Qed. |
| 39 |
|
| 40 |
Local Instance modulo_equiv_modulo_Proper |
| 41 |
: Proper (equiv_modulo ==> (fun x y => x = N /\ N = y) ==> Logic.eq) Z.modulo. |
| 42 |
Proof. |
| 43 |
repeat intro; hnf in *; intuition congruence. |
| 44 |
Qed. |
| 45 |
Local Instance eq_to_ProperProxy : ProperProxy (fun x y : Z => x = N /\ N = y) N. |
| 46 |
Proof. split; reflexivity. Qed. |
| 47 |
|
| 48 |
Lemma div_to_inv_modulo a x x' : x > 0 -> x * x' mod N = 1 mod N -> (a / x) == ((a - a mod x) * x'). |
| 49 |
Proof using Type. |
| 50 |
intros H xinv. |
| 51 |
replace (a / x) with ((a / x) * 1) by lia. |
| 52 |
change (x * x' == 1) in xinv. |
| 53 |
rewrite <- xinv. |
| 54 |
replace ((a / x) * (x * x')) with ((x * (a / x)) * x') by lia. |
| 55 |
rewrite Z.mul_div_eq by assumption. |
| 56 |
reflexivity. |
| 57 |
Qed. |
| 58 |
|
| 59 |
Lemma equiv_modulo_mod_small x y : x == y -> 0 <= x < N -> x = y mod N. |
| 60 |
Proof. transitivity (x mod N); [rewrite Z.mod_small|]; auto. Qed. |
| 61 |
End equiv_modulo. |
| 62 |
#[global] |
| 63 |
Hint Rewrite div_to_inv_modulo using solve [ eassumption | lia ] : zstrip_div. |
| 64 |
|
| 65 |
Module EquivModuloInstances (dummy : Nop). (* work around https://coq.inria.fr/bugs/show_bug.cgi?id=4973 *) |
| 66 |
#[global] |
| 67 |
Existing Instance equiv_modulo_Reflexive. |
| 68 |
#[global] |
| 69 |
Existing Instance eq_Reflexive. (* prioritize [Reflexive eq] *) |
| 70 |
#[global] |
| 71 |
Existing Instance equiv_modulo_Symmetric. |
| 72 |
#[global] |
| 73 |
Existing Instance equiv_modulo_Transitive. |
| 74 |
#[global] |
| 75 |
Existing Instance mul_mod_Proper. |
| 76 |
#[global] |
| 77 |
Existing Instance add_mod_Proper. |
| 78 |
#[global] |
| 79 |
Existing Instance sub_mod_Proper. |
| 80 |
#[global] |
| 81 |
Existing Instance opp_mod_Proper. |
| 82 |
#[global] |
| 83 |
Existing Instance pow_mod_Proper. |
| 84 |
#[global] |
| 85 |
Existing Instance modulo_equiv_modulo_Proper. |
| 86 |
#[global] |
| 87 |
Existing Instance eq_to_ProperProxy. |
| 88 |
End EquivModuloInstances. |
| 89 |
Module RemoveEquivModuloInstances (dummy : Nop). |
| 90 |
Global Remove Hints equiv_modulo_Reflexive equiv_modulo_Symmetric equiv_modulo_Transitive mul_mod_Proper add_mod_Proper sub_mod_Proper opp_mod_Proper pow_mod_Proper modulo_equiv_modulo_Proper eq_to_ProperProxy : typeclass_instances. |
| 91 |
End RemoveEquivModuloInstances. |
| 92 |
End Z. |
| 93 |
|
| 94 |
(** Change [_ mod _ = _ mod _] to [Z.equiv_modulo _ _ _] *) |
| 95 |
Ltac Zmod_to_equiv_modulo := |
| 96 |
repeat match goal with |
| 97 |
| [ H : context T[?x mod ?M = ?y mod ?M] |- _ ] |
| 98 |
=> let T' := context T[Z.equiv_modulo M x y] in change T' in H |
| 99 |
| [ |- context T[?x mod ?M = ?y mod ?M] ] |
| 100 |
=> let T' := context T[Z.equiv_modulo M x y] in change T' |
| 101 |
end. |