Debian Codegraph @

src/Util/ZUtil/TruncatingShiftl.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 Require Import Crypto.Util.ZUtil.Definitions.
4 Require Import Crypto.Util.ZUtil.Notations.
5 Require Import Crypto.Util.ZUtil.Pow.
6 Require Import Crypto.Util.ZUtil.Modulo.
7 Require Import Crypto.Util.ZUtil.Tactics.SolveTestbit.
8 Local Open Scope Z_scope.
9
10 Module Z.
11 Lemma truncating_shiftl_correct bw x y
12 : Z.truncating_shiftl bw x y = (x << y) mod 2^bw.
13 Proof. reflexivity. Qed.
14
15 Lemma truncating_shiftl_correct_land_ones bw x y
16 : Z.truncating_shiftl bw x y = (x << y) &'
17 Z.ones ltac:(match eval hnf in (1 mod 0) with | 0 => exact (Z.max 0 bw) | _ => exact bw end).
18 Proof.
19 rewrite truncating_shiftl_correct.
20 destruct (Z_lt_le_dec bw 0).
21 - assert (Hbw : Z.max 0 bw = 0) by lia.
22 assert (H0 : Z.pred (2 ^ 0) = 0) by reflexivity.
23 now rewrite ?Hbw, Z.ones_equiv, ?H0, ?(Z.base_pow_neg 2 bw), ?Z.land_0_r, ?Z.land_m1_r, ?Zmod_0_r.
24 - assert (Hbw : Z.max 0 bw = bw) by lia.
25 rewrite ?Hbw. now rewrite Z.land_ones.
26 Qed.
27
28 Lemma truncating_shiftl_correct_land_pow2 bw x y
29 : Z.truncating_shiftl bw x y = (x << y) &' (2^(ltac:(match eval hnf in (1 mod 0) with | 0 => exact (Z.max 0 bw) | _ => exact bw end)) - 1).
30 Proof.
31 rewrite truncating_shiftl_correct_land_ones, Z.ones_equiv, <- Z.sub_1_r.
32 reflexivity.
33 Qed.
34
35 Lemma truncating_shiftl_mod a b (Hb : 0 < b):
36 Z.truncating_shiftl b a (b - 1) = 2 ^ (b - 1) * (a mod 2).
37 Proof.
38 unfold Definitions.Z.truncating_shiftl. rewrite Z.shiftl_mul_pow2 by lia.
39 replace (2^b) with (2 * 2^(b-1)) by (rewrite Pow.Z.pow_mul_base, Z.sub_simpl_r; lia).
40 rewrite Zmult_mod_distr_r; ring. Qed.
41
42 Lemma truncating_shiftl_testbit_spec_full m a k i :
43 Z.testbit (Z.truncating_shiftl m a k) i
44 = if (i <? 0) then false else if (orb (i <? m) ((1 mod 0 =? 1) && (m <? 0))) then Z.testbit a (i - k) else false.
45 Proof. unfold Z.truncating_shiftl. Z.solve_testbit. Qed.
46
47 #[global]
48 Hint Rewrite truncating_shiftl_testbit_spec_full : testbit_rewrite.
49
50 Lemma truncating_shiftl_truncating_shiftl m a p q
51 (Hp : 0 <= p)
52 (Hq : 0 <= q) :
53 Z.truncating_shiftl m (Z.truncating_shiftl m a p) q
54 = Z.truncating_shiftl m a (p + q).
55 Proof. Z.solve_using_testbit. Qed.
56 Lemma truncating_shiftl0 m k :
57 Z.truncating_shiftl m 0 k = 0.
58 Proof. Z.solve_using_testbit. Qed.
59
60 Lemma truncating_shiftl_lor m a b k :
61 Z.truncating_shiftl m (a |' b) k
62 = (Z.truncating_shiftl m a k) |' (Z.truncating_shiftl m b k).
63 Proof. Z.solve_using_testbit. Qed.
64
65 (* (* not compatible with x mod 0 = x *)
66 Lemma truncating_shiftl_large m a k (Hk : k >= m) :
67 Z.truncating_shiftl m a k = 0.
68 Proof. Z.solve_using_testbit. Qed. *)
69
70 Lemma truncating_shiftl_range m a k (Hm : 0 <= m) :
71 0 <= Z.truncating_shiftl m a k < 2 ^ m.
72 Proof. unfold Z.truncating_shiftl; apply Z.mod_pos_bound; apply Z.pow_pos_nonneg; lia. Qed.
73
74 #[global]
75 Hint Resolve truncating_shiftl_range : zarith.
76
77 Lemma truncating_shiftl_nonneg m a k (Hm : 0 <= m) :
78 0 <= Z.truncating_shiftl m a k.
79 Proof. unfold Z.truncating_shiftl; apply Z.mod_pos_bound; apply Z.pow_pos_nonneg; lia. Qed.
80 End Z.