Debian Codegraph @

src/Arithmetic/WordByWordMontgomery.v

raw
1 From Coq Require Import ZArith Lia.
2 From Coq Require Import List.
3 Require Import Crypto.Algebra.Ring.
4 Require Import Crypto.Arithmetic.Core.
5 Require Import Crypto.Arithmetic.Freeze.
6 Require Import Crypto.Arithmetic.ModularArithmeticTheorems.
7 Require Import Crypto.Arithmetic.Partition.
8 Require Import Crypto.Arithmetic.PrimeFieldTheorems.
9 Require Import Crypto.Arithmetic.Saturated.
10 Require Import Crypto.Arithmetic.UniformWeight.
11 Require Import Crypto.Util.LetIn.
12 Require Import Crypto.Util.ListUtil.
13 Require Import Crypto.Util.Prod.
14 Require Import Crypto.Util.Sigma.
15 Require Import Crypto.Util.Tactics.SetEvars.
16 Require Import Crypto.Util.Tactics.SubstEvars.
17 Require Import Crypto.Util.ZUtil.Modulo Crypto.Util.ZUtil.Div.
18 Require Import Crypto.Util.ZUtil.Definitions.
19 Require Import Crypto.Util.ZUtil.EquivModulo.
20 Require Import Crypto.Util.ZUtil.Log2.
21 Require Import Crypto.Util.ZUtil.AddGetCarry Crypto.Util.ZUtil.MulSplit.
22 Require Import Crypto.Util.ZUtil.Modulo.PullPush.
23 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
24 Require Import Crypto.Util.ZUtil.Tactics.LinearSubstitute.
25 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
26 Require Import Crypto.Util.ZUtil.Tactics.PeelLe.
27 Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall.
28 Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds.
29 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
30
31 Require Import Crypto.Util.Notations.
32 Import ListNotations. Local Open Scope Z_scope.
33
34 Module WordByWordMontgomery.
35 Section with_args.
36 Context (lgr : Z)
37 (m : Z).
38 Local Notation weight := (uweight lgr).
39 Let T (n : nat) := list Z.
40 Let r := (2^lgr).
41 Definition eval {n} : T n -> Z := Positional.eval weight n.
42 Let zero {n} : T n := Positional.zeros n.
43 Let divmod {n} : T (S n) -> T n * Z := Rows.divmod.
44 Let scmul {n} (c : Z) (p : T n) : T (S n) (* uses double-output multiply *)
45 := let '(v, c) := Rows.mul weight r n (S n) (Positional.extend_to_length 1 n [c]) p in
46 v.
47 Let addT {n} (p q : T n) : T (S n) (* joins carry *)
48 := let '(v, c) := Rows.add weight n p q in
49 v ++ [c].
50 Let drop_high_addT' {n} (p : T (S n)) (q : T n) : T (S n) (* drops carry *)
51 := fst (Rows.add weight (S n) p (Positional.extend_to_length n (S n) q)).
52 Let conditional_sub {n} (arg : T (S n)) (N : T n) : T n (* computes [arg - N] if [N <= arg], and drops high bit *)
53 := Positional.drop_high_to_length n (Rows.conditional_sub weight (S n) arg (Positional.extend_to_length n (S n) N)).
54 Context (R_numlimbs : nat)
55 (N : T R_numlimbs). (* encoding of m *)
56 Let sub_then_maybe_add (a b : T R_numlimbs) : T R_numlimbs (* computes [a - b + if (a - b) <? 0 then N else 0] *)
57 := fst (Rows.sub_then_maybe_add weight R_numlimbs (r-1) a b N).
58 Local Opaque T.
59 Section Iteration.
60 Context (pred_A_numlimbs : nat)
61 (B : T R_numlimbs) (k : Z)
62 (A : T (S pred_A_numlimbs))
63 (S : T (S R_numlimbs)).
64 (* Given A, B < R, we want to compute A * B / R mod N. R = bound 0 * ... * bound (n-1) *)
65 Local Definition A_a := dlet p := @divmod _ A in p. Local Definition A' := fst A_a. Local Definition a := snd A_a.
66 Local Definition S1 := @addT _ S (@scmul _ a B).
67 Local Definition s := snd (@divmod _ S1).
68 Local Definition q := fst (Z.mul_split r s k).
69 Local Definition S2 := @drop_high_addT' _ S1 (@scmul _ q N).
70 Local Definition S3' := fst (@divmod _ S2).
71
72 Local Definition A'_S3
73 := dlet A_a := @divmod _ A in
74 dlet A' := fst A_a in
75 dlet a := snd A_a in
76 dlet S1 := @addT _ S (@scmul _ a B) in
77 dlet s := snd (@divmod _ S1) in
78 dlet q := fst (Z.mul_split r s k) in
79 dlet S2 := @drop_high_addT' _ S1 (@scmul _ q N) in
80 dlet S3 := fst (@divmod _ S2) in
81 (A', S3).
82
83 Lemma A'_S3_alt : A'_S3 = (A', S3').
84 Proof using Type. cbv [A'_S3 A' S3' Let_In S2 q s S1 A' a A_a]; reflexivity. Qed.
85
86 Context (a' : Z).
87
88 Local Definition a'_S3
89 := dlet S1 := @addT _ S (@scmul _ a' B) in
90 dlet s := snd (@divmod _ S1) in
91 dlet q := fst (Z.mul_split r s k) in
92 dlet S2 := @drop_high_addT' _ S1 (@scmul _ q N) in
93 fst (@divmod _ S2).
94
95 Lemma a_S3: a' = a -> a'_S3 = snd A'_S3.
96 Proof using Type. intros; cbv [a'_S3 A'_S3 Let_In]; subst; reflexivity. Qed.
97
98 End Iteration.
99
100 Section loop.
101 Context (A_numlimbs : nat)
102 (A : T A_numlimbs)
103 (B : T R_numlimbs)
104 (k : Z)
105 (S' : T (S R_numlimbs)).
106
107 Definition redc_body {pred_A_numlimbs} : T (S pred_A_numlimbs) * T (S R_numlimbs)
108 -> T pred_A_numlimbs * T (S R_numlimbs)
109 := fun '(A, S') => A'_S3 _ B k A S'.
110
111 Definition redc_loop (count : nat) : T count * T (S R_numlimbs) -> T O * T (S R_numlimbs)
112 := nat_rect
113 (fun count => T count * _ -> _)
114 (fun A_S => A_S)
115 (fun count' redc_loop_count' A_S
116 => redc_loop_count' (redc_body A_S))
117 count.
118
119 Definition pre_redc : T (S R_numlimbs)
120 := snd (redc_loop A_numlimbs (A, @zero (1 + R_numlimbs)%nat)).
121
122 Definition redc : T R_numlimbs
123 := conditional_sub pre_redc N.
124
125 Definition pre_redc_from (S': T (S R_numlimbs)) : T (S R_numlimbs)
126 := snd (redc_loop A_numlimbs (A, S')).
127
128 Definition redc_body_alt: Z * T (S R_numlimbs) -> T (S R_numlimbs)
129 := fun '(a, S') => a'_S3 B k S' a.
130
131 Definition redc_loop_alt: T (S R_numlimbs) :=
132 fold_left (fun S a => redc_body_alt (a, S)) A (@zero (1 + R_numlimbs)%nat).
133
134 Definition redc_loop_alt_from (S': T (S R_numlimbs)) : T (S R_numlimbs) :=
135 fold_left (fun S a => redc_body_alt (a, S)) A S'.
136
137 Definition redc_alt: T (R_numlimbs) :=
138 conditional_sub redc_loop_alt N.
139
140 End loop.
141
142 Lemma redc_loop_alt_from_eq:
143 forall {A_numlimbs: nat} (A: T A_numlimbs) (B: T R_numlimbs) (k: Z) (S_old: T (S R_numlimbs)),
144 redc_loop_alt_from (length A) A B k S_old = pre_redc_from (length A) A B k S_old.
145 Proof.
146 intros. generalize dependent S_old. induction A.
147 - reflexivity.
148 - intros.
149 cbv [pre_redc_from redc_loop_alt_from]. simpl. cbv [redc_loop_alt_from redc_body_alt] in IHA. rewrite IHA.
150 cbv [pre_redc_from]. f_equal.
151 Qed.
152
153 Lemma pre_redc_from_zero:
154 forall {A_numlimbs: nat} (A: T A_numlimbs) (B: T R_numlimbs) (k: Z),
155 pre_redc (length A) A B k = pre_redc_from (length A) A B k zero.
156 Proof.
157 intros. reflexivity.
158 Qed.
159
160 Lemma redc_loop_alt_from_zero:
161 forall {A_numlimbs: nat} (A: T A_numlimbs) (B: T R_numlimbs) (k: Z),
162 redc_loop_alt (length A) A B k = redc_loop_alt_from (length A) A B k zero.
163 Proof.
164 intros. reflexivity.
165 Qed.
166
167 Theorem redc_alt_eq:
168 forall {A_numlimbs: nat} (A: T A_numlimbs) (B: T R_numlimbs) (k : Z),
169 redc (length A) A B k = redc_alt (length A) A B k.
170 Proof.
171 intros. cbv [redc redc_alt]. f_equal. rewrite pre_redc_from_zero. rewrite redc_loop_alt_from_zero.
172 symmetry. apply redc_loop_alt_from_eq.
173 Qed.
174
175 Create HintDb word_by_word_montgomery.
176 Hint Unfold A'_S3 S3' S2 q s S1 a A' A_a Let_In : word_by_word_montgomery.
177
178 Definition add (A B : T R_numlimbs) : T R_numlimbs
179 := conditional_sub (@addT _ A B) N.
180 Definition sub (A B : T R_numlimbs) : T R_numlimbs
181 := sub_then_maybe_add A B.
182 Definition opp (A : T R_numlimbs) : T R_numlimbs
183 := sub (@zero _) A.
184 Definition nonzero (A : list Z) : Z
185 := fold_right Z.lor 0 A.
186
187 Context (lgr_big : 0 < lgr)
188 (R_numlimbs_nz : R_numlimbs <> 0%nat).
189 Let R := (r^Z.of_nat R_numlimbs).
190 Transparent T.
191 Definition small {n} (v : T n) : Prop
192 := v = Partition.partition weight n (eval v).
193 Context (small_N : small N)
194 (N_lt_R : eval N < R)
195 (N_nz : 0 < eval N)
196 (B : T R_numlimbs)
197 (B_bounds : 0 <= eval B < R)
198 (small_B : small B)
199 ri (ri_correct : r*ri mod (eval N) = 1 mod (eval N))
200 (k : Z) (k_correct : k * eval N mod r = (-1) mod r).
201
202 Local Lemma r_big : r > 1.
203 Proof using lgr_big. clear -lgr_big; subst r. auto with zarith. Qed.
204 Local Notation wprops := (@uwprops lgr lgr_big).
205 Local Definition weight_0_wprops := weight_0 wprops.
206 Local Definition weight_positive_wprops := weight_positive wprops.
207 Local Definition weight_multiples_wprops := weight_multiples wprops.
208 Local Definition weight_divides_wprops := weight_divides wprops.
209
210 Local Hint Extern 0 weight_properties => simple apply wprops : core.
211 Local Hint Immediate weight_0_wprops : core.
212 Local Hint Immediate weight_positive_wprops : core.
213 Local Hint Immediate weight_multiples_wprops : core.
214 Local Hint Immediate weight_divides_wprops : core.
215 Local Hint Immediate r_big : core.
216
217 Lemma length_small {n v} : @small n v -> length v = n.
218 Proof using Type. clear; cbv [small]; intro H; rewrite H; autorewrite with distr_length; reflexivity. Qed.
219 Lemma small_bound {n v} : @small n v -> 0 <= eval v < weight n.
220 Proof using lgr_big. clear - lgr_big; cbv [small eval]; intro H; rewrite H; autorewrite with push_eval; auto with zarith. Qed.
221
222 Lemma R_plusR_le : R + R <= weight (S R_numlimbs).
223 Proof using lgr_big.
224 clear - lgr_big.
225 etransitivity; [ | apply uweight_double_le; lia ].
226 rewrite uweight_eq_alt by lia.
227 subst r R; lia.
228 Qed.
229
230 Lemma mask_r_sub1 n x :
231 map (Z.land (r - 1)) (Partition.partition weight n x) = Partition.partition weight n x.
232 Proof using lgr_big.
233 clear - lgr_big. cbv [Partition.partition].
234 rewrite map_map. apply map_ext; intros.
235 rewrite uweight_S by lia.
236 rewrite <-Z.mod_pull_div by auto with zarith.
237 replace (r - 1) with (Z.ones lgr) by (rewrite Z.ones_equiv; subst r; reflexivity).
238 rewrite <-Z.land_comm, Z.land_ones by lia.
239 auto with zarith.
240 Qed.
241
242 Let partition_Proper := (@Partition.partition_Proper _ wprops).
243 Local Existing Instance partition_Proper.
244 Lemma eval_nonzero n A : @small n A -> nonzero A = 0 <-> @eval n A = 0.
245 Proof using lgr_big.
246 clear -lgr_big partition_Proper.
247 cbv [nonzero eval small]; intro Heq.
248 do 2 rewrite Heq.
249 rewrite !eval_partition, Z.mod_mod by auto with zarith.
250 generalize (Positional.eval weight n A); clear Heq A.
251 induction n as [|n IHn].
252 { cbn; rewrite weight_0 by auto; intros; autorewrite with zsimplify_const; lia. }
253 { intro; rewrite partition_step.
254 rewrite fold_right_snoc, Z.lor_comm, <- fold_right_push, Z.lor_eq_0_iff by auto using Z.lor_assoc.
255 assert (Heq : Z.equiv_modulo (weight n) (z mod weight (S n)) (z mod (weight n))).
256 { cbv [Z.equiv_modulo].
257 generalize (weight_multiples ltac:(auto) n).
258 generalize (weight_positive ltac:(auto) n).
259 generalize (weight_positive ltac:(auto) (S n)).
260 generalize (weight (S n)) (weight n); clear; intros wsn wn.
261 clear; intros.
262 Z.div_mod_to_quot_rem; subst.
263 autorewrite with zsimplify_const in *.
264 Z.linear_substitute_all.
265 apply Zminus_eq; ring_simplify.
266 rewrite <- !Z.add_opp_r, !Z.mul_opp_comm, <- !Z.mul_opp_r, <- !Z.mul_assoc.
267 rewrite <- !Z.mul_add_distr_l, Z.mul_eq_0.
268 nia. }
269 rewrite Heq at 1; rewrite IHn.
270 rewrite Z.mod_mod by auto with zarith.
271 generalize (weight_multiples ltac:(auto) n).
272 generalize (weight_positive ltac:(auto) n).
273 generalize (weight_positive ltac:(auto) (S n)).
274 generalize (weight (S n)) (weight n); clear; intros wsn wn; intros.
275 Z.div_mod_to_quot_rem.
276 repeat (intro || apply conj); destruct_head'_or; try lia; destruct_head'_and; subst; autorewrite with zsimplify_const in *; try nia;
277 Z.linear_substitute_all.
278 all: apply Zminus_eq; ring_simplify.
279 all: rewrite <- ?Z.add_opp_r, ?Z.mul_opp_comm, <- ?Z.mul_opp_r, <- ?Z.mul_assoc.
280 all: rewrite <- !Z.mul_add_distr_l, Z.mul_eq_0.
281 all: nia. }
282 Qed.
283
284 Local Ltac push_step :=
285 first [ progress eta_expand
286 | rewrite Rows.mul_partitions
287 | rewrite Rows.mul_div
288 | rewrite Rows.add_partitions
289 | rewrite Rows.add_div
290 | progress autorewrite with push_eval distr_length
291 | match goal with
292 | [ H : ?v = _ |- context[length ?v] ] => erewrite length_small by eassumption
293 | [ H : small ?v |- context[length ?v] ] => erewrite length_small by eassumption
294 end
295 | rewrite Positional.eval_cons by distr_length
296 | progress rewrite ?weight_0, ?uweight_1 by auto;
297 autorewrite with zsimplify_fast
298 | rewrite (weight_0 wprops)
299 | rewrite <- Z.div_mod'' by auto with lia
300 | solve [ trivial ] ].
301 Local Ltac push := repeat push_step.
302
303 Local Ltac t_step :=
304 match goal with
305 | [ H := _ |- _ ] => progress cbv [H] in *
306 | _ => progress push_step
307 | _ => progress autorewrite with zsimplify_const
308 | _ => solve [ auto with lia ]
309 end.
310
311 Local Hint Unfold eval zero small divmod scmul drop_high_addT' addT R : loc.
312 Local Lemma eval_zero : forall n, eval (@zero n) = 0.
313 Proof using Type.
314 clear; autounfold with loc; intros; autorewrite with push_eval; auto.
315 Qed.
316 Local Lemma small_zero : forall n, small (@zero n).
317 Proof using Type.
318 etransitivity; [ eapply Positional.zeros_ext_map | rewrite eval_zero ]; cbv [Partition.partition]; cbn; try reflexivity; autorewrite with distr_length; reflexivity.
319 Qed.
320 Local Hint Immediate small_zero : core.
321
322 Ltac push_recursive_partition :=
323 repeat match goal with
324 | _ => progress cbn [recursive_partition]
325 | H : small _ |- _ => rewrite H; clear H
326 | _ => rewrite recursive_partition_equiv by auto using wprops
327 | _ => rewrite uweight_eval_shift by distr_length
328 | _ => progress push
329 end.
330
331 Lemma eval_div : forall n v, small v -> eval (fst (@divmod n v)) = eval v / r.
332 Proof using lgr_big.
333 pose proof r_big as r_big.
334 clear - r_big lgr_big; intros; autounfold with loc.
335 push_recursive_partition; cbn [Rows.divmod fst tl].
336 autorewrite with zsimplify; reflexivity.
337 Qed.
338 Lemma eval_mod : forall n v, small v -> snd (@divmod n v) = eval v mod r.
339 Proof using lgr_big.
340 clear - lgr_big; intros; autounfold with loc.
341 push_recursive_partition; cbn [Rows.divmod snd hd].
342 autorewrite with zsimplify; reflexivity.
343 Qed.
344 Lemma small_div : forall n v, small v -> small (fst (@divmod n v)).
345 Proof using lgr_big.
346 pose proof r_big as r_big.
347 clear - r_big lgr_big. intros; autounfold with loc.
348 push_recursive_partition. cbn [Rows.divmod fst tl].
349 rewrite <-recursive_partition_equiv by auto.
350 rewrite <-uweight_recursive_partition_equiv with (i:=1%nat) by lia.
351 push.
352 apply Partition.partition_Proper; [ solve [auto] | ].
353 cbv [Z.equiv_modulo]. autorewrite with zsimplify.
354 reflexivity.
355 Qed.
356
357 Definition canon_rep {n} x (v : T n) : Prop :=
358 (v = Partition.partition weight n x) /\ (0 <= x < weight n).
359 Lemma eval_canon_rep n x v : @canon_rep n x v -> eval v = x.
360 Proof using lgr_big.
361 clear - lgr_big.
362 cbv [canon_rep eval]; intros [Hv Hx].
363 rewrite Hv. autorewrite with push_eval.
364 auto using Z.mod_small.
365 Qed.
366 Lemma small_canon_rep n x v : @canon_rep n x v -> small v.
367 Proof using lgr_big.
368 clear - lgr_big.
369 cbv [canon_rep eval small]; intros [Hv Hx].
370 rewrite Hv. autorewrite with push_eval.
371 apply partition_eq_mod; auto; [ ].
372 Z.rewrite_mod_small; reflexivity.
373 Qed.
374
375 Local Lemma scmul_correct: forall n a v, small v -> 0 <= a < r -> 0 <= eval v < r^Z.of_nat n -> canon_rep (a * eval v) (@scmul n a v).
376 Proof using lgr_big.
377 pose proof r_big as r_big.
378 clear - lgr_big r_big.
379 autounfold with loc; intro n; destruct (zerop n); intros *; intro Hsmall; intros.
380 { intros; subst; cbn; rewrite Z.add_with_get_carry_full_mod.
381 split; cbn; autorewrite with zsimplify_fast; auto with zarith. }
382 { rewrite (surjective_pairing (Rows.mul _ _ _ _ _ _)).
383 rewrite Rows.mul_partitions by (try rewrite Hsmall; auto using length_partition, Positional.length_extend_to_length with lia).
384 autorewrite with push_eval zsimplify_fast.
385 split; [reflexivity | ].
386 rewrite uweight_S, uweight_eq_alt by lia.
387 subst r; nia. }
388 Qed.
389
390 Local Lemma addT_correct : forall n a b, small a -> small b -> canon_rep (eval a + eval b) (@addT n a b).
391 Proof using lgr_big.
392 intros n a b Ha Hb.
393 generalize (length_small Ha); generalize (length_small Hb).
394 generalize (small_bound Ha); generalize (small_bound Hb).
395 clear -lgr_big Ha Hb.
396 autounfold with loc; destruct (zerop n); subst.
397 { destruct a, b; cbn; try lia; split; auto with zarith. }
398 { pose proof (uweight_double_le lgr ltac:(lia) n).
399 eta_expand; split; [ | lia ].
400 rewrite Rows.add_partitions, Rows.add_div by auto.
401 rewrite partition_step.
402 Z.rewrite_mod_small; reflexivity. }
403 Qed.
404
405 Local Lemma drop_high_addT'_correct : forall n a b, small a -> small b -> canon_rep ((eval a + eval b) mod (r^Z.of_nat (S n))) (@drop_high_addT' n a b).
406 Proof using lgr_big.
407 intros n a b Ha Hb; generalize (length_small Ha); generalize (length_small Hb).
408 clear -lgr_big Ha Hb.
409 autounfold with loc in *; subst; intros.
410 rewrite Rows.add_partitions by auto using Positional.length_extend_to_length.
411 autorewrite with push_eval.
412 split; try apply partition_eq_mod; auto; rewrite uweight_eq_alt by lia; subst r; Z.rewrite_mod_small; auto with zarith.
413 Qed.
414
415 Local Lemma conditional_sub_correct : forall v, small v -> 0 <= eval v < eval N + R -> canon_rep (eval v + (if eval N <=? eval v then -eval N else 0)) (conditional_sub v N).
416 Proof using small_N lgr_big N_nz N_lt_R.
417 pose proof R_plusR_le as R_plusR_le.
418 clear - small_N lgr_big N_nz N_lt_R R_plusR_le.
419 intros; autounfold with loc; cbv [conditional_sub].
420 repeat match goal with H : small _ |- _ =>
421 rewrite H; clear H end.
422 autorewrite with push_eval.
423 assert (weight R_numlimbs < weight (S R_numlimbs)) by (rewrite !uweight_eq_alt by lia; autorewrite with push_Zof_nat; auto with zarith).
424 assert (eval N mod weight R_numlimbs < weight (S R_numlimbs)) by (pose proof (Z.mod_pos_bound (eval N) (weight R_numlimbs) ltac:(auto)); lia).
425 rewrite Rows.conditional_sub_partitions by (repeat (autorewrite with distr_length push_eval; auto using partition_eq_mod with zarith)).
426 rewrite drop_high_to_length_partition by lia.
427 autorewrite with push_eval.
428 assert (weight R_numlimbs = R) by (rewrite uweight_eq_alt by lia; subst R; reflexivity).
429 Z.rewrite_mod_small.
430 break_match; autorewrite with zsimplify_fast; Z.ltb_to_lt.
431 { split; [ reflexivity | ].
432 rewrite Z.add_opp_r. fold (eval N).
433 auto using Z.mod_small with lia. }
434 { split; auto using Z.mod_small with lia. }
435 Qed.
436
437 Local Lemma sub_then_maybe_add_correct : forall a b, small a -> small b -> 0 <= eval a < eval N -> 0 <= eval b < eval N -> canon_rep (eval a - eval b + (if eval a - eval b <? 0 then eval N else 0)) (sub_then_maybe_add a b).
438 Proof using small_N lgr_big R_numlimbs_nz N_nz N_lt_R.
439 pose proof mask_r_sub1 as mask_r_sub1.
440 clear - small_N lgr_big R_numlimbs_nz N_nz N_lt_R mask_r_sub1.
441 intros; autounfold with loc; cbv [sub_then_maybe_add].
442 repeat match goal with H : small _ |- _ =>
443 rewrite H; clear H end.
444 rewrite Rows.sub_then_maybe_add_partitions by (autorewrite with push_eval distr_length; auto with zarith).
445 autorewrite with push_eval.
446 assert (weight R_numlimbs = R) by (rewrite uweight_eq_alt by lia; subst r R; reflexivity).
447 Z.rewrite_mod_small.
448 split; [ reflexivity | ].
449 break_match; Z.ltb_to_lt; lia.
450 Qed.
451
452 Local Lemma eval_scmul: forall n a v, small v -> 0 <= a < r -> 0 <= eval v < r^Z.of_nat n -> eval (@scmul n a v) = a * eval v.
453 Proof using lgr_big. eauto using scmul_correct, eval_canon_rep. Qed.
454 Local Lemma small_scmul : forall n a v, small v -> 0 <= a < r -> 0 <= eval v < r^Z.of_nat n -> small (@scmul n a v).
455 Proof using lgr_big. eauto using scmul_correct, small_canon_rep. Qed.
456 Local Lemma eval_addT : forall n a b, small a -> small b -> eval (@addT n a b) = eval a + eval b.
457 Proof using lgr_big. eauto using addT_correct, eval_canon_rep. Qed.
458 Local Lemma small_addT : forall n a b, small a -> small b -> small (@addT n a b).
459 Proof using lgr_big. eauto using addT_correct, small_canon_rep. Qed.
460 Local Lemma eval_drop_high_addT' : forall n a b, small a -> small b -> eval (@drop_high_addT' n a b) = (eval a + eval b) mod (r^Z.of_nat (S n)).
461 Proof using lgr_big. eauto using drop_high_addT'_correct, eval_canon_rep. Qed.
462 Local Lemma small_drop_high_addT' : forall n a b, small a -> small b -> small (@drop_high_addT' n a b).
463 Proof using lgr_big. eauto using drop_high_addT'_correct, small_canon_rep. Qed.
464 Local Lemma eval_conditional_sub : forall v, small v -> 0 <= eval v < eval N + R -> eval (conditional_sub v N) = eval v + (if eval N <=? eval v then -eval N else 0).
465 Proof using small_N lgr_big R_numlimbs_nz N_nz N_lt_R. eauto using conditional_sub_correct, eval_canon_rep. Qed.
466 Local Lemma small_conditional_sub : forall v, small v -> 0 <= eval v < eval N + R -> small (conditional_sub v N).
467 Proof using small_N lgr_big R_numlimbs_nz N_nz N_lt_R. eauto using conditional_sub_correct, small_canon_rep. Qed.
468 Local Lemma eval_sub_then_maybe_add : forall a b, small a -> small b -> 0 <= eval a < eval N -> 0 <= eval b < eval N -> eval (sub_then_maybe_add a b) = eval a - eval b + (if eval a - eval b <? 0 then eval N else 0).
469 Proof using small_N lgr_big R_numlimbs_nz N_nz N_lt_R. eauto using sub_then_maybe_add_correct, eval_canon_rep. Qed.
470 Local Lemma small_sub_then_maybe_add : forall a b, small a -> small b -> 0 <= eval a < eval N -> 0 <= eval b < eval N -> small (sub_then_maybe_add a b).
471 Proof using small_N lgr_big R_numlimbs_nz N_nz N_lt_R. eauto using sub_then_maybe_add_correct, small_canon_rep. Qed.
472
473 Local Opaque T addT drop_high_addT' divmod zero scmul conditional_sub sub_then_maybe_add.
474 Create HintDb push_mont_eval discriminated.
475 Create HintDb word_by_word_montgomery.
476 Hint Unfold A'_S3 S3' S2 q s S1 a A' A_a Let_In : word_by_word_montgomery.
477 Let r_big' := r_big. (* to put it in the context *)
478 Local Ltac t_small :=
479 repeat first [ assumption
480 | apply small_addT
481 | apply small_drop_high_addT'
482 | apply small_div
483 | apply small_zero
484 | apply small_scmul
485 | apply small_conditional_sub
486 | apply small_sub_then_maybe_add
487 | apply Z_mod_lt
488 | rewrite Z.mul_split_mod
489 | solve [ auto with zarith ]
490 | lia
491 | progress autorewrite with push_mont_eval
492 | progress autounfold with word_by_word_montgomery
493 | match goal with
494 | [ H : and _ _ |- _ ] => destruct H
495 end ].
496 Hint Rewrite
497 eval_zero
498 eval_div
499 eval_mod
500 eval_addT
501 eval_drop_high_addT'
502 eval_scmul
503 eval_conditional_sub
504 eval_sub_then_maybe_add
505 using (repeat autounfold with word_by_word_montgomery; t_small)
506 : push_mont_eval.
507
508 Local Arguments eval {_} _.
509 Local Arguments small {_} _.
510 Local Arguments divmod {_} _.
511
512 (* Recurse for a as many iterations as A has limbs, varying A := A, S := 0, r, bounds *)
513 Section Iteration_proofs.
514 Context (pred_A_numlimbs : nat)
515 (A : T (S pred_A_numlimbs))
516 (S : T (S R_numlimbs))
517 (small_A : small A)
518 (small_S : small S)
519 (S_nonneg : 0 <= eval S).
520 (* Given A, B < R, we want to compute A * B / R mod N. R = bound 0 * ... * bound (n-1) *)
521
522 Local Coercion eval : T >-> Z.
523
524 Local Notation a := (@a pred_A_numlimbs A).
525 Local Notation A' := (@A' pred_A_numlimbs A).
526 Local Notation S1 := (@S1 pred_A_numlimbs B A S).
527 Local Notation s := (@s pred_A_numlimbs B A S).
528 Local Notation q := (@q pred_A_numlimbs B k A S).
529 Local Notation S2 := (@S2 pred_A_numlimbs B k A S).
530 Local Notation S3 := (@S3' pred_A_numlimbs B k A S).
531
532 Local Notation eval_pre_S3 := ((S + a * B + q * N) / r).
533
534 Lemma eval_S3_eq : eval S3 = eval_pre_S3 mod (r * r ^ Z.of_nat R_numlimbs).
535 Proof using small_A small_S small_B B_bounds N_nz N_lt_R small_N lgr_big.
536 clear -small_A small_S r_big' partition_Proper small_B B_bounds N_nz N_lt_R small_N lgr_big.
537 unfold S3, S2, S1.
538 autorewrite with push_mont_eval push_Zof_nat; [].
539 rewrite !Z.pow_succ_r, <- ?Z.mul_assoc by lia.
540 rewrite Z.mod_pull_div by Z.zero_bounds.
541 do 2 f_equal; nia.
542 Qed.
543
544 Lemma pre_S3_bound
545 : eval S < eval N + eval B
546 -> eval_pre_S3 < eval N + eval B.
547 Proof using small_A small_S small_B B_bounds N_nz N_lt_R small_N lgr_big.
548 clear -small_A small_S r_big' partition_Proper small_B B_bounds N_nz N_lt_R small_N lgr_big.
549 assert (Hmod : forall a b, 0 < b -> a mod b <= b - 1)
550 by (intros x y; pose proof (Z_mod_lt x y); lia).
551 intro HS.
552 eapply Z.le_lt_trans.
553 { transitivity ((N+B-1 + (r-1)*B + (r-1)*N) / r);
554 [ | set_evars; ring_simplify_subterms; subst_evars; reflexivity ].
555 Z.peel_le; repeat apply Z.add_le_mono; repeat apply Z.mul_le_mono_nonneg; try lia;
556 repeat autounfold with word_by_word_montgomery; rewrite ?Z.mul_split_mod;
557 autorewrite with push_mont_eval;
558 try Z.zero_bounds;
559 auto with lia. }
560 rewrite (Z.mul_comm _ r), <- Z.add_sub_assoc, <- Z.add_opp_r, !Z.div_add_l' by lia.
561 autorewrite with zsimplify.
562 simpl; lia.
563 Qed.
564
565 Lemma pre_S3_nonneg : 0 <= eval_pre_S3.
566 Proof using N_nz B_bounds small_B small_A small_S S_nonneg lgr_big.
567 clear -N_nz B_bounds small_B partition_Proper r_big' small_A small_S S_nonneg.
568 repeat autounfold with word_by_word_montgomery; rewrite ?Z.mul_split_mod;
569 autorewrite with push_mont_eval; [].
570 rewrite ?Npos_correct; Z.zero_bounds; lia.
571 Qed.
572
573 Lemma small_A'
574 : small A'.
575 Proof using small_A lgr_big. repeat autounfold with word_by_word_montgomery; t_small. Qed.
576
577 Lemma small_S3
578 : small S3.
579 Proof using small_A small_S small_N N_lt_R N_nz B_bounds small_B lgr_big.
580 clear -small_A small_S small_N N_lt_R N_nz B_bounds small_B partition_Proper r_big'.
581 repeat autounfold with word_by_word_montgomery; t_small.
582 Qed.
583
584 Lemma S3_nonneg : 0 <= eval S3.
585 Proof using small_A small_S small_B B_bounds N_nz N_lt_R small_N lgr_big.
586 clear -small_A small_S r_big' partition_Proper small_B B_bounds N_nz N_lt_R small_N lgr_big sub_then_maybe_add.
587 rewrite eval_S3_eq; Z.zero_bounds.
588 Qed.
589
590 Lemma S3_bound
591 : eval S < eval N + eval B
592 -> eval S3 < eval N + eval B.
593 Proof using N_nz B_bounds small_B small_A small_S S_nonneg B_bounds N_nz N_lt_R small_N lgr_big.
594 clear -N_nz B_bounds small_B small_A small_S S_nonneg B_bounds N_nz N_lt_R small_N lgr_big partition_Proper r_big' sub_then_maybe_add.
595 rewrite eval_S3_eq.
596 intro H; pose proof (pre_S3_bound H); pose proof pre_S3_nonneg.
597 subst R.
598 rewrite Z.mod_small by nia.
599 assumption.
600 Qed.
601
602 Lemma S1_eq : eval S1 = S + a*B.
603 Proof using B_bounds R_numlimbs_nz lgr_big small_A small_B small_S.
604 clear -B_bounds R_numlimbs_nz lgr_big small_A small_B small_S r_big' partition_Proper.
605 cbv [S1 a A'].
606 repeat autorewrite with push_mont_eval.
607 reflexivity.
608 Qed.
609
610 Lemma S2_mod_r_helper : (S + a*B + q * N) mod r = 0.
611 Proof using B_bounds R_numlimbs_nz lgr_big small_A small_B small_S k_correct.
612 clear -B_bounds R_numlimbs_nz lgr_big small_A small_B small_S r_big' partition_Proper k_correct.
613 cbv [S2 q s]; autorewrite with push_mont_eval; rewrite S1_eq.
614 assert (r > 0) by lia.
615 assert (Hr : (-(1 mod r)) mod r = r - 1 /\ (-(1)) mod r = r - 1).
616 { destruct (Z.eq_dec r 1) as [H'|H'].
617 { rewrite H'; split; reflexivity. }
618 { rewrite !Z_mod_nz_opp_full; rewrite ?Z.mod_mod; Z.rewrite_mod_small; [ split; reflexivity | lia.. ]. } }
619 autorewrite with pull_Zmod.
620 replace 0 with (0 mod r) by apply Zmod_0_l.
621 pose (Z.to_pos r) as r'.
622 replace r with (Z.pos r') by (subst r'; rewrite Z2Pos.id; lia).
623 eapply F.eq_of_Z_iff.
624 rewrite Z.mul_split_mod.
625 repeat rewrite ?F.of_Z_add, ?F.of_Z_mul, <-?F.of_Z_mod.
626 rewrite <-!Algebra.Hierarchy.associative.
627 replace ((F.of_Z r' k * F.of_Z r' (eval N))%F) with (F.opp (m:=r') F.one).
628 { cbv [F.of_Z F.add]; simpl.
629 apply path_sig_hprop; [ intro; exact HProp.allpath_hprop | ].
630 simpl.
631 subst r'; rewrite Z2Pos.id by lia.
632 rewrite (proj1 Hr), Z.mul_sub_distr_l.
633 push_Zmod; pull_Zmod.
634 apply (f_equal2 Z.modulo); lia. }
635 { rewrite <- F.of_Z_mul.
636 rewrite F.of_Z_mod.
637 subst r'; rewrite Z2Pos.id by lia.
638 rewrite k_correct.
639 cbv [F.of_Z F.add F.opp F.one]; simpl.
640 change (-(1)) with (-1) in *.
641 apply path_sig_hprop; [ intro; exact HProp.allpath_hprop | ]; simpl.
642 rewrite Z2Pos.id by lia.
643 rewrite (proj1 Hr), (proj2 Hr); Z.rewrite_mod_small; reflexivity. }
644 Qed.
645
646 Lemma pre_S3_mod_N
647 : eval_pre_S3 mod N = (S + a*B)*ri mod N.
648 Proof using B_bounds R_numlimbs_nz lgr_big small_A small_B small_S k_correct ri_correct.
649 clear -B_bounds R_numlimbs_nz lgr_big small_A small_B small_S r_big' partition_Proper k_correct ri_correct sub_then_maybe_add.
650 pose proof fun a => Z.div_to_inv_modulo N a r ri ltac:(lia) ri_correct as HH;
651 cbv [Z.equiv_modulo] in HH; rewrite HH; clear HH.
652 etransitivity; [rewrite (fun a => Z.mul_mod_l a ri N)|
653 rewrite (fun a => Z.mul_mod_l a ri N); reflexivity].
654 rewrite S2_mod_r_helper.
655 push_Zmod; pull_Zmod; autorewrite with zsimplify_const.
656 reflexivity.
657 Qed.
658
659 Lemma S3_mod_N
660 (Hbound : eval S < eval N + eval B)
661 : S3 mod N = (S + a*B)*ri mod N.
662 Proof using B_bounds R_numlimbs_nz lgr_big small_A small_B small_S k_correct ri_correct small_N N_lt_R N_nz S_nonneg.
663 clear -B_bounds R_numlimbs_nz lgr_big small_A small_B small_S r_big' partition_Proper k_correct ri_correct N_nz N_lt_R small_N sub_then_maybe_add Hbound S_nonneg.
664 rewrite eval_S3_eq.
665 pose proof (pre_S3_bound Hbound); pose proof pre_S3_nonneg.
666 rewrite (Z.mod_small _ (r * _)) by (subst R; nia).
667 apply pre_S3_mod_N.
668 Qed.
669 End Iteration_proofs.
670
671 Section redc_proofs.
672 Local Notation redc_body := (@redc_body B k).
673 Local Notation redc_loop := (@redc_loop B k).
674 Local Notation pre_redc A := (@pre_redc _ A B k).
675 Local Notation redc A := (@redc _ A B k).
676
677 Section body.
678 Context (pred_A_numlimbs : nat)
679 (A_S : T (S pred_A_numlimbs) * T (S R_numlimbs)).
680 Let A:=fst A_S.
681 Let S:=snd A_S.
682 Let A_a:=divmod A.
683 Let a:=snd A_a.
684 Context (small_A : small A)
685 (small_S : small S)
686 (S_bound : 0 <= eval S < eval N + eval B).
687
688 Lemma small_fst_redc_body : small (fst (redc_body A_S)).
689 Proof using S_bound small_A small_S lgr_big. destruct A_S; apply small_A'; assumption. Qed.
690 Lemma small_snd_redc_body : small (snd (redc_body A_S)).
691 Proof using small_S small_N small_B small_A lgr_big S_bound B_bounds N_nz N_lt_R.
692 destruct A_S; unfold redc_body; apply small_S3; assumption.
693 Qed.
694 Lemma snd_redc_body_nonneg : 0 <= eval (snd (redc_body A_S)).
695 Proof using small_S small_N small_B small_A lgr_big S_bound N_nz N_lt_R B_bounds.
696 destruct A_S; apply S3_nonneg; assumption.
697 Qed.
698
699 Lemma snd_redc_body_mod_N
700 : (eval (snd (redc_body A_S))) mod (eval N) = (eval S + a*eval B)*ri mod (eval N).
701 Proof using small_S small_N small_B small_A ri_correct lgr_big k_correct S_bound R_numlimbs_nz N_nz N_lt_R B_bounds.
702 clear -small_S small_N small_B small_A ri_correct k_correct S_bound R_numlimbs_nz N_nz N_lt_R B_bounds sub_then_maybe_add r_big' partition_Proper.
703 destruct A_S; apply S3_mod_N; auto; lia.
704 Qed.
705
706 Lemma fst_redc_body
707 : (eval (fst (redc_body A_S))) = eval (fst A_S) / r.
708 Proof using small_S small_A S_bound lgr_big.
709 destruct A_S; simpl; repeat autounfold with word_by_word_montgomery; simpl.
710 autorewrite with push_mont_eval.
711 reflexivity.
712 Qed.
713
714 Lemma fst_redc_body_mod_N
715 : (eval (fst (redc_body A_S))) mod (eval N) = ((eval (fst A_S) - a)*ri) mod (eval N).
716 Proof using small_S small_A ri_correct lgr_big S_bound.
717 clear R_numlimbs_nz.
718 rewrite fst_redc_body.
719 etransitivity; [ eapply Z.div_to_inv_modulo; try eassumption; lia | ].
720 unfold a, A_a, A.
721 autorewrite with push_mont_eval.
722 reflexivity.
723 Qed.
724
725 Lemma redc_body_bound
726 : eval S < eval N + eval B
727 -> eval (snd (redc_body A_S)) < eval N + eval B.
728 Proof using small_S small_N small_B small_A lgr_big S_bound N_nz N_lt_R B_bounds.
729 clear -small_S small_N small_B small_A S_bound N_nz N_lt_R B_bounds r_big' partition_Proper sub_then_maybe_add.
730 destruct A_S; apply S3_bound; unfold S in *; cbn [snd] in *; try assumption; try lia.
731 Qed.
732 End body.
733
734 Local Arguments Z.pow !_ !_.
735 Local Arguments Z.of_nat !_.
736 Local Ltac induction_loop count IHcount
737 := induction count as [|count IHcount]; intros; cbn [redc_loop nat_rect] in *; [ | (*rewrite redc_loop_comm_body in * *) ].
738 Lemma redc_loop_good count A_S
739 (Hsmall : small (fst A_S) /\ small (snd A_S))
740 (Hbound : 0 <= eval (snd A_S) < eval N + eval B)
741 : (small (fst (redc_loop count A_S)) /\ small (snd (redc_loop count A_S)))
742 /\ 0 <= eval (snd (redc_loop count A_S)) < eval N + eval B.
743 Proof using small_N small_B lgr_big N_nz N_lt_R B_bounds.
744 induction_loop count IHcount; auto; [].
745 change (id (0 <= eval B < R)) in B_bounds (* don't let [destruct_head'_and] loop *).
746 destruct_head'_and.
747 repeat first [ apply conj
748 | apply small_fst_redc_body
749 | apply small_snd_redc_body
750 | apply redc_body_bound
751 | apply snd_redc_body_nonneg
752 | apply IHcount
753 | solve [ auto ] ].
754 Qed.
755
756 Lemma small_redc_loop count A_S
757 (Hsmall : small (fst A_S) /\ small (snd A_S))
758 (Hbound : 0 <= eval (snd A_S) < eval N + eval B)
759 : small (fst (redc_loop count A_S)) /\ small (snd (redc_loop count A_S)).
760 Proof using small_N small_B lgr_big N_nz N_lt_R B_bounds. apply redc_loop_good; assumption. Qed.
761
762 Lemma redc_loop_bound count A_S
763 (Hsmall : small (fst A_S) /\ small (snd A_S))
764 (Hbound : 0 <= eval (snd A_S) < eval N + eval B)
765 : 0 <= eval (snd (redc_loop count A_S)) < eval N + eval B.
766 Proof using small_N small_B lgr_big N_nz N_lt_R B_bounds. apply redc_loop_good; assumption. Qed.
767
768 Local Ltac handle_IH_small :=
769 repeat first [ apply redc_loop_good
770 | apply small_fst_redc_body
771 | apply small_snd_redc_body
772 | apply redc_body_bound
773 | apply snd_redc_body_nonneg
774 | apply conj
775 | progress cbn [fst snd]
776 | progress destruct_head' and
777 | solve [ auto ] ].
778
779 Lemma fst_redc_loop count A_S
780 (Hsmall : small (fst A_S) /\ small (snd A_S))
781 (Hbound : 0 <= eval (snd A_S) < eval N + eval B)
782 : eval (fst (redc_loop count A_S)) = eval (fst A_S) / r^(Z.of_nat count).
783 Proof using small_N small_B lgr_big R_numlimbs_nz N_nz N_lt_R B_bounds.
784 clear -small_N small_B r_big' partition_Proper R_numlimbs_nz N_nz N_lt_R B_bounds sub_then_maybe_add Hsmall Hbound.
785 cbv [redc_loop]; induction_loop count IHcount.
786 { simpl; autorewrite with zsimplify; reflexivity. }
787 { rewrite IHcount, fst_redc_body by handle_IH_small.
788 change (1 + R_numlimbs)%nat with (S R_numlimbs) in *.
789 rewrite Zdiv_Zdiv by Z.zero_bounds.
790 rewrite <- (Z.pow_1_r r) at 1.
791 rewrite <- Z.pow_add_r by lia.
792 replace (1 + Z.of_nat count) with (Z.of_nat (S count)) by lia.
793 reflexivity. }
794 Qed.
795
796 Lemma fst_redc_loop_mod_N count A_S
797 (Hsmall : small (fst A_S) /\ small (snd A_S))
798 (Hbound : 0 <= eval (snd A_S) < eval N + eval B)
799 : eval (fst (redc_loop count A_S)) mod (eval N)
800 = (eval (fst A_S) - eval (fst A_S) mod r^Z.of_nat count)
801 * ri^(Z.of_nat count) mod (eval N).
802 Proof using small_N small_B lgr_big R_numlimbs_nz N_nz N_lt_R B_bounds ri_correct.
803 clear -small_N small_B r_big' partition_Proper R_numlimbs_nz N_nz N_lt_R B_bounds sub_then_maybe_add Hsmall Hbound ri_correct.
804 rewrite fst_redc_loop by assumption.
805 destruct count.
806 { simpl; autorewrite with zsimplify; reflexivity. }
807 { etransitivity;
808 [ eapply Z.div_to_inv_modulo;
809 try solve [ eassumption
810 | apply Z.lt_gt, Z.pow_pos_nonneg; lia ]
811 | ].
812 { erewrite <- Z.pow_mul_l, <- Z.pow_1_l.
813 { apply Z.pow_mod_Proper; [ eassumption | reflexivity ]. }
814 { lia. } }
815 reflexivity. }
816 Qed.
817
818 Local Arguments Z.pow : simpl never.
819 Lemma snd_redc_loop_mod_N count A_S
820 (Hsmall : small (fst A_S) /\ small (snd A_S))
821 (Hbound : 0 <= eval (snd A_S) < eval N + eval B)
822 : (eval (snd (redc_loop count A_S))) mod (eval N)
823 = ((eval (snd A_S) + (eval (fst A_S) mod r^(Z.of_nat count))*eval B)*ri^(Z.of_nat count)) mod (eval N).
824 Proof using small_N small_B ri_correct lgr_big R_numlimbs_nz N_nz N_lt_R B_bounds k_correct.
825 clear -small_N small_B ri_correct r_big' partition_Proper R_numlimbs_nz N_nz N_lt_R B_bounds sub_then_maybe_add k_correct Hsmall Hbound.
826 cbv [redc_loop].
827 induction_loop count IHcount.
828 { simpl; autorewrite with zsimplify; reflexivity. }
829 { rewrite IHcount by handle_IH_small.
830 push_Zmod; rewrite snd_redc_body_mod_N, fst_redc_body by handle_IH_small; pull_Zmod.
831 autorewrite with push_mont_eval; [].
832 match goal with
833 | [ |- ?x mod ?N = ?y mod ?N ]
834 => change (Z.equiv_modulo N x y)
835 end.
836 destruct A_S as [A S].
837 cbn [fst snd].
838 change (Z.pos (Pos.of_succ_nat ?n)) with (Z.of_nat (Datatypes.S n)).
839 rewrite !Z.mul_add_distr_r.
840 rewrite <- !Z.mul_assoc.
841 replace (ri * ri^(Z.of_nat count)) with (ri^(Z.of_nat (Datatypes.S count)))
842 by (change (Datatypes.S count) with (1 + count)%nat;
843 autorewrite with push_Zof_nat; rewrite Z.pow_add_r by lia; simpl Z.succ; rewrite Z.pow_1_r; nia).
844 rewrite <- !Z.add_assoc.
845 apply Z.add_mod_Proper; [ reflexivity | ].
846 unfold Z.equiv_modulo; push_Zmod; rewrite (Z.mul_mod_l (_ mod r) _ (eval N)).
847 rewrite Z.mod_pull_div by auto with zarith lia.
848 push_Zmod.
849 erewrite Z.div_to_inv_modulo;
850 [
851 | apply Z.lt_gt; lia
852 | eassumption ].
853 pull_Zmod.
854 match goal with
855 | [ |- ?x mod ?N = ?y mod ?N ]
856 => change (Z.equiv_modulo N x y)
857 end.
858 repeat first [ rewrite <- !Z.pow_succ_r, <- !Nat2Z.inj_succ by lia
859 | rewrite (Z.mul_comm _ ri)
860 | rewrite (Z.mul_assoc _ ri _)
861 | rewrite (Z.mul_comm _ (ri^_))
862 | rewrite (Z.mul_assoc _ (ri^_) _) ].
863 repeat first [ rewrite <- Z.mul_assoc
864 | rewrite <- Z.mul_add_distr_l
865 | rewrite (Z.mul_comm _ (eval B))
866 | rewrite !Nat2Z.inj_succ, !Z.pow_succ_r by lia;
867 rewrite <- Znumtheory.Zmod_div_mod by (apply Z.divide_factor_r || Z.zero_bounds)
868 | rewrite Zplus_minus
869 | rewrite (Z.mul_comm r (r^_))
870 | reflexivity ]. }
871 Qed.
872
873 Lemma pre_redc_bound A_numlimbs (A : T A_numlimbs)
874 (small_A : small A)
875 : 0 <= eval (pre_redc A) < eval N + eval B.
876 Proof using small_N small_B lgr_big N_nz N_lt_R B_bounds.
877 clear -small_N small_B r_big' partition_Proper lgr_big N_nz N_lt_R B_bounds sub_then_maybe_add small_A.
878 unfold pre_redc.
879 apply redc_loop_good; simpl; autorewrite with push_mont_eval;
880 rewrite ?Npos_correct; auto; lia.
881 Qed.
882
883 Lemma small_pre_redc A_numlimbs (A : T A_numlimbs)
884 (small_A : small A)
885 : small (pre_redc A).
886 Proof using small_N small_B lgr_big N_nz N_lt_R B_bounds.
887 clear -small_N small_B r_big' partition_Proper lgr_big N_nz N_lt_R B_bounds sub_then_maybe_add small_A.
888 unfold pre_redc.
889 apply redc_loop_good; simpl; autorewrite with push_mont_eval;
890 rewrite ?Npos_correct; auto; lia.
891 Qed.
892
893 Lemma pre_redc_mod_N A_numlimbs (A : T A_numlimbs) (small_A : small A) (A_bound : 0 <= eval A < r ^ Z.of_nat A_numlimbs)
894 : (eval (pre_redc A)) mod (eval N) = (eval A * eval B * ri^(Z.of_nat A_numlimbs)) mod (eval N).
895 Proof using small_N small_B lgr_big N_nz N_lt_R B_bounds R_numlimbs_nz ri_correct k_correct.
896 clear -small_N small_B r_big' partition_Proper lgr_big N_nz N_lt_R B_bounds R_numlimbs_nz ri_correct k_correct sub_then_maybe_add small_A A_bound.
897 unfold pre_redc.
898 rewrite snd_redc_loop_mod_N; cbn [fst snd];
899 autorewrite with push_mont_eval zsimplify;
900 [ | rewrite ?Npos_correct; auto; lia.. ].
901 Z.rewrite_mod_small.
902 reflexivity.
903 Qed.
904
905 Lemma redc_mod_N A_numlimbs (A : T A_numlimbs) (small_A : small A) (A_bound : 0 <= eval A < r ^ Z.of_nat A_numlimbs)
906 : (eval (redc A)) mod (eval N) = (eval A * eval B * ri^(Z.of_nat A_numlimbs)) mod (eval N).
907 Proof using small_N small_B ri_correct lgr_big k_correct R_numlimbs_nz N_nz N_lt_R B_bounds.
908 pose proof (@small_pre_redc _ A small_A).
909 pose proof (@pre_redc_bound _ A small_A).
910 unfold redc.
911 autorewrite with push_mont_eval; [].
912 break_innermost_match;
913 try rewrite Z.add_opp_r, Zminus_mod, Z_mod_same_full;
914 autorewrite with zsimplify_fast;
915 apply pre_redc_mod_N; auto.
916 Qed.
917
918 Lemma redc_bound_tight A_numlimbs (A : T A_numlimbs)
919 (small_A : small A)
920 : 0 <= eval (redc A) < eval N + eval B + (if eval N <=? eval (pre_redc A) then -eval N else 0).
921 Proof using small_N small_B lgr_big R_numlimbs_nz N_nz N_lt_R B_bounds.
922 clear -small_N small_B lgr_big R_numlimbs_nz N_nz N_lt_R B_bounds r_big' partition_Proper small_A sub_then_maybe_add.
923 pose proof (@small_pre_redc _ A small_A).
924 pose proof (@pre_redc_bound _ A small_A).
925 unfold redc.
926 rewrite eval_conditional_sub by t_small.
927 break_innermost_match; Z.ltb_to_lt; lia.
928 Qed.
929
930 Lemma redc_bound_N A_numlimbs (A : T A_numlimbs)
931 (small_A : small A)
932 : eval B < eval N -> 0 <= eval (redc A) < eval N.
933 Proof using small_N small_B lgr_big R_numlimbs_nz N_nz N_lt_R B_bounds.
934 clear -small_N small_B r_big' partition_Proper R_numlimbs_nz N_nz N_lt_R B_bounds small_A sub_then_maybe_add.
935 pose proof (@small_pre_redc _ A small_A).
936 pose proof (@pre_redc_bound _ A small_A).
937 unfold redc.
938 rewrite eval_conditional_sub by t_small.
939 break_innermost_match; Z.ltb_to_lt; lia.
940 Qed.
941
942 Lemma redc_bound A_numlimbs (A : T A_numlimbs)
943 (small_A : small A)
944 (A_bound : 0 <= eval A < r ^ Z.of_nat A_numlimbs)
945 : 0 <= eval (redc A) < R.
946 Proof using small_N small_B lgr_big R_numlimbs_nz N_nz N_lt_R B_bounds.
947 clear -small_N small_B r_big' partition_Proper R_numlimbs_nz N_nz N_lt_R B_bounds small_A sub_then_maybe_add A_bound.
948 pose proof (@small_pre_redc _ A small_A).
949 pose proof (@pre_redc_bound _ A small_A).
950 unfold redc.
951 rewrite eval_conditional_sub by t_small.
952 break_innermost_match; Z.ltb_to_lt; try lia.
953 Qed.
954
955 Lemma small_redc A_numlimbs (A : T A_numlimbs)
956 (small_A : small A)
957 (A_bound : 0 <= eval A < r ^ Z.of_nat A_numlimbs)
958 : small (redc A).
959 Proof using small_N small_B lgr_big R_numlimbs_nz N_nz N_lt_R B_bounds.
960 clear -small_N small_B r_big' partition_Proper R_numlimbs_nz N_nz N_lt_R B_bounds small_A sub_then_maybe_add A_bound.
961 pose proof (@small_pre_redc _ A small_A).
962 pose proof (@pre_redc_bound _ A small_A).
963 unfold redc.
964 apply small_conditional_sub; [ apply small_pre_redc | .. ]; auto; lia.
965 Qed.
966 End redc_proofs.
967
968 Section add_sub.
969 Context (Av Bv : T R_numlimbs)
970 (small_Av : small Av)
971 (small_Bv : small Bv)
972 (Av_bound : 0 <= eval Av < eval N)
973 (Bv_bound : 0 <= eval Bv < eval N).
974
975 Local Ltac do_clear :=
976 clear dependent B; clear dependent k; clear dependent ri.
977
978 Lemma small_add : small (add Av Bv).
979 Proof using small_Bv small_Av lgr_big N_lt_R Bv_bound Av_bound small_N ri k R_numlimbs_nz N_nz B_bounds B.
980 clear -small_Bv small_Av N_lt_R Bv_bound Av_bound partition_Proper r_big' small_N ri k R_numlimbs_nz N_nz B_bounds B sub_then_maybe_add.
981 unfold add; t_small.
982 Qed.
983 Lemma small_sub : small (sub Av Bv).
984 Proof using small_N small_Bv small_Av partition_Proper lgr_big R_numlimbs_nz N_nz N_lt_R Bv_bound Av_bound. unfold sub; t_small. Qed.
985 Lemma small_opp : small (opp Av).
986 Proof using small_N small_Bv small_Av partition_Proper lgr_big R_numlimbs_nz N_nz N_lt_R Av_bound. unfold opp, sub; t_small. Qed.
987
988 Lemma eval_add : eval (add Av Bv) = eval Av + eval Bv + (if (eval N <=? eval Av + eval Bv) then -eval N else 0).
989 Proof using small_Bv small_Av lgr_big N_lt_R Bv_bound Av_bound small_N ri k R_numlimbs_nz N_nz B_bounds B.
990 clear -small_Bv small_Av lgr_big N_lt_R Bv_bound Av_bound partition_Proper r_big' small_N ri k R_numlimbs_nz N_nz B_bounds B sub_then_maybe_add.
991 unfold add; autorewrite with push_mont_eval; reflexivity.
992 Qed.
993 Lemma eval_sub : eval (sub Av Bv) = eval Av - eval Bv + (if (eval Av - eval Bv <? 0) then eval N else 0).
994 Proof using small_Bv small_Av Bv_bound Av_bound small_N partition_Proper lgr_big R_numlimbs_nz N_nz N_lt_R. unfold sub; autorewrite with push_mont_eval; reflexivity. Qed.
995 Lemma eval_opp : eval (opp Av) = (if (eval Av =? 0) then 0 else eval N) - eval Av.
996 Proof using small_Av Av_bound small_N partition_Proper lgr_big R_numlimbs_nz N_nz N_lt_R.
997 clear -Av_bound N_nz small_Av partition_Proper r_big' small_N lgr_big R_numlimbs_nz N_nz N_lt_R.
998 unfold opp, sub; autorewrite with push_mont_eval.
999 break_innermost_match; Z.ltb_to_lt; lia.
1000 Qed.
1001
1002 Local Ltac t_mod_N :=
1003 repeat first [ progress break_innermost_match
1004 | reflexivity
1005 | let H := fresh in intro H; rewrite H; clear H
1006 | progress autorewrite with zsimplify_const
1007 | rewrite Z.add_opp_r
1008 | progress (push_Zmod; pull_Zmod) ].
1009
1010 Lemma eval_add_mod_N : eval (add Av Bv) mod eval N = (eval Av + eval Bv) mod eval N.
1011 Proof using small_Bv small_Av lgr_big N_lt_R Bv_bound Av_bound small_N ri k R_numlimbs_nz N_nz B_bounds B.
1012 generalize eval_add; clear. t_mod_N.
1013 Qed.
1014 Lemma eval_sub_mod_N : eval (sub Av Bv) mod eval N = (eval Av - eval Bv) mod eval N.
1015 Proof using small_Bv small_Av Bv_bound Av_bound small_N r_big' partition_Proper lgr_big R_numlimbs_nz N_nz N_lt_R. generalize eval_sub; clear. t_mod_N. Qed.
1016 Lemma eval_opp_mod_N : eval (opp Av) mod eval N = (-eval Av) mod eval N.
1017 Proof using small_Av Av_bound small_N r_big' partition_Proper lgr_big R_numlimbs_nz N_nz N_lt_R. generalize eval_opp; clear. t_mod_N. Qed.
1018
1019 Lemma add_bound : 0 <= eval (add Av Bv) < eval N.
1020 Proof using small_Bv small_Av lgr_big R_numlimbs_nz N_lt_R Bv_bound Av_bound small_N ri k N_nz B_bounds B.
1021 generalize eval_add; break_innermost_match; Z.ltb_to_lt; lia.
1022 Qed.
1023 Lemma sub_bound : 0 <= eval (sub Av Bv) < eval N.
1024 Proof using small_Bv small_Av R_numlimbs_nz Bv_bound Av_bound small_N r_big' partition_Proper lgr_big N_nz N_lt_R.
1025 clear dependent ri; clear dependent k.
1026 generalize eval_sub; break_innermost_match; Z.ltb_to_lt; lia.
1027 Qed.
1028 Lemma opp_bound : 0 <= eval (opp Av) < eval N.
1029 Proof using small_Av R_numlimbs_nz Av_bound small_N r_big' partition_Proper lgr_big N_nz N_lt_R.
1030 clear Bv small_Bv Bv_bound k k_correct ri ri_correct.
1031 generalize eval_opp; break_innermost_match; Z.ltb_to_lt; lia.
1032 Qed.
1033 End add_sub.
1034 End with_args.
1035
1036 Section modops.
1037 Context (bitwidth : Z)
1038 (n : nat)
1039 (m : Z).
1040 Let r := 2^bitwidth.
1041 Local Notation weight := (uweight bitwidth).
1042 Local Notation eval := (@eval bitwidth n).
1043 Let m_enc := Partition.partition weight n m.
1044 Local Coercion Z.of_nat : nat >-> Z.
1045 Context (r' : Z)
1046 (m' : Z)
1047 (r'_correct : (r * r') mod m = 1)
1048 (m'_correct : (m * m') mod r = (-1) mod r)
1049 (bitwidth_big : 0 < bitwidth)
1050 (m_big : 1 < m)
1051 (n_nz : n <> 0%nat)
1052 (m_small : m < r^n).
1053
1054 Local Notation wprops := (@uwprops bitwidth bitwidth_big).
1055 Local Notation small := (@small bitwidth n).
1056 Local Definition weight_0_wprops' := weight_0 wprops.
1057 Local Definition weight_positive_wprops' := weight_positive wprops.
1058 Local Definition weight_multiples_wprops' := weight_multiples wprops.
1059 Local Definition weight_divides_wprops' := weight_divides wprops.
1060
1061 Local Hint Extern 0 weight_properties => simple apply wprops : core.
1062 Local Hint Immediate weight_0_wprops' : core.
1063 Local Hint Immediate weight_positive_wprops' : core.
1064 Local Hint Immediate weight_multiples_wprops' : core.
1065 Local Hint Immediate weight_divides_wprops' : core.
1066
1067 Local Lemma r'_correct_alt : ((r mod m) * (r' mod m)) mod m = 1.
1068 Proof using r'_correct. pull_Zmod; apply r'_correct. Qed.
1069
1070 Local Lemma m_enc_correct_montgomery : m = eval m_enc.
1071 Proof using m_small m_big bitwidth_big.
1072 clear -m_small m_big bitwidth_big.
1073 cbv [eval m_enc]; autorewrite with push_eval; auto.
1074 rewrite uweight_eq_alt by lia.
1075 Z.rewrite_mod_small; reflexivity.
1076 Qed.
1077 Local Lemma r'_pow_correct : (r'^n * r^n) mod (eval m_enc) = 1.
1078 Proof using r'_correct m_small m_big bitwidth_big.
1079 clear -r'_correct m_small m_big bitwidth_big.
1080 rewrite <- Z.pow_mul_l, Z.mod_pow_full, ?(Z.mul_comm r'), <- m_enc_correct_montgomery, r'_correct.
1081 autorewrite with zsimplify_const; auto with lia.
1082 Z.rewrite_mod_small; lia.
1083 Qed.
1084 Local Lemma small_m_enc : small m_enc.
1085 Proof using m_small m_big bitwidth_big.
1086 clear -m_small m_big bitwidth_big.
1087 cbv [m_enc small eval]; autorewrite with push_eval; auto.
1088 rewrite uweight_eq_alt by lia.
1089 Z.rewrite_mod_small; reflexivity.
1090 Qed.
1091
1092 Local Ltac t_fin :=
1093 repeat match goal with
1094 | _ => assumption
1095 | [ |- ?x = ?x ] => reflexivity
1096 | [ |- and _ _ ] => split
1097 | _ => rewrite <- !m_enc_correct_montgomery
1098 | _ => rewrite !r'_correct
1099 | _ => rewrite !Z.mod_1_l by assumption; reflexivity
1100 | _ => rewrite !(Z.mul_comm m' m)
1101 | _ => lia
1102 | _ => exact small_m_enc
1103 | [ H : small ?x |- context[eval ?x] ]
1104 => rewrite H; cbv [eval]; rewrite eval_partition by auto
1105 | [ |- context[weight _] ] => rewrite uweight_eq_alt by auto with lia
1106 | _=> progress Z.rewrite_mod_small
1107 | _ => progress Z.zero_bounds
1108 | [ |- _ mod ?x < ?x ] => apply Z.mod_pos_bound
1109 end.
1110
1111 Definition mulmod (a b : list Z) : list Z := @redc bitwidth n m_enc n a b m'.
1112 Definition redc_step (b s : list Z) (a : Z) : list Z := @redc_body_alt bitwidth n m_enc b m' (a, s).
1113 Definition squaremod (a : list Z) : list Z := mulmod a a.
1114 Definition addmod (a b : list Z) : list Z := @add bitwidth n m_enc a b.
1115 Definition submod (a b : list Z) : list Z := @sub bitwidth n m_enc a b.
1116 Definition oppmod (a : list Z) : list Z := @opp bitwidth n m_enc a.
1117 Definition nonzeromod (a : list Z) : Z := @nonzero a.
1118 Definition to_bytesmod (a : list Z) : list Z := @to_bytesmod bitwidth 1 (2^Z.log2_up m) n a.
1119
1120 Definition valid (a : list Z) := small a /\ 0 <= eval a < m.
1121
1122 Lemma mulmod_correct0
1123 : forall a b : list Z,
1124 small a -> small b
1125 -> small (mulmod a b)
1126 /\ (eval b < m -> 0 <= eval (mulmod a b) < m)
1127 /\ (eval (mulmod a b) mod m = (eval a * eval b * r'^n) mod m).
1128 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1129 intros a b Ha Hb; repeat apply conj; cbv [small mulmod eval];
1130 [ eapply small_redc
1131 | rewrite m_enc_correct_montgomery; eapply redc_bound_N
1132 | rewrite !m_enc_correct_montgomery; eapply redc_mod_N ];
1133 t_fin.
1134 Qed.
1135
1136 Definition onemod : list Z := Partition.partition weight n 1.
1137
1138 Definition onemod_correct : eval onemod = 1 /\ valid onemod.
1139 Proof using n_nz m_big bitwidth_big.
1140 clear -n_nz m_big bitwidth_big.
1141 cbv [valid small onemod eval]; autorewrite with push_eval; t_fin.
1142 Qed.
1143
1144 Lemma eval_onemod : eval onemod = 1.
1145 Proof. apply onemod_correct. Qed.
1146
1147 Definition R2mod : list Z := Partition.partition weight n ((r^n * r^n) mod m).
1148
1149 Definition R2mod_correct : eval R2mod mod m = (r^n*r^n) mod m /\ valid R2mod.
1150 Proof using n_nz m_small m_big m'_correct bitwidth_big.
1151 clear -n_nz m_small m_big m'_correct bitwidth_big.
1152 cbv [valid small R2mod eval]; autorewrite with push_eval; t_fin;
1153 rewrite !(Z.mod_small (_ mod m)) by (Z.div_mod_to_quot_rem; subst r; lia);
1154 t_fin.
1155 Qed.
1156
1157 Lemma eval_R2mod : eval R2mod mod m = (r^n*r^n) mod m.
1158 Proof using n_nz m_small m_big m'_correct bitwidth_big. apply R2mod_correct. Qed.
1159
1160 Definition from_montgomerymod (v : list Z) : list Z
1161 := mulmod v onemod.
1162
1163 Lemma from_montgomerymod_correct (v : list Z)
1164 : valid v -> eval (from_montgomerymod v) mod m = (eval v * r'^n) mod m
1165 /\ valid (from_montgomerymod v).
1166 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1167 clear -r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1168 intro Hv; cbv [from_montgomerymod valid] in *; destruct_head'_and.
1169 replace (eval v * r'^n) with (eval v * eval onemod * r'^n) by (rewrite (proj1 onemod_correct); lia).
1170 repeat apply conj; apply mulmod_correct0; auto; try apply onemod_correct; rewrite (proj1 onemod_correct); lia.
1171 Qed.
1172
1173 Lemma eval_from_montgomerymod (v : list Z) : valid v -> eval (from_montgomerymod v) mod m = (eval v * r'^n) mod m.
1174 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1175 intros; apply from_montgomerymod_correct; assumption.
1176 Qed.
1177 Lemma valid_from_montgomerymod (v : list Z)
1178 : valid v -> valid (from_montgomerymod v).
1179 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1180 intros; apply from_montgomerymod_correct; assumption.
1181 Qed.
1182
1183 Lemma mulmod_correct
1184 : (forall a (_ : valid a) b (_ : valid b), eval (from_montgomerymod (mulmod a b)) mod m
1185 = (eval (from_montgomerymod a) * eval (from_montgomerymod b)) mod m)
1186 /\ (forall a (_ : valid a) b (_ : valid b), valid (mulmod a b)).
1187 Proof using r'_correct r' n_nz m_small m_big m'_correct bitwidth_big.
1188 repeat apply conj; intros;
1189 push_Zmod; rewrite ?eval_from_montgomerymod; pull_Zmod; repeat apply conj;
1190 try apply mulmod_correct0; cbv [valid] in *; destruct_head'_and; auto; [].
1191 rewrite !Z.mul_assoc.
1192 apply Z.mul_mod_Proper; [ | reflexivity ].
1193 cbv [Z.equiv_modulo]; etransitivity; [ apply mulmod_correct0 | apply f_equal2; lia ]; auto.
1194 Qed.
1195
1196 Lemma eval_mulmod
1197 : (forall a (_ : valid a) b (_ : valid b),
1198 eval (from_montgomerymod (mulmod a b)) mod m
1199 = (eval (from_montgomerymod a) * eval (from_montgomerymod b)) mod m).
1200 Proof. apply mulmod_correct. Qed.
1201
1202 Lemma squaremod_correct
1203 : (forall a (_ : valid a), eval (from_montgomerymod (squaremod a)) mod m
1204 = (eval (from_montgomerymod a) * eval (from_montgomerymod a)) mod m)
1205 /\ (forall a (_ : valid a), valid (squaremod a)).
1206 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1207 split; intros; cbv [squaremod]; apply mulmod_correct; assumption.
1208 Qed.
1209
1210 Lemma eval_squaremod
1211 : (forall a (_ : valid a),
1212 eval (from_montgomerymod (squaremod a)) mod m
1213 = (eval (from_montgomerymod a) * eval (from_montgomerymod a)) mod m).
1214 Proof. apply squaremod_correct. Qed.
1215
1216 Local Ltac t_valid_side :=
1217 repeat first [ solve [ auto ]
1218 | apply R2mod_correct
1219 | apply mulmod_correct ].
1220
1221 Definition to_montgomerymod (v : list Z) : list Z
1222 := mulmod v R2mod.
1223
1224 Lemma to_montgomerymod_correct
1225 : (forall v (_ : valid v),
1226 eval (from_montgomerymod (to_montgomerymod v)) mod m
1227 = eval v mod m)
1228 /\ (forall v (_ : valid v), valid (to_montgomerymod v)).
1229 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1230 split; intros v ?; cbv [to_montgomerymod]; [ | t_valid_side ].
1231 repeat first [ reflexivity
1232 | rewrite !eval_mulmod by t_valid_side
1233 | rewrite !eval_from_montgomerymod by t_valid_side
1234 | rewrite !eval_R2mod by t_valid_side
1235 | rewrite r'_correct_alt
1236 | rewrite Z.mul_1_r
1237 | rewrite Z.mod_mod by lia
1238 | rewrite (Z.mul_comm (r' mod _) (r mod _))
1239 | progress push_Zmod
1240 | progress (pull_Zmod; rewrite Z.mul_1_r; push_Zmod)
1241 | progress (pull_Zmod; rewrite Z.pow_1_l by lia; push_Zmod)
1242 | progress (pull_Zmod; rewrite <- !Z.mul_assoc, <- !Z.pow_mul_l; push_Zmod) ].
1243 Qed.
1244
1245 Lemma eval_to_montgomerymod
1246 : forall v (_ : valid v),
1247 eval (from_montgomerymod (to_montgomerymod v)) mod m
1248 = eval v mod m.
1249 Proof. apply to_montgomerymod_correct. Qed.
1250
1251 Definition encodemod (v : Z) : list Z
1252 := to_montgomerymod (Partition.partition weight n v).
1253
1254 Local Ltac t_valid v :=
1255 cbv [valid]; repeat apply conj;
1256 auto; cbv [small eval]; autorewrite with push_eval; auto;
1257 rewrite ?uweight_eq_alt by lia;
1258 Z.rewrite_mod_small;
1259 rewrite ?(Z.mod_small (_ mod m)) by (subst r; Z.div_mod_to_quot_rem; lia);
1260 rewrite ?(Z.mod_small v) by (subst r; Z.div_mod_to_quot_rem; lia);
1261 try apply Z.mod_pos_bound; subst r; try lia; try reflexivity.
1262 Lemma encodemod_correct
1263 : (forall v, 0 <= v < m -> eval (from_montgomerymod (encodemod v)) mod m = v mod m)
1264 /\ (forall v, 0 <= v < m -> valid (encodemod v)).
1265 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1266 split; intros v ?; cbv [encodemod];
1267 [ rewrite eval_to_montgomerymod | apply to_montgomerymod_correct ];
1268 [ | now t_valid v.. ].
1269 cbv [eval]; autorewrite with push_eval; auto.
1270 rewrite ?uweight_eq_alt by lia.
1271 rewrite ?(Z.mod_small v) by (subst r; Z.div_mod_to_quot_rem; lia).
1272 reflexivity.
1273 Qed.
1274
1275 Lemma eval_encodemod
1276 : (forall v, 0 <= v < m
1277 -> eval (from_montgomerymod (encodemod v)) mod m = v mod m).
1278 Proof. apply encodemod_correct. Qed.
1279
1280 Lemma addmod_correct
1281 : (forall a (_ : valid a) b (_ : valid b), eval (from_montgomerymod (addmod a b)) mod m
1282 = (eval (from_montgomerymod a) + eval (from_montgomerymod b)) mod m)
1283 /\ (forall a (_ : valid a) b (_ : valid b), valid (addmod a b)).
1284 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1285 repeat apply conj; intros;
1286 push_Zmod; rewrite ?eval_from_montgomerymod; pull_Zmod; repeat apply conj;
1287 cbv [valid addmod] in *; destruct_head'_and; auto;
1288 try rewrite m_enc_correct_montgomery;
1289 try (eapply small_add || eapply add_bound);
1290 cbv [small]; rewrite <- ?m_enc_correct_montgomery;
1291 eauto with lia; [ ].
1292 push_Zmod; erewrite eval_add by (cbv [small]; rewrite <- ?m_enc_correct_montgomery; eauto with lia); pull_Zmod; rewrite <- ?m_enc_correct_montgomery.
1293 break_innermost_match; push_Zmod; pull_Zmod; autorewrite with zsimplify_const; apply f_equal2; nia.
1294 Qed.
1295
1296 Lemma eval_addmod
1297 : (forall a (_ : valid a) b (_ : valid b),
1298 eval (from_montgomerymod (addmod a b)) mod m
1299 = (eval (from_montgomerymod a) + eval (from_montgomerymod b)) mod m).
1300 Proof. apply addmod_correct. Qed.
1301
1302 Lemma submod_correct
1303 : (forall a (_ : valid a) b (_ : valid b), eval (from_montgomerymod (submod a b)) mod m
1304 = (eval (from_montgomerymod a) - eval (from_montgomerymod b)) mod m)
1305 /\ (forall a (_ : valid a) b (_ : valid b), valid (submod a b)).
1306 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1307 repeat apply conj; intros;
1308 push_Zmod; rewrite ?eval_from_montgomerymod; pull_Zmod; repeat apply conj;
1309 cbv [valid submod] in *; destruct_head'_and; auto;
1310 try rewrite m_enc_correct_montgomery;
1311 try (eapply small_sub || eapply sub_bound);
1312 cbv [small]; rewrite <- ?m_enc_correct_montgomery;
1313 eauto with lia; [ ].
1314 push_Zmod; erewrite eval_sub by (cbv [small]; rewrite <- ?m_enc_correct_montgomery; eauto with lia); pull_Zmod; rewrite <- ?m_enc_correct_montgomery.
1315 break_innermost_match; push_Zmod; pull_Zmod; autorewrite with zsimplify_const; apply f_equal2; nia.
1316 Qed.
1317
1318 Lemma eval_submod
1319 : (forall a (_ : valid a) b (_ : valid b),
1320 eval (from_montgomerymod (submod a b)) mod m
1321 = (eval (from_montgomerymod a) - eval (from_montgomerymod b)) mod m).
1322 Proof. apply submod_correct. Qed.
1323
1324 Lemma oppmod_correct
1325 : (forall a (_ : valid a), eval (from_montgomerymod (oppmod a)) mod m
1326 = (-eval (from_montgomerymod a)) mod m)
1327 /\ (forall a (_ : valid a), valid (oppmod a)).
1328 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1329 repeat apply conj; intros;
1330 push_Zmod; rewrite ?eval_from_montgomerymod; pull_Zmod; repeat apply conj;
1331 cbv [valid oppmod] in *; destruct_head'_and; auto;
1332 try rewrite m_enc_correct_montgomery;
1333 try (eapply small_opp || eapply opp_bound);
1334 cbv [small]; rewrite <- ?m_enc_correct_montgomery;
1335 eauto with lia; [ ].
1336 push_Zmod; erewrite eval_opp by (cbv [small]; rewrite <- ?m_enc_correct_montgomery; eauto with lia); pull_Zmod; rewrite <- ?m_enc_correct_montgomery.
1337 break_innermost_match; push_Zmod; pull_Zmod; autorewrite with zsimplify_const; apply f_equal2; nia.
1338 Qed.
1339
1340 Lemma eval_oppmod
1341 : (forall a (_ : valid a),
1342 eval (from_montgomerymod (oppmod a)) mod m
1343 = (-eval (from_montgomerymod a)) mod m).
1344 Proof. apply oppmod_correct. Qed.
1345
1346 Lemma nonzeromod_correct
1347 : (forall a (_ : valid a), (nonzeromod a = 0) <-> ((eval (from_montgomerymod a)) mod m = 0)).
1348 Proof using r'_correct n_nz m_small m_big m'_correct bitwidth_big.
1349 intros a Ha; rewrite eval_from_montgomerymod by assumption.
1350 cbv [nonzeromod valid] in *; destruct_head'_and.
1351 rewrite eval_nonzero; try eassumption; [ | subst r; apply conj; try eassumption; lia.. ].
1352 split; intro H'; [ rewrite H'; autorewrite with zsimplify_const; reflexivity | ].
1353 assert (H'' : ((eval a * r'^n) * r^n) mod m = 0)
1354 by (revert H'; push_Zmod; intro H'; rewrite H'; autorewrite with zsimplify_const; reflexivity).
1355 rewrite <- Z.mul_assoc in H''.
1356 autorewrite with pull_Zpow push_Zmod in H''.
1357 rewrite (Z.mul_comm r' r), r'_correct in H''.
1358 autorewrite with zsimplify_const pull_Zmod in H''; [ | lia.. ].
1359 clear H'.
1360 generalize dependent (eval a); clear.
1361 intros z ???.
1362 assert (z / m = 0) by (Z.div_mod_to_quot_rem; nia).
1363 Z.div_mod_to_quot_rem; nia.
1364 Qed.
1365
1366 Lemma to_bytesmod_correct
1367 : (forall a (_ : valid a), Positional.eval (uweight 8) (bytes_n (2^Z.log2_up m)) (to_bytesmod a)
1368 = eval a mod m)
1369 /\ (forall a (_ : valid a), to_bytesmod a = Partition.partition (uweight 8) (bytes_n (2^Z.log2_up m)) (eval a mod m)).
1370 Proof using m_big n_nz m_small bitwidth_big.
1371 clear -m_big n_nz m_small bitwidth_big.
1372 pose proof (Z.log2_up_le_full m).
1373 generalize (@length_small bitwidth n);
1374 cbv [valid small to_bytesmod eval]; split; intros; (etransitivity; [ apply eval_to_bytesmod | ]);
1375 fold weight in *; fold (uweight 8) in *; subst r;
1376 try solve [ intuition eauto with lia ].
1377 all: repeat first [ rewrite uweight_eq_alt by lia
1378 | lia
1379 | reflexivity
1380 | progress Z.rewrite_mod_small ].
1381 Qed.
1382
1383 Lemma eval_to_bytesmod
1384 : (forall a (_ : valid a),
1385 Positional.eval (uweight 8) (bytes_n (2^Z.log2_up m)) (to_bytesmod a)
1386 = eval a mod m).
1387 Proof. apply to_bytesmod_correct. Qed.
1388 End modops.
1389 End WordByWordMontgomery.