Debian Codegraph @

src/Util/ZUtil/Testbit.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import ZArith.
3 Require Import Crypto.Util.ZUtil.Definitions.
4 Require Import Crypto.Util.ZUtil.Hints.
5 Require Import Crypto.Util.ZUtil.Notations.
6 Require Import Crypto.Util.ZUtil.Lnot.
7 Require Import Crypto.Util.ZUtil.Div.
8 Require Import Crypto.Util.ZUtil.Hints.Core.
9 Require Import Crypto.Util.ZUtil.Hints.Ztestbit.
10 Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds.
11 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
12 Require Import Crypto.Util.Tactics.BreakMatch.
13 Require Import Crypto.Util.Tactics.SplitInContext.
14 Require Import Crypto.Util.Tactics.DestructHead.
15 Require Import Crypto.Util.ZUtil.Log2.
16 Require Import Crypto.Util.ZUtil.Pow.
17 Require Import Crypto.Util.Bool.LeCompat.
18 Local Open Scope Z_scope.
19
20 Module Z.
21 Lemma ones_spec : forall n m, 0 <= n -> 0 <= m -> Z.testbit (Z.ones n) m = if Z_lt_dec m n then true else false.
22 Proof using Type.
23 intros.
24 break_match.
25 + apply Z.ones_spec_low. lia.
26 + apply Z.ones_spec_high. lia.
27 Qed.
28 #[global]
29 Hint Rewrite ones_spec using zutil_arith : Ztestbit.
30
31 Lemma ones_spec' n m (Hn : 0 <= n) (Hm : 0 <= m) :
32 Z.testbit (Z.ones n) m = if (m <? n) then true else false.
33 Proof using Type. rewrite ones_spec by assumption.
34 destruct (Z_lt_dec m n) as [lt|lt];
35 [apply Z.ltb_lt in lt|apply Z.ltb_nlt in lt];
36 rewrite lt; reflexivity. Qed.
37
38 Lemma ones_spec_full : forall n m, Z.testbit (Z.ones n) m
39 = if Z_lt_dec m 0
40 then false
41 else if Z_lt_dec n 0
42 then true
43 else if Z_lt_dec m n then true else false.
44 Proof using Type.
45 intros n m.
46 repeat (break_match || autorewrite with Ztestbit); try reflexivity; try lia.
47 unfold Z.ones.
48 rewrite <- Z.shiftr_opp_r, Z.shiftr_eq_0 by (simpl; lia); simpl.
49 destruct m; simpl in *; try reflexivity.
50 exfalso; auto using Zlt_neg_0.
51 Qed.
52 #[global]
53 Hint Rewrite ones_spec_full : Ztestbit_full.
54
55 Lemma testbit_pow2_mod : forall a n i, 0 <= n ->
56 Z.testbit (Z.pow2_mod a n) i = if Z_lt_dec i n then Z.testbit a i else false.
57 Proof using Type.
58 cbv [Z.pow2_mod]; intros a n i H; destruct (Z_le_dec 0 i);
59 repeat match goal with
60 | |- _ => rewrite Z.testbit_neg_r by lia
61 | |- _ => break_innermost_match_step
62 | |- _ => lia
63 | |- _ => reflexivity
64 | |- _ => progress autorewrite with Ztestbit
65 end.
66 Qed.
67 #[global]
68 Hint Rewrite testbit_pow2_mod using zutil_arith : Ztestbit.
69
70 Lemma testbit_pow2_mod_full : forall a n i,
71 Z.testbit (Z.pow2_mod a n) i = if Z_lt_dec n 0
72 then if Z_lt_dec i 0 then false else Z.testbit a i
73 else if Z_lt_dec i n then Z.testbit a i else false.
74 Proof using Type.
75 intros a n i; destruct (Z_lt_dec n 0); [ | apply testbit_pow2_mod; lia ].
76 unfold Z.pow2_mod.
77 autorewrite with Ztestbit_full;
78 repeat break_match;
79 autorewrite with Ztestbit;
80 reflexivity.
81 Qed.
82 #[global]
83 Hint Rewrite testbit_pow2_mod_full : Ztestbit_full.
84
85 Lemma bits_above_pow2 a n : 0 <= a < 2^n -> Z.testbit a n = false.
86 Proof using Type.
87 intros.
88 destruct (Z_zerop a); subst; autorewrite with Ztestbit; trivial.
89 apply Z.bits_above_log2; auto with zarith concl_log2.
90 Qed.
91 #[global]
92 Hint Rewrite bits_above_pow2 using zutil_arith : Ztestbit.
93
94 Lemma testbit_low : forall n x i, (0 <= i < n) ->
95 Z.testbit x i = Z.testbit (Z.land x (Z.ones n)) i.
96 Proof using Type.
97 intros.
98 rewrite Z.land_ones by lia.
99 symmetry.
100 apply Z.mod_pow2_bits_low.
101 lia.
102 Qed.
103
104 Lemma testbit_add_shiftl_low : forall i, (0 <= i) -> forall a b n, (i < n) ->
105 Z.testbit (a + Z.shiftl b n) i = Z.testbit a i.
106 Proof using Type.
107 intros i H a b n H0.
108 erewrite Z.testbit_low; eauto.
109 rewrite Z.land_ones, Z.shiftl_mul_pow2 by lia.
110 rewrite Z.mod_add by (pose proof (Z.pow_pos_nonneg 2 n); lia).
111 auto using Z.mod_pow2_bits_low.
112 Qed.
113 #[global]
114 Hint Rewrite testbit_add_shiftl_low using zutil_arith : Ztestbit.
115
116 Lemma testbit_sub_pow2 n i x (i_range:0 <= i < n) (x_range:0 < x < 2 ^ n) :
117 Z.testbit (2 ^ n - x) i = negb (Z.testbit (x - 1) i).
118 Proof using Type.
119 rewrite <-Z.lnot_spec, Z.lnot_sub1 by lia.
120 rewrite <-(Z.mod_pow2_bits_low (-x) _ _ (proj2 i_range)).
121 f_equal.
122 rewrite Z.mod_opp_l_nz; autorewrite with zsimplify; lia.
123 Qed.
124
125 Lemma testbit_false_bound : forall a x, 0 <= x ->
126 (forall n, ~ (n < x) -> Z.testbit a n = false) ->
127 a < 2 ^ x.
128 Proof using Type.
129 intros a x H H0.
130 assert (H1 : a = Z.pow2_mod a x). {
131 apply Z.bits_inj'; intros.
132 rewrite Z.testbit_pow2_mod by lia; break_match; auto.
133 }
134 rewrite H1.
135 cbv [Z.pow2_mod]; rewrite Z.land_ones by auto.
136 try apply Z.mod_pos_bound; Z.zero_bounds.
137 Qed.
138
139 Lemma testbit_neg_eq_if x n :
140 0 <= n ->
141 - (2 ^ n) <= x < 2 ^ n ->
142 Z.b2z (if x <? 0 then true else Z.testbit x n) = - (x / 2 ^ n) mod 2.
143 Proof using Type.
144 intros. break_match; Z.ltb_to_lt.
145 { autorewrite with zsimplify. reflexivity. }
146 { autorewrite with zsimplify.
147 rewrite Z.bits_above_pow2 by lia.
148 reflexivity. }
149 Qed.
150
151 Lemma shiftl_spec_full a n m : Z.testbit (a << n) m = if (0 <=? m) then Z.testbit a (m - n) else false.
152 Proof using Type.
153 break_innermost_match; Z.ltb_to_lt; now autorewrite with Ztestbit.
154 Qed.
155 #[global]
156 Hint Rewrite shiftl_spec_full : Ztestbit_full.
157
158 Lemma shiftr_spec_full a n m : Z.testbit (a >> n) m = if (0 <=? m) then Z.testbit a (m + n) else false.
159 Proof using Type.
160 break_innermost_match; Z.ltb_to_lt; now autorewrite with Ztestbit.
161 Qed.
162 #[global]
163 Hint Rewrite shiftr_spec_full : Ztestbit_full.
164
165 Lemma mod_pow2_ones a m :
166 a mod 2 ^ m = if (Z_lt_dec m 0) then ltac:(match eval hnf in (1 mod 0) with | 0 => exact 0 | _ => exact a end) else a &' Z.ones m.
167 Proof using Type. destruct (Z_lt_dec m 0). rewrite Z.pow_neg_r, Zmod_0_r; lia.
168 symmetry; apply Z.land_ones; lia. Qed.
169
170 Lemma bits_1 m :
171 Z.testbit 1 m = if Z.eq_dec m 0 then true else false.
172 Proof using Type.
173 destruct (Z.eq_dec m 0); subst. reflexivity.
174 destruct (Z_lt_dec m 0). rewrite Z.testbit_neg_r by lia. reflexivity.
175 rewrite Z.bits_above_log2; simpl; try reflexivity; lia. Qed.
176
177 Lemma bits_opp_full a i :
178 Z.testbit (- a) i = if (Z_lt_dec i 0) then false else negb (Z.testbit (Z.pred a) i).
179 Proof using Type.
180 destruct (Z_lt_dec i 0). rewrite Z.testbit_neg_r by lia. reflexivity.
181 apply Z.bits_opp; lia. Qed.
182
183 Lemma pow2_bits_full m i :
184 Z.testbit (2 ^ m) i =
185 if (Z_lt_dec m 0) then false else if (Z.eq_dec i m) then true else false.
186 Proof using Type.
187 destruct (Z_lt_dec m 0); [now rewrite Z.pow_neg_r, Z.bits_0|].
188 destruct (Z.eq_dec i m); subst.
189 - apply Z.pow2_bits_true; lia.
190 - apply Z.pow2_bits_false; lia. Qed.
191
192
193 Definition bit_compare (b1 b2 : bool) : comparison
194 := match b1, b2 with
195 | true, true => Eq
196 | true, false => Lt
197 | false, false => Eq
198 | false, true => Gt
199 end.
200
201 Lemma bit_compare_refl (b : bool) : bit_compare b b = Eq.
202 Proof using Type. now destruct b. Qed.
203 #[global]
204 Hint Rewrite bit_compare_refl : Ztestbit.
205
206 Lemma bit_compare_eq_iff (b1 b2 : bool)
207 : bit_compare b1 b2 = Eq <-> b1 = b2.
208 Proof using Type. now destruct b1, b2. Qed.
209
210 Lemma bit_compare_gt_iff (b1 b2 : bool)
211 : bit_compare b1 b2 = Gt <-> (b1 = false /\ b2 = true).
212 Proof using Type. now destruct b1, b2. Qed.
213
214 Lemma bit_compare_lt_iff (b1 b2 : bool)
215 : bit_compare b1 b2 = Lt <-> (b1 = true /\ b2 = false).
216 Proof using Type. now destruct b1, b2. Qed.
217
218 Lemma bits_const_iff z b
219 : (forall n, 0 <= n -> Z.testbit z n = b)
220 <-> z = if b then -1 else 0.
221 Proof using Type.
222 destruct b; [ rewrite <- (Z.bits_inj_iff z (-(1))) | rewrite <- (Z.bits_inj_iff z 0) ];
223 cbv [Z.eqf];
224 split; intros H n; specialize (H n); intros; destruct (Z_le_gt_dec 0 n);
225 try specialize (H ltac:(assumption)); try lia;
226 revert H;
227 rewrite ?Z.bits_0, ?Z.bits_m1, ?Z.testbit_neg_r by lia; trivial.
228 Qed.
229
230 Lemma compare_by_bits_impl z1 z2 c
231 : (forall n, 0 <= n -> bit_compare (Z.testbit z1 n) (Z.testbit z2 n) = c)
232 -> Z.compare z1 z2 = c.
233 Proof using Type.
234 destruct (Z.compare_spec z1 z2), c; subst.
235 all: repeat first [ progress setoid_rewrite bit_compare_refl
236 | progress setoid_rewrite bit_compare_eq_iff
237 | progress setoid_rewrite bit_compare_gt_iff
238 | progress setoid_rewrite bit_compare_lt_iff
239 | reflexivity
240 | assumption
241 | congruence
242 | lia
243 | progress subst
244 | progress intros
245 | progress split_and
246 | match goal with
247 | [ H : forall n, 0 <= n -> ?T |- _ ] => assert T by (eapply H; reflexivity); clear H
248 | [ H : forall n, 0 <= n -> Z.testbit _ _ = Z.testbit _ _ |- _ ]
249 => apply Z.bits_inj' in H
250 | [ H : forall n, 0 <= n -> Z.testbit _ n = ?b |- _ ]
251 => apply bits_const_iff in H
252 end ].
253 Qed.
254
255 Lemma testbit_small_neg a b
256 (Ha : - 2^b <= a < 0)
257 (Hb : 0 < b) :
258 Z.testbit a b = true.
259 Proof using Type.
260 destruct Ha; apply Z_le_lt_eq_dec in H; destruct H.
261 - apply Z.bits_iff_neg with (n:=b) in H0; [assumption|].
262 apply Log2.Z.log2_lt_pow2_alt; try lia.
263 - subst; replace (- 2 ^ b) with ((-1) * 2 ^ b) by ring.
264 rewrite Z.mul_pow2_bits, Z.sub_diag, Z.bit0_odd by lia; reflexivity. Qed.
265
266 Lemma testbit_large a b
267 (Ha : 2 ^ (b - 1) <= a < 2 ^ b)
268 (Hb : 0 < b) :
269 Z.testbit a (b - 1) = true.
270 Proof using Type.
271 destruct (Z.testbit a (b - 1)) eqn:E; try reflexivity.
272 rewrite Z.testbit_false in * by lia.
273 apply Z.div_exact in E; [|lia].
274 rewrite Div.Z.div_between_1 in E; auto with zarith.
275 rewrite Pow.Z.pow_mul_base, Z.sub_simpl_r; lia. Qed.
276
277 Lemma testbit_b2z a m :
278 Z.testbit a m = negb (Z.b2z (Z.testbit a m) =? 0).
279 Proof using Type. destruct (Z.testbit a m); reflexivity. Qed.
280
281 Lemma pos_le_bitwise a b (H : forall i, Bool.le (Pos.testbit a i) (Pos.testbit b i))
282 : Pos.le a b.
283 Proof using Type.
284 revert dependent a; induction b as [b IHb|b IHb|], a as [a|a|]; intros; try lia;
285 try solve [ change (a <= b)%positive;
286 apply IHb;
287 intro i; specialize (H (N.succ i)); destruct i; cbn in H; rewrite ?Pos.pred_N_succ in H; try assumption ].
288 all: first [ cbv [Pos.le]; cbn; rewrite Pos.compare_cont_Lt_not_Gt
289 | cbv [Pos.le]; cbn; rewrite Pos.compare_cont_Gt_not_Gt
290 | idtac ].
291 all: try solve [ apply IHb;
292 intro i; specialize (H (N.succ i)); destruct i; cbn in H; rewrite ?Pos.pred_N_succ in H; try assumption
293 | specialize (H 0%N); cbn in H; congruence ].
294 all: specialize (fun i => H (Npos i)).
295 all: lazymatch type of H with
296 | forall i, Bool.le (Pos.testbit ?a _) (Pos.testbit ?b _)
297 => change (forall i, Bool.le (Z.testbit (Zpos a) (Zpos i)) (Z.testbit (Zpos b) (Zpos i))) in H;
298 specialize (H (Z.to_pos (Z.log2 (Zpos a))))
299 end.
300 all: rewrite Z2Pos.id in H by (apply Z.log2_pos; lia).
301 all: rewrite Z.bit_log2 in H by lia.
302 all: rewrite Z.bits_above_log2 in H; try apply Z.log2_pos; try lia.
303 all: cbn in H; try congruence.
304 Qed.
305
306 Lemma le_bitwise a b (Ha : 0 <= a) (Hb : 0 <= b) (H : forall i, 0 <= i -> Bool.le (Z.testbit a i) (Z.testbit b i))
307 : Z.le a b.
308 Proof using Type.
309 destruct a as [|a|a], b as [|b|b]; try lia.
310 { specialize (H (Z.log2 (Zpos a)) (Z.log2_nonneg (Zpos a))).
311 rewrite Z.testbit_0_l, Z.bit_log2 in H by lia.
312 cbn in H; congruence. }
313 { apply pos_le_bitwise.
314 intro i; destruct i.
315 { specialize (H 0 ltac:(lia)); cbn in H.
316 destruct a, b; cbn in *; try reflexivity; try congruence. }
317 { refine (H (Zpos _) _); lia. } }
318 Qed.
319 End Z.