| 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.Pow. |
| 5 |
Require Import Crypto.Util.ZUtil.Div. |
| 6 |
Require Import Crypto.Util.ZUtil.LandLorShiftBounds. |
| 7 |
Require Import Crypto.Util.ZUtil.TruncatingShiftl. |
| 8 |
Require Import Crypto.Util.ZUtil.Notations. |
| 9 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 10 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 11 |
Local Open Scope Z_scope. |
| 12 |
|
| 13 |
(** Extension of lia to solve ranges involving bitwise operations and power *) |
| 14 |
|
| 15 |
Module Z. |
| 16 |
|
| 17 |
Ltac solve_range := |
| 18 |
repeat match goal with |
| 19 |
| [ |- context[?a ^ ?b] ] => unique assert (0 <= a ^ b) by (apply Z.pow_nonneg; lia) |
| 20 |
| [ |- context[?a ^ ?b] ] => unique assert (0 < a ^ b) by (apply Z.pow_pos_nonneg; lia) |
| 21 |
| [ |- ?a &' ?b < _ ] => unique assert (a &' b <= a) by (apply Z.land_upper_bound_l; lia) |
| 22 |
| [ |- ?a &' ?b < _ ] => unique assert (a &' b <= b) by (apply Z.land_upper_bound_r; lia) |
| 23 |
| [ |- _ /\ _ ] => split |
| 24 |
| [ |- _ <= _ < _ ] => split |
| 25 |
| [ |- _ / _ < _ ] => apply Z.div_lt_upper_bound |
| 26 |
| [ |- _ |' _ < 2 ^ _ ] => apply Z.lor_range |
| 27 |
| [ |- Z.truncating_shiftl _ _ _ < 2 ^ _ ] => apply Z.truncating_shiftl_range |
| 28 |
| _ => progress Z.zero_bounds |
| 29 |
| [ |- 0 <= Z.truncating_shiftl _ _ _ ] => apply Z.truncating_shiftl_nonneg |
| 30 |
| [ |- context[ ?a ^ _ * ?a] ] => rewrite (Z.mul_comm _ a), Z.pow_mul_base |
| 31 |
| [ |- context[ ?a * ?a ^ _] ] => rewrite Z.pow_mul_base |
| 32 |
| [ |- context[ _ - ?a + ?a] ] => rewrite Z.sub_simpl_r |
| 33 |
| [ |- context[ _ >> _] ] => rewrite Z.shiftr_div_pow2 by lia |
| 34 |
| [ |- context[ _ >> _] ] => rewrite Z.shiftr_mul_pow2 by lia |
| 35 |
| [ |- context[ _ << _] ] => rewrite Z.shiftl_mul_pow2 by lia |
| 36 |
| [ |- context[ _ << _] ] => rewrite Z.shiftl_div_pow2 by lia |
| 37 |
| [ |- context[ ?a ^ ?b * ?a ^ ?c ] ] => |
| 38 |
let Hn := fresh in |
| 39 |
let Hm := fresh in |
| 40 |
rewrite <- Z.pow_add_r; |
| 41 |
assert (Hn : a ^ b <= a ^ (b + c)) by (apply Z.pow_le_mono_r; lia); |
| 42 |
assert (Hm : a ^ c <= a ^ (b + c)) by (apply Z.pow_le_mono_r; lia); |
| 43 |
ring_simplify (b + c) in Hn; |
| 44 |
ring_simplify (b + c) in Hm; |
| 45 |
ring_simplify (b + c) |
| 46 |
| [ |- context[ Z.ones_from _ _ ] ] => unfold Z.ones_from |
| 47 |
| [ |- context[ Z.ones_at _ _ ] ] => unfold Z.ones_at |
| 48 |
| [ |- context[ Z.ones _ ] ] => rewrite Z.ones_equiv, <- Z.sub_1_r; ring_simplify |
| 49 |
| [ |- context[ Z.pred _ ] ] => rewrite <- Z.sub_1_r; ring_simplify |
| 50 |
| [ |- _ ] => nia |
| 51 |
end. |
| 52 |
|
| 53 |
End Z. |