| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import Lia. |
| 3 |
Require Import Crypto.Util.ZUtil.Pow2. |
| 4 |
Require Import Crypto.Util.ZUtil.Log2. |
| 5 |
Require Import Crypto.Util.ZUtil.Lnot. |
| 6 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 7 |
Require Import Crypto.Util.ZUtil.Hints.ZArith. |
| 8 |
Require Import Crypto.Util.ZUtil.ZSimplify.Simple. |
| 9 |
Require Import Crypto.Util.ZUtil.Notations. |
| 10 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 11 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 12 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 13 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 14 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 15 |
Local Open Scope bool_scope. Local Open Scope Z_scope. |
| 16 |
|
| 17 |
Module Z. |
| 18 |
Lemma ones_le x y : x <= y -> Z.ones x <= Z.ones y. |
| 19 |
Proof using Type. |
| 20 |
rewrite !Z.ones_equiv; auto with zarith. |
| 21 |
Qed. |
| 22 |
#[global] |
| 23 |
Hint Resolve ones_le : zarith. |
| 24 |
|
| 25 |
Lemma ones_lt_pow2 x y : 0 <= x <= y -> Z.ones x < 2^y. |
| 26 |
Proof using Type. |
| 27 |
rewrite Z.ones_equiv, Z.lt_pred_le. |
| 28 |
auto with zarith. |
| 29 |
Qed. |
| 30 |
#[global] |
| 31 |
Hint Resolve ones_lt_pow2 : zarith. |
| 32 |
|
| 33 |
Lemma log2_ones_full x : Z.log2 (Z.ones x) = Z.max 0 (Z.pred x). |
| 34 |
Proof using Type. |
| 35 |
rewrite Z.ones_equiv, Z.log2_pred_pow2_full; reflexivity. |
| 36 |
Qed. |
| 37 |
#[global] |
| 38 |
Hint Rewrite log2_ones_full : zsimplify. |
| 39 |
|
| 40 |
Lemma log2_ones_lt x y : 0 < x <= y -> Z.log2 (Z.ones x) < y. |
| 41 |
Proof using Type. |
| 42 |
rewrite log2_ones_full; apply Z.max_case_strong; lia. |
| 43 |
Qed. |
| 44 |
#[global] |
| 45 |
Hint Resolve log2_ones_lt : zarith. |
| 46 |
|
| 47 |
Lemma log2_ones_le x y : 0 <= x <= y -> Z.log2 (Z.ones x) <= y. |
| 48 |
Proof using Type. |
| 49 |
rewrite log2_ones_full; apply Z.max_case_strong; lia. |
| 50 |
Qed. |
| 51 |
#[global] |
| 52 |
Hint Resolve log2_ones_le : zarith. |
| 53 |
|
| 54 |
Lemma log2_ones_lt_nonneg x y : 0 < y -> x <= y -> Z.log2 (Z.ones x) < y. |
| 55 |
Proof using Type. |
| 56 |
rewrite log2_ones_full; apply Z.max_case_strong; lia. |
| 57 |
Qed. |
| 58 |
#[global] |
| 59 |
Hint Resolve log2_ones_lt_nonneg : zarith. |
| 60 |
|
| 61 |
Lemma ones_pred : forall i, 0 < i -> Z.ones (Z.pred i) = Z.shiftr (Z.ones i) 1. |
| 62 |
Proof using Type. |
| 63 |
induction i as [|p|p]; [ | | pose proof (Pos2Z.neg_is_neg p) ]; try lia. |
| 64 |
intros. |
| 65 |
unfold Z.ones. |
| 66 |
rewrite !Z.shiftl_1_l, Z.shiftr_div_pow2, <-!Z.sub_1_r, Z.pow_1_r, <-!Z.add_opp_r by lia. |
| 67 |
replace (2 ^ (Z.pos p)) with (2 ^ (Z.pos p - 1)* 2). |
| 68 |
rewrite Z.div_add_l by lia. |
| 69 |
reflexivity. |
| 70 |
change 2 with (2 ^ 1) at 2. |
| 71 |
rewrite <-Z.pow_add_r by (pose proof (Pos2Z.is_pos p); lia). |
| 72 |
f_equal. lia. |
| 73 |
Qed. |
| 74 |
#[global] |
| 75 |
Hint Rewrite <- ones_pred using zutil_arith : push_Zshift. |
| 76 |
|
| 77 |
Lemma ones_succ : forall x, (0 <= x) -> |
| 78 |
Z.ones (Z.succ x) = 2 ^ x + Z.ones x. |
| 79 |
Proof using Type. |
| 80 |
unfold Z.ones; intros. |
| 81 |
rewrite !Z.shiftl_1_l. |
| 82 |
rewrite Z.add_pred_r. |
| 83 |
apply Z.succ_inj. |
| 84 |
rewrite !Z.succ_pred. |
| 85 |
rewrite Z.pow_succ_r; lia. |
| 86 |
Qed. |
| 87 |
|
| 88 |
Lemma ones_nonneg : forall i, (0 <= i) -> 0 <= Z.ones i. |
| 89 |
Proof using Type. |
| 90 |
apply natlike_ind. |
| 91 |
+ unfold Z.ones. simpl; lia. |
| 92 |
+ intros. |
| 93 |
rewrite Z.ones_succ by assumption. |
| 94 |
Z.zero_bounds. |
| 95 |
Qed. |
| 96 |
#[global] |
| 97 |
Hint Resolve ones_nonneg : zarith. |
| 98 |
|
| 99 |
Lemma ones_bound m (Hm : 0 <= m) : |
| 100 |
0 <= Z.ones m < 2 ^ m. |
| 101 |
Proof using Type. split. apply ones_nonneg; lia. rewrite Z.ones_equiv; lia. Qed. |
| 102 |
|
| 103 |
Lemma ones_pos_pos : forall i, (0 < i) -> 0 < Z.ones i. |
| 104 |
Proof using Type. |
| 105 |
intros. |
| 106 |
unfold Z.ones. |
| 107 |
rewrite Z.shiftl_1_l. |
| 108 |
apply Z.lt_succ_lt_pred. |
| 109 |
apply Z.pow_gt_1; lia. |
| 110 |
Qed. |
| 111 |
#[global] |
| 112 |
Hint Resolve ones_pos_pos : zarith. |
| 113 |
|
| 114 |
Lemma lnot_ones_equiv n : Z.lnot (Z.ones n) = -2^n. |
| 115 |
Proof using Type. rewrite Z.ones_equiv, Z.lnot_equiv, <- ?Z.sub_1_r; lia. Qed. |
| 116 |
|
| 117 |
Lemma land_ones_ones n m |
| 118 |
: Z.land (Z.ones n) (Z.ones m) |
| 119 |
= Z.ones (if ((n <? 0) || (m <? 0)) |
| 120 |
then Z.max n m |
| 121 |
else Z.min n m). |
| 122 |
Proof using Type. |
| 123 |
repeat first [ reflexivity |
| 124 |
| break_innermost_match_step |
| 125 |
| progress rewrite ?Bool.orb_true_iff in * |
| 126 |
| progress rewrite ?Bool.orb_false_iff in * |
| 127 |
| progress rewrite ?Z.ltb_lt, ?Z.ltb_ge in * |
| 128 |
| progress destruct_head'_and |
| 129 |
| apply Z.min_case_strong |
| 130 |
| apply Z.max_case_strong |
| 131 |
| progress intros |
| 132 |
| progress destruct_head'_or |
| 133 |
| rewrite !Z.pow_r_Zneg |
| 134 |
| rewrite !Z.land_m1_l |
| 135 |
| rewrite !Z.land_m1_r |
| 136 |
| progress change (Z.pred 0) with (-1) |
| 137 |
| rewrite Z.mod_small by lia |
| 138 |
| match goal with |
| 139 |
| [ H : ?x < 0 |- _ ] => is_var x; destruct x; try lia |
| 140 |
| [ H : ?x <= Z.neg _ |- _ ] => is_var x; destruct x; try lia |
| 141 |
| [ |- context[Z.ones (Z.neg ?x)] ] => rewrite (Z.ones_equiv (Z.neg x)) |
| 142 |
| [ H : ?n <= ?m |- Z.land (Z.ones ?m) (Z.ones ?n) = _ ] |
| 143 |
=> rewrite (Z.land_comm (Z.ones m) (Z.ones n)) |
| 144 |
| [ H : ?n <= ?m |- Z.land (Z.ones ?n) (Z.ones ?m) = _ ] |
| 145 |
=> progress rewrite ?Z.land_ones, ?Z.ones_equiv, <- ?Z.sub_1_r by auto |
| 146 |
| [ H : ?n <= ?m |- _ ] |
| 147 |
=> is_var n; is_var m; unique pose proof (Z.pow_le_mono_r 2 n m ltac:(lia) H) |
| 148 |
| [ |- context[2^?x] ] => unique pose proof (Z.pow2_gt_0 x ltac:(lia)) |
| 149 |
end ]. |
| 150 |
Qed. |
| 151 |
#[global] |
| 152 |
Hint Rewrite land_ones_ones : zsimplify. |
| 153 |
|
| 154 |
Lemma lor_ones_ones n m |
| 155 |
: Z.lor (Z.ones n) (Z.ones m) |
| 156 |
= Z.ones (if ((n <? 0) || (m <? 0)) |
| 157 |
then Z.min n m |
| 158 |
else Z.max n m). |
| 159 |
Proof using Type. |
| 160 |
destruct (Z_zerop n), (Z_zerop m); subst; |
| 161 |
repeat first [ reflexivity |
| 162 |
| break_innermost_match_step |
| 163 |
| progress rewrite ?Bool.orb_true_iff in * |
| 164 |
| progress rewrite ?Bool.orb_false_iff in * |
| 165 |
| progress rewrite ?Z.ltb_lt, ?Z.ltb_ge in * |
| 166 |
| progress destruct_head'_and |
| 167 |
| apply Z.min_case_strong |
| 168 |
| apply Z.max_case_strong |
| 169 |
| progress intros |
| 170 |
| progress destruct_head'_or |
| 171 |
| rewrite !Z.pow_r_Zneg |
| 172 |
| rewrite !Z.lor_m1_l |
| 173 |
| rewrite !Z.lor_m1_r |
| 174 |
| progress change (Z.pred 0) with (-1) |
| 175 |
| rewrite Z.mod_small by lia |
| 176 |
| lia |
| 177 |
| match goal with |
| 178 |
| [ H : ?x < 0 |- _ ] => is_var x; destruct x; try lia |
| 179 |
| [ H : ?x <= Z.neg _ |- _ ] => is_var x; destruct x; try lia |
| 180 |
| [ |- context[Z.ones (Z.neg ?x)] ] => rewrite (Z.ones_equiv (Z.neg x)) |
| 181 |
| [ H : ?n <= ?m |- Z.lor (Z.ones ?m) (Z.ones ?n) = _ ] |
| 182 |
=> rewrite (Z.lor_comm (Z.ones m) (Z.ones n)) |
| 183 |
| [ H : ?n <= ?m |- Z.lor (Z.ones ?n) (Z.ones ?m) = _ ] |
| 184 |
=> progress rewrite ?Z.lor_ones_low; try apply Z.log2_ones_lt_nonneg; rewrite ?Z.ones_equiv, <- ?Z.sub_1_r |
| 185 |
| [ H : ?n <= ?m |- _ ] |
| 186 |
=> is_var n; is_var m; unique pose proof (Z.pow_le_mono_r 2 n m ltac:(lia) H) |
| 187 |
| [ |- context[2^?x] ] => unique pose proof (Z.pow2_gt_0 x ltac:(lia)) |
| 188 |
end ]. |
| 189 |
Qed. |
| 190 |
#[global] |
| 191 |
Hint Rewrite lor_ones_ones : zsimplify. |
| 192 |
|
| 193 |
Lemma lor_pow2_mod_pow2_r x e (He : 0 <= e) : Z.lor x (2^e-1) mod (2^e) = 2^e-1. |
| 194 |
Proof using Type. |
| 195 |
destruct (Z_zerop e). |
| 196 |
{ subst; autorewrite with zsimplify_const; reflexivity. } |
| 197 |
assert (0 <= x mod 2^e < 2^e) by auto with zarith. |
| 198 |
assert (0 <= x mod 2^e <= 2^e-1) by lia. |
| 199 |
assert (Z.log2 (x mod 2^e) <= Z.log2 (2^e-1)) by (apply Z.log2_le_mono; lia). |
| 200 |
assert (Z.log2 (x mod 2^e) < e) by (rewrite Z.sub_1_r, Z.log2_pred_pow2 in * by lia; lia). |
| 201 |
rewrite <- Z.land_ones, Z.land_lor_distr_l by assumption. |
| 202 |
rewrite !Z.sub_1_r, <- !Z.ones_equiv, !Z.land_ones_ones, Z.min_id, Z.max_id, Bool.if_const. |
| 203 |
rewrite Z.land_ones by assumption. |
| 204 |
rewrite Z.lor_ones_low; auto with zarith. |
| 205 |
Qed. |
| 206 |
#[global] |
| 207 |
Hint Rewrite lor_pow2_mod_pow2_r using zutil_arith : zsimplify. |
| 208 |
#[global] |
| 209 |
Hint Rewrite lor_pow2_mod_pow2_r using assumption : zsimplify_fast. |
| 210 |
|
| 211 |
Lemma lor_pow2_mod_pow2_l x e (He : 0 <= e) : Z.lor (2^e-1) x mod (2^e) = 2^e-1. |
| 212 |
Proof using Type. rewrite Z.lor_comm; apply lor_pow2_mod_pow2_r; assumption. Qed. |
| 213 |
#[global] |
| 214 |
Hint Rewrite lor_pow2_mod_pow2_l using zutil_arith : zsimplify. |
| 215 |
#[global] |
| 216 |
Hint Rewrite lor_pow2_mod_pow2_l using assumption : zsimplify_fast. |
| 217 |
|
| 218 |
Lemma lor_pow2_div_pow2_r x e (He : 0 <= e) : (Z.lor x (2^e-1)) / (2^e) = x / 2^e. |
| 219 |
Proof using Type. |
| 220 |
destruct (Z_zerop e). |
| 221 |
{ subst; autorewrite with zsimplify_const; reflexivity. } |
| 222 |
assert (0 < 2^e) by auto with zarith. |
| 223 |
rewrite <- Z.shiftr_div_pow2, Z.shiftr_lor, !Z.shiftr_div_pow2 by lia. |
| 224 |
rewrite (Z.div_small (_-1) _), Z.lor_0_r by lia. |
| 225 |
reflexivity. |
| 226 |
Qed. |
| 227 |
#[global] |
| 228 |
Hint Rewrite lor_pow2_div_pow2_r using zutil_arith : zsimplify. |
| 229 |
#[global] |
| 230 |
Hint Rewrite lor_pow2_div_pow2_r using assumption : zsimplify_fast. |
| 231 |
|
| 232 |
Lemma lor_pow2_div_pow2_l x e (He : 0 <= e) : (Z.lor (2^e-1) x) / (2^e) = x / 2^e. |
| 233 |
Proof using Type. rewrite Z.lor_comm; apply lor_pow2_div_pow2_r; assumption. Qed. |
| 234 |
#[global] |
| 235 |
Hint Rewrite lor_pow2_div_pow2_l using zutil_arith : zsimplify. |
| 236 |
#[global] |
| 237 |
Hint Rewrite lor_pow2_div_pow2_l using assumption : zsimplify_fast. |
| 238 |
|
| 239 |
Lemma land_ones_low_alt a n : 0 <= a < 2^n -> Z.land a (Z.ones n) = a. |
| 240 |
Proof using Type. |
| 241 |
destruct (Z_zerop a); subst; [ intros; now rewrite Z.land_0_l | ]. |
| 242 |
intros; apply Z.land_ones_low; try lia; apply Z.log2_lt_pow2; try lia. |
| 243 |
Qed. |
| 244 |
#[global] |
| 245 |
Hint Rewrite land_ones_low_alt using zutil_arith : zsimplify. |
| 246 |
#[global] |
| 247 |
Hint Rewrite land_ones_low_alt using (idtac + split); assumption : zsimplify_fast. |
| 248 |
|
| 249 |
Lemma land_ones_low_alt_ones a n : 0 <= a <= Z.ones n -> Z.land a (Z.ones n) = a. |
| 250 |
Proof using Type. rewrite Z.ones_equiv at 1; intro H; rewrite land_ones_low_alt; lia. Qed. |
| 251 |
#[global] |
| 252 |
Hint Rewrite land_ones_low_alt_ones using zutil_arith : zsimplify. |
| 253 |
#[global] |
| 254 |
Hint Rewrite land_ones_low_alt_ones using (idtac + split); assumption : zsimplify_fast. |
| 255 |
|
| 256 |
Lemma shiftr_ones_sub n m : 0 <= m <= n -> Z.shiftr (Z.ones n) m = Z.ones (n - m). |
| 257 |
Proof using Type. intro; now rewrite Z.shiftr_div_pow2, Z.ones_div_pow2 by lia. Qed. |
| 258 |
#[global] |
| 259 |
Hint Rewrite shiftr_ones_sub using zutil_arith : zsimplify. |
| 260 |
#[global] |
| 261 |
Hint Rewrite shiftr_ones_sub using (idtac + split); assumption : zsimplify_fast. |
| 262 |
|
| 263 |
Lemma shiftr_ones_same n : 0 <= n -> Z.shiftr (Z.ones n) n = 0. |
| 264 |
Proof using Type. intro; rewrite shiftr_ones_sub, Z.sub_diag by lia; reflexivity. Qed. |
| 265 |
#[global] |
| 266 |
Hint Rewrite shiftr_ones_same using zutil_arith : zsimplify. |
| 267 |
#[global] |
| 268 |
Hint Rewrite shiftr_ones_same using assumption : zsimplify_fast. |
| 269 |
End Z. |