| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import Lia. |
| 3 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 4 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 5 |
Local Open Scope Z_scope. |
| 6 |
|
| 7 |
Module Z. |
| 8 |
Lemma positive_is_nonzero : forall x, x > 0 -> x <> 0. |
| 9 |
Proof. intros; lia. Qed. |
| 10 |
#[global] |
| 11 |
Hint Resolve positive_is_nonzero : zarith. |
| 12 |
|
| 13 |
Lemma le_lt_trans n m p : n <= m -> m < p -> n < p. |
| 14 |
Proof. lia. Qed. |
| 15 |
|
| 16 |
Lemma le_fold_right_max : forall low l x, (forall y, List.In y l -> low <= y) -> |
| 17 |
List.In x l -> x <= List.fold_right Z.max low l. |
| 18 |
Proof. |
| 19 |
induction l as [|a l IHl]; intros ? lower_bound In_list; [cbv [List.In] in *; intuition | ]. |
| 20 |
simpl. |
| 21 |
destruct (List.in_inv In_list); subst. |
| 22 |
+ apply Z.le_max_l. |
| 23 |
+ etransitivity. |
| 24 |
- apply IHl; auto; intuition auto with datatypes. |
| 25 |
- apply Z.le_max_r. |
| 26 |
Qed. |
| 27 |
|
| 28 |
Lemma le_fold_right_max_initial : forall low l, low <= List.fold_right Z.max low l. |
| 29 |
Proof. |
| 30 |
induction l as [|a l IHl]; intros; try reflexivity. |
| 31 |
etransitivity; [ apply IHl | apply Z.le_max_r ]. |
| 32 |
Qed. |
| 33 |
|
| 34 |
Lemma add_compare_mono_r: forall n m p, (n + p ?= m + p) = (n ?= m). |
| 35 |
Proof. |
| 36 |
intros n m p. |
| 37 |
rewrite <-!(Z.add_comm p). |
| 38 |
apply Z.add_compare_mono_l. |
| 39 |
Qed. |
| 40 |
|
| 41 |
Lemma leb_add_same x y : (x <=? y + x) = (0 <=? y). |
| 42 |
Proof. destruct (x <=? y + x) eqn:?, (0 <=? y) eqn:?; Z.ltb_to_lt; try reflexivity; lia. Qed. |
| 43 |
#[global] |
| 44 |
Hint Rewrite leb_add_same : zsimplify. |
| 45 |
|
| 46 |
Lemma ltb_add_same x y : (x <? y + x) = (0 <? y). |
| 47 |
Proof. destruct (x <? y + x) eqn:?, (0 <? y) eqn:?; Z.ltb_to_lt; try reflexivity; lia. Qed. |
| 48 |
#[global] |
| 49 |
Hint Rewrite ltb_add_same : zsimplify. |
| 50 |
|
| 51 |
Lemma geb_add_same x y : (x >=? y + x) = (0 >=? y). |
| 52 |
Proof. destruct (x >=? y + x) eqn:?, (0 >=? y) eqn:?; Z.ltb_to_lt; try reflexivity; lia. Qed. |
| 53 |
#[global] |
| 54 |
Hint Rewrite geb_add_same : zsimplify. |
| 55 |
|
| 56 |
Lemma gtb_add_same x y : (x >? y + x) = (0 >? y). |
| 57 |
Proof. destruct (x >? y + x) eqn:?, (0 >? y) eqn:?; Z.ltb_to_lt; try reflexivity; lia. Qed. |
| 58 |
#[global] |
| 59 |
Hint Rewrite gtb_add_same : zsimplify. |
| 60 |
|
| 61 |
Lemma sub_pos_bound a b X : 0 <= a < X -> 0 <= b < X -> -X < a - b < X. |
| 62 |
Proof. lia. Qed. |
| 63 |
|
| 64 |
Lemma le_sub_1_iff x y : x <= y - 1 <-> x < y. |
| 65 |
Proof. lia. Qed. |
| 66 |
|
| 67 |
Lemma le_add_1_iff x y : x + 1 <= y <-> x < y. |
| 68 |
Proof. lia. Qed. |
| 69 |
End Z. |