| 1 |
From Coq Require Import ZArith. |
| 2 |
Require Import Crypto.Util.Decidable. |
| 3 |
Require Import Crypto.Util.ZUtil.Notations. |
| 4 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 5 |
Require Import Crypto.Util.LetIn. |
| 6 |
Local Open Scope Z_scope. |
| 7 |
|
| 8 |
Module Z. |
| 9 |
Lemma signed_small s v (Hv : (0 <= v <= Z.ones (Z.of_N s-1))) : Z.signed s v = v. |
| 10 |
Proof. |
| 11 |
destruct (N.eq_dec s 0); subst; cbv [Z.signed]. |
| 12 |
{ rewrite Z.land_0_r. cbn in *; Lia.lia. } |
| 13 |
rewrite !Z.land_ones, !Z.shiftl_mul_pow2, ?Z.add_0_r, ?Z.mul_1_l by Lia.lia. |
| 14 |
rewrite Z.ones_equiv in Hv. |
| 15 |
rewrite Z.mod_small; try ring. |
| 16 |
enough (2 ^ Z.of_N s = 2 ^ (Z.of_N s - 1) + 2 ^ (Z.of_N s - 1))%Z; try Lia.lia. |
| 17 |
replace (Z.of_N s) with (1+(Z.of_N s-1))%Z at 1 by Lia.lia. |
| 18 |
rewrite Z.pow_add_r; try Lia.lia. |
| 19 |
Qed. |
| 20 |
|
| 21 |
Lemma signed_0 s : Z.signed s 0 = 0. |
| 22 |
Proof. |
| 23 |
destruct (N.eq_dec s 0); subst; trivial. |
| 24 |
cbv [Z.signed]. |
| 25 |
rewrite !Z.land_ones, !Z.shiftl_mul_pow2, ?Z.add_0_r, ?Z.mul_1_l by Lia.lia. |
| 26 |
rewrite Z.mod_small; try ring. |
| 27 |
split; try (eapply Z.pow_lt_mono_r; Lia.lia). |
| 28 |
eapply Z.pow_nonneg; Lia.lia. |
| 29 |
Qed. |
| 30 |
#[global] |
| 31 |
Hint Rewrite signed_0 : zsimplify_const zsimplify zsimplify_fast. |
| 32 |
Global Hint Resolve signed_0 : zarith. |
| 33 |
End Z. |