| 1 |
From Coq Require Import Lia. |
| 2 |
From Coq Require Import Arith. |
| 3 |
From Coq Require Import ZArith. |
| 4 |
From Coq Require Import List. |
| 5 |
From Coq Require Import Bool. |
| 6 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 7 |
Require Import Crypto.Util.ZUtil.Stabilization. |
| 8 |
Require Import Crypto.Util.ListUtil. |
| 9 |
Require Import Crypto.Util.Option. |
| 10 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 11 |
Import ListNotations. |
| 12 |
Local Open Scope list_scope. |
| 13 |
Local Open Scope bool_scope. |
| 14 |
Local Open Scope Z_scope. |
| 15 |
|
| 16 |
Create HintDb bitlist discriminated. |
| 17 |
|
| 18 |
Module Import Bitlist. |
| 19 |
Local Set Primitive Projections. |
| 20 |
Record t := { internal_bitlist : list bool ; test_signbit : bool }. |
| 21 |
Definition signbit (v : t) : Z := if test_signbit v then -1 else 0. |
| 22 |
Definition sgn (v : t) : Z |
| 23 |
:= if test_signbit v then -1 else if List.existsb (fun b => b) (internal_bitlist v) then 1 else 0. |
| 24 |
Definition testbit (v : t) (n : Z) : bool |
| 25 |
:= if n <? 0 |
| 26 |
then false |
| 27 |
else List.nth_default (Z.testbit (signbit v) n) (internal_bitlist v) (Z.to_nat n). |
| 28 |
Definition bitcount (v : t) : nat := List.length (internal_bitlist v). |
| 29 |
#[global] |
| 30 |
Hint Transparent bitcount : rewrite bitlist. |
| 31 |
Definition bitlist (v : t) (len : nat) : list bool |
| 32 |
:= firstn len (internal_bitlist v) ++ repeat (Z.testbit (signbit v) 0) (len - bitcount v). |
| 33 |
Notation bitlist_default v := (bitlist v (bitcount v)) (only parsing). |
| 34 |
Definition to_Z (v : t) : Z |
| 35 |
:= List.fold_right |
| 36 |
(fun '(n, b) a => (if b : bool then Z.setbit else Z.clearbit) a (Z.of_nat n)) |
| 37 |
(signbit v) |
| 38 |
(List.enumerate (bitlist_default v)). |
| 39 |
Definition of_Z_len (v : Z) (len : Z) : t |
| 40 |
:= Build_t |
| 41 |
(List.map (fun n => Z.testbit v (Z.of_nat n)) (seq 0 (Z.to_nat len))) |
| 42 |
(v <? 0). |
| 43 |
Definition of_Z (v : Z) : t := of_Z_len v (Z.stabilization_time v + 1). |
| 44 |
|
| 45 |
Lemma length_bitlist v len : length (bitlist v len) = len. |
| 46 |
Proof. |
| 47 |
cbv [bitlist bitcount]; autorewrite with distr_length. |
| 48 |
apply Nat.min_case_strong; try lia. |
| 49 |
Qed. |
| 50 |
#[global] |
| 51 |
Hint Rewrite length_bitlist : distr_length bitlist. |
| 52 |
|
| 53 |
Lemma bitcount_of_Z_len v len : bitcount (of_Z_len v len) = Z.to_nat len. |
| 54 |
Proof. cbv [bitcount of_Z_len internal_bitlist]; now autorewrite with distr_length. Qed. |
| 55 |
#[global] |
| 56 |
Hint Rewrite bitcount_of_Z_len : distr_length bitlist. |
| 57 |
|
| 58 |
Lemma bitcount_of_Z v : bitcount (of_Z v) = Z.to_nat (Z.stabilization_time v + 1). |
| 59 |
Proof. apply bitcount_of_Z_len. Qed. |
| 60 |
#[global] |
| 61 |
Hint Rewrite bitcount_of_Z : distr_length bitlist. |
| 62 |
|
| 63 |
Lemma enumerate_bitlist_default_eq v |
| 64 |
: enumerate (bitlist_default v) = List.combine (List.seq 0 (bitcount v)) (internal_bitlist v). |
| 65 |
Proof. |
| 66 |
cbv [enumerate]; autorewrite with distr_length. |
| 67 |
cbv [bitlist bitcount]; autorewrite with natsimplify; cbn [repeat]. |
| 68 |
now rewrite List.app_nil_r, List.firstn_all. |
| 69 |
Qed. |
| 70 |
|
| 71 |
Lemma testbit_to_Z v n : Z.testbit (to_Z v) n = testbit v n. |
| 72 |
Proof. |
| 73 |
cbv [to_Z testbit]. |
| 74 |
destruct (n <? 0) eqn:?; Z.ltb_to_lt. |
| 75 |
{ now rewrite Z.testbit_neg_r by assumption. } |
| 76 |
rewrite enumerate_bitlist_default_eq. |
| 77 |
cbv [bitcount]. |
| 78 |
set (offset := 0%nat) at 1. |
| 79 |
change 0 with (Z.of_nat offset) in * |- . |
| 80 |
replace n with (n - Z.of_nat offset) at 3 by lia. |
| 81 |
clearbody offset; revert dependent offset. |
| 82 |
revert dependent n. |
| 83 |
induction (internal_bitlist v) as [|x xs IHxs]; |
| 84 |
cbn [length seq List.combine fold_right]; |
| 85 |
intros; [ now rewrite nth_default_nil | ]. |
| 86 |
specialize (fun n => IHxs n (S offset)). |
| 87 |
assert (H' : n = Z.of_nat offset \/ Z.of_nat offset < n) by lia. |
| 88 |
destruct H'; subst. |
| 89 |
{ break_innermost_match; |
| 90 |
first [ rewrite Z.setbit_eq by lia | rewrite Z.clearbit_eq by lia ]. |
| 91 |
all: autorewrite with zsimplify; reflexivity. } |
| 92 |
{ lazymatch goal with |
| 93 |
| [ |- Z.testbit (?f ?x ?y) ?n = _ ] => transitivity (Z.testbit x n) |
| 94 |
end. |
| 95 |
{ now break_innermost_match; |
| 96 |
first [ rewrite Z.setbit_neq by lia | rewrite Z.clearbit_neq by lia ]. } |
| 97 |
rewrite IHxs by lia. |
| 98 |
replace (Z.to_nat (n - Z.of_nat (S offset))) with (pred (Z.to_nat (n - Z.of_nat offset))) |
| 99 |
by (* could just use [lia], but only in Coq >= 8.11 *) |
| 100 |
(now rewrite Nat2Z.inj_succ, <- Z.add_1_r, Z.sub_add_distr, Z.sub_1_r, Z2Nat.inj_pred). |
| 101 |
destruct (Z.to_nat (n - Z.of_nat offset)) eqn:H'; [ exfalso | ]. |
| 102 |
{ (* could just use [lia], but only in Coq >= 8.11 *) |
| 103 |
apply (f_equal Z.of_nat) in H'; rewrite Z2Nat.id in H' by lia. |
| 104 |
lia. } |
| 105 |
rewrite nth_default_cons_S; reflexivity. } |
| 106 |
Qed. |
| 107 |
#[global] |
| 108 |
Hint Rewrite testbit_to_Z : bitlist Ztestbit. |
| 109 |
|
| 110 |
Lemma testbit_of_Z_len v n len |
| 111 |
: testbit (of_Z_len v len) n = if n <? len then Z.testbit v n else Z.testbit (if v <? 0 then -1 else 0) n. |
| 112 |
Proof. |
| 113 |
cbv [testbit of_Z_len signbit test_signbit internal_bitlist]. |
| 114 |
cbv [nth_default]. |
| 115 |
rewrite nth_error_map, nth_error_seq. |
| 116 |
break_innermost_match; Z.ltb_to_lt; try solve [ exfalso; lia ]. |
| 117 |
all: repeat first [ reflexivity |
| 118 |
| congruence |
| 119 |
| progress cbn [option_map Nat.add] in * |
| 120 |
| progress inversion_option |
| 121 |
| rewrite Z.testbit_neg_r by lia |
| 122 |
| progress rewrite Z2Nat.id in * by lia |
| 123 |
| fail (* the rest are only needed in Coq < 8.11 *) |
| 124 |
| rewrite Z.bits_m1 by lia |
| 125 |
| lia |
| 126 |
| match goal with |
| 127 |
| [ H : ?x < 0, H' : context[Z.to_nat ?x] |- _ ] |
| 128 |
=> replace (Z.to_nat x) with 0%nat in * by now destruct n |
| 129 |
| [ H : (_ < _)%nat |- _ ] |
| 130 |
=> apply inj_lt in H; rewrite ?Z2Nat.id in H by lia |
| 131 |
| [ H : context[Z.of_nat (Z.to_nat ?x)] |- _ ] |
| 132 |
=> let H' := fresh in |
| 133 |
assert (H' : x < 0 \/ 0 <= x) by lia; |
| 134 |
destruct H'; |
| 135 |
[ replace (Z.to_nat x) with 0%nat in * by now destruct x |
| 136 |
| rewrite Z2Nat.id in H by assumption ] |
| 137 |
end |
| 138 |
| progress zify ]. |
| 139 |
Qed. |
| 140 |
#[global] |
| 141 |
Hint Rewrite testbit_of_Z_len : bitlist Ztestbit. |
| 142 |
|
| 143 |
Lemma testbit_of_Z v n : testbit (of_Z v) n = Z.testbit v n. |
| 144 |
Proof. |
| 145 |
cbv [of_Z]; rewrite testbit_of_Z_len. |
| 146 |
destruct (stabilization_time v) as [b H]. |
| 147 |
pose proof (H n) as H'. |
| 148 |
break_innermost_match_step; Z.ltb_to_lt; [ reflexivity | ]. |
| 149 |
rewrite H' by lia. |
| 150 |
pose proof (Z.stabilization_time_nonneg v). |
| 151 |
break_innermost_match_step; Z.ltb_to_lt; |
| 152 |
[ destruct (proj1 (Z.bits_iff_neg_ex v) ltac:(lia)) as [k H''] |
| 153 |
| pose proof (proj2 (Z.bits_iff_neg_ex v)) as H'' ]. |
| 154 |
{ rewrite Z.bits_m1 by lia. |
| 155 |
specialize (H (Z.max (Z.stabilization_time v + 1) (k + 1)) ltac:(lia)). |
| 156 |
rewrite H'' in H by lia; congruence. } |
| 157 |
{ rewrite Z.testbit_0_l. |
| 158 |
destruct b; [ cut (v < 0); [ lia | ] | reflexivity ]. |
| 159 |
apply H''; eexists; eassumption. } |
| 160 |
Qed. |
| 161 |
#[global] |
| 162 |
Hint Rewrite testbit_of_Z : bitlist Ztestbit. |
| 163 |
|
| 164 |
Lemma path_t (v1 v2 : t) : internal_bitlist v1 = internal_bitlist v2 -> test_signbit v1 = test_signbit v2 -> v1 = v2. |
| 165 |
Proof. destruct v1, v2; cbn; intros; subst; reflexivity. Qed. |
| 166 |
|
| 167 |
Definition bitwise (f : bool -> bool -> bool) (v1 v2 : t) : t |
| 168 |
:= let len := Nat.max (bitcount v1) (bitcount v2) in |
| 169 |
Build_t (List.map (fun '(a, b) => f a b) (List.combine (bitlist v1 len) (bitlist v2 len))) |
| 170 |
(f (test_signbit v1) (test_signbit v2)). |
| 171 |
|
| 172 |
Lemma testbit_signbit v n : 0 <= n -> Z.testbit (signbit v) n = test_signbit v. |
| 173 |
Proof. |
| 174 |
cbv [signbit]; break_innermost_match; intro. |
| 175 |
{ now rewrite Z.bits_m1. } |
| 176 |
{ now rewrite Z.testbit_0_l. } |
| 177 |
Qed. |
| 178 |
#[global] |
| 179 |
Hint Rewrite testbit_signbit : bitlist Ztestbit. |
| 180 |
|
| 181 |
Lemma testbit_bitwise f v1 v2 n : testbit (bitwise f v1 v2) n = if n <? 0 then false else f (testbit v1 n) (testbit v2 n). |
| 182 |
Proof. |
| 183 |
cbv [testbit bitwise nth_default]; |
| 184 |
cbn [internal_bitlist test_signbit]. |
| 185 |
rewrite nth_error_map, nth_error_combine. |
| 186 |
break_innermost_match_step; Z.ltb_to_lt; [ reflexivity | ]. |
| 187 |
rewrite testbit_signbit by lia; cbn [test_signbit]. |
| 188 |
apply Nat.max_case_strong; intro. |
| 189 |
all: cbv [bitlist bitcount] in *; |
| 190 |
autorewrite with natsimplify; |
| 191 |
cbn [List.repeat]; rewrite List.app_nil_r, !firstn_all2, nth_error_app, nth_error_repeat_alt, testbit_signbit by lia. |
| 192 |
all: cbv [option_map]. |
| 193 |
all: break_innermost_match. |
| 194 |
all: repeat first [ reflexivity |
| 195 |
| rewrite testbit_signbit by lia |
| 196 |
| progress inversion_option |
| 197 |
| match goal with |
| 198 |
| [ H : nth_error _ _ = None |- _ ] => apply nth_error_error_length in H; exfalso; lia |
| 199 |
| [ H : context[nth_error _ _] |- _ ] => rewrite nth_error_length_error in H by lia |
| 200 |
end ]. |
| 201 |
Qed. |
| 202 |
#[global] |
| 203 |
Hint Rewrite testbit_bitwise : bitlist Ztestbit. |
| 204 |
End Bitlist. |
| 205 |
#[global] |
| 206 |
Hint Rewrite length_bitlist bitcount_of_Z_len bitcount_of_Z : distr_length bitlist. |
| 207 |
#[global] |
| 208 |
Hint Rewrite testbit_to_Z testbit_of_Z_len testbit_of_Z testbit_bitwise testbit_signbit : bitlist Ztestbit. |
| 209 |
#[global] |
| 210 |
Hint Transparent bitcount : rewrite. |
| 211 |
|
| 212 |
Module Z2Bitlist. |
| 213 |
Lemma id (v : Z) : to_Z (of_Z v) = v. |
| 214 |
Proof. now apply Z.bits_inj; intro; autorewrite with bitlist. Qed. |
| 215 |
End Z2Bitlist. |
| 216 |
|
| 217 |
Definition bitwise (f : bool -> bool -> bool) (v1 v2 : Z) : Z |
| 218 |
:= to_Z (Bitlist.bitwise f (of_Z v1) (of_Z v2)). |
| 219 |
|
| 220 |
Lemma testbit_bitwise f v1 v2 n : Z.testbit (bitwise f v1 v2) n = if n <? 0 then false else f (Z.testbit v1 n) (Z.testbit v2 n). |
| 221 |
Proof. now cbv [bitwise]; autorewrite with bitlist. Qed. |
| 222 |
#[global] |
| 223 |
Hint Rewrite testbit_bitwise : Ztestbit. |
| 224 |
|
| 225 |
Lemma bitwise_land v1 v2 : bitwise andb v1 v2 = Z.land v1 v2. |
| 226 |
Proof. |
| 227 |
apply Z.bits_inj; intro; rewrite Z.land_spec, testbit_bitwise. |
| 228 |
break_innermost_match; Z.ltb_to_lt; [ | reflexivity ]. |
| 229 |
now rewrite !Z.testbit_neg_r by lia. |
| 230 |
Qed. |
| 231 |
|
| 232 |
Lemma bitwise_lor v1 v2 : bitwise orb v1 v2 = Z.lor v1 v2. |
| 233 |
Proof. |
| 234 |
apply Z.bits_inj; intro; rewrite Z.lor_spec, testbit_bitwise. |
| 235 |
break_innermost_match; Z.ltb_to_lt; [ | reflexivity ]. |
| 236 |
now rewrite !Z.testbit_neg_r by lia. |
| 237 |
Qed. |
| 238 |
|
| 239 |
Lemma bitwise_lxor v1 v2 : bitwise xorb v1 v2 = Z.lxor v1 v2. |
| 240 |
Proof. |
| 241 |
apply Z.bits_inj; intro; rewrite Z.lxor_spec, testbit_bitwise. |
| 242 |
break_innermost_match; Z.ltb_to_lt; [ | reflexivity ]. |
| 243 |
now rewrite !Z.testbit_neg_r by lia. |
| 244 |
Qed. |