Debian Codegraph @

src/Arithmetic/FancyMontgomeryReduction.v

raw
1 From Coq Require Import ZArith Lia.
2 Require Import Crypto.Algebra.Nsatz.
3 From Coq Require Import Derive.
4 From Coq Require Import List.
5 Require Import Crypto.Arithmetic.BaseConversion.
6 Require Import Crypto.Arithmetic.Core.
7 Require Import Crypto.Arithmetic.ModOps.
8 Require Import Crypto.Arithmetic.Partition.
9 Require Import Crypto.Arithmetic.Saturated.
10 Require Import Crypto.Arithmetic.UniformWeight.
11 Require Import Crypto.Util.Decidable.
12 Require Import Crypto.Util.LetIn.
13 Require Import Crypto.Util.ListUtil.
14 Require Import Crypto.Arithmetic.MontgomeryReduction.Definition.
15 Require Import Crypto.Arithmetic.MontgomeryReduction.Proofs.
16 Require Import Crypto.Util.Tactics.UniquePose.
17 Require Import Crypto.Util.ZUtil.Definitions.
18 Require Import Crypto.Util.ZUtil.Zselect Crypto.Util.ZUtil.AddModulo.
19 Require Import Crypto.Util.ZUtil.Modulo Crypto.Util.ZUtil.Div.
20 Require Import Crypto.Util.ZUtil.Hints.Core.
21 Require Import Crypto.Util.ZUtil.Modulo.PullPush.
22 Require Import Crypto.Util.ZUtil.EquivModulo.
23 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
24 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
25 Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall.
26 Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds.
27
28 Require Import Crypto.Util.Notations.
29 Import ListNotations. Local Open Scope Z_scope.
30
31 Module MontgomeryReduction.
32 Local Coercion Z.of_nat : nat >-> Z.
33 Section MontRed'.
34 Context (N R N' R' : Z).
35 Context (HN_range : 0 <= N < R) (HN'_range : 0 <= N' < R) (HN_nz : N <> 0) (R_gt_1 : R > 1)
36 (N'_good : Z.equiv_modulo R (N*N') (-1)) (R'_good: Z.equiv_modulo N (R*R') 1).
37
38 Context (Zlog2R : Z) .
39 Let w : nat -> Z := uweight Zlog2R.
40 Context (n:nat) (Hn_nz: n <> 0%nat) (n_good : Zlog2R mod Z.of_nat n = 0).
41 Context (R_big_enough : 2 <= Zlog2R)
42 (R_two_pow : 2^Zlog2R = R).
43 Let w_mul : nat -> Z := uweight (Zlog2R / n).
44
45 Definition montred' (lo hi : Z) :=
46 dlet_nd y := nth_default 0 (BaseConversion.widemul_inlined Zlog2R 1 2 [lo] [N']) 0 in
47 dlet_nd t1_t2 := (BaseConversion.widemul_inlined_reverse Zlog2R 1 2 [N] [y]) in
48 dlet_nd sum_carry := Rows.add w 2 [lo;hi] t1_t2 in
49 dlet_nd y' := Z.zselect (snd sum_carry) 0 N in
50 dlet_nd lo''_carry := Z.sub_get_borrow_full R (nth_default 0 (fst sum_carry) 1) y' in
51 Z.add_modulo (fst lo''_carry) 0 N.
52
53 Local Lemma Hw : forall i, w i = R ^ Z.of_nat i.
54 Proof.
55 clear -R_big_enough R_two_pow; cbv [w uweight weight]; intro.
56 autorewrite with zsimplify.
57 rewrite Z.pow_mul_r, R_two_pow by lia; reflexivity.
58 Qed.
59
60 Declare Equivalent Keys weight w.
61 Local Ltac change_weight := rewrite !Hw, ?Z.pow_0_r, ?Z.pow_1_r, ?Z.pow_2_r, ?Z.pow_1_l in *.
62 Local Ltac solve_range :=
63 repeat match goal with
64 | _ => progress change_weight
65 | |- context [?a mod ?b] => unique pose proof (Z.mod_pos_bound a b ltac:(lia))
66 | |- 0 <= _ => progress Z.zero_bounds
67 | |- 0 <= _ * _ < _ * _ =>
68 split; [ solve [Z.zero_bounds] | apply Z.mul_lt_mono_nonneg; lia ]
69 | _ => solve [auto]
70 | _ => lia
71 end.
72
73 Local Lemma eval2 x y : Positional.eval w 2 [x;y] = x + R * y.
74 Proof. cbn. change_weight. ring. Qed.
75 Local Lemma eval1 x : Positional.eval w 1 [x] = x.
76 Proof. cbn. change_weight. ring. Qed.
77
78 Hint Rewrite BaseConversion.widemul_inlined_reverse_correct BaseConversion.widemul_inlined_correct
79 using (autorewrite with widemul push_nth_default; solve [solve_range]) : widemul.
80
81 (* TODO: move *)
82 Hint Rewrite Nat.mul_1_l : natsimplify.
83
84 Lemma montred'_eq lo hi T (HT_range: 0 <= T < R * N)
85 (Hlo: lo = T mod R) (Hhi: hi = T / R):
86 montred' lo hi = reduce_via_partial N R N' T.
87 Proof.
88 rewrite <-reduce_via_partial_alt_eq by nia.
89 cbv [montred' partial_reduce_alt reduce_via_partial_alt prereduce Let_In].
90 rewrite Hlo, Hhi.
91 assert (0 <= (T mod R) * N' < w 2) by (solve_range).
92 autorewrite with widemul.
93 rewrite Rows.add_partitions, Rows.add_div by (distr_length; apply wprops; lia).
94 (* rewrite R_two_pow. *)
95 cbv [Partition.partition seq].
96 repeat match goal with
97 | _ => progress rewrite ?eval1, ?eval2
98 | _ => progress rewrite ?Z.zselect_correct, ?Z.add_modulo_correct
99 | _ => progress autorewrite with natsimplify push_nth_default push_map to_div_mod
100 end.
101 change_weight.
102
103 (* pull out value before last modular reduction *)
104 match goal with |- (if (?n <=? ?x)%Z then ?x - ?n else ?x) = (if (?n <=? ?y) then ?y - ?n else ?y)%Z =>
105 let P := fresh "H" in assert (x = y) as P; [|rewrite P; reflexivity] end.
106
107 autorewrite with zsimplify.
108 Z.rewrite_mod_small.
109 autorewrite with zsimplify.
110 rewrite (Z.mul_comm (((T mod R) * N') mod R) N) in *.
111 match goal with
112 |- context [(?x - (if dec (?a / ?b = 0) then 0 else ?y)) mod ?m
113 = if (?b <=? ?a) then (?x - ?y) mod ?m else ?x ] =>
114 assert (a / b = 0 <-> a < b) by
115 (rewrite Z.div_between_0_if by (Z.div_mod_to_quot_rem; nia);
116 break_match; Z.ltb_to_lt; lia)
117 end.
118 break_match; Z.ltb_to_lt; try reflexivity; try lia; [ ].
119 autorewrite with zsimplify_fast. Z.rewrite_mod_small. reflexivity.
120 Qed.
121
122 Lemma montred'_correct lo hi T (HT_range: 0 <= T < R * N)
123 (Hlo: lo = T mod R) (Hhi: hi = T / R): montred' lo hi = (T * R') mod N.
124 Proof.
125 erewrite montred'_eq by eauto.
126 apply Z.equiv_modulo_mod_small; auto using reduce_via_partial_correct.
127 replace 0 with (Z.min 0 (R-N)) by (apply Z.min_l; lia).
128 apply reduce_via_partial_in_range; lia.
129 Qed.
130 End MontRed'.
131 End MontgomeryReduction.