diff options
Diffstat (limited to 'src/Util/ZUtil')
-rw-r--r-- | src/Util/ZUtil/Definitions.v | 1 | ||||
-rw-r--r-- | src/Util/ZUtil/LandLorBounds.v | 236 | ||||
-rw-r--r-- | src/Util/ZUtil/LandLorShiftBounds.v | 41 | ||||
-rw-r--r-- | src/Util/ZUtil/Modulo.v | 11 | ||||
-rw-r--r-- | src/Util/ZUtil/Ones.v | 38 | ||||
-rw-r--r-- | src/Util/ZUtil/Tactics/LinearSubstitute.v | 8 | ||||
-rw-r--r-- | src/Util/ZUtil/ZSimplify/Core.v | 10 | ||||
-rw-r--r-- | src/Util/ZUtil/ZSimplify/Simple.v | 3 |
8 files changed, 297 insertions, 51 deletions
diff --git a/src/Util/ZUtil/Definitions.v b/src/Util/ZUtil/Definitions.v index 4ef6b5403..8fe5772f5 100644 --- a/src/Util/ZUtil/Definitions.v +++ b/src/Util/ZUtil/Definitions.v @@ -85,6 +85,7 @@ Module Z. then mul_split_at_bitwidth (Z.log2 s) x y else ((x * y) mod s, (x * y) / s). + (** if positive, round up to 2^k-1 (0b11111....); if negative, round down to -2^k (0b...111000000...) *) Definition round_lor_land_bound (x : Z) : Z := if (0 <=? x)%Z then 2^(Z.log2_up (x+1))-1 diff --git a/src/Util/ZUtil/LandLorBounds.v b/src/Util/ZUtil/LandLorBounds.v index 1b10ecf97..78866ec7f 100644 --- a/src/Util/ZUtil/LandLorBounds.v +++ b/src/Util/ZUtil/LandLorBounds.v @@ -3,8 +3,17 @@ Require Import Coq.ZArith.ZArith. Require Import Coq.Classes.Morphisms. Require Import Crypto.Util.ZUtil.Definitions. Require Import Crypto.Util.ZUtil.Pow2. +Require Import Crypto.Util.ZUtil.Log2. Require Import Crypto.Util.ZUtil.Tactics.PeelLe. +Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. +Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos. +Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. +Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. +Require Import Crypto.Util.ZUtil.Tactics.LinearSubstitute. +Require Import Crypto.Util.ZUtil.Tactics.SplitMinMax. Require Import Crypto.Util.ZUtil.Modulo.PullPush. +Require Import Crypto.Util.ZUtil.LandLorShiftBounds. +Require Import Crypto.Util.ZUtil.Modulo. Require Import Crypto.Util.ZUtil.Ones. Require Import Crypto.Util.ZUtil.Lnot. Require Import Crypto.Util.ZUtil.Land. @@ -14,10 +23,34 @@ Require Import Crypto.Util.Tactics.BreakMatch. Local Open Scope Z_scope. Module Z. + Lemma round_lor_land_bound_bounds x + : (0 <= x <= Z.round_lor_land_bound x) \/ (Z.round_lor_land_bound x <= x <= -1). + Proof. + cbv [Z.round_lor_land_bound]; break_innermost_match; Z.ltb_to_lt. + all: constructor; split; try lia; []. + all: Z.replace_all_neg_with_pos. + all: match goal with |- context[2^Z.log2_up ?x] => pose proof (Z.log2_up_le_full x) end. + all: lia. + Qed. + Hint Resolve round_lor_land_bound_bounds : zarith. + + Lemma round_lor_land_bound_bounds_pos x + : (0 <= Z.pos x <= Z.round_lor_land_bound (Z.pos x)). + Proof. generalize (round_lor_land_bound_bounds (Z.pos x)); lia. Qed. + Hint Resolve round_lor_land_bound_bounds_pos : zarith. + + Lemma round_lor_land_bound_bounds_neg x + : Z.round_lor_land_bound (Z.neg x) <= Z.neg x <= -1. + Proof. generalize (round_lor_land_bound_bounds (Z.neg x)); lia. Qed. + Hint Resolve round_lor_land_bound_bounds_neg : zarith. + Local Ltac saturate := repeat first [ progress cbv [Z.round_lor_land_bound Proper respectful Basics.flip] in * - | progress cbn in * + | progress Z.ltb_to_lt | progress intros + | break_innermost_match_step + | lia + | rewrite !Pos2Z.opp_neg | match goal with | [ |- context[Z.log2_up ?x] ] => unique pose proof (Z.log2_up_nonneg x) @@ -29,13 +62,18 @@ Module Z. => unique assert (Z.pos x <= Z.pos y) by lia | [ H : Pos.le ?x ?y |- context[Z.pos (?x+1)] ] => unique assert (Z.pos (x+1) <= Z.pos (y+1)) by lia + | [ H : Z.le ?x ?y |- context[?x+1] ] + => unique assert (x+1 <= y+1) by lia | [ H : Z.le ?x ?y |- context[2^Z.log2_up ?x] ] => unique assert (2^Z.log2_up x <= 2^Z.log2_up y) by (Z.peel_le; lia) + | [ H : ?a^?b <= ?a^?c |- _ ] + => unique assert (a^(c-b) = a^c/a^b) by auto with zarith; + unique assert (a^c mod a^b = 0) by auto with zarith end ]. Local Ltac do_rewrites_step := match goal with | [ |- ?R ?x ?x ] => reflexivity - | [ |- context[Z.land (-2^_) (-2^_)] ] + (*| [ |- context[Z.land (-2^_) (-2^_)] ] => rewrite <- !Z.lnot_ones_equiv, <- !Z.lnot_lor, !Z.lor_ones_ones, !Z.lnot_ones_equiv | [ |- context[Z.lor (-2^_) (-2^_)] ] => rewrite <- !Z.lnot_ones_equiv, <- !Z.lnot_land, !Z.land_ones_ones, !Z.lnot_ones_equiv @@ -52,8 +90,45 @@ Module Z. | [ |- context[Z.lor (-2^?x) (2^?y-1)] ] => rewrite <- !Z.lnot_ones_equiv, <- (Z.lnot_involutive (2^y-1)), <- !Z.lnot_land, ?Z.lnot_ones_equiv, (Z.lnot_sub1 (2^y)), !Z.ones_equiv, ?Z.lnot_equiv, <- !Z.sub_1_r | [ |- context[-?x mod ?y] ] - => rewrite (@Z.opp_mod_mod_push x y) by Z.NoZMod - | [ H : ?x <= ?x |- _ ] => clear H + => rewrite (@Z.opp_mod_mod_push x y) by Z.NoZMod*) + | [ |- context[Z.land (2^?y-1) ?x] ] + => is_var x; rewrite (Z.land_comm (2^y-1) x) + | [ |- context[Z.lor (2^?y-1) ?x] ] + => is_var x; rewrite (Z.lor_comm (2^y-1) x) + | [ |- context[Z.land (-2^?y) ?x] ] + => is_var x; rewrite (Z.land_comm (-2^y) x) + | [ |- context[Z.lor (-2^?y) ?x] ] + => is_var x; rewrite (Z.lor_comm (-2^y) x) + | [ |- context[Z.land _ (2^_-1)] ] + => rewrite !Z.sub_1_r, <- !Z.ones_equiv, !Z.land_ones by auto with zarith + | [ |- context[Z.land ?x (-2^?y)] ] + => is_var x; + rewrite <- !Z.lnot_ones_equiv, <- (Z.lnot_involutive x), <- !Z.lnot_lor, !Z.ones_equiv, !Z.lnot_equiv, <- !Z.sub_1_r; + let x' := fresh in + remember (-x-1) as x' eqn:?; Z.linear_substitute x; + rename x' into x + | [ |- context[Z.lor ?x (-2^?y)] ] + => is_var x; + rewrite <- !Z.lnot_ones_equiv, <- (Z.lnot_involutive x), <- !Z.lnot_land, !Z.ones_equiv, !Z.lnot_equiv, <- !Z.sub_1_r; + let x' := fresh in + remember (-x-1) as x' eqn:?; Z.linear_substitute x; + rename x' into x + | [ |- Z.lor ?x (?y-1) <= Z.lor ?x (?y'-1) ] + => rewrite (Z.div_mod'' (Z.lor x (y-1)) y), (Z.div_mod'' (Z.lor x (y'-1)) y') by auto with zarith + | [ |- Z.lor ?x (?y-1) = _ ] + => rewrite (Z.div_mod'' (Z.lor x (y-1)) y) by auto with zarith + | [ |- context[?m1 - 1 + (?x - ?x mod ?m1)] ] + => replace (m1 - 1 + (x - x mod m1)) with ((m1 - x mod m1) + (x - 1)) by lia + | _ => progress rewrite ?Z.lor_pow2_div_pow2_r, ?Z.lor_pow2_div_pow2_l, ?Z.lor_pow2_mod_pow2_r, ?Z.lor_pow2_mod_pow2_l by auto with zarith + | _ => rewrite !Z.mul_div_eq by lia + | _ => progress rewrite ?(Z.add_comm 1) in * + | [ |- context[?x mod 2^(Z.log2_up (?x + 1))] ] + => rewrite (Z.mod_small x (2^Z.log2_up (x+1))) by (rewrite <- Z.le_succ_l, <- Z.add_1_r, Z.log2_up_le_pow2 by lia; lia) + | [ H : ?a^?b <= ?a^?c |- context[?x mod ?a^?b] ] + => rewrite (@Z.mod_pow_r_split x a b c) by auto with zarith; + (Z.div_mod_to_quot_rem; nia) + | _ => progress Z.peel_le + (*| [ H : ?x <= ?x |- _ ] => clear H | [ H : ?x < ?y, H' : ?y <= ?z |- _ ] => unique assert (x < z) by lia | [ H : ?x < ?y, H' : ?a <= ?x |- _ ] => unique assert (a < y) by lia | [ H : 2^?x < 2^?y |- context[2^?x mod 2^?y] ] @@ -65,8 +140,15 @@ Module Z. destruct (@Z.pow2_lt_or_divides x y ltac:(lia)) as [H|H]; [ repeat first [ rewrite (Z.mod_small (2^x) (2^y)) by lia | rewrite !(@Z_mod_nz_opp_full (2^x) (2^y)) ] - | rewrite H ] - | _ => progress autorewrite with zsimplify_const + | rewrite H ]*) + | _ => progress autorewrite with zsimplify_fast in * + | [ |- context[-(-?x-1)] ] => replace (-(-x-1)) with (1+x) by lia + | [ H : 0 > -(1+?x) |- _ ] => assert (0 <= x) by (clear -H; lia); clear H + | [ H : 0 > -(?x+1) |- _ ] => assert (0 <= x) by (clear -H; lia); clear H + | [ |- ?a - ?b = ?a' - ?b' ] => apply f_equal2; try reflexivity; [] + | [ |- -?a = -?a' ] => apply f_equal + | _ => rewrite <- !Z.sub_1_r + | _ => lia end. Local Ltac do_rewrites := repeat do_rewrites_step. Local Ltac fin_t := @@ -80,53 +162,129 @@ Module Z. | lia | progress Z.peel_le ]. Local Ltac t := - saturate; do_rewrites; fin_t. + saturate; do_rewrites. Local Instance land_round_Proper_pos_r x - : Proper (Pos.le ==> Z.le) - (fun y => - Z.land (Z.round_lor_land_bound x) (Z.round_lor_land_bound (Z.pos y))). - Proof. destruct x; t. Qed. + : Proper (Pos.le ==> Z.le) (fun y => Z.land x (Z.round_lor_land_bound (Z.pos y))). + Proof. t. Qed. Local Instance land_round_Proper_pos_l y - : Proper (Pos.le ==> Z.le) - (fun x => - Z.land (Z.round_lor_land_bound (Z.pos x)) (Z.round_lor_land_bound y)). - Proof. destruct y; t. Qed. + : Proper (Pos.le ==> Z.le) (fun x => Z.land (Z.round_lor_land_bound (Z.pos x)) y). + Proof. t. Qed. Local Instance lor_round_Proper_pos_r x - : Proper (Pos.le ==> Z.le) - (fun y => - Z.lor (Z.round_lor_land_bound x) (Z.round_lor_land_bound (Z.pos y))). - Proof. destruct x; t. Qed. + : Proper (Pos.le ==> Z.le) (fun y => Z.lor x (Z.round_lor_land_bound (Z.pos y))). + Proof. t. Qed. Local Instance lor_round_Proper_pos_l y - : Proper (Pos.le ==> Z.le) - (fun x => - Z.lor (Z.round_lor_land_bound (Z.pos x)) (Z.round_lor_land_bound y)). - Proof. destruct y; t. Qed. + : Proper (Pos.le ==> Z.le) (fun x => Z.lor (Z.round_lor_land_bound (Z.pos x)) y). + Proof. t. Qed. Local Instance land_round_Proper_neg_r x - : Proper (Basics.flip Pos.le ==> Z.le) - (fun y => - Z.land (Z.round_lor_land_bound x) (Z.round_lor_land_bound (Z.neg y))). - Proof. destruct x; t. Qed. + : Proper (Basics.flip Pos.le ==> Z.le) (fun y => Z.land x (Z.round_lor_land_bound (Z.neg y))). + Proof. t. Qed. Local Instance land_round_Proper_neg_l y - : Proper (Basics.flip Pos.le ==> Z.le) - (fun x => - Z.land (Z.round_lor_land_bound (Z.neg x)) (Z.round_lor_land_bound y)). - Proof. destruct y; t. Qed. + : Proper (Basics.flip Pos.le ==> Z.le) (fun x => Z.land (Z.round_lor_land_bound (Z.neg x)) y). + Proof. t. Qed. Local Instance lor_round_Proper_neg_r x - : Proper (Basics.flip Pos.le ==> Z.le) - (fun y => - Z.lor (Z.round_lor_land_bound x) (Z.round_lor_land_bound (Z.neg y))). - Proof. destruct x; t. Qed. + : Proper (Basics.flip Pos.le ==> Z.le) (fun y => Z.lor x (Z.round_lor_land_bound (Z.neg y))). + Proof. t. Qed. Local Instance lor_round_Proper_neg_l y - : Proper (Basics.flip Pos.le ==> Z.le) - (fun x => - Z.lor (Z.round_lor_land_bound (Z.neg x)) (Z.round_lor_land_bound y)). - Proof. destruct y; t. Qed. + : Proper (Basics.flip Pos.le ==> Z.le) (fun x => Z.lor (Z.round_lor_land_bound (Z.neg x)) y). + Proof. t. Qed. + + Lemma land_round_lor_land_bound_r x + : Z.land x (Z.round_lor_land_bound x) = if (0 <=? x) then x else Z.round_lor_land_bound x. + Proof. t. Qed. + Hint Rewrite land_round_lor_land_bound_r : zsimplify_fast zsimplify. + Lemma land_round_lor_land_bound_l x + : Z.land (Z.round_lor_land_bound x) x = if (0 <=? x) then x else Z.round_lor_land_bound x. + Proof. rewrite Z.land_comm, land_round_lor_land_bound_r; reflexivity. Qed. + Hint Rewrite land_round_lor_land_bound_l : zsimplify_fast zsimplify. + + Lemma lor_round_lor_land_bound_r x + : Z.lor x (Z.round_lor_land_bound x) = if (0 <=? x) then Z.round_lor_land_bound x else x. + Proof. t. Qed. + Hint Rewrite lor_round_lor_land_bound_r : zsimplify_fast zsimplify. + Lemma lor_round_lor_land_bound_l x + : Z.lor (Z.round_lor_land_bound x) x = if (0 <=? x) then Z.round_lor_land_bound x else x. + Proof. rewrite Z.lor_comm, lor_round_lor_land_bound_r; reflexivity. Qed. + Hint Rewrite lor_round_lor_land_bound_l : zsimplify_fast zsimplify. + + Lemma land_round_bound_pos_r v x + : 0 <= Z.land v (Z.pos x) <= Z.land v (Z.round_lor_land_bound (Z.pos x)). + Proof. + rewrite Z.land_nonneg; split; [ lia | ]. + replace (Z.pos x) with (Z.land (Z.pos x) (Z.round_lor_land_bound (Z.pos x))) at 1 + by now rewrite land_round_lor_land_bound_r. + rewrite (Z.land_comm (Z.pos x)), Z.land_assoc. + apply Z.land_upper_bound_l; rewrite ?Z.land_nonneg; t. + Qed. + Hint Resolve land_round_bound_pos_r (fun v x => proj1 (land_round_bound_pos_r v x)) (fun v x => proj2 (land_round_bound_pos_r v x)) : zarith. + Lemma land_round_bound_pos_l v x + : 0 <= Z.land (Z.pos x) v <= Z.land (Z.round_lor_land_bound (Z.pos x)) v. + Proof. rewrite <- !(Z.land_comm v); apply land_round_bound_pos_r. Qed. + Hint Resolve land_round_bound_pos_l (fun v x => proj1 (land_round_bound_pos_l v x)) (fun v x => proj2 (land_round_bound_pos_l v x)) : zarith. + + Lemma land_round_bound_neg_r v x + : Z.land v (Z.round_lor_land_bound (Z.neg x)) <= Z.land v (Z.neg x) <= v. + Proof. + assert (0 < 2 ^ Z.log2_up (Z.pos x)) by auto with zarith. + split; [ | apply Z.land_le; lia ]. + replace (Z.round_lor_land_bound (Z.neg x)) with (Z.land (Z.neg x) (Z.round_lor_land_bound (Z.neg x))) + by now rewrite land_round_lor_land_bound_r. + rewrite !Z.land_assoc. + etransitivity; [ apply Z.land_le; cbn; lia | ]; lia. + Qed. + Hint Resolve land_round_bound_neg_r (fun v x => proj1 (land_round_bound_neg_r v x)) (fun v x => proj2 (land_round_bound_neg_r v x)) : zarith. + Lemma land_round_bound_neg_l v x + : Z.land (Z.round_lor_land_bound (Z.neg x)) v <= Z.land (Z.neg x) v <= v. + Proof. rewrite <- !(Z.land_comm v); apply land_round_bound_neg_r. Qed. + Hint Resolve land_round_bound_neg_l (fun v x => proj1 (land_round_bound_neg_l v x)) (fun v x => proj2 (land_round_bound_neg_l v x)) : zarith. + + Lemma lor_round_bound_neg_r v x + : Z.lor v (Z.round_lor_land_bound (Z.neg x)) <= Z.lor v (Z.neg x) <= -1. + Proof. + change (-1) with (Z.pred 0); rewrite <- Z.lt_le_pred. + rewrite Z.lor_neg; split; [ | lia ]. + replace (Z.neg x) with (Z.lor (Z.neg x) (Z.round_lor_land_bound (Z.neg x))) at 2 + by now rewrite lor_round_lor_land_bound_r. + rewrite (Z.lor_comm (Z.neg x)), Z.lor_assoc. + cbn; rewrite <- !Z.lnot_ones_equiv, <- (Z.lnot_involutive v), <- (Z.lnot_involutive (Z.neg x)), <- !Z.lnot_land, !Z.ones_equiv, !Z.lnot_equiv, <- !Z.sub_1_r, !Pos2Z.opp_neg. + Z.peel_le. + apply Z.land_upper_bound_l; rewrite ?Z.land_nonneg; t. + Qed. + Hint Resolve lor_round_bound_neg_r (fun v x => proj1 (lor_round_bound_neg_r v x)) (fun v x => proj2 (lor_round_bound_neg_r v x)) : zarith. + Lemma lor_round_bound_neg_l v x + : Z.lor (Z.round_lor_land_bound (Z.neg x)) v <= Z.lor (Z.neg x) v <= -1. + Proof. rewrite <- !(Z.lor_comm v); apply lor_round_bound_neg_r. Qed. + Hint Resolve lor_round_bound_neg_l (fun v x => proj1 (lor_round_bound_neg_l v x)) (fun v x => proj2 (lor_round_bound_neg_l v x)) : zarith. + + Lemma lor_round_bound_pos_r v x + : v <= Z.lor v (Z.pos x) <= Z.lor v (Z.round_lor_land_bound (Z.pos x)). + Proof. + assert (0 < 2 ^ Z.log2_up (Z.pos (x + 1))) by auto with zarith. + split; [ apply Z.lor_lower; lia | ]. + replace (Z.round_lor_land_bound (Z.pos x)) with (Z.lor (Z.pos x) (Z.round_lor_land_bound (Z.pos x))) + by now rewrite lor_round_lor_land_bound_r. + rewrite !Z.lor_assoc. + etransitivity; [ | apply Z.lor_lower; rewrite ?Z.lor_nonneg; cbn; lia ]; lia. + Qed. + Hint Resolve lor_round_bound_pos_r (fun v x => proj1 (lor_round_bound_pos_r v x)) (fun v x => proj2 (lor_round_bound_pos_r v x)) : zarith. + Lemma lor_round_bound_pos_l v x + : v <= Z.lor (Z.pos x) v <= Z.lor (Z.round_lor_land_bound (Z.pos x)) v. + Proof. rewrite <- !(Z.lor_comm v); apply lor_round_bound_pos_r. Qed. + Hint Resolve lor_round_bound_pos_l (fun v x => proj1 (lor_round_bound_pos_l v x)) (fun v x => proj2 (lor_round_bound_pos_l v x)) : zarith. + + Lemma land_round_bound_pos_r' v x : Z.land v (Z.pos x) <= Z.land v (Z.round_lor_land_bound (Z.pos x)). Proof. auto with zarith. Qed. + Lemma land_round_bound_pos_l' v x : Z.land (Z.pos x) v <= Z.land (Z.round_lor_land_bound (Z.pos x)) v. Proof. auto with zarith. Qed. + Lemma land_round_bound_neg_r' v x : Z.land v (Z.round_lor_land_bound (Z.neg x)) <= Z.land v (Z.neg x). Proof. auto with zarith. Qed. + Lemma land_round_bound_neg_l' v x : Z.land (Z.round_lor_land_bound (Z.neg x)) v <= Z.land (Z.neg x) v. Proof. auto with zarith. Qed. + Lemma lor_round_bound_neg_r' v x : Z.lor v (Z.round_lor_land_bound (Z.neg x)) <= Z.lor v (Z.neg x). Proof. auto with zarith. Qed. + Lemma lor_round_bound_neg_l' v x : Z.lor (Z.round_lor_land_bound (Z.neg x)) v <= Z.lor (Z.neg x) v. Proof. auto with zarith. Qed. + Lemma lor_round_bound_pos_r' v x : Z.lor v (Z.pos x) <= Z.lor v (Z.round_lor_land_bound (Z.pos x)). Proof. auto with zarith. Qed. + Lemma lor_round_bound_pos_l' v x : Z.lor (Z.pos x) v <= Z.lor (Z.round_lor_land_bound (Z.pos x)) v. Proof. auto with zarith. Qed. End Z. diff --git a/src/Util/ZUtil/LandLorShiftBounds.v b/src/Util/ZUtil/LandLorShiftBounds.v index e978ab6b0..a8e0b2051 100644 --- a/src/Util/ZUtil/LandLorShiftBounds.v +++ b/src/Util/ZUtil/LandLorShiftBounds.v @@ -11,6 +11,7 @@ Require Import Crypto.Util.ZUtil.Testbit. Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos. Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. +Require Import Crypto.Util.ZUtil.Tactics.PeelLe. Require Import Crypto.Util.NUtil.WithoutReferenceToZ. Local Open Scope Z_scope. @@ -70,7 +71,7 @@ Module Z. Qed. Section ZInequalities. - Lemma land_le : forall x y, (0 <= x)%Z -> (Z.land x y <= x)%Z. + Lemma land_le' : forall x y, (0 <= x)%Z -> (Z.land x y <= x)%Z. Proof. intros x y H; apply Z.ldiff_le; [assumption|]. rewrite Z.ldiff_land, Z.land_comm, Z.land_assoc. @@ -79,14 +80,38 @@ Module Z. reflexivity. Qed. - Lemma lor_lower : forall x y, (0 <= x)%Z -> (0 <= y)%Z -> (x <= Z.lor x y)%Z. + Lemma lor_lower : forall x y, (0 <= x -> 0 <= y)%Z -> (x <= Z.lor x y)%Z. Proof. - intros x y H H0; apply Z.ldiff_le; [apply Z.lor_nonneg; auto|]. - rewrite Z.ldiff_land. - apply Z.bits_inj_iff'; intros k Hpos; apply Z.le_ge in Hpos. - rewrite Z.testbit_0_l, Z.land_spec, Z.lnot_spec, Z.lor_spec; - [|apply Z.ge_le; assumption]. - induction (Z.testbit x k), (Z.testbit y k); cbv; reflexivity. + intros x y H. + destruct (Z_lt_le_dec x 0). + { Z.replace_all_neg_with_pos. + replace (-x) with (Z.lnot (x - 1)) by (cbv [Z.pred Z.lnot]; lia). + rewrite <- (Z.lnot_involutive y). + rewrite <- Z.lnot_land. + cbv [Z.lnot]. + rewrite <- !Z.sub_1_r. + Z.peel_le. + apply land_le'; lia. } + { apply Z.ldiff_le; [apply Z.lor_nonneg; auto|]. + rewrite Z.ldiff_land. + apply Z.bits_inj_iff'; intros k Hpos; apply Z.le_ge in Hpos. + rewrite Z.testbit_0_l, Z.land_spec, Z.lnot_spec, Z.lor_spec; + [|apply Z.ge_le; assumption]. + induction (Z.testbit x k), (Z.testbit y k); cbv; reflexivity. } + Qed. + + Lemma land_le : forall x y, (0 <= y -> 0 <= x)%Z -> (Z.land x y <= x)%Z. + Proof. + intros x y H. + destruct (Z_lt_le_dec y 0), (Z_lt_le_dec x 0); auto using land_le' with lia. + Z.replace_all_neg_with_pos. + replace (-x) with (Z.lnot (x - 1)) by (cbv [Z.pred Z.lnot]; lia). + replace (-y) with (Z.lnot (y - 1)) by (cbv [Z.pred Z.lnot]; lia). + rewrite <- Z.lnot_lor. + cbv [Z.lnot]. + rewrite <- !Z.sub_1_r. + Z.peel_le. + apply lor_lower; lia. Qed. Lemma lor_le : forall x y z, diff --git a/src/Util/ZUtil/Modulo.v b/src/Util/ZUtil/Modulo.v index 567d106e3..9944c8124 100644 --- a/src/Util/ZUtil/Modulo.v +++ b/src/Util/ZUtil/Modulo.v @@ -297,6 +297,7 @@ Module Z. apply Z_mod_mult. Qed. Hint Rewrite mod_same_pow using zutil_arith : zsimplify. + Hint Resolve mod_same_pow : zarith. Lemma mod_opp_l_z_iff a b (H : b <> 0) : a mod b = 0 <-> (-a) mod b = 0. Proof. @@ -369,4 +370,14 @@ Module Z. apply Z.mod_small. omega. Qed. + + Lemma mod_pow_r_split x b e1 e2 : 0 <= b -> 0 <= e1 <= e2 -> x mod b^e2 = (x mod b^e1) + (b^e1) * ((x / b^e1) mod b^(e2-e1)). + Proof. + destruct (Z_zerop b). + { destruct (Z_zerop e1), (Z_zerop e2), (Z.eq_dec e1 e2); subst; intros; cbn; autorewrite with zsimplify_fast; lia. } + intros. + replace (b^e2) with (b^e1 * b^(e2 - e1)) by (autorewrite with pull_Zpow; f_equal; lia). + rewrite Z.rem_mul_r by auto with zarith. + reflexivity. + Qed. End Z. diff --git a/src/Util/ZUtil/Ones.v b/src/Util/ZUtil/Ones.v index e856f23a0..37a0d184e 100644 --- a/src/Util/ZUtil/Ones.v +++ b/src/Util/ZUtil/Ones.v @@ -174,4 +174,42 @@ Module Z. end ]. Qed. Hint Rewrite lor_ones_ones : zsimplify. + + Lemma lor_pow2_mod_pow2_r x e (He : 0 <= e) : Z.lor x (2^e-1) mod (2^e) = 2^e-1. + Proof. + destruct (Z_zerop e). + { subst; autorewrite with zsimplify_const; reflexivity. } + assert (0 <= x mod 2^e < 2^e) by auto with zarith. + assert (0 <= x mod 2^e <= 2^e-1) by lia. + assert (Z.log2 (x mod 2^e) <= Z.log2 (2^e-1)) by (apply Z.log2_le_mono; lia). + assert (Z.log2 (x mod 2^e) < e) by (rewrite Z.sub_1_r, Z.log2_pred_pow2 in * by lia; lia). + rewrite <- Z.land_ones, Z.land_lor_distr_l by assumption. + rewrite !Z.sub_1_r, <- !Z.ones_equiv, !Z.land_ones_ones, Z.min_id, Z.max_id, Bool.if_const. + rewrite Z.land_ones by assumption. + rewrite Z.lor_ones_low; auto with zarith. + Qed. + Hint Rewrite lor_pow2_mod_pow2_r using zutil_arith : zsimplify. + Hint Rewrite lor_pow2_mod_pow2_r using assumption : zsimplify_fast. + + Lemma lor_pow2_mod_pow2_l x e (He : 0 <= e) : Z.lor (2^e-1) x mod (2^e) = 2^e-1. + Proof. rewrite Z.lor_comm; apply lor_pow2_mod_pow2_r; assumption. Qed. + Hint Rewrite lor_pow2_mod_pow2_l using zutil_arith : zsimplify. + Hint Rewrite lor_pow2_mod_pow2_l using assumption : zsimplify_fast. + + Lemma lor_pow2_div_pow2_r x e (He : 0 <= e) : (Z.lor x (2^e-1)) / (2^e) = x / 2^e. + Proof. + destruct (Z_zerop e). + { subst; autorewrite with zsimplify_const; reflexivity. } + assert (0 < 2^e) by auto with zarith. + rewrite <- Z.shiftr_div_pow2, Z.shiftr_lor, !Z.shiftr_div_pow2 by lia. + rewrite (Z.div_small (_-1) _), Z.lor_0_r by lia. + reflexivity. + Qed. + Hint Rewrite lor_pow2_div_pow2_r using zutil_arith : zsimplify. + Hint Rewrite lor_pow2_div_pow2_r using assumption : zsimplify_fast. + + Lemma lor_pow2_div_pow2_l x e (He : 0 <= e) : (Z.lor (2^e-1) x) / (2^e) = x / 2^e. + Proof. rewrite Z.lor_comm; apply lor_pow2_div_pow2_r; assumption. Qed. + Hint Rewrite lor_pow2_div_pow2_l using zutil_arith : zsimplify. + Hint Rewrite lor_pow2_div_pow2_l using assumption : zsimplify_fast. End Z. diff --git a/src/Util/ZUtil/Tactics/LinearSubstitute.v b/src/Util/ZUtil/Tactics/LinearSubstitute.v index d03c9d196..960c06ce9 100644 --- a/src/Util/ZUtil/Tactics/LinearSubstitute.v +++ b/src/Util/ZUtil/Tactics/LinearSubstitute.v @@ -12,6 +12,8 @@ Module Z. Proof. omega. Qed. Lemma move_R_Xm x y z : y - x = z -> x = y - z. Proof. omega. Qed. + Lemma move_R_nX x y : -x = y -> x = -y. + Proof. omega. Qed. Lemma move_L_pX x y z : z = x + y -> z - y = x. Proof. omega. Qed. Lemma move_L_mX x y z : z = x - y -> z + y = x. @@ -20,6 +22,8 @@ Module Z. Proof. omega. Qed. Lemma move_L_Xm x y z : z = y - x -> y - z = x. Proof. omega. Qed. + Lemma move_L_nX x y : y = -x -> -y = x. + Proof. omega. Qed. (** [linear_substitute x] attempts to maipulate equations using only addition and subtraction to put [x] on the left, and then @@ -36,6 +40,8 @@ Module Z. | ?a - ?b => first [ contains for_var b; apply move_L_mX in H | contains for_var a; apply move_L_Xm in H ] + | -?a + => contains for_var a; apply move_L_nX in H | for_var => progress symmetry in H end @@ -46,6 +52,8 @@ Module Z. | ?a - ?b => first [ not contains for_var b; apply move_R_mX in H | not contains for_var a; apply move_R_Xm in H ] + | -?a + => contains for_var a; apply move_R_nX in H end ]. Ltac linear_solve_for_in for_var H := repeat linear_solve_for_in_step for_var H. Ltac linear_solve_for for_var := diff --git a/src/Util/ZUtil/ZSimplify/Core.v b/src/Util/ZUtil/ZSimplify/Core.v index c850ddef6..06bc648ff 100644 --- a/src/Util/ZUtil/ZSimplify/Core.v +++ b/src/Util/ZUtil/ZSimplify/Core.v @@ -1,14 +1,16 @@ Require Import Coq.ZArith.ZArith. Require Export Crypto.Util.ZUtil.Hints.Core. -Hint Rewrite Z.div_1_r Z.mul_1_r Z.mul_1_l Z.sub_diag Z.mul_0_r Z.mul_0_l Z.add_0_l Z.add_0_r Z.opp_involutive Z.sub_0_r Z_mod_same_full Z.sub_simpl_r Z.sub_simpl_l Z.add_opp_diag_r Z.add_opp_diag_l Zmod_0_l Z.add_simpl_r Z.add_simpl_l Z.opp_0 Zmod_0_r Zmod_mod Z.mul_succ_l Z.mul_succ_r Z.shiftr_0_r Z.shiftr_0_l Z.mod_1_r Zmod_0_l Zmod_0_r Z.shiftl_0_r Z.shiftl_0_l Z.shiftr_0_r Z.shiftr_0_l Z.sub_diag Zdiv_0_r Zdiv_0_l Z.land_0_l Z.land_0_r Z.lor_0_l Z.lor_0_r Z.lnot_0 Z.land_lnot_diag Z.lnot_involutive Z.lxor_lnot_lnot Z.lnot_m1 Z.lxor_m1_l Z.lxor_m1_r Z.add_lnot_diag Z.lor_lnot_diag Z.pow_0_r Z.pow_0_l Z.pow_1_r : zsimplify_fast. +Hint Rewrite Z.div_1_r Z.mul_1_r Z.mul_1_l Z.sub_diag Z.mul_0_r Z.mul_0_l Z.add_0_l Z.add_0_r Z.opp_involutive Z.sub_0_r Z_mod_same_full Z.sub_simpl_r Z.sub_simpl_l Z.add_opp_diag_r Z.add_opp_diag_l Zmod_0_l Z.add_simpl_r Z.add_simpl_l Z.opp_0 Zmod_0_r Zmod_mod Z.mul_succ_l Z.mul_succ_r Z.shiftr_0_r Z.shiftr_0_l Z.mod_1_r Zmod_0_l Zmod_0_r Z.shiftl_0_r Z.shiftl_0_l Z.shiftr_0_r Z.shiftr_0_l Z.sub_diag Zdiv_0_r Zdiv_0_l Z.land_0_l Z.land_0_r Z.lor_0_l Z.lor_0_r Z.lnot_0 Z.land_lnot_diag Z.lnot_involutive Z.lxor_lnot_lnot Z.lnot_m1 Z.lxor_m1_l Z.lxor_m1_r Z.add_lnot_diag Z.lor_lnot_diag Z.pow_0_r Z.pow_1_r : zsimplify_fast. +Hint Rewrite Z.pow_0_l Z.pow_0_l' using assumption : zsimplify_fast. -Hint Rewrite Z.div_1_r Z.mul_1_r Z.mul_1_l Z.sub_diag Z.mul_0_r Z.mul_0_l Z.add_0_l Z.add_0_r Z.opp_involutive Z.sub_0_r Z_mod_same_full Z.sub_simpl_r Z.sub_simpl_l Z.add_opp_diag_r Z.add_opp_diag_l Zmod_0_l Z.add_simpl_r Z.add_simpl_l Z.opp_0 Zmod_0_r Zmod_mod Z.mul_succ_l Z.mul_succ_r Z.shiftr_0_r Z.shiftr_0_l Z.mod_1_r Zmod_0_l Zmod_0_r Z.shiftl_0_r Z.shiftl_0_l Z.shiftr_0_r Z.shiftr_0_l Zplus_minus Z.add_diag Z.abs_0 Z.sgn_0 Nat2Z.inj_0 Z2Nat.inj_0 Zdiv_0_r Zdiv_0_l Z.land_0_l Z.land_0_r Z.lor_0_l Z.lor_0_r Z.lnot_0 Z.land_lnot_diag Z.lnot_involutive Z.lxor_lnot_lnot Z.lnot_m1 Z.lxor_m1_l Z.lxor_m1_r Z.add_lnot_diag Z.lor_lnot_diag Z.pow_0_r Z.pow_0_l Z.pow_1_r : zsimplify. -Hint Rewrite Z.div_mul Z.div_1_l Z.div_same Z.mod_same Z.div_small Z.mod_small Z.div_add Z.div_add_l Z.mod_add Z.div_0_l Z.mod_mod Z.mod_small Z_mod_zero_opp_full Z.mod_1_l Z.pow_1_l using zutil_arith : zsimplify. +Hint Rewrite Z.div_1_r Z.mul_1_r Z.mul_1_l Z.sub_diag Z.mul_0_r Z.mul_0_l Z.add_0_l Z.add_0_r Z.opp_involutive Z.sub_0_r Z_mod_same_full Z.sub_simpl_r Z.sub_simpl_l Z.add_opp_diag_r Z.add_opp_diag_l Zmod_0_l Z.add_simpl_r Z.add_simpl_l Z.opp_0 Zmod_0_r Zmod_mod Z.mul_succ_l Z.mul_succ_r Z.shiftr_0_r Z.shiftr_0_l Z.mod_1_r Zmod_0_l Zmod_0_r Z.shiftl_0_r Z.shiftl_0_l Z.shiftr_0_r Z.shiftr_0_l Zplus_minus Z.add_diag Z.abs_0 Z.sgn_0 Nat2Z.inj_0 Z2Nat.inj_0 Zdiv_0_r Zdiv_0_l Z.land_0_l Z.land_0_r Z.lor_0_l Z.lor_0_r Z.lnot_0 Z.land_lnot_diag Z.lnot_involutive Z.lxor_lnot_lnot Z.lnot_m1 Z.lxor_m1_l Z.lxor_m1_r Z.add_lnot_diag Z.lor_lnot_diag Z.pow_0_r Z.pow_1_r : zsimplify. +Hint Rewrite Z.div_mul Z.div_1_l Z.div_same Z.mod_same Z.div_small Z.mod_small Z.div_add Z.div_add_l Z.mod_add Z.div_0_l Z.mod_mod Z.mod_small Z_mod_zero_opp_full Z.mod_1_l Z.pow_1_l Z.pow_0_l using zutil_arith : zsimplify. Hint Rewrite <- Z.opp_eq_mul_m1 Z.one_succ Z.two_succ : zsimplify. Hint Rewrite <- Z.div_mod using zutil_arith : zsimplify. Hint Rewrite (fun x y => proj2 (Z.eqb_neq x y)) using assumption : zsimplify. -Hint Rewrite Z.mul_0_l Z.mul_0_r Z.mul_1_l Z.mul_1_r Z.add_0_l Z.add_0_r Z.sub_0_l Z.sub_0_r Zdiv_0_l Zdiv_0_r Zdiv_1_r Zmod_0_l Zmod_0_r Zmod_1_r Z.opp_0 Z.abs_0 Z.sgn_0 Nat2Z.inj_0 Z2Nat.inj_0 Z.land_0_l Z.land_0_r Z.lor_0_l Z.lor_0_r Z.lnot_0 Z.land_lnot_diag Z.lnot_involutive Z.lxor_lnot_lnot Z.lnot_m1 Z.lxor_m1_l Z.lxor_m1_r Z.add_lnot_diag Z.lor_lnot_diag Z.pow_0_r Z.pow_0_l Z.pow_1_r Z.shiftr_0_r Z.shiftr_0_l Z.shiftl_0_r Z.shiftl_0_l : zsimplify_const. +Hint Rewrite Z.mul_0_l Z.mul_0_r Z.mul_1_l Z.mul_1_r Z.add_0_l Z.add_0_r Z.sub_0_l Z.sub_0_r Zdiv_0_l Zdiv_0_r Zdiv_1_r Zmod_0_l Zmod_0_r Zmod_1_r Z.opp_0 Z.abs_0 Z.sgn_0 Nat2Z.inj_0 Z2Nat.inj_0 Z.land_0_l Z.land_0_r Z.lor_0_l Z.lor_0_r Z.lnot_0 Z.land_lnot_diag Z.lnot_involutive Z.lxor_lnot_lnot Z.lnot_m1 Z.lxor_m1_l Z.lxor_m1_r Z.add_lnot_diag Z.lor_lnot_diag Z.pow_0_r Z.pow_1_r Z.shiftr_0_r Z.shiftr_0_l Z.shiftl_0_r Z.shiftl_0_l Z.sub_diag : zsimplify_const. +Hint Rewrite Z.pow_0_l Z.pow_0_l' using assumption : zsimplify_const. Hint Rewrite <- Z.one_succ Z.two_succ : zsimplify_const. diff --git a/src/Util/ZUtil/ZSimplify/Simple.v b/src/Util/ZUtil/ZSimplify/Simple.v index 94fa05e1a..b176c0b21 100644 --- a/src/Util/ZUtil/ZSimplify/Simple.v +++ b/src/Util/ZUtil/ZSimplify/Simple.v @@ -13,6 +13,9 @@ Module Z. Lemma pow_1_l_Zof_nat a : 1^Z.of_nat a = 1. Proof. apply Z.pow_1_l; lia. Qed. Hint Rewrite pow_1_l_Zof_nat : zsimplify_fast zsimplify_const zsimplify. + Lemma pow_0_l_Zpos a : 0^Zpos a = 0. + Proof. apply Z.pow_0_l; lia. Qed. + Hint Rewrite pow_0_l_Zpos : zsimplify_fast zsimplify_const zsimplify. Lemma sub_same_minus (x y : Z) : x - (x - y) = y. Proof. lia. Qed. |