Debian Codegraph @

src/Arithmetic/BarrettReduction/Generalized.v

raw
1 (*** Barrett Reduction *)
2 (** This file implements a slightly-generalized version of Barrett
3 Reduction on [Z]. This version follows a middle path between the
4 Handbook of Applied Cryptography (Algorithm 14.42) and Wikipedia.
5 We split up the shifting and the multiplication so that we don't
6 need to store numbers that are quite so large, but we don't do
7 early reduction modulo [b^(k+offset)] (we generalize from HAC's [k
8 ± 1] to [k ± offset]). This leads to weaker conditions on the
9 base ([b]), exponent ([k]), and the [offset] than those given in
10 the HAC. *)
11 From Coq Require Import ZArith Lia.
12 Require Import Crypto.Util.ZUtil.Div.
13 Require Import Crypto.Util.ZUtil.Modulo.
14 Require Import Crypto.Util.ZUtil.Pow.
15 Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds.
16 Require Import Crypto.Util.ZUtil.Tactics.SimplifyFractionsLe.
17 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
18 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
19 Require Import Crypto.Util.ZUtil.ZSimplify.
20 Require Import Crypto.Util.Tactics.BreakMatch.
21
22 Local Open Scope Z_scope.
23
24 Section barrett.
25 Context (n a : Z)
26 (n_reasonable : n <> 0).
27 (** Quoting Wikipedia <https://en.wikipedia.org/wiki/Barrett_reduction>: *)
28 (** In modular arithmetic, Barrett reduction is a reduction
29 algorithm introduced in 1986 by P.D. Barrett. A naive way of
30 computing *)
31 (** [c = a mod n] *)
32 (** would be to use a fast division algorithm. Barrett reduction is
33 an algorithm designed to optimize this operation assuming [n] is
34 constant, and [a < n²], replacing divisions by
35 multiplications. *)
36
37 (** * General idea *)
38 Section general_idea.
39 (** Let [m = 1 / n] be the inverse of [n] as a floating point
40 number. Then *)
41 (** [a mod n = a - ⌊a m⌋ n] *)
42 (** where [⌊ x ⌋] denotes the floor function. The result is exact,
43 as long as [m] is computed with sufficient accuracy. *)
44
45 (* [/] is [Z.div], which means truncated division *)
46 Local Notation "⌊am⌋" := (a / n) (only parsing).
47
48 Theorem naive_barrett_reduction_correct
49 : a mod n = a - ⌊am⌋ * n.
50 Proof using n_reasonable.
51 apply Zmod_eq_full; assumption.
52 Qed.
53 End general_idea.
54
55 (** * Barrett algorithm *)
56 Section barrett_algorithm.
57 (** Barrett algorithm is a fixed-point analog which expresses
58 everything in terms of integers. Let [k] be the smallest
59 integer such that [2ᵏ > n]. Think of [n] as representing the
60 fixed-point number [n 2⁻ᵏ]. We precompute [m] such that [m =
61 ⌊4ᵏ / n⌋]. Then [m] represents the fixed-point number
62 [m 2⁻ᵏ ≈ (n 2⁻ᵏ)⁻¹]. *)
63 (** N.B. We don't need [k] to be the smallest such integer. *)
64 (** N.B. We generalize to an arbitrary base. *)
65 (** N.B. We generalize from [k ± 1] to [k ± offset]. *)
66 Context (b : Z)
67 (base_good : 0 < b)
68 (k : Z)
69 (k_good : n < b ^ k)
70 (m : Z)
71 (m_good : m = b^(2*k) / n) (* [/] is [Z.div], which is truncated *)
72 (offset : Z)
73 (offset_nonneg : 0 <= offset).
74 (** Wikipedia neglects to mention non-negativity, but we need it.
75 It might be possible to do with a relaxed assumption, such as
76 the sign of [a] and the sign of [n] being the same; but I
77 figured it wasn't worth it. *)
78 Context (n_pos : 0 < n) (* or just [0 <= n], since we have [n <> 0] above *)
79 (a_nonneg : 0 <= a).
80
81 Context (k_big_enough : offset <= k)
82 (a_small : a < b^(2*k))
83 (** We also need that [n] is large enough; [n] larger than
84 [bᵏ⁻¹] works, but we ask for something more precise. *)
85 (n_large : a mod b^(k-offset) <= n).
86
87 (** Now *)
88
89 Let q := (m * (a / b^(k-offset))) / b^(k+offset).
90 Let r := a - q * n.
91 (** Because of the floor function (in Coq, because [/] means
92 truncated division), [q] is an integer and [r ≡ a mod n]. *)
93 Theorem barrett_reduction_equivalent
94 : r mod n = a mod n.
95 Proof using m_good offset.
96 subst r q m.
97 rewrite <- !Z.add_opp_r, !Zopp_mult_distr_l, !Z_mod_plus_full by assumption.
98 reflexivity.
99 Qed.
100
101 Lemma qn_small
102 : q * n <= a.
103 Proof using a_nonneg a_small base_good k_big_enough m_good n_pos n_reasonable offset_nonneg.
104 subst q r m.
105 assert (0 < b^(k-offset)). Z.zero_bounds.
106 assert (0 < b^(k+offset)) by Z.zero_bounds.
107 assert (0 < b^(2 * k)) by Z.zero_bounds.
108 Z.simplify_fractions_le.
109 autorewrite with pull_Zpow pull_Zdiv zsimplify; reflexivity.
110 Qed.
111
112 Lemma q_nice : { b : bool * bool | q = a / n + (if fst b then -1 else 0) + (if snd b then -1 else 0) }.
113 Proof using a_nonneg a_small base_good k_big_enough m_good n_large n_pos n_reasonable offset_nonneg.
114 assert (0 < b^(k+offset)) by Z.zero_bounds.
115 assert (0 < b^(k-offset)) by Z.zero_bounds.
116 assert (a / b^(k-offset) <= b^(2*k) / b^(k-offset)) by auto with zarith lia.
117 assert (a / b^(k-offset) <= b^(k+offset)) by (autorewrite with pull_Zpow zsimplify in *; assumption).
118 subst q r m.
119 rewrite (Z.div_mul_diff_exact''' (b^(2*k)) n (a/b^(k-offset))) by auto with lia zero_bounds.
120 rewrite (Z_div_mod_eq_full (b^(2*k) * _ / n) (b^(k+offset))).
121 autorewrite with push_Zmul push_Zopp zsimplify zstrip_div zdiv_to_mod.
122 rewrite Z.div_sub_mod_cond, !Z.div_sub_small by auto with zero_bounds zarith.
123 eexists (_, _); reflexivity.
124 Qed.
125
126 Lemma r_small : r < 3 * n.
127 Proof using a_nonneg a_small base_good k_big_enough m_good n_large n_pos n_reasonable offset_nonneg q.
128 Hint Rewrite (Z.mul_div_eq' a n) using lia : zstrip_div.
129 assert (a mod n < n) by auto with zarith lia.
130 unfold r; rewrite (proj2_sig q_nice); generalize (proj1_sig q_nice); intro; subst q m.
131 autorewrite with push_Zmul zsimplify zstrip_div.
132 break_match; auto with lia.
133 Qed.
134
135 (** In that case, we have *)
136 Theorem barrett_reduction_small
137 : a mod n = let r := if r <? n then r else r-n in
138 let r := if r <? n then r else r-n in
139 r.
140 Proof using a_nonneg a_small base_good k_big_enough m_good n_large n_pos n_reasonable offset_nonneg q.
141 pose proof r_small. pose proof qn_small. cbv zeta.
142 destruct (r <? n) eqn:Hr, (r-n <? n) eqn:?; try rewrite Hr; Z.ltb_to_lt; try lia.
143 { symmetry; apply (Zmod_unique a n q); subst r; lia. }
144 { symmetry; apply (Zmod_unique a n (q + 1)); subst r; lia. }
145 { symmetry; apply (Zmod_unique a n (q + 2)); subst r; lia. }
146 Qed.
147
148 Section StrongerBounds.
149 Context (n_good : b ^ offset * (b^(2*k) mod n) <= b ^ (k+offset) - m) (a_good : a < n * b ^ k).
150
151 Lemma helper_1 : b ^ (2 * k) * ((a / n) - 1) <= m * (n * (a / n) - b ^ (k - offset)).
152 Proof.
153 pose proof (Z.mod_pos_bound (b ^ (2*k)) n).
154 assert (0 < b ^ (k - offset)) by auto with zarith.
155 assert (a/n < b ^ k) by auto using Z.div_lt_upper_bound with zarith.
156 assert (b ^ (2 * k) - m * n = b ^ (2 * k) mod n) by (subst m; Z.div_mod_to_quot_rem_in_goal; nia).
157 autorewrite with push_Zpow in *; Z.div_mod_to_quot_rem_in_goal; nia.
158 Qed.
159
160 Lemma helper_2 : n * (a / n) - b ^ (k - offset) < b ^ (k - offset) * (a / b ^ (k - offset)).
161 Proof.
162 pose proof (Z.mod_pos_bound a n).
163 pose proof (Z.mod_pos_bound a (b ^ (k - offset))).
164 assert (0 < b ^ (k - offset)) by auto with zarith.
165 Z.div_mod_to_quot_rem_in_goal; lia.
166 Qed.
167
168 Let epsilon := (a / n) * b ^ (k+offset) - (a / b ^ (k - offset)) * m.
169
170 Lemma q_epsilon : q = (a / n) + (- epsilon) / b ^ (k + offset).
171 Proof.
172 subst q epsilon.
173 autorewrite with push_Zpow in *; do 2 Z.div_mod_to_quot_rem_in_goal; nia.
174 Qed.
175
176 Lemma epsilon_lower : - b ^ (k + offset) < epsilon.
177 Proof.
178 pose proof q_epsilon as Hq_epsilon.
179 rewrite (proj2_sig q_nice) in Hq_epsilon.
180 cut (- epsilon / b ^ (k + offset) <= 0);
181 [ Z.div_mod_to_quot_rem_in_goal | break_match_hyps ]; nia.
182 Qed.
183
184 Lemma m_pos : 0 < m.
185 Proof.
186 subst m. Z.zero_bounds; autorewrite with push_Zpow in *; nia.
187 Qed.
188
189 Lemma epsilon_bound : epsilon < b ^ (k + offset).
190 Proof.
191 subst epsilon. pose proof helper_1. pose proof helper_2. pose proof m_pos.
192 replace (b ^ (2 * k)) with (b^(k - offset) * b ^ (k + offset)) in *
193 by (rewrite <-Z.pow_add_r; auto with zarith).
194 apply Z.mul_lt_mono_pos_l with (p:=b^(k-offset)); auto with zarith.
195 nia.
196 Qed.
197
198 Lemma q_nice_strong : { b : bool | q = a / n + if b then -1 else 0}.
199 Proof.
200 exists (0 <? epsilon).
201 rewrite q_epsilon.
202 pose proof epsilon_bound. pose proof epsilon_lower.
203 break_match; Z.ltb_to_lt; Z.div_mod_to_quot_rem_in_goal; nia.
204 Qed.
205
206 Lemma q_bound : a / n - 1 <= q.
207 Proof.
208 rewrite (proj2_sig q_nice_strong).
209 break_match; lia.
210 Qed.
211
212 Lemma r_small_strong : r < 2 * n.
213 Proof.
214 Hint Rewrite (Z.mul_div_eq' a n) using lia : zstrip_div.
215 assert (a mod n < n) by auto with zarith lia.
216 unfold r.
217 apply Z.le_lt_trans with (m:= a - (a / n - 1) * n); [pose proof q_bound; nia | ].
218 autorewrite with push_Zmul zsimplify zstrip_div.
219 auto with lia.
220 Qed.
221
222 Theorem barrett_reduction_small_strong
223 : a mod n = if r <? n then r else r-n.
224 Proof using a a_good a_nonneg a_small b base_good epsilon k k_big_enough k_good m m_good n n_good n_large n_pos n_reasonable offset offset_nonneg q r.
225 pose proof r_small_strong. pose proof qn_small.
226 destruct (r <? n) eqn:Hr; try rewrite Hr; Z.ltb_to_lt; try lia.
227 { symmetry; apply (Zmod_unique a n q); subst r; lia. }
228 { symmetry; apply (Zmod_unique a n (q + 1)); subst r; lia. }
229 Qed.
230 End StrongerBounds.
231 End barrett_algorithm.
232 End barrett.