Debian Codegraph @

src/Util/ZUtil/Ltz.v

raw
1 From Coq Require Import ZArith Lia.
2 Require Import Crypto.Util.ZUtil.Definitions.
3 Require Import Crypto.Util.ZUtil.Modulo.PullPush.
4 Require Import Crypto.Util.ZUtil.Hints.ZArith.
5 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
6 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
7 Require Import Crypto.Util.Tactics.BreakMatch.
8 Local Open Scope Z_scope.
9
10 Module Z.
11 Local Ltac fin_div_ltz :=
12 intros; cbv [Z.ltz]; Z.div_mod_to_quot_rem; break_innermost_match; Z.ltb_to_lt; try nia.
13
14 Lemma ltz_mod_pow2_small x y z :
15 0 < z ->
16 (Z.ltz x y) mod (2 ^ z) = Z.ltz x y.
17 Proof.
18 cbv [Z.ltz]; break_match; intros; Z.ltb_to_lt;
19 apply Z.mod_small; auto with zarith.
20 Qed.
21
22 Lemma add_div_ltz_1 s x y :
23 0 <= x < s
24 -> 0 <= y < s
25 -> (x + y) / s = Z.ltz ((x + y) mod s) x.
26 Proof. fin_div_ltz. Qed.
27
28 Lemma add_div_ltz_2 s x y :
29 0 <= x < s
30 -> 0 <= y < s
31 -> (x + y) / s = Z.ltz ((x + y) mod s) y.
32 Proof. fin_div_ltz. Qed.
33
34 Lemma add_add_div_ltz_2 s c x y :
35 0 <= c < s
36 -> 0 <= x < s
37 -> 0 <= y < s
38 -> (c + x + y) / s =
39 Z.ltz ((c + x) mod s) x + Z.ltz ((c + x + y) mod s) y.
40 Proof.
41 intros. rewrite <-(Z.add_mod_idemp_l (c + x)) by lia.
42 rewrite <-!add_div_ltz_2 by auto with zarith.
43 fin_div_ltz.
44 Qed.
45
46 Lemma sub_div_ltz s x y :
47 0 <= x < s
48 -> 0 <= y < s
49 -> - ((x - y) / s) = Z.ltz x ((x - y) mod s).
50 Proof. fin_div_ltz. Qed.
51
52 Lemma sub_sub_div_ltz s x y c :
53 0 <= c < s
54 -> 0 <= x < s
55 -> 0 <= y < s
56 -> - ((x - y - c) / s) =
57 Z.ltz x ((x - y) mod s) + Z.ltz ((x - y) mod s) ((x - y - c) mod s).
58 Proof.
59 intros. rewrite (Z.sub_mod_l (x-y)).
60 rewrite <-!sub_div_ltz by auto with zarith.
61 fin_div_ltz.
62 Qed.
63 End Z.