Debian Codegraph @

src/Util/ZUtil/LandLorBounds.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import ZArith.
3 From Coq Require Import Morphisms.
4 Require Import Crypto.Util.ZUtil.Definitions.
5 Require Import Crypto.Util.ZUtil.Pow2.
6 Require Import Crypto.Util.ZUtil.Log2.
7 Require Import Crypto.Util.ZUtil.Tactics.PeelLe.
8 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
9 Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos.
10 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
11 Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall.
12 Require Import Crypto.Util.ZUtil.Tactics.LinearSubstitute.
13 Require Import Crypto.Util.ZUtil.Tactics.SplitMinMax.
14 Require Import Crypto.Util.ZUtil.Modulo.PullPush.
15 Require Import Crypto.Util.ZUtil.LandLorShiftBounds.
16 Require Import Crypto.Util.ZUtil.Modulo.
17 Require Import Crypto.Util.ZUtil.Ones.
18 Require Import Crypto.Util.ZUtil.Lnot.
19 Require Import Crypto.Util.ZUtil.Land.
20 Require Import Crypto.Util.Tactics.UniquePose.
21 Require Import Crypto.Util.Tactics.DestructHead.
22 Require Import Crypto.Util.Tactics.BreakMatch.
23 Local Open Scope Z_scope.
24
25 Module Z.
26 Lemma round_lor_land_bound_bounds x
27 : (0 <= x <= Z.round_lor_land_bound x) \/ (Z.round_lor_land_bound x <= x <= -1).
28 Proof.
29 cbv [Z.round_lor_land_bound]; break_innermost_match; Z.ltb_to_lt.
30 all: constructor; split; try lia; [].
31 all: Z.replace_all_neg_with_pos.
32 all: match goal with |- context[2^Z.log2_up ?x] => pose proof (Z.log2_up_le_full x) end.
33 all: lia.
34 Qed.
35 #[global]
36 Hint Resolve round_lor_land_bound_bounds : zarith.
37
38 Lemma round_lor_land_bound_bounds_pos x
39 : (0 <= Z.pos x <= Z.round_lor_land_bound (Z.pos x)).
40 Proof. generalize (round_lor_land_bound_bounds (Z.pos x)); lia. Qed.
41 #[global]
42 Hint Resolve round_lor_land_bound_bounds_pos : zarith.
43
44 Lemma round_lor_land_bound_bounds_neg x
45 : Z.round_lor_land_bound (Z.neg x) <= Z.neg x <= -1.
46 Proof. generalize (round_lor_land_bound_bounds (Z.neg x)); lia. Qed.
47 #[global]
48 Hint Resolve round_lor_land_bound_bounds_neg : zarith.
49
50 Local Ltac saturate :=
51 repeat first [ progress cbv [Z.round_lor_land_bound Proper respectful Basics.flip] in *
52 | progress Z.ltb_to_lt
53 | progress intros
54 | break_innermost_match_step
55 | lia
56 | rewrite !Pos2Z.opp_neg
57 | match goal with
58 | [ |- context[Z.log2_up ?x] ]
59 => unique pose proof (Z.log2_up_nonneg x)
60 | [ |- context[2^?x] ]
61 => unique assert (0 <= 2^x) by (apply Z.pow_nonneg; lia)
62 | [ H : 0 <= ?x |- context[2^?x] ]
63 => unique assert (0 < 2^x) by (apply Z.pow_pos_nonneg; lia)
64 | [ H : Pos.le ?x ?y |- context[Z.pos ?x] ]
65 => unique assert (Z.pos x <= Z.pos y) by lia
66 | [ H : Pos.le ?x ?y |- context[Z.pos (?x+1)] ]
67 => unique assert (Z.pos (x+1) <= Z.pos (y+1)) by lia
68 | [ H : Z.le ?x ?y |- context[?x+1] ]
69 => unique assert (x+1 <= y+1) by lia
70 | [ H : Z.le ?x ?y |- context[2^Z.log2_up ?x] ]
71 => unique assert (2^Z.log2_up x <= 2^Z.log2_up y) by (Z.peel_le; lia)
72 | [ H : ?a^?b <= ?a^?c |- _ ]
73 => unique assert (a^(c-b) = a^c/a^b) by auto with zarith;
74 unique assert (a^c mod a^b = 0) by auto with zarith
75 end ].
76 Local Ltac do_rewrites_step :=
77 match goal with
78 | [ |- ?R ?x ?x ] => reflexivity
79 (*| [ |- context[Z.land (-2^_) (-2^_)] ]
80 => rewrite <- !Z.lnot_ones_equiv, <- !Z.lnot_lor, !Z.lor_ones_ones, !Z.lnot_ones_equiv
81 | [ |- context[Z.lor (-2^_) (-2^_)] ]
82 => rewrite <- !Z.lnot_ones_equiv, <- !Z.lnot_land, !Z.land_ones_ones, !Z.lnot_ones_equiv
83 | [ |- context[Z.land (2^_-1) (2^_-1)] ]
84 => rewrite !Z.sub_1_r, <- !Z.ones_equiv, !Z.land_ones_ones, !Z.ones_equiv, <- !Z.sub_1_r
85 | [ |- context[Z.lor (2^_-1) (2^_-1)] ]
86 => rewrite !Z.sub_1_r, <- !Z.ones_equiv, !Z.lor_ones_ones, !Z.ones_equiv, <- !Z.sub_1_r
87 | [ |- context[Z.land (2^?x-1) (-2^?y)] ]
88 => rewrite (@Z.land_comm (2^x-1) (-2^y))
89 | [ |- context[Z.lor (2^?x-1) (-2^?y)] ]
90 => rewrite (@Z.lor_comm (2^x-1) (-2^y))
91 | [ |- context[Z.land (-2^_) (2^_-1)] ]
92 => rewrite !Z.sub_1_r, <- !Z.ones_equiv, !Z.land_ones, ?Z.ones_equiv, <- ?Z.sub_1_r by lia
93 | [ |- context[Z.lor (-2^?x) (2^?y-1)] ]
94 => rewrite <- !Z.lnot_ones_equiv, <- (Z.lnot_involutive (2^y-1)), <- !Z.lnot_land, ?Z.lnot_ones_equiv, (Z.lnot_sub1 (2^y)), !Z.ones_equiv, ?Z.lnot_equiv, <- !Z.sub_1_r
95 | [ |- context[-?x mod ?y] ]
96 => rewrite (@Z.opp_mod_mod_push x y) by Z.NoZMod*)
97 | [ |- context[Z.land (2^?y-1) ?x] ]
98 => is_var x; rewrite (Z.land_comm (2^y-1) x)
99 | [ |- context[Z.lor (2^?y-1) ?x] ]
100 => is_var x; rewrite (Z.lor_comm (2^y-1) x)
101 | [ |- context[Z.land (-2^?y) ?x] ]
102 => is_var x; rewrite (Z.land_comm (-2^y) x)
103 | [ |- context[Z.lor (-2^?y) ?x] ]
104 => is_var x; rewrite (Z.lor_comm (-2^y) x)
105 | [ |- context[Z.land _ (2^_-1)] ]
106 => rewrite !Z.sub_1_r, <- !Z.ones_equiv, !Z.land_ones by auto with zarith
107 | [ |- context[Z.land ?x (-2^?y)] ]
108 => is_var x;
109 rewrite <- !Z.lnot_ones_equiv, <- (Z.lnot_involutive x), <- !Z.lnot_lor, !Z.ones_equiv, !Z.lnot_equiv, <- !Z.sub_1_r;
110 let x' := fresh in
111 remember (-x-1) as x' eqn:?; Z.linear_substitute x;
112 rename x' into x
113 | [ |- context[Z.lor ?x (-2^?y)] ]
114 => is_var x;
115 rewrite <- !Z.lnot_ones_equiv, <- (Z.lnot_involutive x), <- !Z.lnot_land, !Z.ones_equiv, !Z.lnot_equiv, <- !Z.sub_1_r;
116 let x' := fresh in
117 remember (-x-1) as x' eqn:?; Z.linear_substitute x;
118 rename x' into x
119 | [ |- Z.lor ?x (?y-1) <= Z.lor ?x (?y'-1) ]
120 => rewrite (Z.div_mod'' (Z.lor x (y-1)) y), (Z.div_mod'' (Z.lor x (y'-1)) y') by auto with zarith
121 | [ |- Z.lor ?x (?y-1) = _ ]
122 => rewrite (Z.div_mod'' (Z.lor x (y-1)) y) by auto with zarith
123 | [ |- context[?m1 - 1 + (?x - ?x mod ?m1)] ]
124 => replace (m1 - 1 + (x - x mod m1)) with ((m1 - x mod m1) + (x - 1)) by lia
125 | _ => progress rewrite ?Z.lor_pow2_div_pow2_r, ?Z.lor_pow2_div_pow2_l, ?Z.lor_pow2_mod_pow2_r, ?Z.lor_pow2_mod_pow2_l by auto with zarith
126 | _ => rewrite !Z.mul_div_eq by lia
127 | _ => progress rewrite ?(Z.add_comm 1) in *
128 | [ |- context[?x mod 2^(Z.log2_up (?x + 1))] ]
129 => rewrite (Z.mod_small x (2^Z.log2_up (x+1))) by (rewrite <- Z.le_succ_l, <- Z.add_1_r, Z.log2_up_le_pow2 by lia; lia)
130 | [ H : ?a^?b <= ?a^?c |- context[?x mod ?a^?b] ]
131 => rewrite (@Z.mod_pow_r_split x a b c) by auto with zarith;
132 (Z.div_mod_to_quot_rem; nia)
133 | _ => progress Z.peel_le
134 (*| [ H : ?x <= ?x |- _ ] => clear H
135 | [ H : ?x < ?y, H' : ?y <= ?z |- _ ] => unique assert (x < z) by lia
136 | [ H : ?x < ?y, H' : ?a <= ?x |- _ ] => unique assert (a < y) by lia
137 | [ H : 2^?x < 2^?y |- context[2^?x mod 2^?y] ]
138 => repeat first [ rewrite (Z.mod_small (2^x) (2^y)) by lia
139 | rewrite !(@Z_mod_nz_opp_full (2^x) (2^y)) ]
140 | [ H : ?x < ?y, H' : context[?x mod ?y] |- _ ] => rewrite (Z.mod_small x y) in H' by lia
141 | [ |- context[2^?x mod 2^?y] ]
142 => let H := fresh in
143 destruct (@Z.pow2_lt_or_divides x y ltac:(lia)) as [H|H];
144 [ repeat first [ rewrite (Z.mod_small (2^x) (2^y)) by lia
145 | rewrite !(@Z_mod_nz_opp_full (2^x) (2^y)) ]
146 | rewrite H ]*)
147 | _ => progress autorewrite with zsimplify_fast in *
148 | [ |- context[-(-?x-1)] ] => replace (-(-x-1)) with (1+x) by lia
149 | [ H : 0 > -(1+?x) |- _ ] => assert (0 <= x) by (clear -H; lia); clear H
150 | [ H : 0 > -(?x+1) |- _ ] => assert (0 <= x) by (clear -H; lia); clear H
151 | [ |- ?a - ?b = ?a' - ?b' ] => apply f_equal2; try reflexivity; []
152 | [ |- -?a = -?a' ] => apply f_equal
153 | _ => rewrite <- !Z.sub_1_r
154 | _ => lia
155 end.
156 Local Ltac do_rewrites := repeat do_rewrites_step.
157 Local Ltac fin_t :=
158 repeat first [ progress destruct_head'_and
159 | match goal with
160 | [ H : orb _ _ = _ |- _ ]
161 => progress rewrite ?Bool.orb_true_iff, ?Bool.orb_false_iff, ?Z.ltb_lt, ?Z.ltb_ge in *
162 end
163 | break_innermost_match_step
164 | progress destruct_head'_or
165 | lia
166 | progress Z.peel_le ].
167 Local Ltac t :=
168 saturate; do_rewrites.
169
170 Local Instance land_round_Proper_pos_r x
171 : Proper (Pos.le ==> Z.le) (fun y => Z.land x (Z.round_lor_land_bound (Z.pos y))).
172 Proof. t. Qed.
173
174 Local Instance land_round_Proper_pos_l y
175 : Proper (Pos.le ==> Z.le) (fun x => Z.land (Z.round_lor_land_bound (Z.pos x)) y).
176 Proof. t. Qed.
177
178 Local Instance lor_round_Proper_pos_r x
179 : Proper (Pos.le ==> Z.le) (fun y => Z.lor x (Z.round_lor_land_bound (Z.pos y))).
180 Proof. t. Qed.
181
182 Local Instance lor_round_Proper_pos_l y
183 : Proper (Pos.le ==> Z.le) (fun x => Z.lor (Z.round_lor_land_bound (Z.pos x)) y).
184 Proof. t. Qed.
185
186 Local Instance land_round_Proper_neg_r x
187 : Proper (Basics.flip Pos.le ==> Z.le) (fun y => Z.land x (Z.round_lor_land_bound (Z.neg y))).
188 Proof. t. Qed.
189
190 Local Instance land_round_Proper_neg_l y
191 : Proper (Basics.flip Pos.le ==> Z.le) (fun x => Z.land (Z.round_lor_land_bound (Z.neg x)) y).
192 Proof. t. Qed.
193
194 Local Instance lor_round_Proper_neg_r x
195 : Proper (Basics.flip Pos.le ==> Z.le) (fun y => Z.lor x (Z.round_lor_land_bound (Z.neg y))).
196 Proof. t. Qed.
197
198 Local Instance lor_round_Proper_neg_l y
199 : Proper (Basics.flip Pos.le ==> Z.le) (fun x => Z.lor (Z.round_lor_land_bound (Z.neg x)) y).
200 Proof. t. Qed.
201
202 Lemma land_round_lor_land_bound_r x
203 : Z.land x (Z.round_lor_land_bound x) = if (0 <=? x) then x else Z.round_lor_land_bound x.
204 Proof. t. Qed.
205 #[global]
206 Hint Rewrite land_round_lor_land_bound_r : zsimplify_fast zsimplify.
207 Lemma land_round_lor_land_bound_l x
208 : Z.land (Z.round_lor_land_bound x) x = if (0 <=? x) then x else Z.round_lor_land_bound x.
209 Proof. rewrite Z.land_comm, land_round_lor_land_bound_r; reflexivity. Qed.
210 #[global]
211 Hint Rewrite land_round_lor_land_bound_l : zsimplify_fast zsimplify.
212
213 Lemma lor_round_lor_land_bound_r x
214 : Z.lor x (Z.round_lor_land_bound x) = if (0 <=? x) then Z.round_lor_land_bound x else x.
215 Proof. t. Qed.
216 #[global]
217 Hint Rewrite lor_round_lor_land_bound_r : zsimplify_fast zsimplify.
218 Lemma lor_round_lor_land_bound_l x
219 : Z.lor (Z.round_lor_land_bound x) x = if (0 <=? x) then Z.round_lor_land_bound x else x.
220 Proof. rewrite Z.lor_comm, lor_round_lor_land_bound_r; reflexivity. Qed.
221 #[global]
222 Hint Rewrite lor_round_lor_land_bound_l : zsimplify_fast zsimplify.
223
224 Lemma land_round_bound_pos_r v x
225 : 0 <= Z.land v (Z.pos x) <= Z.land v (Z.round_lor_land_bound (Z.pos x)).
226 Proof.
227 rewrite Z.land_nonneg; split; [ lia | ].
228 replace (Z.pos x) with (Z.land (Z.pos x) (Z.round_lor_land_bound (Z.pos x))) at 1
229 by now rewrite land_round_lor_land_bound_r.
230 rewrite (Z.land_comm (Z.pos x)), Z.land_assoc.
231 apply Z.land_upper_bound_l; rewrite ?Z.land_nonneg; t.
232 Qed.
233 #[global]
234 Hint Resolve land_round_bound_pos_r : zarith.
235 #[global]
236 Hint Extern 0 => simple apply (fun v x => proj1 (land_round_bound_pos_r v x)) : zarith.
237 #[global]
238 Hint Extern 0 => simple apply (fun v x => proj2 (land_round_bound_pos_r v x)) : zarith.
239 Lemma land_round_bound_pos_l v x
240 : 0 <= Z.land (Z.pos x) v <= Z.land (Z.round_lor_land_bound (Z.pos x)) v.
241 Proof. rewrite <- !(Z.land_comm v); apply land_round_bound_pos_r. Qed.
242 #[global]
243 Hint Resolve land_round_bound_pos_l : zarith.
244 #[global]
245 Hint Extern 0 => simple apply (fun v x => proj1 (land_round_bound_pos_l v x)) : zarith.
246 #[global]
247 Hint Extern 0 => simple apply (fun v x => proj2 (land_round_bound_pos_l v x)) : zarith.
248
249 Lemma land_round_bound_neg_r v x
250 : Z.land v (Z.round_lor_land_bound (Z.neg x)) <= Z.land v (Z.neg x) <= v.
251 Proof.
252 assert (0 < 2 ^ Z.log2_up (Z.pos x)) by auto with zarith.
253 split; [ | apply Z.land_le; lia ].
254 replace (Z.round_lor_land_bound (Z.neg x)) with (Z.land (Z.neg x) (Z.round_lor_land_bound (Z.neg x)))
255 by now rewrite land_round_lor_land_bound_r.
256 rewrite !Z.land_assoc.
257 etransitivity; [ apply Z.land_le; cbn; lia | ]; lia.
258 Qed.
259 #[global]
260 Hint Resolve land_round_bound_neg_r : zarith.
261 #[global]
262 Hint Extern 0 => simple apply (fun v x => proj1 (land_round_bound_neg_r v x)) : zarith.
263 #[global]
264 Hint Extern 0 => simple apply (fun v x => proj2 (land_round_bound_neg_r v x)) : zarith.
265 Lemma land_round_bound_neg_l v x
266 : Z.land (Z.round_lor_land_bound (Z.neg x)) v <= Z.land (Z.neg x) v <= v.
267 Proof. rewrite <- !(Z.land_comm v); apply land_round_bound_neg_r. Qed.
268 #[global]
269 Hint Resolve land_round_bound_neg_l : zarith.
270 #[global]
271 Hint Extern 0 => simple apply (fun v x => proj1 (land_round_bound_neg_l v x)) : zarith.
272 #[global]
273 Hint Extern 0 => simple apply (fun v x => proj2 (land_round_bound_neg_l v x)) : zarith.
274
275 Lemma lor_round_bound_neg_r v x
276 : Z.lor v (Z.round_lor_land_bound (Z.neg x)) <= Z.lor v (Z.neg x) <= -1.
277 Proof.
278 change (-1) with (Z.pred 0); rewrite <- Z.lt_le_pred.
279 rewrite Z.lor_neg; split; [ | lia ].
280 replace (Z.neg x) with (Z.lor (Z.neg x) (Z.round_lor_land_bound (Z.neg x))) at 2
281 by now rewrite lor_round_lor_land_bound_r.
282 rewrite (Z.lor_comm (Z.neg x)), Z.lor_assoc.
283 cbn; rewrite <- !Z.lnot_ones_equiv, <- (Z.lnot_involutive v), <- (Z.lnot_involutive (Z.neg x)), <- !Z.lnot_land, !Z.ones_equiv, !Z.lnot_equiv, <- !Z.sub_1_r, !Pos2Z.opp_neg.
284 Z.peel_le.
285 apply Z.land_upper_bound_l; rewrite ?Z.land_nonneg; t.
286 Qed.
287 #[global]
288 Hint Resolve lor_round_bound_neg_r : zarith.
289 #[global]
290 Hint Extern 0 => simple apply (fun v x => proj1 (lor_round_bound_neg_r v x)) : zarith.
291 #[global]
292 Hint Extern 0 => simple apply (fun v x => proj2 (lor_round_bound_neg_r v x)) : zarith.
293 Lemma lor_round_bound_neg_l v x
294 : Z.lor (Z.round_lor_land_bound (Z.neg x)) v <= Z.lor (Z.neg x) v <= -1.
295 Proof. rewrite <- !(Z.lor_comm v); apply lor_round_bound_neg_r. Qed.
296 #[global]
297 Hint Resolve lor_round_bound_neg_l : zarith.
298 #[global]
299 Hint Extern 0 => simple apply (fun v x => proj1 (lor_round_bound_neg_l v x)) : zarith.
300 #[global]
301 Hint Extern 0 => simple apply (fun v x => proj2 (lor_round_bound_neg_l v x)) : zarith.
302
303 Lemma lor_round_bound_pos_r v x
304 : v <= Z.lor v (Z.pos x) <= Z.lor v (Z.round_lor_land_bound (Z.pos x)).
305 Proof.
306 assert (0 < 2 ^ Z.log2_up (Z.pos (x + 1))) by auto with zarith.
307 split; [ apply Z.lor_lower; lia | ].
308 replace (Z.round_lor_land_bound (Z.pos x)) with (Z.lor (Z.pos x) (Z.round_lor_land_bound (Z.pos x)))
309 by now rewrite lor_round_lor_land_bound_r.
310 rewrite !Z.lor_assoc.
311 etransitivity; [ | apply Z.lor_lower; rewrite ?Z.lor_nonneg; cbn; lia ]; lia.
312 Qed.
313 #[global]
314 Hint Resolve lor_round_bound_pos_r : zarith.
315 #[global]
316 Hint Extern 0 => simple apply (fun v x => proj1 (lor_round_bound_pos_r v x)) : zarith.
317 #[global]
318 Hint Extern 0 => simple apply (fun v x => proj2 (lor_round_bound_pos_r v x)) : zarith.
319 Lemma lor_round_bound_pos_l v x
320 : v <= Z.lor (Z.pos x) v <= Z.lor (Z.round_lor_land_bound (Z.pos x)) v.
321 Proof. rewrite <- !(Z.lor_comm v); apply lor_round_bound_pos_r. Qed.
322 #[global]
323 Hint Resolve lor_round_bound_pos_l : zarith.
324 #[global]
325 Hint Extern 0 => simple apply (fun v x => proj1 (lor_round_bound_pos_l v x)) : zarith.
326 #[global]
327 Hint Extern 0 => simple apply (fun v x => proj2 (lor_round_bound_pos_l v x)) : zarith.
328
329 Lemma land_round_bound_pos_r' v x : Z.land v (Z.pos x) <= Z.land v (Z.round_lor_land_bound (Z.pos x)). Proof. auto with zarith. Qed.
330 Lemma land_round_bound_pos_l' v x : Z.land (Z.pos x) v <= Z.land (Z.round_lor_land_bound (Z.pos x)) v. Proof. auto with zarith. Qed.
331 Lemma land_round_bound_neg_r' v x : Z.land v (Z.round_lor_land_bound (Z.neg x)) <= Z.land v (Z.neg x). Proof. auto with zarith. Qed.
332 Lemma land_round_bound_neg_l' v x : Z.land (Z.round_lor_land_bound (Z.neg x)) v <= Z.land (Z.neg x) v. Proof. auto with zarith. Qed.
333 Lemma lor_round_bound_neg_r' v x : Z.lor v (Z.round_lor_land_bound (Z.neg x)) <= Z.lor v (Z.neg x). Proof. auto with zarith. Qed.
334 Lemma lor_round_bound_neg_l' v x : Z.lor (Z.round_lor_land_bound (Z.neg x)) v <= Z.lor (Z.neg x) v. Proof. auto with zarith. Qed.
335 Lemma lor_round_bound_pos_r' v x : Z.lor v (Z.pos x) <= Z.lor v (Z.round_lor_land_bound (Z.pos x)). Proof. auto with zarith. Qed.
336 Lemma lor_round_bound_pos_l' v x : Z.lor (Z.pos x) v <= Z.lor (Z.round_lor_land_bound (Z.pos x)) v. Proof. auto with zarith. Qed.
337 End Z.