Debian Codegraph @

src/Arithmetic/DettmanMultiplication.v

raw
1 Require Import Crypto.Arithmetic.Core.
2 From Coq Require Import ZArith Lia.
3 From Coq Require Import List.
4 Require Import Crypto.Arithmetic.ModOps.
5 From Coq Require Import QArith_base Qround.
6 Local Open Scope list_scope.
7
8 Import Associational Positional.
9 Import ListNotations. Local Open Scope Z_scope.
10
11 Local Coercion Z.of_nat : nat >-> Z.
12
13 Module DettmanMultiplication.
14 Section DettmanMultiplication.
15 Context
16 (s : Z)
17 (c' : Z)
18 (register_width : nat)
19 (n : nat)
20 (last_reduction : nat) (* should be between 1 and n - 3, inclusive.
21 This is the position that the final modular reduction lands in.
22 Larger values correspond to a faster algorithm but looser bounds on the output.
23 *)
24 (weight: nat -> Z)
25 (p_nz' : s - c' <> 0)
26 (n_gteq_4 : (4 <= n)%nat) (* Technically we only need 2 <= n to get the proof to go through, but it doesn't make much sense to try to do this with less than four limbs.
27 Note that having 4 limbs corresponds to zero iterations of the "loop" function defined below. *)
28 (s_small : forall i: nat, (weight (i + n)%nat / weight i) mod s = 0)
29 (s_big : weight (n - 1)%nat <= s)
30 (weight_lt_width : forall i: nat, (weight i * 2^register_width) mod weight (i + 1)%nat = 0)
31 (wprops : @weight_properties weight)
32
33 (weight_0 := weight_0 wprops)
34 (weight_positive := weight_positive wprops)
35 (weight_multiples := weight_multiples wprops)
36 (weight_divides := weight_divides wprops).
37
38 (* Proofs will go through regardless of which encoding we choose for c.
39 We choose this encoding (with the weights given by the weight function)
40 so that our reductions land in the weights given by the weight function.
41 (Note that this won't necessarily work out nicely if we have a fractional
42 limbwidth.)
43 *)
44 Definition c := Positional.to_associational weight n
45 (Positional.simple_encode weight n c').
46
47 Lemma s_positive : s > 0.
48 Proof. remember (weight_positive (n - 1)). lia. Qed.
49
50 Lemma s_nz : s <> 0.
51 Proof. remember s_positive. lia. Qed.
52
53 Lemma weight_nz : forall i, weight i <> 0.
54 Proof. intros i. remember (weight_positive i). lia. Qed.
55
56 Lemma div_mul_le : forall x y, y > 0 -> x / y * y <= x.
57 Proof. intros x y H. remember (Zmod_eq x y H). remember (Z_mod_lt x y H). lia. Qed.
58
59 Lemma div_nz a b : b > 0 -> b <= a -> a / b <> 0.
60 Proof. Z.div_mod_to_equations. lia. Qed.
61
62 Lemma weight_div_nz : forall i j : nat, (i <= j)%nat -> weight j / weight i <> 0.
63 Proof.
64 intros i j H. assert (0 < weight j / weight i); try lia.
65 apply Weight.weight_divides_full; assumption.
66 Qed.
67
68 Lemma s_small' (i j : nat) :
69 (j = i + n)%nat ->
70 weight j / weight i mod s = 0.
71 Proof. intros H. subst. apply s_small. Qed.
72
73 Lemma s_big' : s / weight (n - 1) <> 0.
74 Proof. remember (weight_positive (n - 1)). apply div_nz; lia. Qed.
75
76 Lemma weight_increasing : forall i j : nat, (i <= j)%nat -> weight i <= weight j.
77 Proof.
78 intros i j H.
79 assert (0 < weight j / weight i). { apply Weight.weight_divides_full; try assumption. }
80 assert (1 <= weight j / weight i) by lia.
81 assert (1 * weight i <= weight j / weight i * weight i).
82 { apply Zmult_le_compat_r; try lia. remember (weight_positive i). lia. }
83 apply (Z.le_trans _ (weight j / weight i * weight i) _); try lia.
84 apply div_mul_le. remember (weight_positive i). lia.
85 Qed.
86
87 Lemma mod_quotient_zero : forall x y, 0 < y -> x mod y = 0 -> x mod (x / y) = 0.
88 Proof.
89 intros x y H H1. rewrite Divide.Z.mod_divide_full in H1. destruct H1 as [z H1].
90 subst. rewrite Z_div_mult by lia. rewrite Z.mul_comm. apply Z_mod_mult.
91 Qed.
92
93 Lemma weight_mod_quotient_zero : forall i j : nat,
94 (i <= j)%nat ->
95 (weight j) mod (weight j / weight i) = 0.
96 Proof.
97 intros i j H. apply mod_quotient_zero; try apply weight_positive.
98 apply Weight.weight_multiples_full; assumption.
99 Qed.
100
101 Lemma divisible_implies_nonzero a b :
102 a mod b = 0 ->
103 a <> 0 ->
104 a / b <> 0.
105 Proof. intros H1 H2. remember (Z_div_mod_eq_full a b). lia. Qed.
106
107 Hint Resolve s_positive s_nz weight_nz div_nz s_big' : arith.
108 Hint Resolve weight_0 weight_positive weight_multiples Weight.weight_multiples_full : arith.
109 Hint Resolve weight_div_nz weight_mod_quotient_zero : arith.
110
111 Lemma c_correct : Associational.eval c = c'.
112 Proof.
113 cbv [c]. rewrite eval_to_associational.
114 replace (s - c') with (s - Associational.eval [(1, c')]).
115 - apply eval_simple_encode; auto with arith. lia.
116 - f_equal. cbv [Associational.eval]. simpl. destruct c'; lia.
117 Qed.
118
119 Lemma p_nz : s - Associational.eval c <> 0.
120 Proof. rewrite c_correct. auto with arith. Qed.
121 Hint Resolve p_nz : arith.
122
123 Local Open Scope nat_scope.
124
125 Definition reduce' from to before := dedup_weights (reduce_one s from (from / to) c before).
126 Definition carry' from to before := dedup_weights (Associational.carry from (to / from) before).
127 Definition borrow' from to before := dedup_weights (borrow from (from / to) before).
128
129 Hint Rewrite eval_reduce_one Associational.eval_carry eval_dedup_weights eval_borrow : push_eval.
130
131 Definition carry_from_position i x :=
132 carry' (weight i) (weight (i + 1)) x.
133
134 Lemma eval_carry_from_position i x :
135 Associational.eval (carry_from_position i x) = Associational.eval x.
136 Proof.
137 cbv [carry_from_position carry']. autorewrite with push_eval; auto with arith. Qed.
138 Hint Rewrite eval_carry_from_position : push_eval.
139
140 Definition carry_reduce i x :=
141 let x := carry_from_position (i + n) x in
142 let x := reduce' (weight (i + n)) (weight i) x in
143 carry_from_position i x.
144
145 Lemma eval_carry_reduce i x :
146 (Associational.eval (carry_reduce i x) mod (s - Associational.eval c) =
147 Associational.eval x mod (s - Associational.eval c))%Z.
148 Proof. cbv [carry_reduce reduce']. autorewrite with push_eval; auto with arith. Qed.
149 Hint Rewrite eval_carry_reduce : push_eval.
150
151 (* rw stands for "register_width".
152 The idea here is that, if x has value 0 at weight (i + n + 1), then
153 carry_reduce_rw i x = carry_reduce (i + 1) (carry_reduce i x).
154
155 The benefit of using carry_reduce_rw, then, instead of a couple of carry_reduces,
156 is that (in C, or something similar), the LHS of the equation above takes less work
157 to calculate than the RHS. This is because carries by 2^register_width are very easy.
158 *)
159 Definition carry_reduce_rw i x :=
160 let x := carry' (weight (i + n)) (weight (i + n) * 2^register_width) x in
161 let x := reduce' (weight (i + n)) (weight i) x in
162 let x := carry_from_position i x in
163 let x := reduce' (weight (i + n) * 2^register_width) (weight (i + 1)) x in
164 carry_from_position (i + 1) x.
165
166 Local Open Scope Z_scope.
167
168 Lemma reduction_divides i : weight (n + i - 1) * 2^register_width / weight i mod s = 0.
169 Proof.
170 rewrite Divide.Z.mod_divide_full. apply (Z.divide_trans _ (weight (n + i) / weight i)).
171 - rewrite <- Divide.Z.mod_divide_full. rewrite (Nat.add_comm n i). apply s_small.
172 - apply Z.divide_div.
173 + remember (weight_positive i). lia.
174 + rewrite <- Divide.Z.mod_divide_full. apply Weight.weight_multiples_full; try assumption.
175 lia.
176 + rewrite <- Divide.Z.mod_divide_full.
177 replace (weight (n + i)) with (weight (n + i - 1 + 1)).
178 -- apply weight_lt_width.
179 -- f_equal. lia.
180 Qed.
181
182 Lemma reduction_divides' (i j : nat) :
183 (j = n + i - 1)%nat ->
184 weight j * 2^register_width / weight i mod s = 0.
185 Proof. intros H. subst. apply reduction_divides. Qed.
186
187 Lemma weight_prod_mod_zero (i j : nat) :
188 (i <= j)%nat ->
189 (weight j * 2^register_width) mod (weight i) = 0.
190 Proof.
191 intros H. apply Divide.Z.mod_divide_full. apply Z.divide_mul_l.
192 rewrite <- Divide.Z.mod_divide_full. apply Weight.weight_multiples_full; try assumption.
193 Qed.
194
195 Lemma weight_prod_div_nz (i j : nat) :
196 (i <= j)%nat ->
197 weight j * 2^register_width / weight i <> 0.
198 Proof.
199 intros H. apply divisible_implies_nonzero. apply weight_prod_mod_zero; try lia.
200 remember (weight_positive j). remember (Z.pow_nonneg 2 register_width). lia.
201 Qed.
202
203 Lemma s_small_particular : weight n mod s = 0.
204 Proof.
205 replace (weight n) with (weight n / weight 0).
206 - apply s_small'; lia.
207 - Z.div_mod_to_equations. lia.
208 Qed.
209
210 Lemma eval_carry_reduce_rw i x :
211 (Associational.eval (carry_reduce_rw i x) mod (s - Associational.eval c) =
212 Associational.eval x mod (s - Associational.eval c))%Z.
213 Proof.
214 cbv [carry_reduce_rw carry' reduce']. autorewrite with push_eval; auto with arith.
215 - apply weight_prod_div_nz; lia.
216 - apply weight_prod_div_nz; lia.
217 - apply mod_quotient_zero; auto with arith. apply weight_prod_mod_zero; lia.
218 - apply reduction_divides'; lia.
219 Qed.
220 Hint Rewrite eval_carry_reduce_rw : push_eval.
221
222 Definition carry_chain idxs x :=
223 fold_right carry_from_position x (rev idxs).
224
225 Lemma eval_carry_chain idxs x :
226 Associational.eval (carry_chain idxs x) = Associational.eval x.
227 Proof.
228 cbv [carry_chain]. induction (rev idxs).
229 - reflexivity.
230 - simpl. autorewrite with push_eval. assumption.
231 Qed.
232 Hint Rewrite eval_carry_chain : push_eval.
233
234 Definition carry_reduce_chain idxs x :=
235 fold_right carry_reduce x (rev idxs).
236
237 Lemma eval_carry_reduce_chain idxs x :
238 ((Associational.eval (carry_reduce_chain idxs x)) mod (s - Associational.eval c) = (Associational.eval x) mod (s - Associational.eval c))%Z.
239 Proof.
240 cbv [carry_reduce_chain]. induction (rev idxs).
241 - reflexivity.
242 - simpl. autorewrite with push_eval. assumption.
243 Qed.
244 Hint Rewrite eval_carry_reduce_chain : push_eval.
245
246 Hint Resolve s_small_particular divisible_implies_nonzero Z_mod_same_full : arith.
247
248 (* combine the values at position (n - 1) and n;
249 then, split the combined value into
250 pieces at position (n - 1) and weight s.
251 *)
252 Definition move_to_weight_s x :=
253 let x := carry' (weight (n - 1)) s x in
254 borrow' (weight n) s x.
255
256 Lemma eval_move_to_weight_s x :
257 Associational.eval (move_to_weight_s x) = Associational.eval x.
258 Proof.
259 cbv [move_to_weight_s borrow' carry']. autorewrite with push_eval; auto with arith.
260 apply mod_quotient_zero; auto with arith. remember s_positive. lia.
261 Qed.
262 Hint Rewrite eval_move_to_weight_s : push_eval.
263
264 (* This function is a reduction from position n to position 0---
265 the main idea is to do something like 'let x := carry_reduce 0 x'.
266
267 In fact, we move everything from position n to weight s, as well
268 as taking the top few bits off position (n - 1) to put them in weight s,
269 before doing the reduction from weight s to position 0.
270
271 This gives us the opportunity to reduce from the top of position (n - 1),
272 as well as from position n, while only having to do one reduction.
273 *)
274 Definition carry_reduce_s x :=
275 let x := carry_from_position n x in
276 let x := move_to_weight_s x in
277 let x := reduce' s (weight 0) x in
278 carry_from_position 0 x.
279
280 Lemma eval_carry_reduce_s x :
281 ((Associational.eval (carry_reduce_s x)) mod (s - Associational.eval c) =
282 (Associational.eval x) mod (s - Associational.eval c))%Z.
283 Proof.
284 cbv [carry_reduce_s reduce']. autorewrite with push_eval; auto with arith.
285 - rewrite weight_0. rewrite Z.div_1_r. apply s_nz.
286 - rewrite weight_0. rewrite Z.div_1_r. auto with arith.
287 - rewrite weight_0. rewrite Z.div_1_r. auto with arith.
288 Qed.
289 Hint Rewrite eval_carry_reduce_s : push_eval.
290
291 Local Open Scope nat_scope.
292
293 Definition seq_from_to a b := seq a (Z.to_nat (b - a + 1)).
294
295 (* The main idea here is to have something like this:
296 Definition overly_simple_reduce_carry_borrow x :=
297 carry_reduce_chain (seq_from_to 0 (n - 1)) x.
298 Below, we have something like this, with a few extra things added in.
299 *)
300 Definition reduce_carry_borrow x :=
301 let x := dedup_weights x in
302
303 (* In the 'overly_simple_reduce_carry_borrow', the following code block
304 would belong in the position marked with an 'A' down below.
305 It turns out that doing that might make the output bounds too loose.
306
307 In particular, we need last_reduction to be small enough so that the final
308 carry chain (right below the 'A') does not overflow position (n - 1).
309 We solve this issue by putting the code block here (instad of at 'A').
310
311 This way, the reductions that land in positions
312 last_reduction + 1, last_reduction + 2, ..., n - 2, n - 1
313 are carried up to position n (before the reduction from position n),
314 so we don't have to worry about them overflowing position (n - 1).
315
316 Note that, other than this constraint requiring last_reduction to be
317 sufficiently small, we want to choose the last_reduction parameter to
318 be as large as possible, since when it gets smaller, the final carry
319 chain gets longer (and everything else takes the same amount of time).
320 *)
321 let x := carry_reduce_chain (seq_from_to (last_reduction + 1) (n - 3)) x in
322 let x := carry_reduce_rw (n - 2) x in
323
324 (* This next code block is similar to writing
325 let x := carry_reduce 0 x,
326 except it's a bit more clever.
327 While reducing from position n to position 0, we simultaneously reduce
328 the top few bits of position (n - 1) down to position 0.
329 *)
330 let x := carry_reduce_s x in
331
332 (* This code block is what remains (after we've finished adding in all the
333 extra stuff) of the 'overly_simple_reduce_carry_borrow' referred to above.
334
335 The only clever part is that the last two carry_reduces have been replaced
336 with a carry_reduce_rw.
337 *)
338 let x := carry_reduce_chain (seq_from_to 1 (last_reduction - 1)) x in
339 let x := carry_reduce_rw last_reduction x in
340
341 (* A *)
342
343 let x := carry_chain (seq_from_to (last_reduction + 2) (n - 2)) x in
344
345 Positional.from_associational weight n x.
346
347 Definition mulmod a b :=
348 let a_assoc := Positional.to_associational weight n a in
349 let b_assoc := Positional.to_associational weight n b in
350 let x := Associational.mul a_assoc b_assoc in
351 reduce_carry_borrow x.
352
353 Definition squaremod a :=
354 let a_assoc := Positional.to_associational weight n a in
355 let x := Associational.square a_assoc in
356 reduce_carry_borrow x.
357
358 Hint Rewrite Positional.eval_from_associational Positional.eval_to_associational : push_eval.
359
360 Local Open Scope Z_scope.
361
362 Lemma eval_reduce_carry_borrow r0 :
363 (Positional.eval weight n (reduce_carry_borrow r0)) mod (s - Associational.eval c) =
364 (Associational.eval r0) mod (s - Associational.eval c).
365 Proof.
366 cbv [reduce_carry_borrow carry' reduce' borrow'].
367 autorewrite with push_eval; auto with arith; try lia.
368 Qed.
369 Hint Rewrite eval_reduce_carry_borrow : push_eval.
370
371 Theorem eval_mulmod a b :
372 (Positional.eval weight n (mulmod a b)) mod (s - c') =
373 (Positional.eval weight n a * Positional.eval weight n b) mod (s - c').
374 Proof.
375 cbv [mulmod]. rewrite <- c_correct. autorewrite with push_eval. reflexivity.
376 Qed.
377
378 Theorem eval_squaremod a :
379 (Positional.eval weight n (squaremod a)) mod (s - c') =
380 (Positional.eval weight n a * Positional.eval weight n a) mod (s - c').
381 Proof.
382 cbv [squaremod]. rewrite <- c_correct. autorewrite with push_eval. reflexivity.
383 Qed.
384 End DettmanMultiplication.
385 End DettmanMultiplication.
386
387 Module dettman_multiplication_mod_ops.
388 Section dettman_multiplication_mod_ops.
389 Import DettmanMultiplication.
390 Local Open Scope Z_scope.
391 Local Coercion QArith_base.inject_Z : Z >-> Q.
392 Local Coercion Z.pos : positive >-> Z.
393 Context
394 (s : Z)
395 (c : Z)
396 (register_width : nat)
397 (n : nat)
398 (last_limb_width : nat)
399 (last_reduction : nat)
400 (p_nz : s - c <> 0)
401 (n_gteq_4 : (4 <= n)%nat)
402 (last_limb_width_small : last_limb_width * n <= Z.log2 s)
403 (last_limb_width_big : 1 <= last_limb_width)
404 (s_power_of_2 : 2 ^ (Z.log2 s) = s).
405
406 Local Notation limbwidth_num' := (Z.log2 s - last_limb_width).
407 Local Notation limbwidth_den' := (n - 1). (* can't use Q here, or else reification doesn't work *)
408
409 Context
410 (registers_big : limbwidth_num' <= register_width * limbwidth_den') (* stated somewhat awkwardly in terms of Z; i think we might want to avoid Q here too *)
411 (weight_big : Z.log2 s <= n * limbwidth_num' / limbwidth_den').
412
413 (* I don't want these to be automatically unfolded in the proofs below. *)
414 Definition limbwidth_num := limbwidth_num'.
415 Definition limbwidth_den := limbwidth_den'.
416
417 Definition weight := (weight limbwidth_num limbwidth_den).
418
419 Definition mulmod := mulmod s c register_width n last_reduction weight.
420 Definition squaremod := squaremod s c register_width n last_reduction weight.
421
422 Lemma n_small : n - 1 <= Z.log2 s - last_limb_width.
423 Proof.
424 replace (Z.of_nat n) with (Z.of_nat n - 1 + 1) in last_limb_width_small by lia.
425 remember (Z.of_nat n - 1) as n'.
426 rewrite Z.mul_add_distr_l in last_limb_width_small.
427 remember (Z.of_nat last_limb_width) as l.
428 assert (H: n' <= l * n').
429 { replace n' with (1 * n') by lia. replace (l * (1 * n')) with (l * n') by lia.
430 apply Zmult_le_compat_r; lia. }
431 lia.
432 Qed.
433
434 Lemma limbwidth_good : 0 < limbwidth_den <= limbwidth_num.
435 Proof. remember n_small. cbv [limbwidth_den limbwidth_num]. lia. Qed.
436
437 Local Notation wprops := (@wprops limbwidth_num limbwidth_den limbwidth_good).
438
439 Lemma Qceiling_diff x y : Qfloor (x - y) <= Qceiling x - Qceiling y.
440 Proof.
441 assert (H: Qfloor (x - y) + Qceiling y <= Qceiling x).
442 - replace (Qceiling x) with (Qceiling (x - y + y))%Q.
443 + apply QUtil.add_floor_l_le_ceiling.
444 + cbv [Qminus].
445 rewrite <- Qplus_assoc. rewrite (Qplus_comm (-y) y).
446 rewrite Qplus_opp_r. rewrite Qplus_0_r. reflexivity.
447 - lia.
448 Qed.
449
450 Lemma Qopp_distr_mul_r x y : (- (x * y) == x * -y)%Q.
451 Proof. cbv [Qmult Qopp Qeq]. simpl. lia. Qed.
452
453 Lemma s_small : forall i : nat, (weight (i + n) / weight i) mod s = 0.
454 Proof.
455 intros i. repeat rewrite (ModOps.weight_ZQ_correct _ _ limbwidth_good).
456 rewrite <- Z.pow_sub_r; try lia.
457 - rewrite <- s_power_of_2. apply Modulo.Z.mod_same_pow. split.
458 + apply Z.log2_nonneg.
459 + remember (_ * (i + n)%nat)%Q as x. remember (_ * i)%Q as y.
460 apply (Z.le_trans _ (Qfloor (x - y))).
461 -- subst. cbv [Qminus]. rewrite Qopp_distr_mul_r. rewrite <- Qmult_plus_distr_r.
462 rewrite <- inject_Z_opp. rewrite <- inject_Z_plus.
463 replace (Z.of_nat (i + n) + - Z.of_nat i) with (Z.of_nat n) by lia.
464 replace (Z.log2 s) with (Qfloor (inject_Z (Z.log2 s))).
465 ++ apply Qfloor_resp_le. rewrite Qmult_comm.
466 apply (Qle_trans _ (inject_Z (n * limbwidth_num / limbwidth_den)))%Z.
467 --- rewrite <- Zle_Qle. apply weight_big.
468 --- cbv [Qdiv]. rewrite Zdiv_Qdiv.
469 apply (Qle_trans _ ((n * limbwidth_num)%Z / limbwidth_den)).
470 +++ apply Qfloor_le.
471 +++ rewrite inject_Z_mult. rewrite Qmult_assoc. apply Qle_refl.
472 ++ apply Qfloor_Z.
473 -- apply Qceiling_diff.
474 - remember limbwidth_good as H eqn:clearMe; clear clearMe. split.
475 + replace 0 with (Qceiling 0) by reflexivity. apply Qceiling_resp_le.
476 apply Qmult_le_0_compat.
477 -- cbv [Qdiv]. apply Qmult_le_0_compat.
478 ++ replace 0%Q with (inject_Z 0) by reflexivity. rewrite <- Zle_Qle. lia.
479 ++ apply Qinv_le_0_compat. replace 0%Q with (inject_Z 0) by reflexivity.
480 rewrite <- Zle_Qle. lia.
481 -- replace 0%Q with (inject_Z 0) by reflexivity. rewrite <- Zle_Qle. lia.
482 + apply Qceiling_resp_le. rewrite Qmult_comm. rewrite (Qmult_comm (_ / _)).
483 apply Qmult_le_compat_r.
484 -- rewrite <- Zle_Qle. lia.
485 -- cbv [Qdiv]. apply Qmult_le_0_compat.
486 ++ replace 0%Q with (inject_Z 0) by reflexivity. rewrite <- Zle_Qle. lia.
487 ++ apply Qinv_le_0_compat. replace 0%Q with (inject_Z 0) by reflexivity.
488 rewrite <- Zle_Qle. lia.
489 Qed.
490
491 Lemma s_gt_0 : 0 < s.
492 assert (H: s <= 0 \/ 0 < s) by lia. destruct H as [H|H].
493 - apply Z.log2_nonpos in H. lia.
494 - assumption.
495 Qed.
496
497 Lemma s_big : weight (n - 1) <= s.
498 Proof.
499 rewrite (ModOps.weight_ZQ_correct _ _ limbwidth_good).
500 cbv [limbwidth_num limbwidth_den].
501 remember (Z.log2_spec _ s_gt_0) as H eqn:clearMe. clear clearMe.
502 destruct H as [H _].
503 apply (Z.le_trans _ (2 ^ Z.log2 s)); try apply H.
504 apply Z.pow_le_mono_r; try lia.
505 rewrite Zle_Qle. cbv [Qdiv]. rewrite <- (Qmult_assoc _ (Qinv _)).
506 rewrite (Qmult_comm (Qinv _)). rewrite Nat2Z.inj_sub; try lia. simpl. cbv [Z.sub].
507 rewrite inject_Z_plus. simpl. cbv [Qminus]. rewrite Qmult_inv_r.
508 - rewrite <- inject_Z_plus. rewrite Qmult_1_r. rewrite Qceiling_Z.
509 rewrite <- Zle_Qle. remember (Z.le_log2_up_succ_log2 s). lia.
510 - replace 0%Q with (inject_Z 0) by reflexivity. rewrite inject_Z_injective. lia.
511 Qed.
512
513 Lemma weight_lt_width : forall i: nat, (weight i * 2^register_width) mod weight (i + 1)%nat = 0.
514 Proof.
515 intros i. repeat rewrite (ModOps.weight_ZQ_correct _ _ limbwidth_good).
516 remember limbwidth_good eqn:clearMe; clear clearMe.
517 rewrite <- Z.pow_add_r; try lia.
518 - apply Modulo.Z.mod_same_pow. split.
519 + remember (_ / _ * _)%Q as x. replace 0 with (Qceiling 0%Z) by reflexivity.
520 apply Qceiling_resp_le. subst. replace (inject_Z 0) with 0%Q by reflexivity.
521 cbv [Qdiv]. apply Qmult_le_0_compat.
522 -- apply Qmult_le_0_compat.
523 ++ replace 0%Q with (inject_Z 0) by reflexivity. rewrite <- Zle_Qle. lia.
524 ++ apply Qinv_le_0_compat. replace 0%Q with (inject_Z 0) by reflexivity.
525 rewrite <- Zle_Qle. lia.
526 -- replace 0%Q with (inject_Z 0) by reflexivity. rewrite <- Zle_Qle. lia.
527 + rewrite Nat2Z.inj_add. rewrite inject_Z_plus. rewrite Qmult_plus_distr_r.
528 remember (_ / _ * i)%Q as x. remember (_ / _ * 1%nat)%Q as y.
529 apply (Z.le_trans _ (Qceiling x + Qceiling y)).
530 -- apply QUtil.Qceiling_le_add.
531 -- assert (Qceiling y <= register_width); try lia.
532 replace (Z.of_nat register_width) with (Qceiling (inject_Z register_width)).
533 ++ apply Qceiling_resp_le. subst.
534 replace (inject_Z (Z.of_nat 1)) with 1%Q by reflexivity.
535 rewrite Qmult_1_r. apply Qle_shift_div_r.
536 --- remember limbwidth_good. replace 0%Q with (inject_Z 0) by reflexivity.
537 rewrite <- Zlt_Qlt. lia.
538 --- rewrite <- inject_Z_mult. rewrite <- Zle_Qle.
539 cbv [limbwidth_num limbwidth_den]. lia.
540 ++ apply Qceiling_Z.
541 - replace 0 with (Qceiling 0) by reflexivity. apply Qceiling_resp_le.
542 apply Qmult_le_0_compat.
543 + cbv [Qdiv]. apply Qmult_le_0_compat.
544 -- replace 0%Q with (inject_Z 0) by reflexivity. rewrite <- Zle_Qle. lia.
545 -- apply Qinv_le_0_compat. replace 0%Q with (inject_Z 0) by reflexivity.
546 rewrite <- Zle_Qle. lia.
547 + replace 0%Q with (inject_Z 0) by reflexivity. rewrite <- Zle_Qle. lia.
548 Qed.
549
550 Definition eval_mulmod := eval_mulmod s c register_width n last_reduction weight p_nz n_gteq_4 s_small s_big weight_lt_width wprops.
551 Definition eval_squaremod := eval_squaremod s c register_width n last_reduction weight p_nz n_gteq_4 s_small s_big weight_lt_width wprops.
552 End dettman_multiplication_mod_ops.
553 End dettman_multiplication_mod_ops.
554
555 Module Export Hints.
556 Import dettman_multiplication_mod_ops.
557 #[global]
558 Hint Rewrite eval_mulmod using solve [ auto with zarith | congruence ] : push_eval.
559 #[global]
560 Hint Rewrite eval_squaremod using solve [ auto with zarith | congruence ] : push_eval.
561 End Hints.