| 1 |
From Coq Require Import Bool. |
| 2 |
From Coq Require Import ZArith. |
| 3 |
From Coq Require Import Lia. |
| 4 |
|
| 5 |
Require Import Crypto.Util.ZUtil.Log2. |
| 6 |
Require Import Crypto.Util.ZUtil.Testbit. |
| 7 |
Require Import Crypto.Util.ZUtil.Notations. |
| 8 |
|
| 9 |
Require Import Crypto.Util.Decidable. |
| 10 |
|
| 11 |
Local Open Scope Z_scope. |
| 12 |
|
| 13 |
Import Notations. |
| 14 |
|
| 15 |
Module Z. |
| 16 |
|
| 17 |
#[global] |
| 18 |
Hint Rewrite Z.bits_opp_full Z.shiftl_spec_full Z.shiftr_spec_full Z.lor_spec Z.land_spec Z.ones_spec_full Z.bits_0 Z.bits_1 andb_false_r andb_true_r orb_false_r orb_true_r Z.mod_pow2_ones : testbit_rewrite. |
| 19 |
|
| 20 |
#[global] |
| 21 |
Hint Rewrite <- Z.ones_equiv : testbit_rewrite. |
| 22 |
|
| 23 |
|
| 24 |
Ltac solve_testbit := |
| 25 |
repeat (match goal with |
| 26 |
| _ => progress autorewrite with testbit_rewrite; cbn [negb Z.b2z Z.eqb Z.ltb Z.leb Z.geb Z.gtb] |
| 27 |
| [ |- context[Z.testbit (2 ^ ?m) ?i] ] => rewrite Z.pow2_bits_full |
| 28 |
| [ |- context[Z.testbit (?a / 2)] ] => |
| 29 |
replace (a / 2) with (a / 2 ^ 1) by (apply f_equal2; [reflexivity|apply Z.pow_1_r]); rewrite <- Z.shiftr_div_pow2 by lia |
| 30 |
| [ |- context[Z.testbit (?a / 2 ^ ?k)] ] => |
| 31 |
rewrite <- Z.shiftr_div_pow2 by lia |
| 32 |
| [ |- context[_ - ?a + ?a] ] => rewrite Z.sub_simpl_r |
| 33 |
| [ |- context[dec] ] => destruct dec |
| 34 |
| [ |- context[Z_lt_dec ?a ?b] ] => |
| 35 |
destruct (Z_lt_dec a b) |
| 36 |
| [ |- context[Z.eq_dec ?a ?b] ] => |
| 37 |
destruct (Z.eq_dec a b) |
| 38 |
| [ |- context[?a <? ?b] ] => |
| 39 |
let E := fresh in |
| 40 |
destruct (a <? b) eqn:E; [rewrite Z.ltb_lt in E|rewrite Z.ltb_ge in E] |
| 41 |
| [ |- context[?a <=? ?b] ] => |
| 42 |
let E := fresh in |
| 43 |
destruct (a <=? b) eqn:E; [rewrite Z.leb_le in E|rewrite Z.leb_gt in E] |
| 44 |
| [ |- context[?a =? ?b] ] => |
| 45 |
let E := fresh in |
| 46 |
destruct (a =? b) eqn:E; [rewrite Z.eqb_eq in E; subst|rewrite Z.eqb_neq in E] |
| 47 |
| [ H: 0 <= ?a < 2 ^ ?m |- context[Z.testbit ?a ?i] ] => |
| 48 |
assert (m <= i) by lia; |
| 49 |
assert (2 ^ m <= 2 ^ i) by (apply Z.pow_le_mono; lia); |
| 50 |
rewrite (Z.bits_above_log2 a i) by (try apply Log2.Z.log2_lt_pow2_alt; lia) |
| 51 |
| [ |- Z.testbit _ _ = Z.testbit _ _ ] => |
| 52 |
apply f_equal2; lia |
| 53 |
| [ |- false = _ ] => rewrite Z.testbit_neg_r by lia |
| 54 |
| [ |- _ = false ] => rewrite Z.testbit_neg_r by lia |
| 55 |
| [ |- _ <= _ < _] => solve [ auto with zarith ] |
| 56 |
end; subst); |
| 57 |
try reflexivity; try discriminate; try lia. |
| 58 |
|
| 59 |
Ltac solve_using_testbit := apply Z.bits_inj'; intros; solve_testbit. |
| 60 |
|
| 61 |
End Z. |