Debian Codegraph @

src/Arithmetic/BYInv.v

raw
1 From Coq Require Import Bool.
2 From Coq Require Import ZArith.
3 From Coq Require Import List.
4 From Coq Require Import Nsatz.
5 From Coq Require Import Lia.
6 Require Import Crypto.Arithmetic.UniformWeight.
7 Require Import Crypto.Arithmetic.Saturated.
8 Require Import Crypto.Arithmetic.Core.
9 Require Import Crypto.Arithmetic.WordByWordMontgomery.
10
11 Require Import Crypto.Util.LetIn.
12 Require Import Crypto.Util.ListUtil.
13 Require Import Crypto.Util.ZUtil.Notations.
14 Require Import Crypto.Arithmetic.Partition.
15 Require Import Crypto.Util.Decidable.
16 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
17 Require Import Crypto.Util.ZUtil.Definitions.
18 Require Import Crypto.Util.ZUtil.TruncatingShiftl.
19 Require Import Crypto.Util.ZUtil.Modulo.
20 Require Import Crypto.Util.ZUtil.Testbit.
21 Require Import Crypto.Util.ZUtil.Shift.
22 Require Import Crypto.Util.ZUtil.Div.
23 Require Import Crypto.Util.ZUtil.Lor.
24 Require Import Crypto.Util.ZUtil.Land.
25 Require Import Crypto.Util.ZUtil.ArithmeticShiftr.
26 Require Import Crypto.Util.ZUtil.TwosComplement.
27 Require Import Crypto.Util.ZUtil.SignBit.
28 Require Import Crypto.Util.ZUtil.Pow.
29
30 Import Positional.
31 Import ListNotations.
32 Import Definitions.
33
34 (** An implementation of the divsteps2 algorithm from "Fast constant-time gcd computation and modular inversion."
35 by D. J. Bernstein et al. See the file inversion-c/readme.txt for generation of C-code.
36 For a C-implementation using this generated code to implement modular inversion, see inversion-c/test-inversion.c. **)
37
38 Local Coercion Z.of_nat : nat >-> Z.
39
40 Local Open Scope Z_scope.
41
42 (************* *)
43 (**Definitions *)
44 (************* *)
45
46 (*Evaluation function for multi-limb integers in twos complement *)
47 Definition eval_twos_complement machine_wordsize n f :=
48 dlet eval_f := eval (uweight machine_wordsize) n f in
49 Z.twos_complement (machine_wordsize * Z.of_nat n) eval_f.
50
51 (*Saturated addition of multi-limb integers *)
52 Local Definition sat_add machine_wordsize n f g :=
53 fst (Rows.add (uweight machine_wordsize) n f g).
54
55 (*Saturated (logical) right shift *)
56 Definition sat_shiftr machine_wordsize n f :=
57 (map
58 (fun i =>
59 Z.lor (Z.shiftr (nth_default 0 f i) 1)
60 (Z.truncating_shiftl machine_wordsize
61 (nth_default 0 f (S i))
62 (machine_wordsize - 1)))
63 (seq 0 (n - 1))) ++ [(nth_default 0 f (n - 1)) >> 1].
64
65 (* arithmetic right shift of saturated multi-limb integers *)
66 Definition sat_arithmetic_shiftr1 machine_wordsize n f :=
67 (map
68 (fun i =>
69 ((nth_default 0 f i) >> 1) |' (Z.truncating_shiftl machine_wordsize
70 (nth_default 0 f (i + 1))
71 (machine_wordsize - 1)))
72 (seq 0 (n - 1)))
73 ++ [Z.arithmetic_shiftr1 machine_wordsize (nth_default 0 f (n - 1))].
74
75 (* Multi-limb parity check *)
76 Definition sat_mod2 f :=
77 nth_default 0 f 0 mod 2.
78
79 Definition sat_one n := cons 1 (zeros (n - 1)).
80
81 Definition sat_opp machine_wordsize n f :=
82 sat_add machine_wordsize n
83 (sat_one n)
84 (map (fun i => Z.lnot_modulo i (2^machine_wordsize)) f).
85
86 Definition sat_zero n :=
87 zeros n.
88
89 (*********************************************************************** *)
90 (**Section for small independent lemmas which do not fit anywhere else *)
91 (*********************************************************************** *)
92
93 Lemma eval_bound machine_wordsize n f
94 (mw0 : 0 < machine_wordsize)
95 (Hz : forall z, In z f -> 0 <= z < 2^machine_wordsize)
96 (Hf : length f = n) :
97 0 <= eval (uweight machine_wordsize) n f < 2 ^ (machine_wordsize * n).
98 Proof.
99 generalize dependent n; induction f; intros; subst; simpl in *.
100 - rewrite eval0, Z.mul_0_r, Z.pow_0_r; lia.
101 - rewrite eval_cons, uweight_eval_shift, uweight_0, uweight_eq_alt' by lia.
102 assert (0 <= 2 ^ machine_wordsize) by (apply Z.pow_nonneg; lia).
103 specialize (IHf (fun z H => Hz z (or_intror H)) (length f) eq_refl).
104 specialize (Hz a (or_introl eq_refl)).
105 split; ring_simplify; rewrite Z.mul_1_r.
106 + nia.
107 + replace (machine_wordsize * Z.pos (Pos.of_succ_nat (length f))) with
108 (machine_wordsize + machine_wordsize * Z.of_nat (length f)) by nia.
109 rewrite Z.pow_add_r; nia. Qed.
110
111 (****************************************************************************** *)
112 (**Section for lemma relating to bitwise operations and the Z.testbits function *)
113 (****************************************************************************** *)
114
115 Lemma eval_testbit machine_wordsize n f m
116 (Hz : forall z, In z f -> 0 <= z < 2^machine_wordsize)
117 (mw0 : 0 < machine_wordsize)
118 (Hf : length f = n)
119 (Hm : 0 <= m) :
120 Z.testbit (eval (uweight machine_wordsize) n f) m =
121 Z.testbit (nth_default 0 f (Z.abs_nat (m / machine_wordsize))) (m mod machine_wordsize).
122 Proof.
123 generalize dependent n. generalize dependent m. induction f; intros; simpl in *; subst.
124 - rewrite ListUtil.nth_default_nil, eval0, !Z.testbit_0_l; reflexivity.
125 - rewrite eval_cons, uweight_eval_shift
126 by (specialize (Hz a (or_introl eq_refl)); lia).
127 rewrite uweight_0, uweight_eq_alt', Z.mul_1_l, Z.mul_1_r, Z.mul_comm, <- Z.shiftl_mul_pow2, Shift.Z.testbit_add_shiftl_full
128 by (specialize (Hz a (or_introl eq_refl)); lia).
129 break_innermost_match; Z.ltb_to_lt; try lia.
130 + rewrite Z.div_small by lia.
131 rewrite ListUtil.nth_default_cons, Z.mod_small by lia. reflexivity.
132 + rewrite IHf; try lia.
133 replace (Z.abs_nat (m / machine_wordsize)) with
134 (S (Z.abs_nat ((m - machine_wordsize) / machine_wordsize))).
135 rewrite ListUtil.nth_default_cons_S, PullPush.Z.sub_mod_r, Z_mod_same_full, Z.sub_0_r; reflexivity.
136 rewrite <- Zabs2Nat.inj_succ_abs, Z.abs_eq.
137 replace machine_wordsize with (1 * machine_wordsize) at 1 by ring.
138 rewrite Div.Z.div_sub, Z.sub_1_r, Z.succ_pred; lia.
139 apply Div.Z.div_nonneg; lia.
140 intros; specialize (Hz z); tauto. Qed.
141
142 (**************************************************** *)
143 (**Functional correctness of multi-limb logical shift *)
144 (**************************************************** *)
145
146 Lemma eval_sat_shiftr machine_wordsize n f
147 (mw0 : 0 < machine_wordsize)
148 (Hf : length f = n)
149 (Hz : forall z, In z f -> 0 <= z < 2^machine_wordsize) :
150 eval (uweight machine_wordsize) n (sat_shiftr machine_wordsize n f) =
151 eval (uweight machine_wordsize) n f >> 1.
152 Proof.
153 generalize dependent n. induction f; intros; subst.
154 - unfold sat_shiftr; simpl; rewrite eval0; reflexivity.
155 - unfold sat_shiftr; simpl.
156 rewrite eval_cons, uweight_eval_shift by lia.
157 destruct f; simpl in *.
158 + rewrite
159 eval_cons, uweight_0, eval0, ListUtil.nth_default_cons, uweight_eq_alt',
160 Z.mul_0_r, !Z.add_0_r, !Z.mul_1_l; reflexivity.
161 + rewrite eval_cons, uweight_eval_shift;
162 try (rewrite ?length_snoc, ?map_length, ?seq_length; lia).
163 replace ((map _ _) ++ _) with (sat_shiftr machine_wordsize (S (length f)) (z :: f)).
164 * rewrite IHf; try (intros; try apply Hz; tauto).
165 rewrite uweight_0, ListUtil.nth_default_cons_S, uweight_eq_alt'.
166 rewrite !ListUtil.nth_default_cons, !Z.mul_1_l, !Z.mul_1_r.
167 rewrite <- !Z.div2_spec, !Z.div2_div, Z.div2_split by lia.
168 rewrite eval_cons at 2 by reflexivity.
169 rewrite uweight_eval_shift, uweight_0, uweight_eq_alt, Z.pow_1_r by lia.
170 rewrite PullPush.Z.add_mod_r, PullPush.Z.mul_mod_l.
171 replace 2 with (2 ^ 1) at 7 by trivial.
172 rewrite Modulo.Z.mod_same_pow, Z.mul_0_l by lia. ring_simplify.
173 rewrite Z.mul_1_l, Z.add_0_r, Z.truncating_shiftl_mod by lia. rewrite Z.lor_add; try ring.
174 destruct (Z.odd z) eqn:E; rewrite Zmod_odd, E.
175 ** specialize (Hz a (or_introl eq_refl)).
176 rewrite Z.mul_1_r, Z.land_div2; rewrite ?Z.sub_simpl_r; lia.
177 ** now rewrite Z.mul_0_r, Z.land_0_r.
178 * unfold sat_shiftr. replace (S (length f) - 1)%nat with (length f) by lia.
179 apply f_equal2.
180 ** apply map_seq_ext; intros; [|lia].
181 rewrite Nat.sub_0_r, Nat.add_1_r.
182 rewrite !(ListUtil.nth_default_cons_S _ a); reflexivity.
183 ** now rewrite ListUtil.nth_default_cons_S. Qed.
184
185 (*********************************************************************************** *)
186 (**Section for properties of the multi-limb addtion including functional correctness *)
187 (*********************************************************************************** *)
188
189 Lemma eval_sat_add machine_wordsize n f g (mw0 : 0 < machine_wordsize) (Hf : length f = n) (Hg : length g = n) :
190 eval (uweight machine_wordsize) n (sat_add machine_wordsize n f g) =
191 (eval (uweight machine_wordsize) n f + eval (uweight machine_wordsize) n g) mod (2^(machine_wordsize * n)).
192 Proof.
193 unfold sat_add.
194 rewrite Rows.add_partitions;
195 rewrite ?eval_partition; try assumption; try apply (uwprops machine_wordsize mw0).
196 rewrite uweight_eq_alt'; reflexivity. Qed.
197
198 Lemma length_sat_add machine_wordsize n f g
199 (mw0 : 0 < machine_wordsize)
200 (Hf : length f = n)
201 (Hg : length g = n) :
202 length (sat_add machine_wordsize n f g) = n.
203 Proof.
204 unfold sat_add, Rows.add.
205 autorewrite with distr_length. reflexivity.
206 apply (uwprops machine_wordsize mw0).
207 intros row H; destruct H as [|[H|[]]]; subst; auto. Qed.
208
209 #[global]
210 Hint Rewrite length_sat_add : length_distr.
211
212 Lemma sat_add_bounds machine_wordsize n f g
213 (mw0 : 0 < machine_wordsize)
214 (Hf : length f = n)
215 (Hg : length g = n) :
216 forall z, In z (sat_add machine_wordsize n f g) -> 0 <= z < 2 ^ machine_wordsize.
217 Proof.
218 pose proof uwprops machine_wordsize mw0.
219 intros z Hin; unfold sat_add in *; rewrite Rows.add_partitions in *; auto.
220 unfold Partition.partition in Hin. apply ListAux.in_map_inv in Hin.
221 destruct Hin as [a [Hin]].
222 rewrite H0, !uweight_eq_alt by lia. split.
223 - apply Z.div_le_lower_bound;
224 ring_simplify; try apply Z.mod_pos_bound;
225 apply Z.pow_pos_nonneg; try apply Z.pow_pos_nonneg; lia.
226 - apply Z.div_lt_upper_bound.
227 apply Z.pow_pos_nonneg; try apply Z.pow_pos_nonneg; lia.
228 replace ((_ ^ _) ^ _ * _) with ((2 ^ machine_wordsize) ^ Z.of_nat (S a)).
229 apply Z.mod_pos_bound; apply Z.pow_pos_nonneg; try apply Z.pow_pos_nonneg; lia.
230 rewrite Z.mul_comm, Pow.Z.pow_mul_base;
231 try (replace (Z.of_nat (S a)) with ((Z.of_nat a + 1)) by lia; lia). Qed.
232
233 (********************************************************* *)
234 (**Section on properties of sat_mod2 including correctness *)
235 (********************************************************* *)
236
237 Lemma sat_mod2_cons a f : sat_mod2 (a :: f) = a mod 2.
238 Proof. unfold sat_mod2. now rewrite ListUtil.nth_default_cons. Qed.
239
240 Lemma eval_sat_mod2 machine_wordsize n f
241 (Hf : length f = n)
242 (Hmw : 0 < machine_wordsize) :
243 (eval (uweight machine_wordsize) n f) mod 2 = sat_mod2 f.
244 Proof.
245 generalize dependent Hf. generalize dependent n.
246 induction f; intros; simpl in *; subst.
247 - unfold sat_mod2. rewrite eval0; reflexivity.
248 - rewrite eval_cons by trivial. rewrite uweight_eval_shift by lia.
249 rewrite <- Zplus_mod_idemp_r, <- Zmult_mod_idemp_r, IHf by reflexivity.
250 unfold uweight, ModOps.weight; simpl.
251 rewrite Z.mul_0_r, Z.mul_1_r, !Zdiv_1_r, !Z.opp_involutive, !Z.pow_0_r, Z.mul_1_l.
252 rewrite <- Zmult_mod_idemp_l, PullPush.Z.mod_pow_full, Z_mod_same_full, Z.pow_0_l by assumption.
253 rewrite Zmod_0_l, Z.add_0_r; reflexivity. Qed.
254
255 (*********************************************************************** *)
256 (**Section for properties of sat_arithmetic_shiftr1 including correctness *)
257 (*********************************************************************** *)
258
259 Lemma eval_sat_arithmetic_shiftr1 machine_wordsize n f
260 (mw0 : 0 < machine_wordsize)
261 (Hz : forall z, In z f -> 0 <= z < 2^machine_wordsize)
262 (Hf : length f = n) :
263 eval (uweight machine_wordsize) n (sat_arithmetic_shiftr1 machine_wordsize n f) =
264 Z.arithmetic_shiftr1 (machine_wordsize * n) (eval (uweight machine_wordsize) n f).
265 Proof.
266 generalize dependent n.
267 induction f as [|z f IHf]; intros; subst.
268 - rewrite !eval0; reflexivity.
269 - destruct f.
270 + cbn; rewrite uweight_0, !Z.mul_1_r, !Z.mul_1_l, !Z.add_0_r; reflexivity.
271 + unfold sat_arithmetic_shiftr1; simpl.
272 rewrite eval_cons, !uweight_eval_shift by (autorewrite with distr_length; try reflexivity; lia).
273 replace (map _ _ ++ _) with (sat_arithmetic_shiftr1 machine_wordsize (S (length f)) (z0 :: f)).
274 rewrite IHf; try reflexivity.
275
276 unfold Z.arithmetic_shiftr1.
277 rewrite !Z.land_pow2_testbit, !eval_testbit; try tauto; try nia; simpl.
278 replace (Z.abs_nat ((machine_wordsize * Z.pos (Pos.succ (Pos.of_succ_nat (length f))) - 1) / machine_wordsize)) with
279 (S (Z.abs_nat ((machine_wordsize * Z.pos (Pos.of_succ_nat (length f)) - 1) / machine_wordsize))).
280 rewrite !ListUtil.nth_default_cons_S, !ListUtil.nth_default_cons by lia.
281
282 unfold Z.sub.
283 rewrite !Modulo.Z.mod_add_l', uweight_0, uweight_eq_alt', !Z.mul_1_l, Z.mul_1_r, Z.truncating_shiftl_mod by lia.
284 destruct (Z.testbit
285 (nth_default 0 (z0 :: f)
286 (Z.abs_nat
287 ((machine_wordsize * Z.pos (Pos.of_succ_nat (length f)) + - (1)) /
288 machine_wordsize)))
289 ((- (1)) mod machine_wordsize)) eqn:E.
290 * rewrite <- !Z.div2_spec, !Z.div2_div, !Z.lor_add; ring_simplify.
291 rewrite (eval_cons _ (S _)), uweight_eval_shift by (try reflexivity; lia).
292 rewrite uweight_0, uweight_eq_alt', !Z.mul_1_l, Z.mul_1_r, Z.div2_split, <- Z.pow_add_r by nia.
293 rewrite eval_sat_mod2 by (try reflexivity; lia). unfold sat_mod2.
294 rewrite ListUtil.nth_default_cons.
295 replace (machine_wordsize + (_ + _)) with
296 (machine_wordsize * Z.pos (Pos.succ (Pos.of_succ_nat (length f))) + - (1)) by lia; ring.
297
298 rewrite Z.land_comm, Z.land_pow2_small; [lia|].
299 split.
300 apply Div.Z.div_nonneg; try lia.
301 assert (0 <= eval (uweight machine_wordsize) (S (S (length f))) (z :: z0 :: f)).
302 { apply eval_bound; try lia. intros; apply Hz; simpl; tauto. reflexivity. }
303 specialize (Hz z (or_introl eq_refl)); nia.
304 apply Div.Z.div_lt_upper_bound'; [lia|].
305 rewrite Z.mul_comm, Pow.Z.pow_mul_base by nia.
306 simpl; rewrite <- Z.add_assoc, Z.add_0_r.
307 apply eval_bound; try lia; tauto.
308
309 rewrite Z.land_comm, Z.land_pow2_small; [lia|].
310 split.
311 apply Div.Z.div_nonneg; [|lia].
312 apply eval_bound; [lia| |]. intros; apply Hz; simpl; tauto. reflexivity.
313
314 apply Div.Z.div_lt_upper_bound'; try lia.
315 rewrite Z.mul_comm, Pow.Z.pow_mul_base by nia. rewrite <- Z.add_assoc, Z.add_0_r.
316 apply eval_bound; try assumption. intros; apply Hz; simpl; tauto. reflexivity.
317
318 rewrite Zmod_odd.
319 specialize (Hz z (or_introl eq_refl)).
320 destruct (Z.odd z0).
321 ** rewrite Z.mul_1_r, Z.land_pow2_small; [reflexivity|]. split.
322 apply Div.Z.div_nonneg; lia.
323 apply Div.Z.div_lt_upper_bound'; [lia|].
324 rewrite Z.mul_comm, Pow.Z.pow_mul_base, Z.sub_simpl_r; lia.
325 ** rewrite Z.mul_0_r, Z.land_0_r; reflexivity.
326 * rewrite !Z.lor_0_l, (eval_cons _ (S _)), uweight_eval_shift
327 by (try reflexivity; lia).
328 rewrite uweight_0, uweight_eq_alt', Z.mul_1_l, Z.mul_1_r.
329 rewrite <- !Z.div2_spec, !Z.div2_div, Z.div2_split by lia.
330 rewrite eval_sat_mod2, sat_mod2_cons, Z.lor_add;
331 [reflexivity| |reflexivity|lia].
332 rewrite Zmod_odd. specialize (Hz z (or_introl eq_refl)).
333 destruct (Z.odd z0).
334 ** rewrite Z.mul_1_r, Z.land_pow2_small; try lia.
335 split.
336 apply Div.Z.div_nonneg; lia.
337 apply Div.Z.div_lt_upper_bound'; [lia|].
338 rewrite Z.mul_comm, Pow.Z.pow_mul_base, Z.sub_simpl_r; lia.
339 ** rewrite Z.mul_0_r, Z.land_0_r; reflexivity.
340 * rewrite <- Zabs2Nat.inj_succ.
341 apply f_equal.
342 rewrite <- Z.add_1_r.
343 unfold Z.sub.
344 rewrite !Div.Z.div_add_l'; lia.
345 set (x := Pos.of_succ_nat (length f)).
346 set (a := machine_wordsize). apply Div.Z.div_nonneg; nia.
347 * intros; apply Hz; simpl in *; tauto.
348 * intros; apply Hz; simpl in *; tauto.
349 * unfold sat_arithmetic_shiftr1.
350 replace (S (length f) - 1)%nat with (length f) by lia.
351 rewrite ListUtil.nth_default_cons_S.
352 apply f_equal2; [|reflexivity].
353 apply map_seq_ext; [|lia].
354 intros; simpl; rewrite !Nat.add_1_r, !(ListUtil.nth_default_cons_S _ z); reflexivity. Qed.
355
356 (************************************ *)
357 (**Properties of sat_one and sat_zero *)
358 (************************************ *)
359
360 Lemma length_sat_one n (Hn : (0 < n)%nat) :
361 length (sat_one n) = n.
362 Proof. unfold sat_one; simpl; rewrite length_zeros; lia. Qed.
363
364 #[global]
365 Hint Rewrite length_sat_one : distr_length.
366
367 Lemma eval_sat_one n machine_wordsize
368 (Hn : (0 < n)%nat)
369 (Hmw : 0 <= machine_wordsize) :
370 eval (uweight machine_wordsize) n (sat_one n) = 1.
371 Proof. unfold sat_one. destruct n. lia.
372 rewrite eval_cons, uweight_eval_shift, uweight_0, uweight_1 by (try rewrite length_zeros; lia).
373 replace (S n - 1)%nat with n by lia. rewrite eval_zeros; lia. Qed.
374
375 Lemma length_sat_zero n :
376 length (sat_zero n) = n.
377 Proof. auto with distr_length. Qed.
378
379 Lemma eval_sat_zero machine_wordsize n (mw : 0 < machine_wordsize):
380 eval (uweight machine_wordsize) n (sat_zero n) = 0.
381 Proof. apply eval_zeros. Qed.
382
383 (******************************************************** *)
384 (**Properties of sat_opp including functional correctness *)
385 (******************************************************** *)
386
387 Lemma length_sat_opp machine_wordsize n f
388 (mw0 : 0 < machine_wordsize)
389 (Hn : (0 < n)%nat)
390 (Hf : length f = n) :
391 length (sat_opp machine_wordsize n f) = n.
392 Proof. unfold sat_opp; rewrite length_sat_add; auto with distr_length. Qed.
393
394 #[global]
395 Hint Rewrite length_sat_opp : distr_length.
396
397 Lemma eval_sat_opp machine_wordsize n f
398 (mw0 : 0 < machine_wordsize)
399 (Hn : (0 < n)%nat)
400 (Hz : forall z, In z f -> 0 <= z < 2^machine_wordsize)
401 (Hf : length f = n) :
402 eval (uweight machine_wordsize) n (sat_opp machine_wordsize n f) =
403 (- eval (uweight machine_wordsize) n f) mod (2^(machine_wordsize * n)).
404 Proof.
405 assert (2 ^ machine_wordsize <> 0) by (apply Z.pow_nonzero; lia).
406 assert (2 ^ (machine_wordsize * Z.of_nat (length f)) <> 0) by (apply Z.pow_nonzero; nia).
407
408 symmetry. unfold sat_opp.
409 rewrite eval_sat_add by distr_length.
410 generalize dependent n. induction f; intros.
411 - simpl in *; rewrite <- Hf; rewrite eval0; rewrite !Zmod_0_l; reflexivity.
412 - simpl in *. subst.
413 assert (2 ^ (machine_wordsize * Z.of_nat (length f)) <> 0) by (apply Z.pow_nonzero; nia).
414
415 rewrite !eval_cons, uweight_0, !uweight_eval_shift, uweight_eq_alt' by distr_length.
416 rewrite !Z.mul_1_l, !Z.mul_1_r, Z.opp_add_distr, Zopp_mult_distr_r, <- Z.add_mod_idemp_r by distr_length.
417 symmetry. rewrite Z.add_assoc, <- Z.add_mod_idemp_r; try (rewrite Zpos_P_of_succ_nat in H0; rewrite Nat2Z.inj_succ; assumption).
418 replace (2 ^ (machine_wordsize * Z.of_nat (S (length f)))) with
419 ((2 ^ machine_wordsize) * (2 ^ (machine_wordsize * Z.of_nat (length f)))).
420 rewrite !Z.mul_mod_distr_l by lia.
421 unfold Z.lnot_modulo. rewrite Lnot.Z.lnot_equiv. unfold Z.pred.
422 destruct (length f) eqn:E.
423 + rewrite !eval0.
424 rewrite !Z.mul_0_r, !Z.mul_1_r, !Z.add_0_r.
425 replace (-a + -1) with (- (a + 1)) by ring.
426 rewrite eval_sat_one.
427 rewrite Z.mod_opp_small.
428 replace (1 + (2 ^ machine_wordsize - (a + 1))) with
429 (-a + 2 ^ machine_wordsize) by ring.
430 rewrite <- Z.add_mod_idemp_r.
431 rewrite Z_mod_same_full. rewrite Z.add_0_r. reflexivity.
432 apply Z.pow_nonzero. lia. lia.
433 specialize (Hz a (or_introl eq_refl)). lia. lia. lia.
434 + rewrite IHf by (try (intros; apply Hz; right; assumption); lia).
435 rewrite !eval_sat_one in * by lia.
436 rewrite <- Z.mul_mod_distr_l, Z.add_mod_idemp_r by nia.
437 symmetry.
438 rewrite <- Z.mul_mod_distr_l, Z.add_mod_idemp_r, Z.mul_add_distr_l, Z.mul_1_r by nia.
439 replace (-a + -1) with (- (a + 1)) by ring.
440 rewrite Z.mod_opp_small.
441 unfold Z.lnot_modulo.
442 rewrite Z.add_assoc.
443 replace (1 + (2 ^ machine_wordsize - (a + 1))) with
444 (- a + 2 ^ machine_wordsize) by ring.
445 reflexivity.
446 specialize (Hz a (or_introl eq_refl)); lia.
447 + rewrite <- Z.pow_add_r; try apply f_equal; nia. Qed.
448
449 Lemma sat_opp_mod2 machine_wordsize n f
450 (mw0 : 0 < machine_wordsize)
451 (Hn : (0 < n)%nat)
452 (Hf : length f = n)
453 (Hz : forall z, In z f -> 0 <= z < 2^machine_wordsize) :
454 sat_mod2 (sat_opp machine_wordsize n f) = sat_mod2 f.
455 Proof.
456 rewrite <- !(eval_sat_mod2 machine_wordsize n) by distr_length.
457 rewrite eval_sat_opp; auto.
458 set (x := eval (uweight machine_wordsize) n f).
459 destruct f.
460 - subst. reflexivity.
461 - rewrite <- (Znumtheory.Zmod_div_mod _ (2 ^ (machine_wordsize * Z.of_nat n))).
462 rewrite <- Z.opp_mod2; reflexivity. lia. apply Z.pow_pos_nonneg; nia.
463 apply Zpow_facts.Zpower_divide. simpl in *. nia. Qed.
464
465 (*********************************** *)
466 (**Properties of twos_complement_opp *)
467 (*********************************** *)
468
469 Lemma twos_complement_opp_bound m a (Hm : 0 <= m) :
470 0 <= Z.twos_complement_opp m a < 2 ^ m.
471 Proof. unfold Z.twos_complement_opp; apply Z.mod_pos_bound.
472 apply Z.pow_pos_nonneg; lia. Qed.
473
474 Lemma twos_complement_opp_correct m a :
475 (Z.twos_complement_opp m a) = - a mod 2 ^ m.
476 Proof. unfold Z.twos_complement_opp, Z.lnot_modulo, Z.lnot.
477 rewrite Zplus_mod_idemp_l, <- Z.sub_1_r; apply f_equal2; lia. Qed.
478
479 Lemma twos_complement_zopp a m
480 (Hm : 0 < m) (corner_case : Z.twos_complement m a <> - 2 ^ (m - 1)) :
481 Z.twos_complement m (- a) = - Z.twos_complement m a.
482 Proof.
483 pose proof Z.twos_complement_bounds a m Hm.
484 apply Z.twos_complement_spec; [lia|split]; [|lia].
485 rewrite (PullPush.Z.opp_mod_mod (Z.twos_complement _ _)), Z.twos_complement_mod' by lia;
486 apply PullPush.Z.opp_mod_mod. Qed.
487
488 Lemma twos_complement_opp_spec m a
489 (Hm : 0 < m) (corner_case : Z.twos_complement m a <> - 2 ^ (m - 1)) :
490 Z.twos_complement m (Z.twos_complement_opp m a) = - (Z.twos_complement m a).
491 Proof. apply Z.twos_complement_spec; [lia|split]; [|pose proof Z.twos_complement_bounds a m Hm; lia].
492 rewrite <- twos_complement_zopp, twos_complement_opp_correct, Z.twos_complement_mod', Z.mod_mod by (try apply Z.pow_nonzero; lia). reflexivity. Qed.
493
494 (************************* *)
495 (**Properties of twos_complement_pos *)
496 (************************* *)
497
498 Lemma twos_complement_pos_spec m a
499 (mw0 : 0 < m)
500 (corner_case : Z.twos_complement m a <> - 2 ^ (m - 1)) : (* note that this has to be true for Z.twos_complement_pos to work *)
501 Z.twos_complement_pos m a = Z.b2z (0 <? Z.twos_complement m a).
502 Proof.
503 replace (0 <? Z.twos_complement m a) with (- Z.twos_complement m a <? 0) by
504 (destruct (0 <? Z.twos_complement m a) eqn:?; destruct (- Z.twos_complement m a <? 0) eqn:?;
505 rewrite ?Z.ltb_lt in *; rewrite ?Z.ltb_ge in *; try reflexivity; lia).
506 unfold Z.twos_complement_pos; pose proof twos_complement_opp_bound m a ltac:(lia).
507 rewrite <- twos_complement_opp_spec, unfold_Let_In, Z.twos_complement_neg_equiv by lia.
508 rewrite Z.sign_bit_testbit, Z.twos_complement_cond_equiv, negb_involutive; lia. Qed.
509
510 (********************************************************************* *)
511 (**Section for interaction between eval_twos_complement and saturated operations *)
512 (********************************************************************* *)
513
514 Lemma eval_twos_complement_sat_arithmetic_shiftr1 machine_wordsize n f
515 (mw0 : 0 < machine_wordsize)
516 (n0 : (0 < n)%nat)
517 (Hz : forall z, In z f -> 0 <= z < 2^machine_wordsize)
518 (Hf : length f = n) :
519 eval_twos_complement machine_wordsize n (sat_arithmetic_shiftr1 machine_wordsize n f) =
520 (eval_twos_complement machine_wordsize n f) / 2.
521 Proof.
522 assert (0 < Z.of_nat n) by lia. unfold eval_twos_complement, Let_In. rewrite eval_sat_arithmetic_shiftr1, Z.arithmetic_shiftr1_spec; auto; [nia|].
523 apply eval_bound; auto. Qed.
524
525 Lemma eval_twos_complement_sat_add machine_wordsize n f g
526 (mw0 : 0 < machine_wordsize)
527 (n0 : (0 < n)%nat)
528 (Hf : length f = n)
529 (Hg : length g = n)
530 (overflow_f : - 2 ^ (machine_wordsize * n - 2) <
531 Z.twos_complement (machine_wordsize * n) (eval (uweight machine_wordsize) n f) <
532 2 ^ (machine_wordsize * n - 2))
533 (overflow_g : - 2 ^ (machine_wordsize * n - 2) <
534 Z.twos_complement (machine_wordsize * n) (eval (uweight machine_wordsize) n g) <
535 2 ^ (machine_wordsize * n - 2)) :
536 eval_twos_complement machine_wordsize n (sat_add machine_wordsize n f g) =
537 eval_twos_complement machine_wordsize n f + eval_twos_complement machine_wordsize n g.
538 Proof. assert (0 < Z.of_nat n) by lia; unfold eval_twos_complement, Let_In.
539 rewrite eval_sat_add, <- Z.twos_complement_add_weak, Z.twos_complement_mod; nia. Qed.
540
541 Lemma eval_twos_complement_select machine_wordsize n cond f g
542 (Hf : length f = n)
543 (Hg : length g = n) :
544 eval_twos_complement machine_wordsize n (select cond f g) = if dec (cond = 0)
545 then (eval_twos_complement machine_wordsize n f)
546 else (eval_twos_complement machine_wordsize n g).
547 Proof. unfold eval_twos_complement; rewrite eval_select; auto; destruct (dec (cond = 0)); auto. Qed.
548
549 Lemma eval_twos_complement_sat_opp machine_wordsize n f
550 (mw0 : 0 < machine_wordsize)
551 (n0 : (0 < n)%nat)
552 (Hz : forall z, In z f -> 0 <= z < 2^machine_wordsize)
553 (Hf : length f = n)
554 (corner_case : eval_twos_complement machine_wordsize n f <> - 2 ^ (machine_wordsize * n - 1)):
555 eval_twos_complement machine_wordsize n (sat_opp machine_wordsize n f) =
556 - eval_twos_complement machine_wordsize n f.
557 Proof. assert (0 < Z.of_nat n) by lia; unfold eval_twos_complement, Let_In in *; rewrite eval_sat_opp, Z.twos_complement_mod, twos_complement_zopp; try tauto; nia. Qed.
558
559 Lemma eval_twos_complement_sat_zero machine_wordsize n
560 (mw0 : 0 < machine_wordsize)
561 (n0 : (0 < n)%nat) :
562 eval_twos_complement machine_wordsize n (sat_zero n) = 0.
563 Proof. unfold eval_twos_complement; rewrite eval_sat_zero by lia; apply Z.twos_complement_zero; nia. Qed.
564
565 Lemma eval_twos_complement_sat_mod2 machine_wordsize n f
566 (mw0 : 0 < machine_wordsize)
567 (n0 : (0 < n)%nat)
568 (Hf : length f = n) :
569 (eval_twos_complement machine_wordsize n f) mod 2 = sat_mod2 f.
570 Proof. unfold eval_twos_complement, Let_In; rewrite Z.twos_complement_mod2, eval_sat_mod2; nia. Qed.
571
572 (** ******************************************************************************* *)
573 (** This section is for the implementation and correctness of the divstep algorithm *)
574 (** ******************************************************************************* *)
575
576 Import WordByWordMontgomery.
577 Import Positional.
578
579 (* This version does not introduce larger types unnecessarily *)
580 Definition twos_complement_opp' m a :=
581 (fst (Z.add_get_carry_full (2^m) (Z.lnot_modulo a (2 ^ m)) 1)) mod (2 ^ m).
582
583 Lemma twos_complement_opp'_spec m a :
584 twos_complement_opp' m a = Z.twos_complement_opp m a.
585 Proof.
586 unfold twos_complement_opp', Z.twos_complement_opp.
587 destruct (Z_lt_dec m 0).
588 - rewrite !Z.pow_neg_r, !Zmod_0_r by assumption. cbn.
589 rewrite ?Zmod_0_r. reflexivity.
590 - rewrite AddGetCarry.Z.add_get_carry_full_mod.
591 rewrite Z.mod_mod; [reflexivity|]. apply Z.pow_nonzero; lia. Qed.
592
593 (* This version does not introduce larger types unnecessarily *)
594 Definition twos_complement_pos' m a :=
595 dlet b := twos_complement_opp' m a in (Z.shiftr b (m-1)).
596 Lemma twos_complement_pos'_spec m a :
597 twos_complement_pos' m a = Z.twos_complement_pos m a.
598 Proof. unfold twos_complement_pos'. rewrite twos_complement_opp'_spec. reflexivity. Qed.
599
600 Definition divstep_aux (machine_wordsize : Z) (sat_limbs mont_limbs : nat) (m : Z) (data : Z * (list Z) * (list Z) * (list Z) * (list Z)) :=
601 let '(d,f,g,v,r) := data in
602 dlet cond := Z.land (twos_complement_pos' machine_wordsize d) (sat_mod2 g) in
603 dlet d' := Z.zselect cond d (twos_complement_opp' machine_wordsize d) in
604 dlet f' := select cond f g in
605 dlet g' := select cond g (sat_opp machine_wordsize sat_limbs f) in
606 dlet v' := select cond v r in
607 let v'':= addmod machine_wordsize mont_limbs m v' v' in
608 dlet r' := select cond r (oppmod machine_wordsize mont_limbs m v) in
609 dlet g0 := sat_mod2 g' in
610 let d'' := (fst (Z.add_get_carry_full (2^machine_wordsize) d' 1)) in
611 dlet f'' := select g0 (sat_zero sat_limbs) f' in
612 let g'' := sat_arithmetic_shiftr1 machine_wordsize sat_limbs (sat_add machine_wordsize sat_limbs g' f'') in
613 dlet v''' := select g0 (sat_zero mont_limbs) v' in
614 let r'' := addmod machine_wordsize mont_limbs m r' v''' in
615 (d'',f',g'',v'',r'').
616
617 Definition divstep machine_wordsize sat_limbs mont_limbs m d f g v r :=
618 divstep_aux machine_wordsize sat_limbs mont_limbs m (d, f, g, v, r).
619 Definition divstep_safe machine_wordsize n :=
620 let sat_limbs := (n + 1)%nat in
621 divstep machine_wordsize sat_limbs n.
622
623 Definition divstep_spec d f g :=
624 if (0 <? d) && Z.odd g
625 then (1 - d, g, (g - f) / 2)
626 else (1 + d, f, (g + (g mod 2) * f) / 2 ).
627
628 Definition divstep_spec2 m d g v r :=
629 if (0 <? d) && Z.odd g
630 then (2 * r mod m, (r - v) mod m)
631 else (2 * v mod m, (r + (g mod 2) * v) mod m).
632
633 Definition divstep_spec_full m d f g v r :=
634 if (0 <? d) && Z.odd g
635 then (1 - d, g, (g - f) / 2, 2 * r mod m, (r - v) mod m)
636 else (1 + d, f, (g + (g mod 2) * f) / 2, 2 * v mod m, (r + (g mod 2) * v) mod m).
637
638 Lemma divstep_g machine_wordsize sat_limbs mont_limbs m d f g v r
639 (mw0 : 0 < machine_wordsize)
640 (sat_limbs0 : (0 < sat_limbs)%nat)
641 (fodd : Z.odd (eval_twos_complement machine_wordsize sat_limbs f) = true)
642 (Hf : length f = sat_limbs)
643 (Hg : length g = sat_limbs)
644 (corner_case : Z.twos_complement machine_wordsize d <> - 2 ^ (machine_wordsize - 1))
645 (overflow_f : - 2 ^ (machine_wordsize * sat_limbs - 2) <
646 eval_twos_complement machine_wordsize sat_limbs f <
647 2 ^ (machine_wordsize * sat_limbs - 2))
648 (overflow_g : - 2 ^ (machine_wordsize * sat_limbs - 2) <
649 eval_twos_complement machine_wordsize sat_limbs g <
650 2 ^ (machine_wordsize * sat_limbs - 2))
651 (Hf2 : forall z, In z f -> 0 <= z < 2^machine_wordsize)
652 (Hg2 : forall z, In z g -> 0 <= z < 2^machine_wordsize) :
653 let '(d1,f1,g1,v1,r1) := (divstep machine_wordsize sat_limbs mont_limbs m d f g v r) in
654 eval_twos_complement machine_wordsize sat_limbs g1 =
655 snd (divstep_spec
656 (Z.twos_complement machine_wordsize d)
657 (eval_twos_complement machine_wordsize sat_limbs f)
658 (eval_twos_complement machine_wordsize sat_limbs g)).
659 Proof.
660 set (bw := machine_wordsize * Z.of_nat sat_limbs) in *.
661
662 simpl.
663 assert (0 < Z.of_nat sat_limbs) by lia.
664 assert (bw0 : 0 < bw) by nia.
665 assert (bw1 : 1 < bw) by (destruct (dec (bw = 1)); rewrite ?e in *; simpl in *; lia).
666 assert (2 ^ machine_wordsize = 2 * 2 ^ (machine_wordsize - 1))
667 by (rewrite Pow.Z.pow_mul_base, Z.sub_simpl_r; lia).
668 assert (0 < 2 ^ bw) by (apply Z.pow_pos_nonneg; lia).
669 assert (2 ^ (bw - 2) <= 2 ^ (bw - 1)) by (apply Z.pow_le_mono; lia).
670
671 #[global]
672 Hint Rewrite length_sat_add length_sat_opp length_select : distr_length.
673
674 rewrite eval_twos_complement_sat_arithmetic_shiftr1; distr_length; autorewrite with distr_length; try nia.
675 rewrite eval_twos_complement_sat_add; auto; try (autorewrite with distr_length; lia).
676 rewrite !eval_twos_complement_select; auto; try (autorewrite with distr_length; lia).
677 rewrite eval_twos_complement_sat_opp; auto; try (autorewrite with distr_length; lia).
678 rewrite select_push; try (autorewrite with distr_length; lia).
679
680 rewrite sat_opp_mod2; auto.
681 unfold divstep_spec.
682 rewrite twos_complement_pos'_spec.
683 rewrite twos_complement_pos_spec; auto.
684 rewrite <- !(eval_twos_complement_sat_mod2 machine_wordsize sat_limbs); auto.
685 rewrite !Zmod_odd; auto.
686
687 set (g' := eval_twos_complement machine_wordsize sat_limbs g) in *.
688 set (f' := eval_twos_complement machine_wordsize sat_limbs f) in *.
689 assert (corner_case_f : f' <> - 2 ^ (bw - 1)) by
690 (replace (- _) with (- (2 * 2 ^ (bw - 2))); rewrite ?Pow.Z.pow_mul_base; ring_simplify (bw - 2 + 1); nia).
691
692 destruct (0 <? Z.twos_complement machine_wordsize d);
693 destruct (Z.odd g') eqn:g'_odd; rewrite ?fodd, ?eval_sat_zero; auto.
694 - simpl; rewrite Z.add_comm; reflexivity.
695 - simpl; rewrite eval_twos_complement_sat_zero; auto.
696 - rewrite Z.mul_1_l; simpl; reflexivity.
697 - simpl; rewrite eval_twos_complement_sat_zero; auto.
698 - replace (_ * _) with bw by reflexivity; lia.
699 - rewrite twos_complement_pos'_spec.
700 let v := constr:(eval_twos_complement
701 machine_wordsize sat_limbs
702 (select (Z.twos_complement_pos machine_wordsize d &' sat_mod2 g) g (sat_opp machine_wordsize sat_limbs f))) in
703 let v' := (eval cbv [eval_twos_complement Let_In] in v) in
704 change v' with v.
705 rewrite eval_twos_complement_select; try apply length_sat_opp; auto.
706 destruct (dec (_)); replace (machine_wordsize * _) with bw by reflexivity;
707 try rewrite eval_twos_complement_sat_opp; auto; replace (machine_wordsize * _) with bw by reflexivity; lia.
708 - rewrite twos_complement_pos'_spec.
709 let v := constr:(eval_twos_complement
710 machine_wordsize sat_limbs
711 (select (sat_mod2 (select (Z.twos_complement_pos machine_wordsize d &' sat_mod2 g) g (sat_opp machine_wordsize sat_limbs f)))
712 (sat_zero sat_limbs) (select (Z.twos_complement_pos machine_wordsize d &' sat_mod2 g) f g))) in
713 let v' := (eval cbv [eval_twos_complement Let_In] in v) in
714 change v' with v.
715 rewrite eval_twos_complement_select; try apply length_sat_zero; try apply length_select; auto.
716 destruct (dec (_)); try rewrite eval_twos_complement_sat_zero; try rewrite eval_twos_complement_select;
717 replace (machine_wordsize * _) with bw by reflexivity; try lia; destruct (dec (_)); lia.
718 - apply sat_add_bounds; repeat (rewrite ?length_sat_opp, ?(length_select sat_limbs), ?length_sat_zero; auto); lia. Qed.
719
720 Lemma divstep_d machine_wordsize sat_limbs mont_limbs m d f g v r
721 (mw1 : 1 < machine_wordsize)
722 (sat_limbs0 : (0 < sat_limbs)%nat)
723 (Hg : length g = sat_limbs)
724 (overflow_d : - 2 ^ (machine_wordsize - 1) + 1 <
725 Z.twos_complement machine_wordsize d <
726 2 ^ (machine_wordsize - 1) - 1) :
727 let '(d1,f1,g1,v1,r1) := (divstep machine_wordsize sat_limbs mont_limbs m d f g v r) in
728 Z.twos_complement machine_wordsize d1 =
729 fst (fst (divstep_spec (Z.twos_complement machine_wordsize d)
730 (eval_twos_complement machine_wordsize sat_limbs f)
731 (eval_twos_complement machine_wordsize sat_limbs g))).
732 Proof.
733 assert (helper0 : 0 < Z.of_nat sat_limbs) by lia.
734 assert (mw0 : 0 < machine_wordsize) by lia.
735 assert (helper : 1 < 2 ^ machine_wordsize) by (apply Zpow_facts.Zpower_gt_1; lia).
736 assert (helper2 : 1 < 2 ^ (machine_wordsize - 1)) by (apply Zpow_facts.Zpower_gt_1; lia).
737 assert (helper3 : 2 ^ machine_wordsize = 2 * 2 ^ (machine_wordsize - 1))
738 by (rewrite Pow.Z.pow_mul_base, Z.sub_simpl_r; lia).
739
740 simpl; unfold divstep_spec.
741 rewrite AddGetCarry.Z.add_get_carry_full_mod.
742 rewrite twos_complement_pos'_spec.
743 rewrite twos_complement_opp'_spec.
744 rewrite twos_complement_pos_spec, <- (eval_twos_complement_sat_mod2 machine_wordsize sat_limbs), Zmod_odd by nia.
745 fold (eval_twos_complement machine_wordsize sat_limbs g); set (g' := eval_twos_complement machine_wordsize sat_limbs g) in *.
746 destruct ((0 <? Z.twos_complement machine_wordsize d) && (Z.odd g')) eqn:E.
747 - apply andb_prop in E. destruct E; rewrite H, H0. simpl Z.b2z; simpl Z.land; cbv [fst].
748 unfold Z.zselect. simpl (if _ =? _ then _ else _).
749 rewrite twos_complement_opp_correct, Z.twos_complement_mod, Z.twos_complement_add_full, Z.twos_complement_mod, twos_complement_zopp, Z.twos_complement_one; try lia.
750 rewrite Z.twos_complement_mod, twos_complement_zopp, Z.twos_complement_one; lia.
751 - apply andb_false_iff in E.
752 destruct E; rewrite H;
753 unfold Z.zselect; cbv [fst]; simpl (if _ =? _ then _ else _);
754 [destruct (Z.odd g') | rewrite Z.land_0_r; simpl (if _ =? _ then _ else _)];
755 rewrite Z.twos_complement_mod, Z.twos_complement_add_full, Z.twos_complement_one; rewrite ?Z.twos_complement_one; lia. Qed.
756
757 Lemma divstep_f machine_wordsize sat_limbs mont_limbs m d f g v r
758 (mw0 : 0 < machine_wordsize)
759 (Hf : length f = sat_limbs)
760 (Hg : length g = sat_limbs)
761 (corner_case : Z.twos_complement machine_wordsize d <> - 2 ^ (machine_wordsize - 1)) :
762 let '(d1,f1,g1,v1,r1) := (divstep machine_wordsize sat_limbs mont_limbs m d f g v r) in
763 eval_twos_complement machine_wordsize sat_limbs f1 =
764 snd (fst (divstep_spec (Z.twos_complement machine_wordsize d)
765 (eval_twos_complement machine_wordsize sat_limbs f)
766 (eval_twos_complement machine_wordsize sat_limbs g))).
767 Proof.
768 destruct sat_limbs. subst.
769 unfold eval_twos_complement, Let_In.
770 rewrite !eval0; rewrite Z.mul_0_r.
771 replace (Z.twos_complement 0 0) with (-1). unfold divstep_spec.
772 destruct (0 <? Z.twos_complement machine_wordsize d); reflexivity. reflexivity.
773
774 unfold divstep_spec.
775 simpl.
776
777 set (n' := S sat_limbs) in *.
778 assert (0 < n')%nat by apply Nat.lt_0_succ.
779 rewrite twos_complement_pos'_spec.
780
781 rewrite eval_twos_complement_select, twos_complement_pos_spec, <- (eval_twos_complement_sat_mod2 machine_wordsize n'), Zmod_odd; auto.
782 fold (eval_twos_complement machine_wordsize n' g).
783 destruct (0 <? Z.twos_complement machine_wordsize d); destruct (Z.odd (eval_twos_complement machine_wordsize n' g));
784 try reflexivity; lia. Qed.
785
786 Theorem divstep_correct machine_wordsize sat_limbs mont_limbs m d f g v r
787 (mw1 : 1 < machine_wordsize)
788 (sat_limbs0 : (0 < sat_limbs)%nat)
789 (fodd : Z.odd (eval_twos_complement machine_wordsize sat_limbs f) = true)
790 (Hf : length f = sat_limbs)
791 (Hg : length g = sat_limbs)
792 (overflow_d : - 2 ^ (machine_wordsize - 1) + 1 <
793 Z.twos_complement machine_wordsize d <
794 2 ^ (machine_wordsize - 1) - 1)
795 (overflow_f : - 2 ^ (machine_wordsize * sat_limbs - 2) <
796 eval_twos_complement machine_wordsize sat_limbs f <
797 2 ^ (machine_wordsize * sat_limbs - 2))
798 (overflow_g : - 2 ^ (machine_wordsize * sat_limbs - 2) <
799 eval_twos_complement machine_wordsize sat_limbs g <
800 2 ^ (machine_wordsize * sat_limbs - 2))
801 (Hf2 : forall z, In z f -> 0 <= z < 2^machine_wordsize)
802 (Hg2 : forall z, In z g -> 0 <= z < 2^machine_wordsize) :
803 let '(d1,f1,g1,v1,r1) := (divstep machine_wordsize sat_limbs mont_limbs m d f g v r) in
804 (Z.twos_complement machine_wordsize d1,
805 eval_twos_complement machine_wordsize sat_limbs f1,
806 eval_twos_complement machine_wordsize sat_limbs g1) =
807 divstep_spec (Z.twos_complement machine_wordsize d)
808 (eval_twos_complement machine_wordsize sat_limbs f)
809 (eval_twos_complement machine_wordsize sat_limbs g).
810 Proof.
811 assert (0 < machine_wordsize) by lia. simpl.
812 apply injective_projections.
813 apply injective_projections.
814 rewrite <- (divstep_d _ _ mont_limbs m _ _ _ v r); auto.
815 rewrite <- (divstep_f _ _ mont_limbs m _ _ _ v r); auto; lia.
816 rewrite <- (divstep_g _ _ mont_limbs m _ _ _ v r); auto; lia. Qed.
817
818 Lemma divstep_v machine_wordsize sat_limbs mont_limbs m (r' : Z) m' d f g v r
819 (fodd : Z.odd (eval_twos_complement machine_wordsize sat_limbs f) = true)
820
821 (r'_correct : (2 ^ machine_wordsize * r') mod m = 1)
822 (m'_correct : (m * m') mod 2 ^ machine_wordsize = -1 mod 2 ^ machine_wordsize)
823 (m_big : 1 < m)
824 (m_small : m < (2 ^ machine_wordsize) ^ Z.of_nat mont_limbs)
825 (Hv2 : valid machine_wordsize mont_limbs m v)
826 (Hr2 : valid machine_wordsize mont_limbs m r)
827 (mw0 : 0 < machine_wordsize)
828 (sat_limbs0 : (0 < sat_limbs)%nat)
829 (mont_limbs0 : (0 < mont_limbs)%nat)
830 (Hv : length v = mont_limbs)
831 (Hr : length r = mont_limbs)
832 (Hg : length g = sat_limbs)
833 (overflow_d : - 2 ^ (machine_wordsize - 1) + 1 <
834 Z.twos_complement machine_wordsize d <
835 2 ^ (machine_wordsize - 1) - 1) :
836 let '(d1,f1,g1,v1,r1) := (divstep machine_wordsize sat_limbs mont_limbs m d f g v r) in
837 @WordByWordMontgomery.eval machine_wordsize mont_limbs
838 (from_montgomerymod machine_wordsize mont_limbs m m' v1) mod m =
839 fst (divstep_spec2 m (Z.twos_complement machine_wordsize d)
840 (eval_twos_complement machine_wordsize sat_limbs g)
841 (@WordByWordMontgomery.eval machine_wordsize mont_limbs
842 (from_montgomerymod machine_wordsize mont_limbs m m' v))
843 (@WordByWordMontgomery.eval machine_wordsize mont_limbs
844 (from_montgomerymod machine_wordsize mont_limbs m m' r))).
845 Proof.
846 assert (sat_limbs <> 0%nat) by lia.
847 assert (mont_limbs <> 0%nat) by lia.
848 simpl.
849 rewrite twos_complement_pos'_spec.
850
851 rewrite twos_complement_pos_spec, <- (eval_twos_complement_sat_mod2 machine_wordsize sat_limbs) by lia.
852 rewrite Zmod_odd, (select_eq (uweight machine_wordsize) _ mont_limbs); auto.
853 unfold divstep_spec2.
854 destruct (0 <? Z.twos_complement machine_wordsize d);
855 destruct (Z.odd (eval_twos_complement machine_wordsize sat_limbs g));
856 rewrite ?(eval_addmod _ _ _ r'), ?Z.add_diag; auto. Qed.
857
858 Lemma nonzero_zero n :
859 nonzeromod (sat_zero n) = 0.
860 Proof. unfold nonzero, sat_zero; induction n; auto. Qed.
861
862 Lemma zero_valid machine_wordsize n m
863 (mw0 : 0 < machine_wordsize)
864 (m0 : 0 < m) :
865 valid machine_wordsize n m (sat_zero n).
866 Proof.
867 unfold valid, small, WordByWordMontgomery.eval.
868 rewrite eval_sat_zero, partition_0; try split; auto; lia. Qed.
869
870 Lemma divstep_r machine_wordsize sat_limbs mont_limbs m (r' : Z) m' d f g v r
871 (fodd : Z.odd (eval_twos_complement machine_wordsize sat_limbs f) = true)
872 (r'_correct : (2 ^ machine_wordsize * r') mod m = 1)
873 (m'_correct : (m * m') mod 2 ^ machine_wordsize = -1 mod 2 ^ machine_wordsize)
874 (m_big : 1 < m)
875 (m_small : m < (2 ^ machine_wordsize) ^ Z.of_nat mont_limbs)
876 (Hv2 : valid machine_wordsize mont_limbs m v)
877 (Hr2 : valid machine_wordsize mont_limbs m r)
878 (mw0 : 0 < machine_wordsize)
879 (sat_limbs0 : (0 < sat_limbs)%nat)
880 (mont_limbs0 : (0 < mont_limbs)%nat)
881 (Hv : length v = mont_limbs)
882 (Hr : length r = mont_limbs)
883 (Hf : length f = sat_limbs)
884 (Hg : length g = sat_limbs)
885 (overflow_d : - 2 ^ (machine_wordsize - 1) + 1 <
886 Z.twos_complement machine_wordsize d <
887 2 ^ (machine_wordsize - 1) - 1)
888 (Hf2 : forall z, In z f -> 0 <= z < 2^machine_wordsize) :
889 let '(d1,f1,g1,v1,r1) := (divstep machine_wordsize sat_limbs mont_limbs m d f g v r) in
890 @WordByWordMontgomery.eval machine_wordsize mont_limbs
891 (from_montgomerymod machine_wordsize mont_limbs m m' r1) mod m =
892 snd (divstep_spec2 m (Z.twos_complement machine_wordsize d)
893 (eval_twos_complement machine_wordsize sat_limbs g)
894 (@WordByWordMontgomery.eval machine_wordsize mont_limbs
895 (from_montgomerymod machine_wordsize mont_limbs m m' v))
896 (@WordByWordMontgomery.eval machine_wordsize mont_limbs
897 (from_montgomerymod machine_wordsize mont_limbs m m' r))).
898 Proof.
899 assert (sat_limbs0' : (sat_limbs <> 0%nat)) by lia.
900 assert (mont_limbs0' : mont_limbs <> 0%nat) by lia.
901 unfold divstep_spec2.
902 pose proof (oppmod_correct machine_wordsize mont_limbs m r' m' r'_correct m'_correct mw0 m_big (ltac:(lia)) m_small) as H.
903 destruct H as [H1 H2].
904 assert (zero_valid : valid machine_wordsize mont_limbs m (sat_zero mont_limbs)) by (apply zero_valid; lia).
905 pose proof (nonzero_zero mont_limbs) as H3.
906 rewrite (nonzeromod_correct machine_wordsize mont_limbs m r' m') in H3
907 by (try apply zero_valid; lia).
908 cbn -[Z.mul oppmod sat_opp select].
909 rewrite select_push; rewrite ?length_sat_opp; auto.
910 rewrite sat_opp_mod2; auto.
911 rewrite twos_complement_pos'_spec.
912 rewrite twos_complement_pos_spec, <- !(eval_twos_complement_sat_mod2 machine_wordsize sat_limbs) by lia.
913 rewrite !Zmod_odd, !(select_eq (uweight machine_wordsize) _ mont_limbs), fodd;
914 try apply length_select; auto.
915 destruct (0 <? Z.twos_complement machine_wordsize d);
916 destruct (Z.odd (eval_twos_complement machine_wordsize sat_limbs g)); cbn -[Z.mul oppmod].
917 - rewrite (eval_addmod _ _ _ r'); auto.
918 rewrite <- Zplus_mod_idemp_l; auto.
919 rewrite (eval_oppmod _ _ _ r'); auto.
920 rewrite Zplus_mod_idemp_l; auto.
921 rewrite Z.add_comm; unfold Z.sub; auto.
922 - rewrite (eval_addmod _ _ _ r'); auto.
923 rewrite <- Zplus_mod_idemp_r; auto.
924 rewrite H3.
925 rewrite Z.mul_0_l, Z.add_0_r; auto.
926 - rewrite (eval_addmod _ _ _ r'); auto.
927 rewrite Z.mul_1_l; auto.
928 - rewrite (eval_addmod _ _ _ r'); auto.
929 rewrite <- Zplus_mod_idemp_r; auto.
930 rewrite H3.
931 rewrite Z.mul_0_l, Z.add_0_r; auto.
932 - apply length_sat_zero.
933 - unfold oppmod, WordByWordMontgomery.opp,
934 WordByWordMontgomery.sub, Rows.sub_then_maybe_add, Rows.add.
935 destruct (Rows.sub (uweight machine_wordsize) mont_limbs (zeros mont_limbs)) eqn:E.
936 destruct (Rows.flatten (uweight machine_wordsize) mont_limbs
937 [l; zselect (2 ^ machine_wordsize - 1) (- z) (Partition.partition (uweight machine_wordsize) mont_limbs m)]) eqn:E2.
938 simpl.
939 inversion E; subst.
940 inversion E2.
941 apply Rows.length_sum_rows.
942 apply (uwprops machine_wordsize mw0).
943 apply Rows.length_sum_rows.
944 apply (uwprops machine_wordsize mw0).
945 apply length_zeros.
946 apply map_length.
947 rewrite length_zselect.
948 apply length_partition. Qed.
949
950 Lemma divstep_spec_divstep_spec_full_d m d f g v r :
951 fst (fst (divstep_spec d f g)) = fst (fst (fst (fst (divstep_spec_full m d f g v r)))).
952 Proof. unfold divstep_spec, divstep_spec_full.
953 destruct ((0 <? d) && Z.odd g); reflexivity. Qed.
954
955 Lemma divstep_spec_divstep_spec_full_f m d f g v r :
956 snd (fst (divstep_spec d f g)) = snd (fst (fst (fst (divstep_spec_full m d f g v r)))).
957 Proof. unfold divstep_spec, divstep_spec_full.
958 destruct ((0 <? d) && Z.odd g); reflexivity. Qed.
959
960 Lemma divstep_spec_divstep_spec_full_g m d f g v r :
961 snd (divstep_spec d f g) = snd (fst (fst (divstep_spec_full m d f g v r))).
962 Proof. unfold divstep_spec, divstep_spec_full.
963 destruct ((0 <? d) && Z.odd g); reflexivity. Qed.
964
965 Lemma divstep_spec2_divstep_spec_full_v m d f g v r :
966 fst (divstep_spec2 m d g v r) = snd (fst (divstep_spec_full m d f g v r)).
967 Proof. unfold divstep_spec2, divstep_spec_full.
968 destruct ((0 <? d) && Z.odd g); reflexivity. Qed.
969
970 Lemma divstep_spec2_divstep_spec_full_r m d f g v r :
971 snd (divstep_spec2 m d g v r) = snd (divstep_spec_full m d f g v r).
972 Proof. unfold divstep_spec2, divstep_spec_full.
973 destruct ((0 <? d) && Z.odd g); reflexivity. Qed.
974
975 Theorem divstep_correct_full machine_wordsize sat_limbs mont_limbs m r' m' d f g v r
976 (fodd : Z.odd (eval_twos_complement machine_wordsize sat_limbs f) = true)
977 (r'_correct : (2 ^ machine_wordsize * r') mod m = 1)
978 (m'_correct : (m * m') mod 2 ^ machine_wordsize = -1 mod 2 ^ machine_wordsize)
979 (m_big : 1 < m)
980 (m_small : m < (2 ^ machine_wordsize) ^ Z.of_nat mont_limbs)
981 (mw1 : 1 < machine_wordsize)
982 (sat_limbs0 : (0 < sat_limbs)%nat)
983 (mont_limbs0 : (0 < mont_limbs)%nat)
984 (Hv : length v = mont_limbs)
985 (Hr : length r = mont_limbs)
986 (Hf : length f = sat_limbs)
987 (Hg : length g = sat_limbs)
988 (overflow_d : - 2 ^ (machine_wordsize - 1) + 1 <
989 Z.twos_complement machine_wordsize d <
990 2 ^ (machine_wordsize - 1) - 1)
991 (overflow_f : - 2 ^ (machine_wordsize * sat_limbs - 2) <
992 eval_twos_complement machine_wordsize sat_limbs f <
993 2 ^ (machine_wordsize * sat_limbs - 2))
994 (overflow_g : - 2 ^ (machine_wordsize * sat_limbs - 2) <
995 eval_twos_complement machine_wordsize sat_limbs g <
996 2 ^ (machine_wordsize * sat_limbs - 2))
997 (Hf2 : forall z, In z f -> 0 <= z < 2^machine_wordsize)
998 (Hg2 : forall z, In z g -> 0 <= z < 2^machine_wordsize)
999 (Hv2 : valid machine_wordsize mont_limbs m v)
1000 (Hr2 : valid machine_wordsize mont_limbs m r) :
1001 let '(d1,f1,g1,v1,r1) := (divstep machine_wordsize sat_limbs mont_limbs m d f g v r) in
1002 (Z.twos_complement machine_wordsize d1,
1003 eval_twos_complement machine_wordsize sat_limbs f1,
1004 eval_twos_complement machine_wordsize sat_limbs g1,
1005 @WordByWordMontgomery.eval machine_wordsize mont_limbs (from_montgomerymod machine_wordsize mont_limbs m m' v1) mod m,
1006 @WordByWordMontgomery.eval machine_wordsize mont_limbs (from_montgomerymod machine_wordsize mont_limbs m m' r1) mod m) =
1007 divstep_spec_full m (Z.twos_complement machine_wordsize d)
1008 (eval_twos_complement machine_wordsize sat_limbs f)
1009 (eval_twos_complement machine_wordsize sat_limbs g)
1010 (@WordByWordMontgomery.eval machine_wordsize mont_limbs (from_montgomerymod machine_wordsize mont_limbs m m' v))
1011 (@WordByWordMontgomery.eval machine_wordsize mont_limbs (from_montgomerymod machine_wordsize mont_limbs m m' r)).
1012 Proof.
1013 assert (0 < machine_wordsize) by lia.
1014 repeat apply injective_projections.
1015 rewrite <- divstep_spec_divstep_spec_full_d, <- (divstep_d _ _ mont_limbs m _ _ _ v r); auto.
1016 rewrite <- divstep_spec_divstep_spec_full_f, <- (divstep_f _ _ mont_limbs m _ _ _ v r); auto; lia.
1017 rewrite <- divstep_spec_divstep_spec_full_g, <- (divstep_g _ _ mont_limbs m _ _ _ v r); auto; lia.
1018 rewrite <- divstep_spec2_divstep_spec_full_v;
1019 rewrite <- (divstep_v _ _ _ _ r' _ _ f _ _ _); auto.
1020 rewrite <- divstep_spec2_divstep_spec_full_r;
1021 rewrite <- (divstep_r _ _ _ _ r' _ _ f _ _ _); auto. Qed.