Debian Codegraph @

src/Util/ZUtil/CC.v

raw
1 From Coq Require Import Morphisms.
2 From Coq Require Import ZArith Lia.
3 Require Import Crypto.Util.Tactics.BreakMatch.
4 Require Import Crypto.Util.Tactics.DestructHead.
5 Require Import Crypto.Util.ZUtil.Definitions.
6 Require Import Crypto.Util.ZUtil.Hints.PullPush.
7 Require Import Crypto.Util.ZUtil.ZSimplify.Core.
8 Require Import Crypto.Util.ZUtil.ZSimplify.Simple.
9 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
10 Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds.
11 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
12 Local Open Scope Z_scope.
13
14 Module Z.
15 #[global]
16 Hint Rewrite Z.log2_pow2 Z.pow_1_r using solve [auto using Z.log2_nonneg with zarith] : push_Zpow.
17
18 Lemma cc_m_eq_full : forall s x, Z.cc_m s x = if (s =? 1) then x * 2 else x / (s / 2).
19 Proof.
20 intros; cbv [Z.cc_m].
21 destruct (Decidable.dec (2 < s)).
22 { assert (1 <= Z.log2 s) by (rewrite <-Z.log2_2; auto using Z.log2_le_mono with lia).
23 break_match; Z.ltb_to_lt; try lia; [].
24 match goal with H : _ = s |- _ => rewrite <-H end.
25 autorewrite with Zshift_to_pow push_Zpow.
26 reflexivity. }
27 { assert (s < 0 \/ s = 0 \/ s = 1 \/ s = 2) by lia.
28 destruct_head'_or; (subst s || destruct s); try reflexivity; try (cbn; autorewrite with zsimplify_const; lia).
29 change (Z.log2 1 - 1) with (-1).
30 rewrite (Z.shiftr_opp_r _ 1), Z.shiftl_mul_pow2 by lia.
31 reflexivity. }
32 Qed.
33
34 Lemma cc_m_eq : forall s x, 2 < s -> Z.cc_m s x = x / (s / 2).
35 Proof. intros; rewrite cc_m_eq_full; break_match; Z.ltb_to_lt; lia. Qed.
36
37 Lemma cc_m_small : forall s x, s mod 2 = 0 -> 0 <= x < s -> 0 <= Z.cc_m s x < 2.
38 Proof.
39 intros. rewrite cc_m_eq_full.
40 repeat match goal with
41 | _ => break_innermost_match_step
42 | _ => progress Z.ltb_to_lt
43 | _ => split
44 | _ => apply Z.div_lt_upper_bound
45 | _ => solve [Z.zero_bounds]
46 end.
47 Z.div_mod_to_quot_rem_in_goal; lia.
48 Qed.
49
50 Lemma cc_m_Proper_le_r_gen s
51 : (0 <= s /\ Proper (Z.le ==> Z.le) (Definitions.Z.cc_m s))
52 \/ (s < 0 /\ Proper (Basics.flip Z.le ==> Z.le) (Definitions.Z.cc_m s)).
53 Proof.
54 cbv [Basics.flip].
55 destruct (Z_lt_le_dec s 0); [ right | left ]; (split; [ assumption | ]).
56 all: intros x0 x1 H; rewrite !cc_m_eq_full; break_innermost_match; Z.ltb_to_lt; [ nia | ].
57 all: Z.div_mod_to_quot_rem; nia.
58 Qed.
59 #[global]
60 Hint Resolve cc_m_Proper_le_r_gen : zarith.
61
62 Lemma cc_m_Proper_le_r s
63 : Proper (Z.le ==> Z.le) (Definitions.Z.cc_m s)
64 \/ Proper (Basics.flip Z.le ==> Z.le) (Definitions.Z.cc_m s).
65 Proof. pose proof (cc_m_Proper_le_r_gen s); tauto. Qed.
66 #[global]
67 Hint Resolve cc_m_Proper_le_r : zarith.
68
69 Lemma cc_m_Proper_le_r_pos s
70 : Proper (Z.le ==> Z.le) (Definitions.Z.cc_m (Z.pos s)).
71 Proof. pose proof (cc_m_Proper_le_r_gen (Z.pos s)); intuition lia. Qed.
72 #[global]
73 Hint Resolve cc_m_Proper_le_r_pos : zarith.
74
75 Lemma cc_m_Proper_le_r_neg s
76 : Proper (Basics.flip Z.le ==> Z.le) (Definitions.Z.cc_m (Z.neg s)).
77 Proof. pose proof (cc_m_Proper_le_r_gen (Z.neg s)); intuition lia. Qed.
78 #[global]
79 Hint Resolve cc_m_Proper_le_r_neg : zarith.
80
81 Lemma cc_m_Proper_le_r_0
82 : Proper (Z.le ==> Z.le) (Definitions.Z.cc_m 0).
83 Proof. pose proof (cc_m_Proper_le_r_gen 0); intuition lia. Qed.
84 #[global]
85 Hint Resolve cc_m_Proper_le_r_0 : zarith.
86 End Z.