| 1 |
From Coq Require Import Bool. |
| 2 |
From Coq Require Import ZArith. |
| 3 |
From Coq Require Import Lia. |
| 4 |
|
| 5 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 6 |
Require Import Crypto.Util.ZUtil.Pow. |
| 7 |
Require Import Crypto.Util.ZUtil.Testbit. |
| 8 |
Require Import Crypto.Util.ZUtil.Modulo. |
| 9 |
Require Import Crypto.Util.ZUtil.Lor. |
| 10 |
Require Import Crypto.Util.ZUtil.Notations. |
| 11 |
|
| 12 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush. |
| 13 |
Require Import Crypto.Util.ZUtil.Tactics.SolveTestbit. |
| 14 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 15 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 16 |
|
| 17 |
Require Import Crypto.Util.Decidable. |
| 18 |
|
| 19 |
Import Notations. |
| 20 |
|
| 21 |
Local Open Scope Z_scope. |
| 22 |
|
| 23 |
Module Z. |
| 24 |
Local Ltac destruct_ltb a b := |
| 25 |
let E := fresh in |
| 26 |
destruct (a <? b) eqn:E; [rewrite Z.ltb_lt in E|rewrite Z.ltb_ge in E]. |
| 27 |
Local Ltac destruct_leb a b := |
| 28 |
let E := fresh in |
| 29 |
destruct (a <=? b) eqn:E; [rewrite Z.leb_le in E|rewrite Z.leb_gt in E]. |
| 30 |
Local Ltac destruct_eqb a b := |
| 31 |
let E := fresh in |
| 32 |
destruct (a =? b) eqn:E; [rewrite Z.eqb_eq in E|rewrite Z.eqb_neq in E]. |
| 33 |
|
| 34 |
Lemma twos_complement_neg_equiv m a |
| 35 |
(Hm : 0 < m) : |
| 36 |
Z.twos_complement m a <? 0 = negb (a mod 2 ^ m <? 2 ^ (m - 1)). |
| 37 |
Proof. |
| 38 |
assert (0 < 2 ^ m) by (apply Z.pow_pos_nonneg; lia). |
| 39 |
assert (0 <= a mod 2 ^ m < 2 ^ m) by (apply Z.mod_pos_bound; lia). |
| 40 |
unfold Z.twos_complement. Z.ltb_to_lt; break_match; try discriminate; lia. Qed. |
| 41 |
|
| 42 |
Lemma twos_complement_cond_equiv m a |
| 43 |
(Hm : 0 < m) : |
| 44 |
(a mod 2 ^ m) <? 2 ^ (m - 1) = negb (Z.testbit a (m - 1)). |
| 45 |
Proof. |
| 46 |
pose proof Z.mod_pos_bound a (2 ^ m) ltac:(apply Z.pow_pos_nonneg;lia). |
| 47 |
destruct_ltb (a mod 2 ^ m) (2 ^ (m - 1)). |
| 48 |
rewrite <- (Z.mod_pow2_bits_low _ m) by lia. |
| 49 |
destruct (dec (a mod 2 ^ m = 0)) as [amod0|]; rewrite ?amod0, ?Z.bits_0, ?Z.bits_above_log2; try reflexivity. |
| 50 |
apply Z.mod_pos_bound; lia. apply Z.log2_lt_pow2; lia. |
| 51 |
rewrite <- (Z.mod_pow2_bits_low _ m), Z.testbit_large by lia; reflexivity. Qed. |
| 52 |
|
| 53 |
Lemma twos_complement_spec m a b |
| 54 |
(Hm : 0 < m) : |
| 55 |
Z.twos_complement m a = b <-> a mod 2 ^ m = b mod 2 ^ m /\ - 2 ^ (m - 1) <= b < 2 ^ (m - 1). |
| 56 |
Proof. |
| 57 |
unfold Z.twos_complement. |
| 58 |
assert (0 < 2 ^ m) by (apply Z.pow_pos_nonneg; lia). |
| 59 |
assert (0 <= a mod 2 ^ m < 2 ^ m) by (apply Z.mod_pos_bound; lia). |
| 60 |
assert (2 ^ (m - 1) <= 2 ^ m) by (apply Z.pow_le_mono_r; lia). |
| 61 |
assert (2 * 2 ^ (m - 1) = 2 ^ m) by (rewrite Z.pow_mul_base by lia; apply f_equal2; lia). |
| 62 |
split; destruct_ltb (a mod 2 ^ m) (2 ^ (m - 1)); intros H4; subst; try split; |
| 63 |
repeat (pull_Zmod; push_Zmod; rewrite ?Z.mod_0_l, ?Z.sub_0_r); try lia. |
| 64 |
- destruct H4. rewrite H4 in *. rewrite Z.mod_small. lia. |
| 65 |
destruct (dec (b < 0)); [|lia]. pose proof Z.mod_neg_small b (2 ^ m) ltac:(nia). |
| 66 |
rewrite Z.mod_small in H6; lia. |
| 67 |
- destruct H4. rewrite H4 in *. rewrite Z.mod_neg_small. lia. |
| 68 |
destruct (dec (0 <= b)); [|lia]. rewrite Z.mod_small in H3; lia. Qed. |
| 69 |
|
| 70 |
Lemma twos_complement_testbit_spec_full m a i |
| 71 |
(Hm : 0 < m) : |
| 72 |
Z.testbit (Z.twos_complement m a) i = if (i <? m) then Z.testbit a i else Z.testbit a (m - 1). |
| 73 |
Proof. unfold Z.twos_complement. |
| 74 |
unfold Z.sub at 2. |
| 75 |
rewrite <- Z.lor_add by Z.solve_using_testbit. |
| 76 |
rewrite Z.twos_complement_cond_equiv by assumption. |
| 77 |
destruct (Z.testbit a (m - 1)); Z.solve_testbit. Qed. |
| 78 |
|
| 79 |
#[global] |
| 80 |
Hint Rewrite twos_complement_testbit_spec_full : testbit_pos_rewrite. |
| 81 |
#[global] |
| 82 |
Hint Rewrite twos_complement_testbit_spec_full : testbit_rewrite. |
| 83 |
|
| 84 |
Lemma twos_complement_one m (Hm : 1 < m) : |
| 85 |
Z.twos_complement m 1 = 1. |
| 86 |
Proof. Z.solve_using_testbit. Qed. |
| 87 |
|
| 88 |
Lemma twos_complement_zero m (Hm : 0 < m): |
| 89 |
Z.twos_complement m 0 = 0. |
| 90 |
Proof. Z.solve_using_testbit. Qed. |
| 91 |
|
| 92 |
Lemma twos_complement_mod a m (Hm : 0 < m) : |
| 93 |
Z.twos_complement m (a mod 2 ^ m) = Z.twos_complement m a. |
| 94 |
Proof. unfold Z.twos_complement; rewrite Z.mod_mod; try apply Z.pow_nonzero; lia. Qed. |
| 95 |
|
| 96 |
Lemma twos_complement_mod' a m (Hm : 0 < m) : |
| 97 |
Z.twos_complement m a mod 2 ^ m = a mod 2 ^ m. |
| 98 |
Proof. unfold Z.twos_complement. |
| 99 |
destruct_ltb (a mod 2 ^ m) (2 ^ (m - 1)); repeat (pull_Zmod; push_Zmod; rewrite Z.mod_0_l, Z.sub_0_r); |
| 100 |
try apply Z.mod_mod; apply Z.pow_nonzero; lia. Qed. |
| 101 |
|
| 102 |
Lemma twos_complement_odd m a (Hm : 0 < m) : Z.odd (Z.twos_complement m a) = Z.odd a. |
| 103 |
Proof. rewrite <- !Z.bit0_odd; Z.solve_testbit. Qed. |
| 104 |
|
| 105 |
Lemma twos_complement_mod2 m a (Hm : 0 < m) : Z.twos_complement m a mod 2 = a mod 2. |
| 106 |
Proof. rewrite Zmod_odd, twos_complement_odd, <- Zmod_odd by lia. reflexivity. Qed. |
| 107 |
|
| 108 |
Lemma twos_complement_bounds a m (Hm : 0 < m) : |
| 109 |
- 2 ^ (m - 1) <= Z.twos_complement m a < 2 ^ (m - 1). |
| 110 |
Proof. |
| 111 |
unfold Z.twos_complement. |
| 112 |
assert (0 < 2 ^ m) by (apply Z.pow_pos_nonneg; lia). |
| 113 |
assert (2 ^ m = 2 * 2 ^ (m - 1)) by (rewrite Z.pow_mul_base by lia; apply f_equal2; lia). |
| 114 |
pose proof (Z.mod_pos_bound a (2 ^ m) H); destruct_ltb (a mod 2 ^ m) (2 ^ (m - 1)); lia. Qed. |
| 115 |
|
| 116 |
#[global] |
| 117 |
Hint Resolve twos_complement_bounds : zarith. |
| 118 |
|
| 119 |
Lemma twos_complement_add_full a b m |
| 120 |
(Hm : 0 < m) |
| 121 |
(Hsum : - 2 ^ (m - 1) <= Z.twos_complement m a + Z.twos_complement m b < 2 ^ (m - 1)) : |
| 122 |
Z.twos_complement m (a + b) = Z.twos_complement m a + Z.twos_complement m b. |
| 123 |
Proof. |
| 124 |
apply twos_complement_spec; try split; try lia; unfold Z.twos_complement. |
| 125 |
destruct_ltb (a mod 2 ^ m) (2 ^ (m - 1)); |
| 126 |
destruct_ltb (b mod 2 ^ m) (2 ^ (m - 1)); repeat (push_Zmod; pull_Zmod; rewrite ?Z.sub_0_r); reflexivity. Qed. |
| 127 |
|
| 128 |
Lemma twos_complement_add_weak a b m |
| 129 |
(Hm : 0 < m) |
| 130 |
(Ha : - 2 ^ (m - 2) <= Z.twos_complement m a < 2 ^ (m - 2)) |
| 131 |
(Hb : - 2 ^ (m - 2) <= Z.twos_complement m b < 2 ^ (m - 2)) : |
| 132 |
Z.twos_complement m (a + b) = Z.twos_complement m a + Z.twos_complement m b. |
| 133 |
Proof. destruct (dec (m = 1)); [subst; rewrite Z.pow_neg_r in *; lia|]. |
| 134 |
assert (2 * 2 ^ (m - 2) = 2 ^ (m - 1)) by (rewrite Z.pow_mul_base by lia; apply f_equal2; lia). |
| 135 |
rewrite twos_complement_add_full; lia. Qed. |
| 136 |
|
| 137 |
Lemma twos_complement_mul_full m a b |
| 138 |
(Hm : 0 < m) |
| 139 |
(Hprod : - 2 ^ (m - 1) <= Z.twos_complement m a * Z.twos_complement m b < 2 ^ (m - 1)) : |
| 140 |
Z.twos_complement m (a * b) = Z.twos_complement m a * Z.twos_complement m b. |
| 141 |
Proof. |
| 142 |
apply twos_complement_spec; try split; try lia; unfold Z.twos_complement. |
| 143 |
destruct_ltb (a mod 2 ^ m) (2 ^ (m - 1)); |
| 144 |
destruct_ltb (b mod 2 ^ m) (2 ^ (m - 1)); repeat (push_Zmod; pull_Zmod; rewrite ?Z.sub_0_r); reflexivity. Qed. |
| 145 |
End Z. |