Debian Codegraph @

src/Arithmetic/BaseConversion.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Derive.
3 From Coq Require Import List.
4 Require Import Crypto.Arithmetic.Core.
5 Require Import Crypto.Arithmetic.ModOps.
6 Require Import Crypto.Arithmetic.Partition.
7 Require Import Crypto.Arithmetic.Saturated.
8 Require Import Crypto.Util.LetIn.
9 Require Import Crypto.Util.ListUtil.
10 Require Import Crypto.Util.ZUtil.Definitions.
11
12 Require Import Crypto.Util.Notations.
13 Local Open Scope Z_scope.
14
15 Module BaseConversion.
16 Import Positional.
17 Section BaseConversion.
18 Context (sw dw : nat -> Z) (* source/destination weight functions *)
19 {swprops : @weight_properties sw}
20 {dwprops : @weight_properties dw}.
21
22 Definition convert_bases (sn dn : nat) (p : list Z) : list Z :=
23 let p_assoc := Positional.to_associational sw sn p in
24 let p_cols := Columns.from_associational dw dn p_assoc in
25 (* reverse is not needed for correctness, but this way has better
26 performance because we add the small numbers first *)
27 let p_cols := Columns.reverse p_cols in
28 let p_flattened := Columns.flatten dw Z.add_split p_cols in
29 let r := fst p_flattened in
30 let carry := (snd p_flattened * (dw dn / dw (pred dn))) in
31 let r := add_to_nth (pred dn) carry r in
32 r.
33
34 Hint Rewrite @Columns.length_from_associational
35 @Columns.length_flatten @Columns.length_reverse
36 : distr_length.
37 Hint Rewrite @Columns.eval_from_associational
38 @Columns.eval_reverse @Positional.eval_to_associational
39 using (auto; solve [distr_length])
40 : push_eval.
41
42 Lemma eval_convert_bases sn dn p :
43 (dn <> 0%nat) -> length p = sn ->
44 eval dw dn (convert_bases sn dn p) = eval sw sn p.
45 Proof using dwprops.
46 cbv [convert_bases]; intros.
47 rewrite eval_add_to_nth by distr_length.
48 rewrite Columns.flatten_mod by (auto; distr_length).
49 erewrite Columns.flatten_div by (auto; distr_length).
50 distr_length.
51 autorewrite with push_eval.
52 match goal with
53 | |- context [?a * (?b * (?c / ?a))] =>
54 replace (a * (b * (c / a))) with (a * (c / a) * b) by Lia.lia
55 end.
56 rewrite <-Weight.weight_div_mod by (auto; Lia.lia).
57 pose proof (weight_positive dwprops dn).
58 rewrite <-Z.div_mod; auto; Lia.lia.
59 Qed.
60
61 Lemma length_convert_bases sn dn p
62 : length (convert_bases sn dn p) = dn.
63 Proof using Type.
64 cbv [convert_bases]; now repeat autorewrite with distr_length.
65 Qed.
66 Hint Rewrite length_convert_bases : distr_length.
67
68 Lemma convert_bases_partitions sn dn p
69 (dw_unique : forall i j : nat, (i <= dn)%nat -> (j <= dn)%nat -> dw i = dw j -> i = j)
70 (dn_pos : dn <> 0%nat)
71 (p_bounded : 0 <= eval sw sn p < dw dn)
72 : convert_bases sn dn p = Partition.partition dw dn (eval sw sn p).
73 Proof using dwprops.
74 cbv [convert_bases].
75 erewrite Columns.flatten_div by (auto; distr_length).
76 distr_length.
77 erewrite Columns.flatten_correct by (auto; distr_length).
78 distr_length.
79 autorewrite with push_eval.
80 rewrite Z.div_small by Lia.lia.
81 autorewrite with zsimplify_fast.
82 rewrite add_to_nth_zero. reflexivity.
83 Qed.
84
85 Hint Rewrite
86 @Rows.eval_from_associational
87 @Associational.eval_carry
88 @Associational.eval_mul
89 Associational.eval_carryterm
90 @eval_convert_bases using solve [auto with zarith] : push_eval.
91
92 Ltac push_eval := intros; autorewrite with push_eval; auto with zarith.
93
94 (* convert from positional in one weight to the other, then to associational *)
95 Definition to_associational n m p : list (Z * Z) :=
96 let p' := convert_bases n m p in
97 Positional.to_associational dw m p'.
98
99 (* TODO : move to Associational? *)
100 Section reorder.
101 Definition reordering_carry (w fw : Z) (p : list (Z * Z)) :=
102 fold_right (fun t acc =>
103 let r := Associational.carryterm w fw t in
104 if fst t =? w then acc ++ r else r ++ acc) nil p.
105
106 Lemma eval_reordering_carry w fw p (_:fw<>0):
107 Associational.eval (reordering_carry w fw p) = Associational.eval p.
108 Proof using Type.
109 cbv [reordering_carry]. induction p; [reflexivity |].
110 autorewrite with push_fold_right. break_match; push_eval.
111 Qed.
112 End reorder.
113 Hint Rewrite eval_reordering_carry using solve [auto with zarith] : push_eval.
114
115 (* carry at specified indices in dw, then use Rows.flatten to convert to Positional with sw *)
116 Definition from_associational idxs n (p : list (Z * Z)) : list Z :=
117 (* important not to use Positional.carry here; we don't want to accumulate yet *)
118 let p' := fold_right (fun i acc => reordering_carry (dw i) (dw (S i) / dw i) acc) (Associational.bind_snd p) (rev idxs) in
119 fst (Rows.flatten sw n (Rows.from_associational sw n p')).
120
121 Lemma eval_carries p idxs :
122 Associational.eval (fold_right (fun i acc => reordering_carry (dw i) (dw (S i) / dw i) acc) p idxs) =
123 Associational.eval p.
124 Proof using dwprops. apply fold_right_invariant; push_eval. Qed.
125 Hint Rewrite eval_carries: push_eval.
126
127 Lemma eval_to_associational n m p :
128 m <> 0%nat -> length p = n ->
129 Associational.eval (to_associational n m p) = Positional.eval sw n p.
130 Proof using dwprops. cbv [to_associational]; push_eval. Qed.
131 Hint Rewrite eval_to_associational using solve [push_eval; distr_length] : push_eval.
132
133 Lemma eval_from_associational idxs n p :
134 n <> 0%nat -> 0 <= Associational.eval p < sw n ->
135 Positional.eval sw n (from_associational idxs n p) = Associational.eval p.
136 Proof using dwprops swprops.
137 cbv [from_associational]; intros.
138 rewrite Rows.flatten_mod by eauto using Rows.length_from_associational.
139 rewrite Associational.bind_snd_correct.
140 push_eval.
141 Qed.
142 Hint Rewrite eval_from_associational using solve [push_eval; distr_length] : push_eval.
143
144 Lemma from_associational_partitions n idxs p (_:n<>0%nat):
145 from_associational idxs n p = Partition.partition sw n (Associational.eval p).
146 Proof using dwprops swprops.
147 intros. cbv [from_associational].
148 rewrite Rows.flatten_correct with (n:=n) by eauto using Rows.length_from_associational.
149 rewrite Associational.bind_snd_correct.
150 push_eval.
151 Qed.
152
153 Derive from_associational_inlined
154 SuchThat (forall idxs n p,
155 from_associational_inlined idxs n p = from_associational idxs n p)
156 As from_associational_inlined_correct.
157 Proof.
158 intros.
159 cbv beta iota delta [from_associational reordering_carry Associational.carryterm].
160 cbv beta iota delta [Let_In]. (* inlines all shifts/lands from carryterm *)
161 cbv beta iota delta [from_associational Rows.from_associational Columns.from_associational].
162 cbv beta iota delta [Let_In]. (* inlines the shifts from place *)
163 subst from_associational_inlined; reflexivity.
164 Qed.
165
166 Derive to_associational_inlined
167 SuchThat (forall n m p,
168 to_associational_inlined n m p = to_associational n m p)
169 As to_associational_inlined_correct.
170 Proof.
171 intros.
172 cbv beta iota delta [ to_associational convert_bases ].
173 cbv beta iota delta [ Columns.from_associational ].
174 cbv beta iota delta [Let_In]. (* inlines dlets from Columns.from_associaitonal *)
175 cbv beta iota delta [ Columns.flatten Columns.flatten_step
176 Columns.flatten_column ].
177 cbv beta iota delta [Let_In]. (* inlines dlets from Columns.flatten *)
178 subst to_associational_inlined; reflexivity.
179 Qed.
180
181 (* carry chain that aligns terms in the intermediate weight with the final weight *)
182 Definition aligned_carries (log_dw_sw nout : nat)
183 := (map (fun i => ((log_dw_sw * (i + 1)) - 1))%nat (seq 0 nout)).
184
185 Section mul_converted.
186 Definition mul_converted
187 n1 n2 (* lengths in original format *)
188 m1 m2 (* lengths in converted format *)
189 (n3 : nat) (* final length *)
190 (idxs : list nat) (* carries to do -- this helps preemptively line up weights *)
191 (p1 p2 : list Z) :=
192 let p1_a := to_associational n1 m1 p1 in
193 let p2_a := to_associational n2 m2 p2 in
194 let p3_a := Associational.mul p1_a p2_a in
195 from_associational idxs n3 p3_a.
196
197 Lemma eval_mul_converted n1 n2 m1 m2 n3 idxs p1 p2 (_:n3<>0%nat) (_:m1<>0%nat) (_:m2<>0%nat):
198 length p1 = n1 -> length p2 = n2 ->
199 0 <= (Positional.eval sw n1 p1 * Positional.eval sw n2 p2) < sw n3 ->
200 Positional.eval sw n3 (mul_converted n1 n2 m1 m2 n3 idxs p1 p2) = (Positional.eval sw n1 p1) * (Positional.eval sw n2 p2).
201 Proof using dwprops swprops. cbv [mul_converted]; push_eval. Qed.
202 Hint Rewrite eval_mul_converted : push_eval.
203
204 Lemma mul_converted_partitions n1 n2 m1 m2 n3 idxs p1 p2 (_:n3<>0%nat) (_:m1<>0%nat) (_:m2<>0%nat):
205 length p1 = n1 -> length p2 = n2 ->
206 mul_converted n1 n2 m1 m2 n3 idxs p1 p2 = Partition.partition sw n3 (Positional.eval sw n1 p1 * Positional.eval sw n2 p2).
207 Proof using dwprops swprops.
208 intros; cbv [mul_converted].
209 rewrite from_associational_partitions by auto. push_eval.
210 Qed.
211 End mul_converted.
212 End BaseConversion.
213 #[global]
214 Hint Rewrite length_convert_bases : distr_length.
215
216 (* multiply two (n*k)-bit numbers by converting them to n k-bit limbs each, multiplying, then converting back *)
217 Section widemul.
218 Context (log2base : Z) (log2base_pos : 0 < log2base).
219 Context (m n : nat) (m_nz : m <> 0%nat) (n_nz : n <> 0%nat) (n_le_log2base : Z.of_nat n <= log2base).
220 Let dw : nat -> Z := weight (log2base / Z.of_nat n) 1.
221 Let sw : nat -> Z := weight log2base 1.
222 Let mn := (m * n)%nat.
223 Let nout := (m * 2)%nat.
224
225 Local Lemma mn_nonzero : mn <> 0%nat. Proof. subst mn. apply Nat.neq_mul_0. auto. Qed.
226 Local Hint Resolve mn_nonzero : core.
227 Local Lemma nout_nonzero : nout <> 0%nat. Proof. subst nout. apply Nat.neq_mul_0. auto. Qed.
228 Local Hint Resolve nout_nonzero : core.
229 Local Lemma base_bounds : 0 < 1 <= log2base. Proof using log2base_pos. clear -log2base_pos; auto with zarith. Qed.
230 Local Lemma dbase_bounds : 0 < 1 <= log2base / Z.of_nat n. Proof using n_nz n_le_log2base. clear -n_nz n_le_log2base; auto with zarith. Qed.
231 Let dwprops : @weight_properties dw := wprops (log2base / Z.of_nat n) 1 dbase_bounds.
232 Let swprops : @weight_properties sw := wprops log2base 1 base_bounds.
233 Local Notation deval := (Positional.eval dw).
234 Local Notation seval := (Positional.eval sw).
235
236 Definition widemul a b := mul_converted sw dw m m mn mn nout (aligned_carries n nout) a b.
237
238 Lemma widemul_correct a b :
239 length a = m ->
240 length b = m ->
241 widemul a b = Partition.partition sw nout (seval m a * seval m b).
242 Proof. apply mul_converted_partitions; auto with zarith. Qed.
243
244 Derive widemul_inlined
245 SuchThat (forall a b,
246 length a = m ->
247 length b = m ->
248 widemul_inlined a b = Partition.partition sw nout (seval m a * seval m b))
249 As widemul_inlined_correct.
250 Proof.
251 intros.
252 rewrite <-widemul_correct by auto.
253 cbv beta iota delta [widemul mul_converted].
254 rewrite <-to_associational_inlined_correct with (p:=a).
255 rewrite <-to_associational_inlined_correct with (p:=b).
256 rewrite <-from_associational_inlined_correct.
257 subst widemul_inlined; reflexivity.
258 Qed.
259
260 Derive widemul_inlined_reverse
261 SuchThat (forall a b,
262 length a = m ->
263 length b = m ->
264 widemul_inlined_reverse a b = Partition.partition sw nout (seval m a * seval m b))
265 As widemul_inlined_reverse_correct.
266 Proof.
267 intros.
268 rewrite <-widemul_inlined_correct by assumption.
269 cbv [widemul_inlined].
270 match goal with |- _ = from_associational_inlined sw dw ?idxs ?n ?p =>
271 transitivity (from_associational_inlined sw dw idxs n (rev p));
272 [ | transitivity (from_associational sw dw idxs n p); [ | reflexivity ] ](* reverse to make addc chains line up *)
273 end.
274 { subst widemul_inlined_reverse; reflexivity. }
275 { rewrite from_associational_inlined_correct by auto.
276 cbv [from_associational].
277 rewrite !Rows.flatten_correct by eauto using Rows.length_from_associational.
278 rewrite !Rows.eval_from_associational by auto.
279 f_equal.
280 rewrite !eval_carries, !Associational.bind_snd_correct, !Associational.eval_rev by auto.
281 reflexivity. }
282 Qed.
283 End widemul.
284 End BaseConversion.
285
286 Section base_conversion_mod_ops.
287 Import Positional.
288 Import BaseConversion.
289 Local Coercion Z.of_nat : nat >-> Z.
290 (* Design constraints:
291 - inputs must be [Z] (b/c reification does not support Q)
292 - internal structure must not match on the arguments (b/c reification does not support [positive]) *)
293 Context (src_limbwidth_num src_limbwidth_den : Z)
294 (src_limbwidth_good : 0 < src_limbwidth_den <= src_limbwidth_num)
295 (dst_limbwidth_num dst_limbwidth_den : Z)
296 (dst_limbwidth_good : 0 < dst_limbwidth_den <= dst_limbwidth_num)
297 (src_n : nat)
298 (dst_n : nat)
299 (bitwidth : Z)
300 (Hsrc_n_nz : src_n <> 0%nat)
301 (Hdst_n_nz : dst_n <> 0%nat).
302 Local Notation src_weight := (@weight src_limbwidth_num src_limbwidth_den).
303 Local Notation dst_weight := (@weight dst_limbwidth_num dst_limbwidth_den).
304
305 Local Notation src_wprops := (@wprops src_limbwidth_num src_limbwidth_den src_limbwidth_good).
306 Local Notation dst_wprops := (@wprops dst_limbwidth_num dst_limbwidth_den dst_limbwidth_good).
307
308 Local Definition src_wunique := (@weight_unique src_limbwidth_num src_limbwidth_den src_limbwidth_good).
309 Local Definition dst_wunique := (@weight_unique dst_limbwidth_num dst_limbwidth_den dst_limbwidth_good).
310
311 Local Definition src_wprops' := src_wprops.
312 Local Definition dst_wprops' := dst_wprops.
313 Local Definition weight_0_src_wprops := weight_0 src_wprops.
314 Local Definition weight_positive_src_wprops := weight_positive src_wprops.
315 Local Definition weight_multiples_src_wprops := weight_multiples src_wprops.
316 Local Definition weight_divides_src_wprops := weight_divides src_wprops.
317 Local Definition weight_0_dst_wprops := weight_0 dst_wprops.
318 Local Definition weight_positive_dst_wprops := weight_positive dst_wprops.
319 Local Definition weight_multiples_dst_wprops := weight_multiples dst_wprops.
320 Local Definition weight_divides_dst_wprops := weight_divides dst_wprops.
321
322 Local Hint Immediate src_wprops' : core.
323 Local Hint Immediate src_wunique : core.
324 Local Hint Immediate weight_0_src_wprops : core.
325 Local Hint Immediate weight_positive_src_wprops : core.
326 Local Hint Immediate weight_multiples_src_wprops : core.
327 Local Hint Immediate weight_divides_src_wprops : core.
328 Local Hint Immediate dst_wprops' : core.
329 Local Hint Immediate dst_wunique : core.
330 Local Hint Immediate weight_0_dst_wprops : core.
331 Local Hint Immediate weight_positive_dst_wprops : core.
332 Local Hint Immediate weight_multiples_dst_wprops : core.
333 Local Hint Immediate weight_divides_dst_wprops : core.
334
335 Definition convert_bases (v : list Z)
336 := BaseConversion.convert_bases src_weight dst_weight src_n dst_n v.
337
338 Definition convert_basesmod (f : list Z) : list Z
339 := convert_bases f.
340
341 Lemma eval_convert_bases
342 : forall (f : list Z)
343 (Hf : length f = src_n),
344 eval dst_weight dst_n (convert_bases f) = eval src_weight src_n f.
345 Proof using Hdst_n_nz src_limbwidth_good dst_limbwidth_good.
346 generalize src_wprops dst_wprops; clear -Hdst_n_nz.
347 intros.
348 cbv [convert_bases].
349 rewrite BaseConversion.eval_convert_bases by auto.
350 reflexivity.
351 Qed.
352
353 Lemma convert_bases_partitions
354 : forall (f : list Z)
355 (Hf_small : 0 <= eval src_weight src_n f < dst_weight dst_n),
356 convert_bases f = Partition.partition dst_weight dst_n (Positional.eval src_weight src_n f).
357 Proof using dst_limbwidth_good Hdst_n_nz.
358 clear -dst_limbwidth_good Hdst_n_nz.
359 intros; cbv [convert_bases].
360 apply BaseConversion.convert_bases_partitions; eauto.
361 Qed.
362
363 Lemma eval_convert_basesmod
364 : forall (f : list Z)
365 (Hf : length f = src_n),
366 eval dst_weight dst_n (convert_basesmod f) = eval src_weight src_n f.
367 Proof using Hdst_n_nz src_limbwidth_good dst_limbwidth_good. apply eval_convert_bases. Qed.
368
369 Lemma convert_basesmod_partitions
370 : forall (f : list Z)
371 (Hf_small : 0 <= eval src_weight src_n f < dst_weight dst_n),
372 convert_basesmod f = Partition.partition dst_weight dst_n (Positional.eval src_weight src_n f).
373 Proof using dst_limbwidth_good Hdst_n_nz. apply convert_bases_partitions. Qed.
374
375 Lemma eval_convert_basesmod_and_partitions
376 : forall (f : list Z)
377 (Hf : length f = src_n)
378 (Hf_small : 0 <= eval src_weight src_n f < dst_weight dst_n),
379 eval dst_weight dst_n (convert_basesmod f) = eval src_weight src_n f
380 /\ convert_basesmod f = Partition.partition dst_weight dst_n (Positional.eval src_weight src_n f).
381 Proof using src_limbwidth_good dst_limbwidth_good Hdst_n_nz.
382 now (split; [ apply eval_convert_basesmod | apply convert_bases_partitions ]).
383 Qed.
384 End base_conversion_mod_ops.
385 #[global]
386 Hint Rewrite eval_convert_basesmod eval_convert_bases : push_eval.