Debian Codegraph @

src/Arithmetic/ModOps.v

raw
1 From Coq Require Import ZArith Lia.
2 From Coq Require Import Derive.
3 Require Import QArith_base Qround Crypto.Util.QUtil.
4 Require Import Crypto.Arithmetic.Core.
5 Require Import Crypto.Util.ListUtil.
6 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
7 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
8
9 Require Import Crypto.Util.Notations.
10 Local Open Scope Z_scope.
11
12 Section mod_ops.
13 Import Positional.
14 Local Coercion Z.of_nat : nat >-> Z.
15 Local Coercion QArith_base.inject_Z : Z >-> Q.
16 (* Design constraints:
17 - inputs must be [Z] (b/c reification does not support Q)
18 - internal structure must not match on the arguments (b/c reification does not support [positive]) *)
19 Context (limbwidth_num limbwidth_den : Z)
20 (limbwidth_good : 0 < limbwidth_den <= limbwidth_num)
21 (s : Z)
22 (c : list (Z*Z))
23 (n : nat)
24 (len_c : nat)
25 (idxs : list nat)
26 (len_idxs : nat)
27 (m_nz:s - Associational.eval c <> 0) (s_nz:s <> 0)
28 (Hn_nz : n <> 0%nat)
29 (Hc : length c = len_c)
30 (Hidxs : length idxs = len_idxs).
31 Definition weight (i : nat)
32 := 2^(-(-(limbwidth_num * i) / limbwidth_den)).
33
34 Local Ltac Q_cbv :=
35 cbv [Qceiling inject_Z Qle Qfloor Qdiv Qnum Qden Qmult Qinv Qopp].
36
37 Local Lemma weight_ZQ_correct i
38 (limbwidth := (limbwidth_num / limbwidth_den)%Q)
39 : weight i = 2^Qceiling(limbwidth*i).
40 Proof using limbwidth_good.
41 clear -limbwidth_good.
42 cbv [limbwidth weight]; Q_cbv.
43 destruct limbwidth_num, limbwidth_den, i; try reflexivity;
44 repeat rewrite ?Pos.mul_1_l, ?Pos.mul_1_r, ?Z.mul_0_l, ?Zdiv_0_l, ?Zdiv_0_r, ?Z.mul_1_l, ?Z.mul_1_r, <- ?Z.opp_eq_mul_m1, ?Pos2Z.opp_pos;
45 try reflexivity; try lia.
46 Qed.
47
48 Local Ltac t_weight_with lem :=
49 clear -limbwidth_good;
50 intros; rewrite !weight_ZQ_correct;
51 apply lem;
52 try lia; Q_cbv; destruct limbwidth_den; cbn; try lia.
53
54 Definition wprops : @weight_properties weight.
55 Proof using limbwidth_good.
56 constructor.
57 { cbv [weight Z.of_nat]; autorewrite with zsimplify_fast; reflexivity. }
58 { intros; apply Z.gt_lt. t_weight_with (@pow_ceil_mul_nat_pos 2). }
59 { t_weight_with (@pow_ceil_mul_nat_multiples 2). }
60 { intros; apply Z.gt_lt. t_weight_with (@pow_ceil_mul_nat_divide 2). }
61 Defined.
62 Local Definition weight_0_wprops := weight_0 wprops.
63 Local Definition weight_positive_wprops := weight_positive wprops.
64 Local Definition weight_multiples_wprops := weight_multiples wprops.
65 Local Definition weight_divides_wprops := weight_divides wprops.
66
67 Local Hint Immediate weight_0_wprops : core.
68 Local Hint Immediate weight_positive_wprops : core.
69 Local Hint Immediate weight_multiples_wprops : core.
70 Local Hint Immediate weight_divides_wprops : core.
71
72 Local Lemma weight_1_gt_1 : weight 1 > 1.
73 Proof using limbwidth_good.
74 clear -limbwidth_good.
75 cut (1 < weight 1); [ lia | ].
76 cbv [weight Z.of_nat]; autorewrite with zsimplify_fast.
77 apply Z.pow_gt_1; [ lia | ].
78 Z.div_mod_to_quot_rem_in_goal; nia.
79 Qed.
80
81 Lemma weight_unique_iff : forall i j, (i <= n)%nat -> (j <= n)%nat -> weight i = weight j <-> i = j.
82 Proof using limbwidth_good.
83 clear Hn_nz; clear dependent c.
84 cbv [weight]; split; intro H'; subst; trivial; [].
85 apply (f_equal (fun x => limbwidth_den * (- Z.log2 x))) in H'.
86 rewrite !Z.log2_pow2, !Z.opp_involutive in H' by (Z.div_mod_to_quot_rem; nia).
87 Z.div_mod_to_quot_rem.
88 destruct i as [|i], j as [|j]; autorewrite with zsimplify_const in *; [ reflexivity | exfalso; nia.. | ].
89 nia.
90 Qed.
91 Lemma weight_unique : forall i j, (i <= n)%nat -> (j <= n)%nat -> weight i = weight j -> i = j.
92 Proof using limbwidth_good. apply weight_unique_iff. Qed.
93
94 Derive carry_mulmod
95 SuchThat (forall (f g : list Z)
96 (Hf : length f = n)
97 (Hg : length g = n),
98 (eval weight n (carry_mulmod f g)) mod (s - Associational.eval c)
99 = (eval weight n f * eval weight n g) mod (s - Associational.eval c))
100 As eval_carry_mulmod.
101 Proof.
102 revert carry_mulmod; instantiate (1:=ltac:(clear -idxs s c n)); intro carry_mulmod.
103 clear -m_nz s_nz limbwidth_good Hn_nz idxs.
104 intros.
105 rewrite <-eval_mulmod with (s:=s) (c:=c) by auto with zarith.
106 etransitivity;
107 [ | rewrite <- @eval_chained_carries with (s:=s) (c:=c) (idxs:=idxs)
108 by auto with zarith; reflexivity ].
109 eapply f_equal2; [|trivial]. eapply f_equal.
110 subst carry_mulmod; reflexivity.
111 Qed.
112
113 Derive carry_squaremod
114 SuchThat (forall (f : list Z)
115 (Hf : length f = n),
116 (eval weight n (carry_squaremod f)) mod (s - Associational.eval c)
117 = (eval weight n f * eval weight n f) mod (s - Associational.eval c))
118 As eval_carry_squaremod.
119 Proof.
120 revert carry_squaremod; instantiate (1:=ltac:(clear -idxs s c n)); intro carry_squaremod.
121 clear -m_nz s_nz limbwidth_good Hn_nz idxs.
122 intros.
123 rewrite <-eval_squaremod with (s:=s) (c:=c) by auto with zarith.
124 etransitivity;
125 [ | rewrite <- @eval_chained_carries with (s:=s) (c:=c) (idxs:=idxs)
126 by auto with zarith; reflexivity ].
127 eapply f_equal2; [|trivial]. eapply f_equal.
128 subst carry_squaremod; reflexivity.
129 Qed.
130
131 Derive carry_scmulmod
132 SuchThat (forall (x : Z) (f : list Z)
133 (Hf : length f = n),
134 (eval weight n (carry_scmulmod x f)) mod (s - Associational.eval c)
135 = (x * eval weight n f) mod (s - Associational.eval c))
136 As eval_carry_scmulmod.
137 Proof.
138 revert carry_scmulmod; instantiate (1:=ltac:(clear -idxs s c n)); intro carry_scmulmod.
139 clear -m_nz s_nz limbwidth_good Hn_nz idxs.
140 intros.
141 push_Zmod.
142 rewrite <-eval_encode with (s:=s) (c:=c) (x:=x) (weight:=weight) (n:=n) by auto with zarith.
143 pull_Zmod.
144 rewrite<-eval_mulmod with (s:=s) (c:=c) by (auto with zarith; distr_length).
145 etransitivity;
146 [ | rewrite <- @eval_chained_carries with (s:=s) (c:=c) (idxs:=idxs)
147 by auto with zarith; reflexivity ].
148 eapply f_equal2; [|trivial]. eapply f_equal.
149 subst carry_scmulmod; reflexivity.
150 Qed.
151
152 Derive carrymod
153 SuchThat (forall (f : list Z)
154 (Hf : length f = n),
155 (eval weight n (carrymod f)) mod (s - Associational.eval c)
156 = (eval weight n f) mod (s - Associational.eval c))
157 As eval_carrymod.
158 Proof.
159 revert carrymod; instantiate (1:=ltac:(clear -idxs s c n)); intro carrymod.
160 clear -m_nz s_nz limbwidth_good Hn_nz idxs.
161 intros.
162 etransitivity;
163 [ | rewrite <- @eval_chained_carries with (s:=s) (c:=c) (idxs:=idxs)
164 by auto with zarith; reflexivity ].
165 eapply f_equal2; [|trivial]. eapply f_equal.
166 subst carrymod; reflexivity.
167 Qed.
168
169 Derive addmod
170 SuchThat (forall (f g : list Z)
171 (Hf : length f = n)
172 (Hg : length g = n),
173 (eval weight n (addmod f g)) mod (s - Associational.eval c)
174 = (eval weight n f + eval weight n g) mod (s - Associational.eval c))
175 As eval_addmod.
176 Proof.
177 revert addmod; instantiate (1:=ltac:(clear -s c n)); intro addmod.
178 clear -limbwidth_good.
179 intros.
180 rewrite <-eval_add by auto with zarith.
181 eapply f_equal2; [|trivial]. eapply f_equal.
182 subst addmod; reflexivity.
183 Qed.
184
185 Derive submod
186 SuchThat (forall (balance : list Z)
187 (f g : list Z)
188 (length_balance : length balance = n)
189 (eval_balance : eval weight n balance mod (s - Associational.eval c) = 0)
190 (Hf : length f = n)
191 (Hg : length g = n),
192 (eval weight n (submod balance f g)) mod (s - Associational.eval c)
193 = (eval weight n f - eval weight n g) mod (s - Associational.eval c))
194 As eval_submod.
195 Proof.
196 revert submod; instantiate (1:=ltac:(clear -s c n)); intro submod.
197 clear -s_nz m_nz limbwidth_good.
198 intros.
199 rewrite <-eval_sub with (balance:=balance) by auto with zarith.
200 eapply f_equal2; [|trivial]. eapply f_equal.
201 subst submod; reflexivity.
202 Qed.
203
204 Derive oppmod
205 SuchThat (forall (balance : list Z)
206 (f: list Z)
207 (length_balance : length balance = n)
208 (eval_balance : eval weight n balance mod (s - Associational.eval c) = 0)
209 (Hf : length f = n),
210 (eval weight n (oppmod balance f)) mod (s - Associational.eval c)
211 = (- eval weight n f) mod (s - Associational.eval c))
212 As eval_oppmod.
213 Proof.
214 revert oppmod; instantiate (1:=ltac:(clear -s c n)); intro oppmod.
215 clear -s_nz m_nz limbwidth_good.
216 intros.
217 rewrite <-eval_opp with (balance:=balance) by auto with zarith.
218 eapply f_equal2; [|trivial]. eapply f_equal.
219 subst oppmod; reflexivity.
220 Qed.
221
222 Derive carry_addmod
223 SuchThat (forall (f g : list Z)
224 (Hf : length f = n)
225 (Hg : length g = n),
226 (eval weight n (carry_addmod f g)) mod (s - Associational.eval c)
227 = (eval weight n f + eval weight n g) mod (s - Associational.eval c))
228 As eval_carry_addmod.
229 Proof.
230 revert carry_addmod; instantiate (1:=ltac:(clear -idxs s c n)); intro carry_addmod.
231 clear -m_nz s_nz limbwidth_good Hn_nz idxs.
232 intros.
233 rewrite <-eval_add by auto with zarith.
234 etransitivity;
235 [ | rewrite <- @eval_chained_carries with (s:=s) (c:=c) (idxs:=idxs)
236 by auto with zarith; reflexivity ].
237 eapply f_equal2; [|trivial]. eapply f_equal.
238 subst carry_addmod; reflexivity.
239 Qed.
240
241 Derive carry_submod
242 SuchThat (forall (balance : list Z)
243 (f g : list Z)
244 (length_balance : length balance = n)
245 (eval_balance : eval weight n balance mod (s - Associational.eval c) = 0)
246 (Hf : length f = n)
247 (Hg : length g = n),
248 (eval weight n (carry_submod balance f g)) mod (s - Associational.eval c)
249 = (eval weight n f - eval weight n g) mod (s - Associational.eval c))
250 As eval_carry_submod.
251 Proof.
252 revert carry_submod; instantiate (1:=ltac:(clear -idxs s c n)); intro carry_submod.
253 clear -m_nz s_nz limbwidth_good Hn_nz idxs.
254 intros.
255 rewrite <-eval_sub with (balance:=balance) by auto with zarith.
256 etransitivity;
257 [ | rewrite <- @eval_chained_carries with (s:=s) (c:=c) (idxs:=idxs)
258 by auto with zarith; reflexivity ].
259 eapply f_equal2; [|trivial]. eapply f_equal.
260 subst carry_submod; reflexivity.
261 Qed.
262
263 Derive carry_oppmod
264 SuchThat (forall (balance : list Z)
265 (f: list Z)
266 (length_balance : length balance = n)
267 (eval_balance : eval weight n balance mod (s - Associational.eval c) = 0)
268 (Hf : length f = n),
269 (eval weight n (carry_oppmod balance f)) mod (s - Associational.eval c)
270 = (- eval weight n f) mod (s - Associational.eval c))
271 As eval_carry_oppmod.
272 Proof.
273 revert carry_oppmod; instantiate (1:=ltac:(clear -idxs s c n)); intro carry_oppmod.
274 clear -m_nz s_nz limbwidth_good Hn_nz idxs.
275 intros.
276 rewrite <-eval_opp with (balance:=balance) by auto with zarith.
277 etransitivity;
278 [ | rewrite <- @eval_chained_carries with (s:=s) (c:=c) (idxs:=idxs)
279 by auto with zarith; reflexivity ].
280 eapply f_equal2; [|trivial]. eapply f_equal.
281 subst carry_oppmod; reflexivity.
282 Qed.
283
284 Derive encodemod
285 SuchThat (forall (f:Z),
286 (eval weight n (encodemod f)) mod (s - Associational.eval c)
287 = f mod (s - Associational.eval c))
288 As eval_encodemod.
289 Proof.
290 revert encodemod; instantiate (1:=ltac:(clear -s c n)); intro encodemod.
291 clear -limbwidth_good s_nz m_nz Hn_nz.
292 intros.
293 etransitivity.
294 2:rewrite <-@eval_encode with (weight:=weight) (n:=n) by auto with zarith; reflexivity.
295 eapply f_equal2; [|trivial]. eapply f_equal.
296 subst encodemod; reflexivity.
297 Qed.
298 End mod_ops.