| 1 |
From Coq Require Import Morphisms. |
| 2 |
From Coq Require Import ZArith. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 5 |
Require Import Crypto.Util.ZUtil.LandLorBounds. |
| 6 |
Require Import Crypto.Util.ZUtil.LandLorShiftBounds. |
| 7 |
Require Import Crypto.Util.ZUtil.Morphisms. |
| 8 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 9 |
Require Import Crypto.Util.ZUtil.Tactics.SplitMinMax. |
| 10 |
Require Import Crypto.Util.ZRange. |
| 11 |
Require Import Crypto.Util.ZRange.Operations. |
| 12 |
Require Import Crypto.Util.ZRange.BasicLemmas. |
| 13 |
Require Import Crypto.Util.ZRange.CornersMonotoneBounds. |
| 14 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 15 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 16 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 17 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 18 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 19 |
|
| 20 |
Local Open Scope bool_scope. |
| 21 |
Local Open Scope Z_scope. |
| 22 |
|
| 23 |
Module ZRange. |
| 24 |
Import Operations.ZRange. |
| 25 |
Import CornersMonotoneBounds.ZRange. |
| 26 |
|
| 27 |
Lemma is_bounded_by_bool_lor_land_bound_helper |
| 28 |
f (Hf : f = Z.lor \/ f = Z.land) |
| 29 |
x x_bs y y_bs |
| 30 |
(Hboundedx : is_bounded_by_bool x x_bs = true) |
| 31 |
(Hboundedy : is_bounded_by_bool y y_bs = true) |
| 32 |
: is_tighter_than_bool |
| 33 |
(constant (f (Z.round_lor_land_bound x) (Z.round_lor_land_bound y))) |
| 34 |
(four_corners_and_zero (fun x y => f (Z.round_lor_land_bound x) (Z.round_lor_land_bound y)) x_bs y_bs) |
| 35 |
= true. |
| 36 |
Proof. |
| 37 |
apply monotoneb_four_corners_and_zero_gen with (x:=x) (y:=y); |
| 38 |
destruct_head'_or; subst f; eauto with typeclass_instances zarith core. |
| 39 |
Qed. |
| 40 |
|
| 41 |
Local Existing Instances Z.land_round_Proper_pos_r |
| 42 |
Z.land_round_Proper_pos_l |
| 43 |
Z.lor_round_Proper_pos_r |
| 44 |
Z.lor_round_Proper_pos_l |
| 45 |
Z.land_round_Proper_neg_r |
| 46 |
Z.land_round_Proper_neg_l |
| 47 |
Z.lor_round_Proper_neg_r |
| 48 |
Z.lor_round_Proper_neg_l. |
| 49 |
|
| 50 |
Lemma is_bounded_by_bool_land_lor_bounds_helper |
| 51 |
f (Hf : f = Z.lor \/ f = Z.land) |
| 52 |
x x_bs y y_bs |
| 53 |
(Hboundedx : is_bounded_by_bool x x_bs = true) |
| 54 |
(Hboundedy : is_bounded_by_bool y y_bs = true) |
| 55 |
: is_bounded_by_bool (f x y) (land_lor_bounds f x_bs y_bs) = true. |
| 56 |
Proof. |
| 57 |
pose proof (is_bounded_by_bool_lor_land_bound_helper f Hf x (extend_land_lor_bounds x_bs) y (extend_land_lor_bounds y_bs)) as H. |
| 58 |
pose proof (fun pf1 pf2 => is_bounded_by_bool_lor_land_bound_helper f Hf 0 (extend_land_lor_bounds x_bs) y (extend_land_lor_bounds y_bs) pf2 pf1) as H0x. |
| 59 |
pose proof (fun pf1 pf2 => is_bounded_by_bool_lor_land_bound_helper f Hf (-1) (extend_land_lor_bounds x_bs) y (extend_land_lor_bounds y_bs) pf2 pf1) as Hm1x. |
| 60 |
pose proof (is_bounded_by_bool_lor_land_bound_helper f Hf x (extend_land_lor_bounds x_bs) 0 (extend_land_lor_bounds y_bs)) as H0y. |
| 61 |
pose proof (is_bounded_by_bool_lor_land_bound_helper f Hf x (extend_land_lor_bounds x_bs) (-1) (extend_land_lor_bounds y_bs)) as Hm1y. |
| 62 |
pose proof (is_bounded_by_bool_lor_land_bound_helper f Hf 0 (extend_land_lor_bounds x_bs) 0 (extend_land_lor_bounds y_bs)) as H00. |
| 63 |
pose proof (is_bounded_by_bool_lor_land_bound_helper f Hf (-1) (extend_land_lor_bounds x_bs) 0 (extend_land_lor_bounds y_bs)) as Hm10. |
| 64 |
pose proof (is_bounded_by_bool_lor_land_bound_helper f Hf 0 (extend_land_lor_bounds x_bs) (-1) (extend_land_lor_bounds y_bs)) as H0m1. |
| 65 |
pose proof (is_bounded_by_bool_lor_land_bound_helper f Hf (-1) (extend_land_lor_bounds x_bs) (-1) (extend_land_lor_bounds y_bs)) as Hm1m1. |
| 66 |
specialize_by (eapply ZRange.is_bounded_by_of_is_tighter_than; try eapply ZRange.is_tighter_than_bool_extend_land_lor_bounds; eassumption || reflexivity). |
| 67 |
revert H H0x Hm1x H0y Hm1y H00 Hm1m1 H0m1 Hm10. |
| 68 |
cbv [land_lor_bounds constant is_tighter_than_bool is_bounded_by_bool is_true] in *; cbn [lower upper] in *. |
| 69 |
rewrite !Bool.andb_true_iff, !Z.leb_le in *. |
| 70 |
cbv [extend_land_lor_bounds]. |
| 71 |
cbn [lower upper] in *. |
| 72 |
destruct (Z.round_lor_land_bound_bounds x) as [Hx|Hx], (Z.round_lor_land_bound_bounds y) as [Hy|Hy]. |
| 73 |
all: repeat apply Z.min_case_strong; repeat apply Z.max_case_strong. |
| 74 |
all: try (intros; exfalso; clear dependent f; lia). |
| 75 |
all: destruct x as [|x|x], y as [|y|y]; try (intros; exfalso; clear dependent f; lia). |
| 76 |
all: change (Z.round_lor_land_bound 0) with 0 in *. |
| 77 |
all: change (Z.round_lor_land_bound (-1)) with (-1) in *. |
| 78 |
all: intros. |
| 79 |
all: repeat match goal with |
| 80 |
| _ => assumption |
| 81 |
| [ H : ?T, H' : ?T |- _ ] => clear H' |
| 82 |
| [ H : 0 <= 0 <= 0 |- _ ] => clear H |
| 83 |
| [ H : 0 <= -1 <= _ -> _ |- _ ] => clear H |
| 84 |
| [ H : 0 <= -1 -> _ |- _ ] => clear H |
| 85 |
| [ H : ?x <= ?x <= _ -> _ |- _ ] => specialize (fun pf => H (conj (@Z.le_refl x) pf)) |
| 86 |
| [ H : _ <= ?x <= ?x -> _ |- _ ] => specialize (fun pf => H (conj pf (@Z.le_refl x))) |
| 87 |
| [ H : ?T, H' : ?T /\ _ -> _ |- _ ] => specialize (fun pf => H' (conj H pf)) |
| 88 |
| [ H : ?T, H' : _ /\ ?T -> _ |- _ ] => specialize (fun pf => H' (conj pf H)) |
| 89 |
| _ => progress specialize_by_assumption |
| 90 |
| _ => progress specialize_by (destruct_head'_and; assumption) |
| 91 |
| [ H : _ <= Z.pos _ <= ?x |- _ ] => unique assert (0 <= x) by (clear -H; lia) |
| 92 |
| [ H : ?x <= Z.neg _ <= _ |- _ ] => unique assert (x <= -1) by (clear -H; lia) |
| 93 |
| [ H : ?a <= _ <= ?c, H' : ?c <= ?d, H'' : ?a <= ?d -> _ |- _ ] |
| 94 |
=> unique assert (a <= d) by (clear -H H'; lia) |
| 95 |
| [ H : ?a <= ?b -> ?c <= ?d <= ?e -> _ |- _ ] => specialize (fun pf1 pf2 => H pf2 pf1) |
| 96 |
end. |
| 97 |
all: destruct Hf; subst f. |
| 98 |
all: split. |
| 99 |
all: destruct_head'_and. |
| 100 |
all: split_and. |
| 101 |
all: repeat lazymatch goal with |
| 102 |
| [ |- Z.land _ (Z.pos ?x) <= _ ] |
| 103 |
=> is_var x; etransitivity; [ apply Z.land_round_bound_pos_r' | ] |
| 104 |
| [ |- Z.land (Z.pos ?x) _ <= _ ] |
| 105 |
=> is_var x; etransitivity; [ apply Z.land_round_bound_pos_l' | ] |
| 106 |
| [ |- _ <= Z.land _ (Z.pos ?x) ] |
| 107 |
=> is_var x; etransitivity; [ | apply Z.land_round_bound_pos_r ] |
| 108 |
| [ |- _ <= Z.land (Z.pos ?x) _ ] |
| 109 |
=> is_var x; etransitivity; [ | apply Z.land_round_bound_pos_l ] |
| 110 |
| [ |- Z.land _ (Z.neg ?x) <= _ ] |
| 111 |
=> is_var x; etransitivity; [ apply Z.land_round_bound_neg_r | ] |
| 112 |
| [ |- Z.land (Z.neg ?x) _ <= _ ] |
| 113 |
=> is_var x; etransitivity; [ apply Z.land_round_bound_neg_l | ] |
| 114 |
| [ |- _ <= Z.land _ (Z.neg ?x) ] |
| 115 |
=> is_var x; etransitivity; [ | apply Z.land_round_bound_neg_r' ] |
| 116 |
| [ |- _ <= Z.land (Z.neg ?x) _ ] |
| 117 |
=> is_var x; etransitivity; [ | apply Z.land_round_bound_neg_l' ] |
| 118 |
| [ |- Z.lor _ (Z.pos ?x) <= _ ] |
| 119 |
=> is_var x; etransitivity; [ apply Z.lor_round_bound_pos_r' | ] |
| 120 |
| [ |- Z.lor (Z.pos ?x) _ <= _ ] |
| 121 |
=> is_var x; etransitivity; [ apply Z.lor_round_bound_pos_l' | ] |
| 122 |
| [ |- _ <= Z.lor _ (Z.pos ?x) ] |
| 123 |
=> is_var x; etransitivity; [ | apply Z.lor_round_bound_pos_r ] |
| 124 |
| [ |- _ <= Z.lor (Z.pos ?x) _ ] |
| 125 |
=> is_var x; etransitivity; [ | apply Z.lor_round_bound_pos_l ] |
| 126 |
| [ |- Z.lor _ (Z.neg ?x) <= _ ] |
| 127 |
=> is_var x; etransitivity; [ apply Z.lor_round_bound_neg_r | ] |
| 128 |
| [ |- Z.lor (Z.neg ?x) _ <= _ ] |
| 129 |
=> is_var x; etransitivity; [ apply Z.lor_round_bound_neg_l | ] |
| 130 |
| [ |- _ <= Z.lor _ (Z.neg ?x) ] |
| 131 |
=> is_var x; etransitivity; [ | apply Z.lor_round_bound_neg_r' ] |
| 132 |
| [ |- _ <= Z.lor (Z.neg ?x) _ ] |
| 133 |
=> is_var x; etransitivity; [ | apply Z.lor_round_bound_neg_l' ] |
| 134 |
| [ |- Z.pos ?x <= _ ] |
| 135 |
=> is_var x; etransitivity; [ eassumption | ] |
| 136 |
| [ |- _ <= Z.neg ?x ] |
| 137 |
=> is_var x; etransitivity; [ | eassumption ] |
| 138 |
| [ |- _ <= Z.pos ?x ] |
| 139 |
=> is_var x; transitivity 0; [ assumption | lia ] |
| 140 |
| [ |- Z.neg ?x <= _ ] |
| 141 |
=> is_var x; transitivity (-1); [ lia | assumption ] |
| 142 |
| _ => idtac |
| 143 |
end. |
| 144 |
all: try assumption. |
| 145 |
all: try (rewrite ?Z.lor_0_l, ?Z.lor_0_r, ?Z.land_0_l, ?Z.land_0_r, ?Z.lor_m1_l, ?Z.lor_m1_r, ?Z.land_m1_l, ?Z.land_m1_r in *; reflexivity || assumption). |
| 146 |
Qed. |
| 147 |
|
| 148 |
Lemma is_bounded_by_bool_land_bounds |
| 149 |
x x_bs y y_bs |
| 150 |
(Hboundedx : is_bounded_by_bool x x_bs = true) |
| 151 |
(Hboundedy : is_bounded_by_bool y y_bs = true) |
| 152 |
: is_bounded_by_bool (Z.land x y) (ZRange.land_bounds x_bs y_bs) = true. |
| 153 |
Proof. |
| 154 |
cbv [ZRange.land_bounds]. |
| 155 |
cbv [is_bounded_by_bool] in *. |
| 156 |
cbn [upper lower]. |
| 157 |
repeat match goal with |
| 158 |
| H : (_ && _)%bool = true |- _ => |
| 159 |
apply Bool.andb_true_iff in H; destruct H |
| 160 |
end. |
| 161 |
Z.ltb_to_lt. |
| 162 |
pose proof (Z.log2_up_le_mono (-x) (- lower x_bs) ltac:(lia)). |
| 163 |
pose proof (Z.log2_up_le_mono (-y) (- lower y_bs) ltac:(lia)). |
| 164 |
assert (0 <= 2 ^ (Z.max (Z.log2_up (- lower x_bs)) |
| 165 |
(Z.log2_up (- lower y_bs)))) |
| 166 |
by (apply Z.pow_nonneg; lia). |
| 167 |
pose proof (Z.pow_le_mono |
| 168 |
2 (Z.max (Z.log2_up (- x)) (Z.log2_up (- y))) |
| 169 |
2 (Z.max (Z.log2_up (- lower x_bs)) (Z.log2_up (- lower y_bs))) |
| 170 |
ltac:(lia) ltac:(lia)). |
| 171 |
destruct (Z_lt_le_dec x 0); destruct (Z_lt_le_dec y 0). |
| 172 |
all:lazymatch goal with |
| 173 |
| Hx : ?x < 0, Hy : ?y < 0 |- context [Z.land ?x ?y] => |
| 174 |
pose proof (Z.land_neg_lower x y ltac:(lia) ltac:(lia)); |
| 175 |
pose proof (proj2 (Z.land_neg x y) ltac:(lia)); |
| 176 |
pose proof (Z.land_le x y ltac:(lia)) |
| 177 |
| Hx : ?x < 0, Hy : 0 <= ?y |- context [Z.land ?x ?y] => |
| 178 |
pose proof (Z.land_neg_l_range x y ltac:(lia) ltac:(lia)); |
| 179 |
pose proof (proj2 (Z.land_nonneg x y) ltac:(lia)) |
| 180 |
| Hx : 0 <= ?x, Hy : ?y < 0 |- context [Z.land ?x ?y] => |
| 181 |
pose proof (Z.land_neg_r_range x y ltac:(lia) ltac:(lia)); |
| 182 |
pose proof (proj2 (Z.land_nonneg x y) ltac:(lia)) |
| 183 |
| Hx : 0 <= ?x, Hy : 0 <= ?y |- context [Z.land ?x ?y] => |
| 184 |
pose proof (Z.land_le x y ltac:(lia)); |
| 185 |
let H := fresh in |
| 186 |
pose proof (Z.land_le y x ltac:(lia)) as H; |
| 187 |
rewrite Z.land_comm in H; |
| 188 |
pose proof (proj2 (Z.land_nonneg x y) ltac:(lia)) |
| 189 |
end. |
| 190 |
all:break_innermost_match; |
| 191 |
repeat match goal with |
| 192 |
| H : (_ && _)%bool = true |- _ => |
| 193 |
apply Bool.andb_true_iff in H; destruct H |
| 194 |
| H : (_ && _)%bool = false |- _ => |
| 195 |
apply Bool.andb_false_iff in H; destruct H |
| 196 |
end; Z.ltb_to_lt. |
| 197 |
all:try lia. |
| 198 |
all:apply Bool.andb_true_iff; split; |
| 199 |
Z.ltb_to_lt; try lia. |
| 200 |
Qed. |
| 201 |
|
| 202 |
Lemma is_bounded_by_bool_lor_bounds |
| 203 |
x x_bs y y_bs |
| 204 |
(Hboundedx : is_bounded_by_bool x x_bs = true) |
| 205 |
(Hboundedy : is_bounded_by_bool y y_bs = true) |
| 206 |
: is_bounded_by_bool (Z.lor x y) (ZRange.lor_bounds x_bs y_bs) = true. |
| 207 |
Proof. apply is_bounded_by_bool_land_lor_bounds_helper; auto. Qed. |
| 208 |
End ZRange. |