Debian Codegraph @

src/Util/ZUtil/AddGetCarry.v

raw
1 From Coq Require Import ZArith Lia.
2 Require Import Crypto.Util.ZUtil.Definitions.
3 Require Import Crypto.Util.ZUtil.Ltz.
4 Require Import Crypto.Util.ZUtil.Hints.ZArith.
5 Require Import Crypto.Util.ZUtil.Modulo.PullPush.
6 Require Import Crypto.Util.Prod.
7 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
8 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
9 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
10 Require Import Crypto.Util.LetIn.
11 Require Import Crypto.Util.Tactics.BreakMatch.
12 Require Import Crypto.Util.Tactics.DestructHead.
13 Require Import Crypto.Util.Tactics.SpecializeBy.
14 Require Import Crypto.Util.Tactics.RewriteHyp.
15 Local Open Scope Z_scope.
16
17 Local Notation eta x := (fst x, snd x).
18
19 Module Z.
20 Section with_bitwidth.
21 Context (bitwidth : Z).
22
23 Local Notation ADD := (Z.add_get_carry bitwidth).
24 Local Notation ADC := (Z.add_with_get_carry bitwidth).
25
26 Local Arguments Z.pow : simpl never.
27
28 Lemma add_get_carry_to_add_with_get_carry_cps_gen {T} c a b (P : Z -> Z -> Z -> Z -> Z -> T)
29 : (let '(a', c1) := ADD a b in
30 let '(s, c2) := ADD c a' in
31 dlet c := c1 + c2 in
32 P a' c1 c2 s c)
33 = let '(a', c1) := eta (ADD a b) in
34 let '(s, c2) := eta (ADD c a') in
35 let '(s, c) := ADC c a b in
36 P a' c1 c2 s c.
37 Proof.
38 unfold Let_In, ADD, ADC, Z.get_carry, Z.add_with_carry; simpl.
39 destruct (Z_dec bitwidth 0) as [ [?|?] | ? ].
40 { rewrite Z.pow_neg_r by assumption.
41 autorewrite with zsimplify. rewrite ?Z.add_assoc.
42 reflexivity. }
43 { f_equal.
44 { push_Zmod; pull_Zmod; apply f_equal2; lia. }
45 { Z.div_mod_to_quot_rem_in_goal; nia. } }
46 { subst; autorewrite with zsimplify; f_equal; lia. }
47 Qed.
48
49 Lemma add_get_carry_to_add_with_get_carry_cps {T} c a b (P : Z -> Z -> T)
50 : (let '(a', c1) := ADD a b in
51 let '(s, c2) := ADD c a' in
52 dlet c := c1 + c2 in
53 P s c)
54 = let '(s, c) := ADC c a b in
55 P s c.
56 Proof.
57 apply add_get_carry_to_add_with_get_carry_cps_gen.
58 Qed.
59 End with_bitwidth.
60
61 Local Ltac easypeasy :=
62 repeat progress autounfold;
63 break_match; Z.ltb_to_lt; rewrite_hyp ?*; autorewrite with cancel_pair zsimplify;
64 solve [repeat (f_equal; try ring)].
65
66 Local Hint Unfold Z.get_carry Z.get_borrow
67 Z.add_get_carry_full Z.add_with_get_carry_full
68 Z.add_get_carry Z.add_with_get_carry Z.add_with_carry
69 Z.sub_get_borrow_full Z.sub_with_get_borrow_full
70 Z.sub_get_borrow Z.sub_with_get_borrow Z.sub_with_borrow Let_In
71 : core.
72
73 Lemma add_get_carry_full_mod s x y :
74 fst (Z.add_get_carry_full s x y) = (x + y) mod s.
75 Proof. easypeasy. Qed.
76 #[global]
77 Hint Rewrite add_get_carry_full_mod : to_div_mod.
78
79 Lemma add_get_carry_full_div s x y :
80 snd (Z.add_get_carry_full s x y) = (x + y) / s.
81 Proof. easypeasy. Qed.
82 #[global]
83 Hint Rewrite add_get_carry_full_div : to_div_mod.
84
85 Lemma add_with_get_carry_full_mod s c x y :
86 fst (Z.add_with_get_carry_full s c x y) = (c + x + y) mod s.
87 Proof. easypeasy. Qed.
88 #[global]
89 Hint Rewrite add_with_get_carry_full_mod : to_div_mod.
90
91 Lemma add_with_get_carry_full_div s c x y :
92 snd (Z.add_with_get_carry_full s c x y) = (c + x + y) / s.
93 Proof. easypeasy. Qed.
94 #[global]
95 Hint Rewrite add_with_get_carry_full_div : to_div_mod.
96
97 Lemma sub_with_borrow_to_add_get_carry c x y
98 : Z.sub_with_borrow c x y = Z.add_with_carry (-c) x (-y).
99 Proof. reflexivity. Qed.
100
101 Lemma sub_get_borrow_full_mod s x y :
102 fst (Z.sub_get_borrow_full s x y) = (x - y) mod s.
103 Proof. easypeasy. Qed.
104 #[global]
105 Hint Rewrite sub_get_borrow_full_mod : to_div_mod.
106
107 Lemma sub_get_borrow_full_div s x y :
108 snd (Z.sub_get_borrow_full s x y) = - ((x - y) / s).
109 Proof. easypeasy. Qed.
110 #[global]
111 Hint Rewrite sub_get_borrow_full_div : to_div_mod.
112
113 Lemma sub_with_get_borrow_full_mod s c x y :
114 fst (Z.sub_with_get_borrow_full s c x y) = (x - y - c) mod s.
115 Proof. easypeasy. Qed.
116 #[global]
117 Hint Rewrite sub_with_get_borrow_full_mod : to_div_mod.
118
119 Lemma sub_with_get_borrow_full_div s c x y :
120 snd (Z.sub_with_get_borrow_full s c x y) = - ((x - y - c) / s).
121 Proof. easypeasy. Qed.
122 #[global]
123 Hint Rewrite sub_with_get_borrow_full_div : to_div_mod.
124
125 Lemma add_get_carry_full_ltz_1 s x y :
126 0 <= x < s
127 -> 0 <= y < s
128 -> snd (Z.add_get_carry_full s x y) = Z.ltz ((x + y) mod s) x.
129 Proof.
130 intros; rewrite add_get_carry_full_div, Z.add_div_ltz_1; lia.
131 Qed.
132
133 Lemma add_get_carry_full_ltz_2 s x y :
134 0 <= x < s
135 -> 0 <= y < s
136 -> snd (Z.add_get_carry_full s x y) = Z.ltz ((x + y) mod s) y.
137 Proof.
138 intros; rewrite add_get_carry_full_div, Z.add_div_ltz_2; lia.
139 Qed.
140
141 Lemma sub_get_borrow_full_ltz s x y :
142 0 <= x < s
143 -> 0 <= y < s
144 -> snd (Z.sub_get_borrow_full s x y) = Z.ltz x ((x - y) mod s).
145 Proof.
146 intros; rewrite sub_get_borrow_full_div, Z.sub_div_ltz; lia.
147 Qed.
148 End Z.