| 1 |
From Coq Require Import ZArith Lia. |
| 2 |
From Coq Require Import Znumtheory. |
| 3 |
Require Import Crypto.Util.ZUtil.Tactics.CompareToSgn. |
| 4 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 5 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 6 |
Require Import Crypto.Util.ZUtil.Le. |
| 7 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 8 |
Require Import Crypto.Util.ZUtil.Hints.ZArith. |
| 9 |
Require Import Crypto.Util.ZUtil.Hints.PullPush. |
| 10 |
Require Import Crypto.Util.ZUtil.Hints. |
| 11 |
Require Import Crypto.Util.ZUtil.ZSimplify.Core. |
| 12 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 13 |
Require Import Crypto.Util.ZUtil.Pow. |
| 14 |
Local Open Scope Z_scope. |
| 15 |
|
| 16 |
Module Z. |
| 17 |
Lemma div_mul' : forall a b : Z, b <> 0 -> (b * a) / b = a. |
| 18 |
Proof. intros. rewrite Z.mul_comm. apply Z.div_mul; auto. Qed. |
| 19 |
#[global] |
| 20 |
Hint Rewrite div_mul' using zutil_arith : zsimplify. |
| 21 |
|
| 22 |
Local Ltac replace_to_const c := |
| 23 |
repeat match goal with |
| 24 |
| [ H : ?x = ?x |- _ ] => clear H |
| 25 |
| [ H : ?x = c, H' : context[?x] |- _ ] => rewrite H in H' |
| 26 |
| [ H : c = ?x, H' : context[?x] |- _ ] => rewrite <- H in H' |
| 27 |
| [ H : ?x = c |- context[?x] ] => rewrite H |
| 28 |
| [ H : c = ?x |- context[?x] ] => rewrite <- H |
| 29 |
end. |
| 30 |
|
| 31 |
Lemma lt_div_0 n m : n / m < 0 <-> ((n < 0 < m \/ m < 0 < n) /\ 0 < -(n / m)). |
| 32 |
Proof. |
| 33 |
Z.compare_to_sgn; rewrite Z.sgn_opp; simpl. |
| 34 |
pose proof (Zdiv_sgn n m) as H. |
| 35 |
pose proof (Z.sgn_spec (n / m)) as H'. |
| 36 |
repeat first [ progress intuition auto |
| 37 |
| progress simpl in * |
| 38 |
| congruence |
| 39 |
| lia |
| 40 |
| progress replace_to_const (-1) |
| 41 |
| progress replace_to_const 0 |
| 42 |
| progress replace_to_const 1 |
| 43 |
| match goal with |
| 44 |
| [ x : Z |- _ ] => destruct x |
| 45 |
end ]. |
| 46 |
Qed. |
| 47 |
|
| 48 |
Lemma div_add' a b c : c <> 0 -> (a + c * b) / c = a / c + b. |
| 49 |
Proof. intro; rewrite <- Z.div_add, (Z.mul_comm c); try lia. Qed. |
| 50 |
Lemma div_add_l' a b c : b <> 0 -> (b * a + c) / b = a + c / b. |
| 51 |
Proof. intro; rewrite <- Z.div_add_l, (Z.mul_comm b); lia. Qed. |
| 52 |
#[global] |
| 53 |
Hint Rewrite Z.div_add' Z.div_add_l' using zutil_arith : push_Zdiv. |
| 54 |
#[global] |
| 55 |
Hint Rewrite <- Z.div_add' Z.div_add_l' using zutil_arith : pull_Zdiv. |
| 56 |
#[global] |
| 57 |
Hint Rewrite div_add_l' div_add' using zutil_arith : zsimplify. |
| 58 |
|
| 59 |
Lemma div_sub a b c : c <> 0 -> (a - b * c) / c = a / c - b. |
| 60 |
Proof. intros; rewrite <- !Z.add_opp_r, <- Z.div_add by lia; apply f_equal2; lia. Qed. |
| 61 |
|
| 62 |
Lemma div_sub' a b c : c <> 0 -> (a - c * b) / c = a / c - b. |
| 63 |
Proof. intro; rewrite <- div_sub, (Z.mul_comm c); try lia. Qed. |
| 64 |
|
| 65 |
#[global] |
| 66 |
Hint Rewrite div_sub div_sub' using zutil_arith : zsimplify. |
| 67 |
|
| 68 |
Lemma div_add_sub_l a b c d : b <> 0 -> (a * b + c - d) / b = a + (c - d) / b. |
| 69 |
Proof. rewrite <- Z.add_sub_assoc; apply Z.div_add_l. Qed. |
| 70 |
|
| 71 |
Lemma div_add_sub_l' a b c d : b <> 0 -> (b * a + c - d) / b = a + (c - d) / b. |
| 72 |
Proof. rewrite <- Z.add_sub_assoc; apply Z.div_add_l'. Qed. |
| 73 |
|
| 74 |
Lemma div_add_sub a b c d : c <> 0 -> (a + b * c - d) / c = (a - d) / c + b. |
| 75 |
Proof. rewrite (Z.add_comm _ (_ * _)), (Z.add_comm (_ / _)); apply Z.div_add_sub_l. Qed. |
| 76 |
|
| 77 |
Lemma div_add_sub' a b c d : c <> 0 -> (a + c * b - d) / c = (a - d) / c + b. |
| 78 |
Proof. rewrite (Z.add_comm _ (_ * _)), (Z.add_comm (_ / _)); apply Z.div_add_sub_l'. Qed. |
| 79 |
|
| 80 |
#[global] |
| 81 |
Hint Rewrite Z.div_add_sub Z.div_add_sub' Z.div_add_sub_l Z.div_add_sub_l' using zutil_arith : zsimplify. |
| 82 |
|
| 83 |
Lemma div_mul_skip a b k : 0 < b -> 0 < k -> a * b / k / b = a / k. |
| 84 |
Proof. |
| 85 |
intros; rewrite Z.div_div, (Z.mul_comm k), <- Z.div_div by lia. |
| 86 |
autorewrite with zsimplify. reflexivity. |
| 87 |
Qed. |
| 88 |
|
| 89 |
Lemma div_mul_skip' a b k : 0 < b -> 0 < k -> b * a / k / b = a / k. |
| 90 |
Proof. |
| 91 |
intros; rewrite Z.div_div, (Z.mul_comm k), <- Z.div_div by lia. |
| 92 |
autorewrite with zsimplify; reflexivity. |
| 93 |
Qed. |
| 94 |
|
| 95 |
#[global] |
| 96 |
Hint Rewrite Z.div_mul_skip Z.div_mul_skip' using zutil_arith : zsimplify. |
| 97 |
|
| 98 |
Lemma div_mul_skip_pow base e0 e1 x y : 0 < y -> 0 < base -> 0 <= e1 <= e0 -> x * base^e0 / y / base^e1 = x * base^(e0 - e1) / y. |
| 99 |
Proof. |
| 100 |
intros. |
| 101 |
assert (0 < base^e1) by auto with zarith. |
| 102 |
replace (base^e0) with (base^(e0 - e1) * base^e1) by (autorewrite with pull_Zpow zsimplify; reflexivity). |
| 103 |
rewrite !Z.mul_assoc. |
| 104 |
autorewrite with zsimplify; lia. |
| 105 |
Qed. |
| 106 |
#[global] |
| 107 |
Hint Rewrite div_mul_skip_pow using zutil_arith : zsimplify. |
| 108 |
|
| 109 |
Lemma div_mul_skip_pow' base e0 e1 x y : 0 < y -> 0 < base -> 0 <= e1 <= e0 -> base^e0 * x / y / base^e1 = base^(e0 - e1) * x / y. |
| 110 |
Proof. |
| 111 |
intros. |
| 112 |
rewrite (Z.mul_comm (base^e0) x), div_mul_skip_pow by lia. |
| 113 |
auto using f_equal2 with lia. |
| 114 |
Qed. |
| 115 |
#[global] |
| 116 |
Hint Rewrite div_mul_skip_pow' using zutil_arith : zsimplify. |
| 117 |
|
| 118 |
Lemma div_le_mono_nonneg a b c : 0 <= c -> a <= b -> a / c <= b / c. |
| 119 |
Proof. |
| 120 |
destruct (Z_zerop c). |
| 121 |
{ subst; simpl; autorewrite with zsimplify; reflexivity. } |
| 122 |
{ intros; apply Z.div_le_mono; lia. } |
| 123 |
Qed. |
| 124 |
#[global] |
| 125 |
Hint Resolve div_le_mono_nonneg : zarith. |
| 126 |
|
| 127 |
Lemma div_le_mono_pow_pos a b c e : a <= b -> a / Z.pos c ^ e <= b / Z.pos c ^ e. |
| 128 |
Proof. auto with zarith. Qed. |
| 129 |
|
| 130 |
Lemma div_nonneg a b : 0 <= a -> 0 <= b -> 0 <= a / b. |
| 131 |
Proof. |
| 132 |
destruct (Z_zerop b); subst; rewrite ?Zdiv_0_r; [ reflexivity | ]. |
| 133 |
intros; apply Z.div_pos; lia. |
| 134 |
Qed. |
| 135 |
#[global] |
| 136 |
Hint Resolve div_nonneg : zarith. |
| 137 |
|
| 138 |
Lemma div_add_exact x y d : d <> 0 -> x mod d = 0 -> (x + y) / d = x / d + y / d. |
| 139 |
Proof. |
| 140 |
intros; rewrite (Z_div_exact_full_2 x d) at 1 by assumption. |
| 141 |
rewrite Z.div_add_l' by assumption; lia. |
| 142 |
Qed. |
| 143 |
#[global] |
| 144 |
Hint Rewrite div_add_exact using zutil_arith : zsimplify. |
| 145 |
|
| 146 |
Lemma Z_divide_div_mul_exact' a b c : b <> 0 -> (b | a) -> a * c / b = c * (a / b). |
| 147 |
Proof. intros. rewrite Z.mul_comm. auto using Z.divide_div_mul_exact. Qed. |
| 148 |
|
| 149 |
Lemma div_sub_mod_exact a b : b <> 0 -> a / b = (a - a mod b) / b. |
| 150 |
Proof. |
| 151 |
intro. |
| 152 |
rewrite (Z.div_mod a b) at 2 by lia. |
| 153 |
autorewrite with zsimplify. |
| 154 |
reflexivity. |
| 155 |
Qed. |
| 156 |
|
| 157 |
Lemma div_sub_mod_cond x y d |
| 158 |
: d <> 0 |
| 159 |
-> (x - y) / d |
| 160 |
= x / d + ((x mod d - y) / d). |
| 161 |
Proof. clear. |
| 162 |
intro. |
| 163 |
replace (x - y) with ((x - x mod d) + (x mod d - y)) by lia. |
| 164 |
rewrite Z.div_add_exact by (autorewrite with pull_Zmod zsimplify; auto). |
| 165 |
rewrite <- Z.div_sub_mod_exact by lia; lia. |
| 166 |
Qed. |
| 167 |
#[global] |
| 168 |
Hint Resolve div_sub_mod_cond : zarith. |
| 169 |
|
| 170 |
Lemma div_add_mod_cond_l : forall x y d, d <> 0 -> (x + y) / d = (x mod d + y) / d + x / d. |
| 171 |
Proof. |
| 172 |
intros. replace (x + y) with ((x - x mod d) + (x mod d + y)) by lia. |
| 173 |
rewrite Z.div_add_exact by (autorewrite with pull_Zmod zsimplify; auto). |
| 174 |
rewrite <- Z.div_sub_mod_exact by lia; lia. |
| 175 |
Qed. |
| 176 |
|
| 177 |
Lemma div_add_mod_cond_r : forall x y d, d <> 0 -> (x + y) / d = (x + y mod d) / d + y / d. |
| 178 |
Proof. |
| 179 |
intros. rewrite Z.add_comm, div_add_mod_cond_l by auto. repeat (f_equal; try ring). |
| 180 |
Qed. |
| 181 |
|
| 182 |
Lemma div_le_zero x y : 0 < y -> x / y <= 0 -> x < y. |
| 183 |
Proof. |
| 184 |
clear. intros. |
| 185 |
apply Z.nle_gt; intro. |
| 186 |
pose proof (Z.div_str_pos x y ltac:(lia)). lia. |
| 187 |
Qed. |
| 188 |
|
| 189 |
Lemma div_between_full n a b : |
| 190 |
0 < b -> n * b <= a < (1 + n) * b -> |
| 191 |
a / b = n. |
| 192 |
Proof. |
| 193 |
intros. |
| 194 |
pose proof (Z.div_le_lower_bound a b n ltac:(lia) ltac:(lia)). |
| 195 |
pose proof (Z.div_lt_upper_bound a b (n+1) ltac:(lia) ltac:(lia)). |
| 196 |
lia. |
| 197 |
Qed. |
| 198 |
|
| 199 |
Lemma mod_small_n_neg n a b : n < 0 -> 0 < b -> n * b <= a < (1 + n) * b -> |
| 200 |
a mod b = a - n * b. |
| 201 |
Proof. |
| 202 |
intros. rewrite Z.mod_eq, div_between_full with (n:=n) by lia. ring. |
| 203 |
Qed. |
| 204 |
|
| 205 |
Lemma div_div_comm : forall x y z, 0 < y -> 0 < z -> x / y / z = x / z / y. |
| 206 |
Proof. |
| 207 |
intros; rewrite !Z.div_div by lia. |
| 208 |
f_equal; ring. |
| 209 |
Qed. |
| 210 |
|
| 211 |
Lemma div_lt_upper_bound' a b q : 0 < b -> a < q * b -> a / b < q. |
| 212 |
Proof. intros; apply Z.div_lt_upper_bound; nia. Qed. |
| 213 |
#[global] |
| 214 |
Hint Resolve div_lt_upper_bound' : zarith. |
| 215 |
|
| 216 |
Lemma div_cross_le_abs a b c' d : c' <> 0 -> d <> 0 -> a * Z.sgn c' * Z.abs d <= b * Z.sgn d * Z.abs c' -> a / c' <= b / d. |
| 217 |
Proof. |
| 218 |
clear. |
| 219 |
destruct c', d; cbn [Z.abs Z.sgn]; |
| 220 |
rewrite ?Zdiv_0_r, ?Z.mul_0_r, ?Z.mul_0_l, ?Z.mul_1_l, ?Z.mul_1_r; |
| 221 |
try lia; intros ?? H; |
| 222 |
Z.div_mod_to_quot_rem_in_goal; |
| 223 |
subst. |
| 224 |
all: repeat match goal with |
| 225 |
| [ H : context[_ * -1] |- _ ] => rewrite (Z.mul_add_distr_r _ _ (-1)), <- ?(Z.mul_comm (-1)), ?Z.mul_assoc in H |
| 226 |
| [ H : context[-1 * _] |- _ ] => rewrite (Z.mul_add_distr_l (-1)), <- ?(Z.mul_comm (-1)), ?Z.mul_assoc in H |
| 227 |
| [ H : context[-1 * Z.neg ?x] |- _ ] => rewrite (Z.mul_comm (-1) (Z.neg x)), <- Z.opp_eq_mul_m1 in H |
| 228 |
| [ H : context[-1 * ?x] |- _ ] => rewrite (Z.mul_comm (-1) x), <- Z.opp_eq_mul_m1 in H |
| 229 |
| [ H : context[-Z.neg _] |- _ ] => cbn [Z.opp] in H |
| 230 |
end. |
| 231 |
all:lazymatch goal with |
| 232 |
| [ H : (Z.pos ?p * ?q + ?r) * Z.pos ?p' <= (Z.pos ?p' * ?q' + ?r') * Z.pos ?p |- _ ] |
| 233 |
=> let H' := fresh in |
| 234 |
assert (H' : q <= q' + (r' * Z.pos p - r * Z.pos p') / (Z.pos p * Z.pos p')) by (Z.div_mod_to_quot_rem_in_goal; nia); |
| 235 |
revert H' |
| 236 |
end. |
| 237 |
all:Z.div_mod_to_quot_rem_in_goal; nia. |
| 238 |
Qed. |
| 239 |
|
| 240 |
Lemma div_positive_gt_0 : forall a b, a > 0 -> b > 0 -> a mod b = 0 -> |
| 241 |
a / b > 0. |
| 242 |
Proof. |
| 243 |
intros; rewrite Z.gt_lt_iff. |
| 244 |
apply Z.div_str_pos. |
| 245 |
split; intuition auto with lia. |
| 246 |
apply Z.divide_pos_le; try (apply Zmod_divide); lia. |
| 247 |
Qed. |
| 248 |
|
| 249 |
Lemma div_opp_l_complete a b (Hb : b <> 0) : -a/b = -(a/b) - (if Z_zerop (a mod b) then 0 else 1). |
| 250 |
Proof. |
| 251 |
destruct (Z_zerop (a mod b)); autorewrite with zsimplify push_Zopp; reflexivity. |
| 252 |
Qed. |
| 253 |
|
| 254 |
Lemma div_opp_l_complete' a b (Hb : b <> 0) : -(a/b) = -a/b + (if Z_zerop (a mod b) then 0 else 1). |
| 255 |
Proof. |
| 256 |
destruct (Z_zerop (a mod b)); autorewrite with zsimplify pull_Zopp; lia. |
| 257 |
Qed. |
| 258 |
|
| 259 |
#[global] |
| 260 |
Hint Rewrite Z.div_opp_l_complete using zutil_arith : pull_Zopp. |
| 261 |
#[global] |
| 262 |
Hint Rewrite Z.div_opp_l_complete' using zutil_arith : push_Zopp. |
| 263 |
|
| 264 |
Lemma div_opp a : a <> 0 -> -a / a = -1. |
| 265 |
Proof. |
| 266 |
intros; autorewrite with pull_Zopp zsimplify; lia. |
| 267 |
Qed. |
| 268 |
|
| 269 |
#[global] |
| 270 |
Hint Rewrite Z.div_opp using zutil_arith : zsimplify. |
| 271 |
|
| 272 |
Lemma div_sub_1_0 x : x > 0 -> (x - 1) / x = 0. |
| 273 |
Proof. auto with zarith lia. Qed. |
| 274 |
|
| 275 |
#[global] |
| 276 |
Hint Rewrite div_sub_1_0 using zutil_arith : zsimplify. |
| 277 |
|
| 278 |
Lemma div_same' a b : b <> 0 -> a = b -> a / b = 1. |
| 279 |
Proof. |
| 280 |
intros; subst; auto with zarith. |
| 281 |
Qed. |
| 282 |
#[global] |
| 283 |
Hint Resolve div_same' : zarith. |
| 284 |
|
| 285 |
Lemma div_opp_r a b : a / (-b) = ((-a) / b). |
| 286 |
Proof. Z.div_mod_to_quot_rem; nia. Qed. |
| 287 |
#[global] |
| 288 |
Hint Resolve div_opp_r : zarith. |
| 289 |
|
| 290 |
Lemma div_floor : forall a b c, 0 < b -> a < b * (Z.succ c) -> a / b <= c. |
| 291 |
Proof. |
| 292 |
intros. |
| 293 |
apply Z.lt_succ_r. |
| 294 |
apply Z.div_lt_upper_bound; try lia. |
| 295 |
Qed. |
| 296 |
|
| 297 |
Lemma mul_div_le x y z |
| 298 |
(Hx : 0 <= x) (Hy : 0 <= y) (Hz : 0 < z) |
| 299 |
(Hyz : y <= z) |
| 300 |
: x * y / z <= x. |
| 301 |
Proof. |
| 302 |
transitivity (x * z / z); [ | rewrite Z.div_mul by lia; lia ]. |
| 303 |
apply Z_div_le; nia. |
| 304 |
Qed. |
| 305 |
#[global] |
| 306 |
Hint Resolve mul_div_le : zarith. |
| 307 |
|
| 308 |
Lemma div_mul_diff_exact a b c |
| 309 |
(Ha : 0 <= a) (Hb : 0 < b) (Hc : 0 <= c) |
| 310 |
: c * a / b = c * (a / b) + (c * (a mod b)) / b. |
| 311 |
Proof. |
| 312 |
rewrite (Z_div_mod_eq_full a b) at 1. |
| 313 |
rewrite Z.mul_add_distr_l. |
| 314 |
replace (c * (b * (a / b))) with ((c * (a / b)) * b) by lia. |
| 315 |
rewrite Z.div_add_l by lia. |
| 316 |
lia. |
| 317 |
Qed. |
| 318 |
|
| 319 |
Lemma div_mul_diff_exact' a b c |
| 320 |
(Ha : 0 <= a) (Hb : 0 < b) (Hc : 0 <= c) |
| 321 |
: c * (a / b) = c * a / b - (c * (a mod b)) / b. |
| 322 |
Proof. |
| 323 |
rewrite div_mul_diff_exact by assumption; lia. |
| 324 |
Qed. |
| 325 |
|
| 326 |
Lemma div_mul_diff_exact'' a b c |
| 327 |
(Ha : 0 <= a) (Hb : 0 < b) (Hc : 0 <= c) |
| 328 |
: a * c / b = (a / b) * c + (c * (a mod b)) / b. |
| 329 |
Proof. |
| 330 |
rewrite (Z.mul_comm a c), div_mul_diff_exact by lia; lia. |
| 331 |
Qed. |
| 332 |
|
| 333 |
Lemma div_mul_diff_exact''' a b c |
| 334 |
(Ha : 0 <= a) (Hb : 0 < b) (Hc : 0 <= c) |
| 335 |
: (a / b) * c = a * c / b - (c * (a mod b)) / b. |
| 336 |
Proof. |
| 337 |
rewrite (Z.mul_comm a c), div_mul_diff_exact by lia; lia. |
| 338 |
Qed. |
| 339 |
|
| 340 |
Lemma div_mul_diff a b c |
| 341 |
(Ha : 0 <= a) (Hb : 0 < b) (Hc : 0 <= c) |
| 342 |
: c * a / b - c * (a / b) <= c. |
| 343 |
Proof. |
| 344 |
rewrite div_mul_diff_exact by assumption. |
| 345 |
ring_simplify; auto with zarith. |
| 346 |
Qed. |
| 347 |
|
| 348 |
Lemma div_mul_le_le a b c |
| 349 |
: 0 <= a -> 0 < b -> 0 <= c -> c * (a / b) <= c * a / b <= c * (a / b) + c. |
| 350 |
Proof. |
| 351 |
pose proof (Z.div_mul_diff a b c); split; try apply Z.div_mul_le; lia. |
| 352 |
Qed. |
| 353 |
|
| 354 |
Lemma div_mul_le_le_offset a b c |
| 355 |
: 0 <= a -> 0 < b -> 0 <= c -> c * a / b - c <= c * (a / b). |
| 356 |
Proof. |
| 357 |
pose proof (Z.div_mul_le_le a b c); lia. |
| 358 |
Qed. |
| 359 |
#[global] |
| 360 |
Hint Resolve div_mul_le_le_offset : zarith. |
| 361 |
|
| 362 |
Lemma div_x_y_x x y : 0 < x -> 0 < y -> x / y / x = 1 / y. |
| 363 |
Proof. |
| 364 |
intros; rewrite Z.div_div, (Z.mul_comm y x), <- Z.div_div, Z.div_same by lia. |
| 365 |
reflexivity. |
| 366 |
Qed. |
| 367 |
#[global] |
| 368 |
Hint Rewrite div_x_y_x using zutil_arith : zsimplify. |
| 369 |
|
| 370 |
Lemma sub_pos_bound_div a b X : 0 <= a < X -> 0 <= b < X -> -1 <= (a - b) / X <= 0. |
| 371 |
Proof. |
| 372 |
intros H0 H1; pose proof (Z.sub_pos_bound a b X H0 H1). |
| 373 |
assert (Hn : -X <= a - b) by lia. |
| 374 |
assert (Hp : a - b <= X - 1) by lia. |
| 375 |
split; etransitivity; [ | apply Z_div_le, Hn; lia | apply Z_div_le, Hp; lia | ]; |
| 376 |
autorewrite with zsimplify; try reflexivity. |
| 377 |
Qed. |
| 378 |
|
| 379 |
#[global] |
| 380 |
Hint Extern 2 => simple apply (fun a b X H0 H1 => proj1 (Z.sub_pos_bound_div a b X H0 H1)) : zarith. |
| 381 |
#[global] |
| 382 |
Hint Extern 2 => simple apply (fun a b X H0 H1 => proj1 (Z.sub_pos_bound_div a b X H0 H1)) : zarith. |
| 383 |
|
| 384 |
Lemma sub_pos_bound_div_eq a b X : 0 <= a < X -> 0 <= b < X -> (a - b) / X = if a <? b then -1 else 0. |
| 385 |
Proof. |
| 386 |
intros H0 H1; pose proof (Z.sub_pos_bound_div a b X H0 H1). |
| 387 |
destruct (a <? b) eqn:?; Z.ltb_to_lt. |
| 388 |
{ cut ((a - b) / X <> 0); [ lia | ]. |
| 389 |
autorewrite with zstrip_div; auto with zarith lia. } |
| 390 |
{ autorewrite with zstrip_div; auto with zarith lia. } |
| 391 |
Qed. |
| 392 |
|
| 393 |
Lemma add_opp_pos_bound_div_eq a b X : 0 <= a < X -> 0 <= b < X -> (-b + a) / X = if a <? b then -1 else 0. |
| 394 |
Proof. |
| 395 |
rewrite !(Z.add_comm (-_)), !Z.add_opp_r. |
| 396 |
apply Z.sub_pos_bound_div_eq. |
| 397 |
Qed. |
| 398 |
|
| 399 |
#[global] |
| 400 |
Hint Rewrite Z.sub_pos_bound_div_eq Z.add_opp_pos_bound_div_eq using zutil_arith : zstrip_div. |
| 401 |
|
| 402 |
Lemma div_small_sym a b : 0 <= a < b -> 0 = a / b. |
| 403 |
Proof. intros; symmetry; apply Z.div_small; assumption. Qed. |
| 404 |
#[global] |
| 405 |
Hint Resolve div_small_sym : zarith. |
| 406 |
|
| 407 |
Lemma mod_eq_le_div_1 a b : 0 < a <= b -> a mod b = 0 -> a / b = 1. |
| 408 |
Proof. intros; Z.div_mod_to_quot_rem; nia. Qed. |
| 409 |
#[global] |
| 410 |
Hint Resolve mod_eq_le_div_1 : zarith. |
| 411 |
#[global] |
| 412 |
Hint Rewrite mod_eq_le_div_1 using zutil_arith : zsimplify. |
| 413 |
|
| 414 |
Lemma div_small_neg x y : 0 < -x <= y -> x / y = -1. |
| 415 |
Proof. intros; Z.div_mod_to_quot_rem; nia. Qed. |
| 416 |
#[global] |
| 417 |
Hint Rewrite div_small_neg using zutil_arith : zsimplify. |
| 418 |
|
| 419 |
Lemma div_sub_small x y z : 0 <= x < z -> 0 <= y <= z -> (x - y) / z = if x <? y then -1 else 0. |
| 420 |
Proof. |
| 421 |
pose proof (Zlt_cases x y). |
| 422 |
(destruct (x <? y) eqn:?); |
| 423 |
intros; autorewrite with zsimplify; try lia. |
| 424 |
Qed. |
| 425 |
#[global] |
| 426 |
Hint Rewrite div_sub_small using zutil_arith : zsimplify. |
| 427 |
|
| 428 |
Lemma mul_div_lt_by_le x y z b : 0 <= y < z -> 0 <= x < b -> x * y / z < b. |
| 429 |
Proof. |
| 430 |
intros [? ?] [? ?]; eapply Z.le_lt_trans; [ | eassumption ]. |
| 431 |
auto with zarith. |
| 432 |
Qed. |
| 433 |
#[global] |
| 434 |
Hint Resolve mul_div_lt_by_le : zarith. |
| 435 |
|
| 436 |
Definition mul_div_le' |
| 437 |
:= fun x y z w p H0 H1 H2 H3 => @Z.le_trans _ _ w (@Z.mul_div_le x y z H0 H1 H2 H3) p. |
| 438 |
#[global] |
| 439 |
Hint Resolve mul_div_le' : zarith. |
| 440 |
Lemma mul_div_le'' x y z w : y <= w -> 0 <= x -> 0 <= y -> 0 < z -> x <= z -> x * y / z <= w. |
| 441 |
Proof. |
| 442 |
rewrite (Z.mul_comm x y); intros; apply mul_div_le'; assumption. |
| 443 |
Qed. |
| 444 |
#[global] |
| 445 |
Hint Resolve mul_div_le'' : zarith. |
| 446 |
|
| 447 |
Lemma div_between n a b : 0 <= n -> b <> 0 -> n * b <= a < (1 + n) * b -> a / b = n. |
| 448 |
Proof. intros; Z.div_mod_to_quot_rem_in_goal; nia. Qed. |
| 449 |
#[global] |
| 450 |
Hint Rewrite div_between using zutil_arith : zsimplify. |
| 451 |
|
| 452 |
Lemma div_between_1 a b : b <> 0 -> b <= a < 2 * b -> a / b = 1. |
| 453 |
Proof. intros; rewrite (div_between 1) by lia; reflexivity. Qed. |
| 454 |
#[global] |
| 455 |
Hint Rewrite div_between_1 using zutil_arith : zsimplify. |
| 456 |
|
| 457 |
Lemma div_between_if n a b : 0 <= n -> b <> 0 -> n * b <= a < (2 + n) * b -> (a / b = if (1 + n) * b <=? a then 1 + n else n)%Z. |
| 458 |
Proof. |
| 459 |
intros. |
| 460 |
break_match; Z.ltb_to_lt; |
| 461 |
apply div_between; lia. |
| 462 |
Qed. |
| 463 |
|
| 464 |
Lemma div_between_0_if a b : b <> 0 -> 0 <= a < 2 * b -> a / b = if b <=? a then 1 else 0. |
| 465 |
Proof. intros; rewrite (div_between_if 0) by lia; autorewrite with zsimplify_const; reflexivity. Qed. |
| 466 |
|
| 467 |
Lemma div2_split a b c (Hc : 0 < c) : |
| 468 |
(a + 2 ^ c * b) / 2 = a / 2 + 2 ^ (c - 1) * (b mod 2) + 2 ^ c * (b / 2). |
| 469 |
Proof. |
| 470 |
replace (2^c * b) with (2 * (2^(c - 1) * b)). |
| 471 |
rewrite Div.Z.div_add', <- (Z.div2_div b) by lia. |
| 472 |
destruct (Z.odd b) eqn:E; rewrite Zmod_odd, E; |
| 473 |
rewrite (Zdiv2_odd_eqn b) at 1; rewrite E; ring_simplify; |
| 474 |
rewrite Pow.Z.pow_mul_base, Z.sub_simpl_r; lia. |
| 475 |
rewrite Z.mul_assoc, Pow.Z.pow_mul_base, Z.sub_simpl_r; lia. Qed. |
| 476 |
End Z. |