Debian Codegraph @

src/Util/ZUtil/Pow.v

raw
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.ListUtil.
5 Local Open Scope Z_scope.
6
7 Module Z.
8 Lemma base_pow_neg b n : n < 0 -> b^n = 0.
9 Proof.
10 destruct n; intro H; try reflexivity; compute in H; congruence.
11 Qed.
12 #[global]
13 Hint Rewrite base_pow_neg using zutil_arith : zsimplify.
14
15 Lemma nonneg_pow_pos a b : 0 < a -> 0 < a^b -> 0 <= b.
16 Proof.
17 destruct (Z_lt_le_dec b 0); intros; auto.
18 erewrite Z.pow_neg_r in * by eassumption.
19 lia.
20 Qed.
21 #[global]
22 Hint Resolve nonneg_pow_pos : zarith.
23 #[global]
24 Hint Extern 1 => simple apply (fun n => nonneg_pow_pos 2 n Z.lt_0_2) : zarith.
25 Lemma nonneg_pow_pos_helper a b dummy : 0 < a -> 0 <= dummy < a^b -> 0 <= b.
26 Proof. eauto with zarith lia. Qed.
27 #[global]
28 Hint Resolve nonneg_pow_pos_helper : zarith.
29 #[global]
30 Hint Extern 2 => simple apply (fun n dummy => nonneg_pow_pos_helper 2 n dummy Z.lt_0_2) : zarith.
31
32 Lemma div_pow2succ : forall n x, (0 <= x) ->
33 n / 2 ^ Z.succ x = Z.div2 (n / 2 ^ x).
34 Proof.
35 intros.
36 rewrite Z.pow_succ_r, Z.mul_comm by auto.
37 rewrite <- Z.div_div by (try apply Z.pow_nonzero; lia).
38 rewrite Zdiv2_div.
39 reflexivity.
40 Qed.
41
42 Definition pow_sub_r'
43 := fun a b c y H0 H1 => @Logic.eq_trans _ _ _ y (@Z.pow_sub_r a b c H0 H1).
44 Definition pow_sub_r'_sym
45 := fun a b c y p H0 H1 => Logic.eq_sym (@Logic.eq_trans _ y _ _ (Logic.eq_sym p) (@Z.pow_sub_r a b c H0 H1)).
46 #[global]
47 Hint Resolve pow_sub_r' pow_sub_r'_sym Z.eq_le_incl : zarith.
48
49 Local Definition f_equal_pow_b b := f_equal (fun e => b ^ e).
50 Local Definition f_equal_pow_e e := f_equal (fun b => b ^ e).
51 #[global]
52 Hint Resolve f_equal_pow_b f_equal_pow_e : zarith.
53
54 Lemma two_p_two_eq_four : 2^(2) = 4.
55 Proof. reflexivity. Qed.
56 #[global]
57 Hint Rewrite <- two_p_two_eq_four : push_Zpow.
58
59 Lemma pow_pos_le a b : 0 < a -> 0 < b -> a <= a ^ b.
60 Proof.
61 intros; transitivity (a ^ 1).
62 { rewrite Z.pow_1_r; reflexivity. }
63 { apply Z.pow_le_mono; auto with zarith. }
64 Qed.
65 #[global]
66 Hint Resolve pow_pos_le : zarith.
67
68 Lemma pow_pos_lt a b : 1 < a -> 1 < b -> a < a ^ b.
69 Proof.
70 intros; eapply Z.le_lt_trans with (m:=a ^ 1).
71 { rewrite Z.pow_1_r; reflexivity. }
72 { apply Z.pow_lt_mono_r; auto with zarith. }
73 Qed.
74 #[global]
75 Hint Resolve pow_pos_lt : zarith.
76
77 Lemma pow_div_base a b : a <> 0 -> 0 < b -> a ^ b / a = a ^ (b - 1).
78 Proof. intros; rewrite Z.pow_sub_r, Z.pow_1_r; lia. Qed.
79 #[global]
80 Hint Rewrite pow_div_base using zutil_arith : pull_Zpow.
81
82 Lemma pow_mul_base a b : 0 <= b -> a * a ^ b = a ^ (b + 1).
83 Proof. intros; rewrite <-Z.pow_succ_r, <-Z.add_1_r by lia; reflexivity. Qed.
84 #[global]
85 Hint Rewrite pow_mul_base using zutil_arith : pull_Zpow.
86 End Z.