Debian Codegraph @

src/Util/ZUtil/Rshi.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 Require Import Crypto.Util.Tactics.BreakMatch.
4 Require Import Crypto.Util.ZUtil.ZSimplify.
5 Require Import Crypto.Util.ZUtil.ZSimplify.Core.
6 Require Import Crypto.Util.ZUtil.ZSimplify.Simple.
7 Require Import Crypto.Util.ZUtil.Definitions.
8 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
9 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
10 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
11 Require Import Crypto.Util.ZUtil.Hints.PullPush.
12 Local Open Scope Z_scope.
13
14 Module Z.
15 Lemma rshi_correct_full : forall s a b n,
16 Z.rshi s a b n = if (0 <=? n)
17 then ((b + a * s) / 2 ^ n) mod s
18 else ((b + a * s) * 2 ^ (-n)) mod s.
19 Proof.
20 cbv [Z.rshi]; intros. pose proof (Z.log2_nonneg s).
21 destruct (Decidable.dec (0 <= n)), (Z_zerop s); subst;
22 break_match;
23 now repeat match goal with
24 | H : _ = s |- _ => rewrite H
25 | _ => rewrite Z.land_ones by auto with zarith
26 | _ => progress Z.ltb_to_lt
27 | _ => progress autorewrite with Zshift_to_pow push_Zpow zsimplify_const
28 | _ => reflexivity
29 | _ => lia
30 end.
31 Qed.
32
33 Lemma rshi_correct_full_alt : forall s a b n,
34 Z.rshi s a b n = if (0 <=? n)
35 then ((b + a * s) / 2 ^ n) mod s
36 else (b * 2 ^ (-n)) mod s.
37 Proof. intros; rewrite rshi_correct_full; push_Zmod; pull_Zmod; autorewrite with zsimplify_const; reflexivity. Qed.
38
39 Lemma rshi_correct : forall s a b n, 0 <= n -> s <> 0 ->
40 Z.rshi s a b n = ((b + a * s) / 2 ^ n) mod s.
41 Proof. intros; rewrite rshi_correct_full; break_match; Z.ltb_to_lt; lia. Qed.
42
43 Lemma rshi_small s a b n :
44 (0 <= b < s) ->
45 (0 <= a < 2^ n) ->
46 (0 <= n) ->
47 Z.rshi s a b n = ((b + a * s) / 2 ^ n).
48 Proof.
49 intros.
50 rewrite Z.rshi_correct by auto with zarith.
51 apply Z.mod_small.
52 Z.div_mod_to_quot_rem; nia.
53 Qed.
54
55 End Z.