| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import Lia. |
| 3 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 4 |
Local Open Scope Z_scope. |
| 5 |
|
| 6 |
Module N2Z. |
| 7 |
Lemma inj_land n m : Z.of_N (N.land n m) = Z.land (Z.of_N n) (Z.of_N m). |
| 8 |
Proof. destruct n, m; reflexivity. Qed. |
| 9 |
#[global] |
| 10 |
Hint Rewrite inj_land : push_Zof_N. |
| 11 |
#[global] |
| 12 |
Hint Rewrite <- inj_land : pull_Zof_N. |
| 13 |
|
| 14 |
Lemma inj_lor n m : Z.of_N (N.lor n m) = Z.lor (Z.of_N n) (Z.of_N m). |
| 15 |
Proof. destruct n, m; reflexivity. Qed. |
| 16 |
#[global] |
| 17 |
Hint Rewrite inj_lor : push_Zof_N. |
| 18 |
#[global] |
| 19 |
Hint Rewrite <- inj_lor : pull_Zof_N. |
| 20 |
|
| 21 |
Lemma inj_shiftl: forall x y, Z.of_N (N.shiftl x y) = Z.shiftl (Z.of_N x) (Z.of_N y). |
| 22 |
Proof. |
| 23 |
intros x y. |
| 24 |
apply Z.bits_inj_iff'; intros k Hpos. |
| 25 |
rewrite Z2N.inj_testbit; [|assumption]. |
| 26 |
rewrite Z.shiftl_spec; [|assumption]. |
| 27 |
|
| 28 |
assert ((Z.to_N k) >= y \/ (Z.to_N k) < y)%N as g by ( |
| 29 |
unfold N.ge, N.lt; induction (N.compare (Z.to_N k) y); [left|auto|left]; |
| 30 |
intro H; inversion H). |
| 31 |
|
| 32 |
destruct g as [g|g]; |
| 33 |
[ rewrite N.shiftl_spec_high; [|apply N2Z.inj_le; rewrite Z2N.id|apply N.ge_le] |
| 34 |
| rewrite N.shiftl_spec_low]; try assumption. |
| 35 |
|
| 36 |
- rewrite <- N2Z.inj_testbit; f_equal. |
| 37 |
rewrite N2Z.inj_sub, Z2N.id; [reflexivity|assumption|apply N.ge_le; assumption]. |
| 38 |
|
| 39 |
- apply N2Z.inj_lt in g. |
| 40 |
rewrite Z2N.id in g; [symmetry|assumption]. |
| 41 |
apply Z.testbit_neg_r; lia. |
| 42 |
Qed. |
| 43 |
#[global] |
| 44 |
Hint Rewrite inj_shiftl : push_Zof_N. |
| 45 |
#[global] |
| 46 |
Hint Rewrite <- inj_shiftl : pull_Zof_N. |
| 47 |
|
| 48 |
Lemma inj_shiftr: forall x y, Z.of_N (N.shiftr x y) = Z.shiftr (Z.of_N x) (Z.of_N y). |
| 49 |
Proof. |
| 50 |
intros. |
| 51 |
apply Z.bits_inj_iff'; intros k Hpos. |
| 52 |
rewrite Z2N.inj_testbit; [|assumption]. |
| 53 |
rewrite Z.shiftr_spec, N.shiftr_spec; [|apply N2Z.inj_le; rewrite Z2N.id|]; try assumption. |
| 54 |
rewrite <- N2Z.inj_testbit; f_equal. |
| 55 |
rewrite N2Z.inj_add; f_equal. |
| 56 |
apply Z2N.id; assumption. |
| 57 |
Qed. |
| 58 |
#[global] |
| 59 |
Hint Rewrite inj_shiftr : push_Zof_N. |
| 60 |
#[global] |
| 61 |
Hint Rewrite <- inj_shiftr : pull_Zof_N. |
| 62 |
End N2Z. |