Debian Codegraph @

src/Util/ZUtil/Shift.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 Require Import Crypto.Util.ZUtil.Hints.Core.
4 Require Import Crypto.Util.ZUtil.Ones.
5 Require Import Crypto.Util.ZUtil.Definitions.
6 Require Import Crypto.Util.ZUtil.Testbit.
7 Require Import Crypto.Util.ZUtil.Pow2Mod.
8 Require Import Crypto.Util.ZUtil.Le.
9 Require Import Crypto.Util.ZUtil.Div.
10 Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds.
11 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
12 Require Import Crypto.Util.ZUtil.LandLorShiftBounds.
13 Require Import Crypto.Util.ZUtil.Notations.
14 Require Import Crypto.Util.Tactics.BreakMatch.
15 Require Import Crypto.Util.Tactics.SpecializeBy.
16 Local Open Scope Z_scope.
17
18 Module Z.
19 Lemma shiftr_add_shiftl_high : forall n m a b, 0 <= n <= m -> 0 <= a < 2 ^ n ->
20 Z.shiftr (a + (Z.shiftl b n)) m = Z.shiftr b (m - n).
21 Proof.
22 intros n m a b H H0.
23 rewrite !Z.shiftr_div_pow2, Z.shiftl_mul_pow2 by lia.
24 replace (2 ^ m) with (2 ^ n * 2 ^ (m - n)) by
25 (rewrite <-Z.pow_add_r by lia; f_equal; ring).
26 rewrite <-Z.div_div, Z.div_add, (Z.div_small a) ; try solve
27 [assumption || apply Z.pow_nonzero || apply Z.pow_pos_nonneg; lia].
28 f_equal; ring.
29 Qed.
30 #[global]
31 Hint Rewrite Z.shiftr_add_shiftl_high using zutil_arith : pull_Zshift.
32 #[global]
33 Hint Rewrite <- Z.shiftr_add_shiftl_high using zutil_arith : push_Zshift.
34
35 Lemma shiftr_add_shiftl_low : forall n m a b, 0 <= m <= n -> 0 <= a < 2 ^ n ->
36 Z.shiftr (a + (Z.shiftl b n)) m = Z.shiftr a m + Z.shiftr b (m - n).
37 Proof.
38 intros n m a b H H0.
39 rewrite !Z.shiftr_div_pow2, Z.shiftl_mul_pow2, Z.shiftr_mul_pow2 by lia.
40 replace (2 ^ n) with (2 ^ (n - m) * 2 ^ m) by
41 (rewrite <-Z.pow_add_r by lia; f_equal; ring).
42 rewrite Z.mul_assoc, Z.div_add by (apply Z.pow_nonzero; lia).
43 repeat f_equal; ring.
44 Qed.
45 #[global]
46 Hint Rewrite Z.shiftr_add_shiftl_low using zutil_arith : pull_Zshift.
47 #[global]
48 Hint Rewrite <- Z.shiftr_add_shiftl_low using zutil_arith : push_Zshift.
49
50 Lemma testbit_add_shiftl_high : forall i, (0 <= i) -> forall a b n, (0 <= n <= i) ->
51 0 <= a < 2 ^ n ->
52 Z.testbit (a + Z.shiftl b n) i = Z.testbit b (i - n).
53 Proof.
54 intros i ?.
55 apply natlike_ind with (x := i); [ intros a b n | intros x H0 H1 a b n | ]; intros; try assumption;
56 (destruct (Z.eq_dec 0 n); [ subst; rewrite Z.pow_0_r in *;
57 replace a with 0 by lia; f_equal; ring | ]); try lia.
58 rewrite <-Z.add_1_r at 1. rewrite <-Z.shiftr_spec by assumption.
59 replace (Z.succ x - n) with (x - (n - 1)) by ring.
60 rewrite shiftr_add_shiftl_low, <-Z.shiftl_opp_r with (a := b) by lia.
61 rewrite <-H1 with (a := Z.shiftr a 1); try lia; [ repeat f_equal; ring | ].
62 rewrite Z.shiftr_div_pow2 by lia.
63 split; apply Z.div_pos || apply Z.div_lt_upper_bound;
64 try solve [rewrite ?Z.pow_1_r; lia].
65 rewrite <-Z.pow_add_r by lia.
66 replace (1 + (n - 1)) with n by ring; lia.
67 Qed.
68 #[global]
69 Hint Rewrite testbit_add_shiftl_high using zutil_arith : Ztestbit.
70
71 Lemma testbit_add_shiftl i a b n
72 (Ha : 0 <= a < 2^n)
73 (Hb : 0 <= n)
74 (Hn : 0 <= i) :
75 Z.testbit (a + Z.shiftl b n) i = if (i <? n)
76 then Z.testbit a i
77 else Z.testbit b (i - n).
78 Proof. destruct (Z.ltb_spec i n); autorewrite with Ztestbit; reflexivity. Qed.
79
80 Lemma shiftr_succ : forall n x,
81 Z.shiftr n (Z.succ x) = Z.shiftr (Z.shiftr n x) 1.
82 Proof.
83 intros.
84 rewrite Z.shiftr_shiftr by lia.
85 reflexivity.
86 Qed.
87 #[global]
88 Hint Rewrite Z.shiftr_succ using zutil_arith : push_Zshift.
89 #[global]
90 Hint Rewrite <- Z.shiftr_succ using zutil_arith : pull_Zshift.
91
92 Lemma shiftr_1_r_le : forall a b, a <= b ->
93 Z.shiftr a 1 <= Z.shiftr b 1.
94 Proof.
95 intros.
96 rewrite !Z.shiftr_div_pow2, Z.pow_1_r by lia.
97 apply Z.div_le_mono; lia.
98 Qed.
99 #[global]
100 Hint Resolve shiftr_1_r_le : zarith.
101
102 Lemma shiftr_le : forall a b i : Z, 0 <= i -> a <= b -> a >> i <= b >> i.
103 Proof.
104 intros a b i ?; revert a b. apply natlike_ind with (x := i); intros; auto.
105 rewrite !shiftr_succ, shiftr_1_r_le; eauto. reflexivity.
106 Qed.
107 #[global]
108 Hint Resolve shiftr_le : zarith.
109
110 Lemma shiftr_ones' : forall a n, 0 <= a < 2 ^ n -> forall i, (0 <= i) ->
111 Z.shiftr a i <= Z.ones (n - i) \/ n <= i.
112 Proof.
113 intros a n H.
114 apply natlike_ind.
115 + unfold Z.ones.
116 rewrite Z.shiftr_0_r, Z.shiftl_1_l, Z.sub_0_r.
117 lia.
118 + intros x H0 H1.
119 destruct (Z_lt_le_dec x n); try lia.
120 intuition auto with zarith lia.
121 left.
122 rewrite shiftr_succ.
123 replace (n - Z.succ x) with (Z.pred (n - x)) by lia.
124 rewrite Z.ones_pred by lia.
125 apply Z.shiftr_1_r_le.
126 assumption.
127 Qed.
128
129 Lemma shiftr_ones : forall a n i, 0 <= a < 2 ^ n -> (0 <= i) -> (i <= n) ->
130 Z.shiftr a i <= Z.ones (n - i) .
131 Proof.
132 intros a n i G G0 G1.
133 destruct (Z_le_lt_eq_dec i n G1).
134 + destruct (Z.shiftr_ones' a n G i G0); lia.
135 + subst; rewrite Z.sub_diag.
136 destruct (Z.eq_dec a 0).
137 - subst; rewrite Z.shiftr_0_l; reflexivity.
138 - rewrite Z.shiftr_eq_0; try lia; try reflexivity.
139 apply Z.log2_lt_pow2; lia.
140 Qed.
141 #[global]
142 Hint Resolve shiftr_ones : zarith.
143
144 Lemma shiftr_upper_bound : forall a n, 0 <= n -> 0 <= a <= 2 ^ n -> Z.shiftr a n <= 1.
145 Proof.
146 intros a ? ? [a_nonneg a_upper_bound].
147 apply Z_le_lt_eq_dec in a_upper_bound.
148 destruct a_upper_bound.
149 + destruct (Z.eq_dec 0 a).
150 - subst; rewrite Z.shiftr_0_l; lia.
151 - rewrite Z.shiftr_eq_0; auto; try lia.
152 apply Z.log2_lt_pow2; auto; lia.
153 + subst.
154 rewrite Z.shiftr_div_pow2 by assumption.
155 assert (0 < 2 ^ n) by (apply Z.pow_pos_nonneg; lia).
156 rewrite Z.div_same; lia.
157 Qed.
158 #[global]
159 Hint Resolve shiftr_upper_bound : zarith.
160
161 Lemma lor_shiftl : forall a b n, 0 <= n -> 0 <= a < 2 ^ n ->
162 Z.lor a (Z.shiftl b n) = a + (Z.shiftl b n).
163 Proof.
164 intros a b n H H0.
165 apply Z.bits_inj'; intros t ?.
166 rewrite Z.lor_spec, Z.shiftl_spec by assumption.
167 destruct (Z_lt_dec t n).
168 + rewrite Z.testbit_add_shiftl_low by lia.
169 rewrite Z.testbit_neg_r with (n := t - n) by lia.
170 apply Bool.orb_false_r.
171 + rewrite testbit_add_shiftl_high by lia.
172 replace (Z.testbit a t) with false; [ apply Bool.orb_false_l | ].
173 symmetry.
174 apply Z.testbit_false; try lia.
175 rewrite Z.div_small; try reflexivity.
176 split; try eapply Z.lt_le_trans with (m := 2 ^ n); try lia.
177 apply Z.pow_le_mono_r; lia.
178 Qed.
179 #[global]
180 Hint Rewrite <- Z.lor_shiftl using zutil_arith : convert_to_Ztestbit.
181
182 Lemma lor_shiftl' : forall a b n, 0 <= n -> 0 <= a < 2 ^ n ->
183 Z.lor (Z.shiftl b n) a = (Z.shiftl b n) + a.
184 Proof.
185 intros; rewrite Z.lor_comm, Z.add_comm; apply lor_shiftl; assumption.
186 Qed.
187 #[global]
188 Hint Rewrite <- Z.lor_shiftl' using zutil_arith : convert_to_Ztestbit.
189
190 Lemma shiftl_spec_full a n m
191 : Z.testbit (a << n) m = if Z_lt_dec m n
192 then false
193 else if Z_le_dec 0 m
194 then Z.testbit a (m - n)
195 else false.
196 Proof.
197 repeat break_match; auto using Z.shiftl_spec_low, Z.shiftl_spec, Z.testbit_neg_r with lia.
198 Qed.
199 #[global]
200 Hint Rewrite shiftl_spec_full : Ztestbit_full.
201
202 Lemma shiftr_spec_full a n m
203 : Z.testbit (a >> n) m = if Z_lt_dec m (-n)
204 then false
205 else if Z_le_dec 0 m
206 then Z.testbit a (m + n)
207 else false.
208 Proof.
209 rewrite <- Z.shiftl_opp_r, shiftl_spec_full, Z.sub_opp_r; reflexivity.
210 Qed.
211 #[global]
212 Hint Rewrite shiftr_spec_full : Ztestbit_full.
213
214 Lemma testbit_add_shiftl_full i a b n (Ha : 0 <= a < 2^n)
215 : Z.testbit (a + b << n) i
216 = if (i <? 0) then false else if (i <? n) then Z.testbit a i else Z.testbit b (i - n).
217 Proof.
218 assert (0 < 2^n) by lia.
219 assert (0 <= n) by eauto 2 with zarith.
220 pose proof (Zlt_cases i n); break_match; autorewrite with Ztestbit; reflexivity.
221 Qed.
222 #[global]
223 Hint Rewrite testbit_add_shiftl_full using zutil_arith : Ztestbit.
224
225 Lemma land_add_land : forall n m a b, (m <= n)%nat ->
226 Z.land ((Z.land a (Z.ones (Z.of_nat n))) + (Z.shiftl b (Z.of_nat n))) (Z.ones (Z.of_nat m)) = Z.land a (Z.ones (Z.of_nat m)).
227 Proof.
228 intros n m a b H.
229 rewrite !Z.land_ones by apply Nat2Z.is_nonneg.
230 rewrite Z.shiftl_mul_pow2 by apply Nat2Z.is_nonneg.
231 replace (b * 2 ^ Z.of_nat n) with
232 ((b * 2 ^ Z.of_nat (n - m)) * 2 ^ Z.of_nat m) by
233 (rewrite <-(Nat.sub_add m n H) at 2;
234 rewrite Nat2Z.inj_add, Z.pow_add_r by apply Nat2Z.is_nonneg; ring).
235 rewrite Z.mod_add by (pose proof (Z.pow_pos_nonneg 2 (Z.of_nat m)); lia).
236 symmetry. apply Znumtheory.Zmod_div_mod; try (apply Z.pow_pos_nonneg; lia).
237 rewrite <-(Nat.sub_add m n) by assumption.
238 rewrite Nat2Z.inj_add, Z.pow_add_r by apply Nat2Z.is_nonneg.
239 now rewrite Z.mul_comm; apply Z.divide_factor_l.
240 Qed.
241
242 Lemma shiftl_add x y z : 0 <= z -> (x + y) << z = (x << z) + (y << z).
243 Proof. intros; autorewrite with Zshift_to_pow; lia. Qed.
244 #[global]
245 Hint Rewrite shiftl_add using zutil_arith : push_Zshift.
246 #[global]
247 Hint Rewrite <- shiftl_add using zutil_arith : pull_Zshift.
248
249 Lemma shiftr_add x y z : z <= 0 -> (x + y) >> z = (x >> z) + (y >> z).
250 Proof. intros; autorewrite with Zshift_to_pow; lia. Qed.
251 #[global]
252 Hint Rewrite shiftr_add using zutil_arith : push_Zshift.
253 #[global]
254 Hint Rewrite <- shiftr_add using zutil_arith : pull_Zshift.
255
256 Lemma shiftl_sub x y z : 0 <= z -> (x - y) << z = (x << z) - (y << z).
257 Proof. intros; autorewrite with Zshift_to_pow; lia. Qed.
258 #[global]
259 Hint Rewrite shiftl_sub using zutil_arith : push_Zshift.
260 #[global]
261 Hint Rewrite <- shiftl_sub using zutil_arith : pull_Zshift.
262
263 Lemma shiftr_sub x y z : z <= 0 -> (x - y) >> z = (x >> z) - (y >> z).
264 Proof. intros; autorewrite with Zshift_to_pow; lia. Qed.
265 #[global]
266 Hint Rewrite shiftr_sub using zutil_arith : push_Zshift.
267 #[global]
268 Hint Rewrite <- shiftr_sub using zutil_arith : pull_Zshift.
269
270 Lemma compare_add_shiftl : forall x1 y1 x2 y2 n, 0 <= n ->
271 Z.pow2_mod x1 n = x1 -> Z.pow2_mod x2 n = x2 ->
272 x1 + (y1 << n) ?= x2 + (y2 << n) =
273 if Z.eq_dec y1 y2
274 then x1 ?= x2
275 else y1 ?= y2.
276 Proof.
277 repeat match goal with
278 | |- _ => progress intros
279 | |- _ => progress subst y1
280 | |- _ => rewrite Z.shiftl_mul_pow2 by lia
281 | |- _ => rewrite Z.add_compare_mono_r
282 | |- _ => rewrite <-Z.mul_sub_distr_r
283 | |- _ => break_innermost_match_step
284 | H : Z.pow2_mod _ _ = _ |- _ => rewrite Z.pow2_mod_id_iff in H by lia
285 | H : ?a <> ?b |- _ = (?a ?= ?b) =>
286 case_eq (a ?= b); rewrite ?Z.compare_eq_iff, ?Z.compare_gt_iff, ?Z.compare_lt_iff
287 | |- _ + (_ * _) > _ + (_ * _) => cbv [Z.gt]
288 | |- _ + (_ * ?x) < _ + (_ * ?x) =>
289 apply Z.lt_sub_lt_add; apply Z.lt_le_trans with (m := 1 * x); [lia|]
290 | |- _ => apply Z.mul_le_mono_nonneg_r; lia
291 | |- _ => reflexivity
292 | |- _ => congruence
293 end.
294 Qed.
295
296 (* (* not compatible with x mod 0 = x *)
297 Lemma shiftl_opp_l a n
298 : Z.shiftl (-a) n = - Z.shiftl a n - (if Z_zerop (a mod 2 ^ (- n)) then 0 else 1).
299 Proof.
300 destruct (Z_dec 0 n) as [ [?|?] | ? ];
301 subst;
302 rewrite ?Z.pow_neg_r by lia;
303 autorewrite with zsimplify_const;
304 [ | | simpl; lia ].
305 { rewrite !Z.shiftl_mul_pow2 by lia.
306 nia. }
307 { rewrite !Z.shiftl_div_pow2 by lia.
308 rewrite Z.div_opp_l_complete by auto with zarith.
309 reflexivity. }
310 Qed.
311 Hint Rewrite shiftl_opp_l : push_Zshift.
312 Hint Rewrite <- shiftl_opp_l : pull_Zshift.
313
314 Lemma shiftr_opp_l a n
315 : Z.shiftr (-a) n = - Z.shiftr a n - (if Z_zerop (a mod 2 ^ n) then 0 else 1).
316 Proof.
317 unfold Z.shiftr; rewrite shiftl_opp_l at 1; rewrite Z.opp_involutive.
318 reflexivity.
319 Qed.
320 Hint Rewrite shiftr_opp_l : push_Zshift.
321 Hint Rewrite <- shiftr_opp_l : pull_Zshift. *)
322
323 Lemma shl_shr_lt x y n m (Hx : 0 <= x < 2^n) (Hy : 0 <= y < 2^n) (Hm : 0 <= m <= n)
324 : 0 <= (x >> (n - m)) + ((y << m) mod 2^n) < 2^n.
325 Proof.
326 cut (0 <= (x >> (n - m)) + ((y << m) mod 2^n) <= 2^n - 1); [ lia | ].
327 assert (0 <= x <= 2^n - 1) by lia.
328 assert (0 <= y <= 2^n - 1) by lia.
329 assert (0 < 2 ^ (n - m)) by auto with zarith.
330 assert (0 <= y mod 2 ^ (n - m) < 2^(n-m)) by auto with zarith.
331 assert (0 <= y mod 2 ^ (n - m) <= 2 ^ (n - m) - 1) by lia.
332 assert (0 <= (y mod 2 ^ (n - m)) * 2^m <= (2^(n-m) - 1)*2^m) by auto with zarith.
333 assert (0 <= x / 2^(n-m) < 2^n / 2^(n-m)).
334 { split; Z.zero_bounds.
335 apply Z.div_lt_upper_bound; autorewrite with pull_Zpow zsimplify; nia. }
336 autorewrite with Zshift_to_pow.
337 split; Z.zero_bounds.
338 replace (2^n) with (2^(n-m) * 2^m) by (autorewrite with pull_Zpow; f_equal; lia).
339 rewrite Zmult_mod_distr_r.
340 autorewrite with pull_Zpow zsimplify push_Zmul in * |- .
341 nia.
342 Qed.
343
344 Lemma add_shift_mod x y n m
345 (Hx : 0 <= x < 2^n) (Hy : 0 <= y)
346 (Hn : 0 <= n) (Hm : 0 < m)
347 : (x + y << n) mod (m * 2^n) = x + (y mod m) << n.
348 Proof.
349 pose proof (Z.mod_bound_pos y m).
350 specialize_by lia.
351 assert (0 < 2^n) by auto with zarith.
352 autorewrite with Zshift_to_pow.
353 rewrite Zplus_mod, !Zmult_mod_distr_r.
354 rewrite Zplus_mod, !Zmod_mod, <- Zplus_mod.
355 rewrite !(Zmod_eq (_ + _)) by nia.
356 etransitivity; [ | apply Z.add_0_r ].
357 rewrite <- !Z.add_opp_r, <- !Z.add_assoc.
358 repeat apply f_equal.
359 ring_simplify.
360 cut (((x + y mod m * 2 ^ n) / (m * 2 ^ n)) = 0); [ nia | ].
361 apply Z.div_small; split; nia.
362 Qed.
363
364 Lemma add_mul_mod x y n m
365 (Hx : 0 <= x < 2^n) (Hy : 0 <= y)
366 (Hn : 0 <= n) (Hm : 0 < m)
367 : (x + y * 2^n) mod (m * 2^n) = x + (y mod m) * 2^n.
368 Proof.
369 generalize (add_shift_mod x y n m).
370 autorewrite with Zshift_to_pow; auto.
371 Qed.
372
373 Lemma lt_pow_2_shiftr : forall a n, 0 <= a < 2 ^ n -> a >> n = 0.
374 Proof.
375 intros a n H.
376 destruct (Z_le_dec 0 n).
377 + rewrite Z.shiftr_div_pow2 by assumption.
378 auto using Z.div_small.
379 + assert (2 ^ n = 0) by (apply Z.pow_neg_r; lia).
380 lia.
381 Qed.
382
383 #[global]
384 Hint Rewrite Z.pow2_bits_eqb using zutil_arith : Ztestbit.
385 Lemma pow_2_shiftr : forall n, 0 <= n -> (2 ^ n) >> n = 1.
386 Proof.
387 intros; apply Z.bits_inj'; intros.
388 replace 1 with (2 ^ 0) by ring.
389 repeat match goal with
390 | |- _ => progress intros
391 | |- _ => progress rewrite ?Z.eqb_eq, ?Z.eqb_neq in *
392 | |- _ => progress autorewrite with Ztestbit
393 | |- context[Z.eqb ?a ?b] => case_eq (Z.eqb a b)
394 | |- _ => reflexivity || lia
395 end.
396 Qed.
397
398 Lemma lt_mul_2_pow_2_shiftr : forall a n, 0 <= a < 2 * 2 ^ n ->
399 a >> n = if Z_lt_dec a (2 ^ n) then 0 else 1.
400 Proof.
401 intros a n H; break_match; [ apply lt_pow_2_shiftr; lia | ].
402 destruct (Z_le_dec 0 n).
403 + replace (2 * 2 ^ n) with (2 ^ (n + 1)) in *
404 by (rewrite Z.pow_add_r; try lia; ring).
405 pose proof (Z.shiftr_ones a (n + 1) n H).
406 pose proof (Z.shiftr_le (2 ^ n) a n).
407 specialize_by lia.
408 replace (n + 1 - n) with 1 in * by ring.
409 replace (Z.ones 1) with 1 in * by reflexivity.
410 rewrite pow_2_shiftr in * by lia.
411 lia.
412 + assert (2 ^ n = 0) by (apply Z.pow_neg_r; lia).
413 lia.
414 Qed.
415
416 Lemma shiftr_nonneg_le : forall a n, 0 <= a -> 0 <= n -> a >> n <= a.
417 Proof.
418 intros.
419 repeat match goal with
420 | [ H : _ <= _ |- _ ]
421 => rewrite Z.lt_eq_cases in H
422 | [ H : _ \/ _ |- _ ] => destruct H
423 | _ => progress subst
424 | _ => progress autorewrite with zsimplify Zshift_to_pow
425 | _ => solve [ auto with zarith lia ]
426 end.
427 Qed.
428 #[global]
429 Hint Resolve shiftr_nonneg_le : zarith.
430
431 Lemma lor_shift_land_ones x n
432 : 0 <= n -> x = Z.lor ((x >> n) << n) (Z.land x (Z.ones n)).
433 Proof.
434 intro Hn.
435 apply Z.bits_inj; intro n'.
436 autorewrite with Ztestbit Ztestbit_full.
437 break_innermost_match; Z.ltb_to_lt;
438 repeat rewrite ?Bool.andb_false_l, ?Bool.andb_false_r, ?Bool.orb_false_l, ?Bool.orb_false_r, ?Bool.andb_true_l, ?Bool.andb_true_r, ?Z.sub_add, ?Bool.orb_diag;
439 autorewrite with Ztestbit;
440 try lia; reflexivity.
441 Qed.
442
443 Lemma add_shift_land_ones x n
444 : 0 <= n -> x = ((x >> n) << n) + (Z.land x (Z.ones n)).
445 Proof.
446 pose proof (Z.ones_lt_pow2 n n).
447 pose proof (Z.ones_nonneg n).
448 intro Hn; etransitivity; [ eapply lor_shift_land_ones; eassumption | ].
449 apply lor_shiftl'; [ assumption | ].
450 rewrite Z.land_nonneg, Z.lt_le_pred.
451 split.
452 { right; now apply Z.ones_nonneg. }
453 { rewrite Z.land_comm; etransitivity; [ apply Z.land_le | ]; lia. }
454 Qed.
455 End Z.