| 1 |
(*** Barrett Reduction *) |
| 2 |
(** This file implements Barrett Reduction on [Z]. We follow Wikipedia. *) |
| 3 |
From Coq Require Import ZArith Lia. |
| 4 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 5 |
Require Import Crypto.Util.ZUtil.Tactics.SimplifyFractionsLe. |
| 6 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 7 |
Require Import Crypto.Util.ZUtil.Div. |
| 8 |
Require Import Crypto.Util.ZUtil.Modulo. |
| 9 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 10 |
|
| 11 |
Local Open Scope Z_scope. |
| 12 |
|
| 13 |
Section barrett. |
| 14 |
Context (n a : Z) |
| 15 |
(n_reasonable : n <> 0). |
| 16 |
(** Quoting Wikipedia <https://en.wikipedia.org/wiki/Barrett_reduction>: *) |
| 17 |
(** In modular arithmetic, Barrett reduction is a reduction |
| 18 |
algorithm introduced in 1986 by P.D. Barrett. A naive way of |
| 19 |
computing *) |
| 20 |
(** [c = a mod n] *) |
| 21 |
(** would be to use a fast division algorithm. Barrett reduction is |
| 22 |
an algorithm designed to optimize this operation assuming [n] is |
| 23 |
constant, and [a < n²], replacing divisions by |
| 24 |
multiplications. *) |
| 25 |
|
| 26 |
(** * General idea *) |
| 27 |
Section general_idea. |
| 28 |
(** Let [m = 1 / n] be the inverse of [n] as a floating point |
| 29 |
number. Then *) |
| 30 |
(** [a mod n = a - ⌊a m⌋ n] *) |
| 31 |
(** where [⌊ x ⌋] denotes the floor function. The result is exact, |
| 32 |
as long as [m] is computed with sufficient accuracy. *) |
| 33 |
|
| 34 |
(* [/] is [Z.div], which means truncated division *) |
| 35 |
Local Notation "⌊am⌋" := (a / n) (only parsing). |
| 36 |
|
| 37 |
Theorem naive_barrett_reduction_correct |
| 38 |
: a mod n = a - ⌊am⌋ * n. |
| 39 |
Proof using n_reasonable. |
| 40 |
apply Zmod_eq_full; assumption. |
| 41 |
Qed. |
| 42 |
End general_idea. |
| 43 |
|
| 44 |
(** * Barrett algorithm *) |
| 45 |
Section barrett_algorithm. |
| 46 |
(** Barrett algorithm is a fixed-point analog which expresses |
| 47 |
everything in terms of integers. Let [k] be the smallest |
| 48 |
integer such that [2ᵏ > n]. Think of [n] as representing the |
| 49 |
fixed-point number [n 2⁻ᵏ]. We precompute [m] such that [m = |
| 50 |
⌊4ᵏ / n⌋]. Then [m] represents the fixed-point number |
| 51 |
[m 2⁻ᵏ ≈ (n 2⁻ᵏ)⁻¹]. *) |
| 52 |
(** N.B. We don't need [k] to be the smallest such integer. *) |
| 53 |
Context (k : Z) |
| 54 |
(k_good : n < 2 ^ k) |
| 55 |
(m : Z) |
| 56 |
(m_good : m = 4^k / n). (* [/] is [Z.div], which is truncated *) |
| 57 |
(** Wikipedia neglects to mention non-negativity, but we need it. |
| 58 |
It might be possible to do with a relaxed assumption, such as |
| 59 |
the sign of [a] and the sign of [n] being the same; but I |
| 60 |
figured it wasn't worth it. *) |
| 61 |
Context (n_pos : 0 < n) (* or just [0 <= n], since we have [n <> 0] above *) |
| 62 |
(a_nonneg : 0 <= a). |
| 63 |
|
| 64 |
Lemma k_nonnegative : 0 <= k. |
| 65 |
Proof using Type*. |
| 66 |
destruct (Z_lt_le_dec k 0); try assumption. |
| 67 |
rewrite !Z.pow_neg_r in * by lia; lia. |
| 68 |
Qed. |
| 69 |
|
| 70 |
(** Now *) |
| 71 |
Let q := (m * a) / 4^k. |
| 72 |
Let r := a - q * n. |
| 73 |
(** Because of the floor function (in Coq, because [/] means |
| 74 |
truncated division), [q] is an integer and [r ≡ a mod n]. *) |
| 75 |
Theorem barrett_reduction_equivalent |
| 76 |
: r mod n = a mod n. |
| 77 |
Proof using m_good. |
| 78 |
subst r q m. |
| 79 |
rewrite <- !Z.add_opp_r, !Zopp_mult_distr_l, !Z_mod_plus_full by assumption. |
| 80 |
reflexivity. |
| 81 |
Qed. |
| 82 |
|
| 83 |
Lemma qn_small |
| 84 |
: q * n <= a. |
| 85 |
Proof using a_nonneg k_good m_good n_pos n_reasonable. |
| 86 |
pose proof k_nonnegative; subst q r m. |
| 87 |
assert (0 <= 2^(k-1)) by Z.zero_bounds. |
| 88 |
Z.simplify_fractions_le. |
| 89 |
Qed. |
| 90 |
|
| 91 |
(** Also, if [a < n²] then [r < 2n]. *) |
| 92 |
(** N.B. It turns out that it is sufficient to assume [a < 4ᵏ]. *) |
| 93 |
Context (a_small : a < 4^k). |
| 94 |
Lemma q_nice : { b : bool | q = a / n + if b then -1 else 0 }. |
| 95 |
Proof using a_nonneg a_small k_good m_good n_pos n_reasonable. |
| 96 |
assert (0 <= (4 ^ k * a / n) mod 4 ^ k < 4 ^ k) by auto with zarith lia. |
| 97 |
assert (0 <= a * (4 ^ k mod n) / n < 4 ^ k) by (auto with zero_bounds zarith lia). |
| 98 |
subst q r m. |
| 99 |
rewrite (Z.div_mul_diff_exact''' (4^k) n a) by lia. |
| 100 |
rewrite (Z_div_mod_eq_full (4^k * _ / n) (4^k)). |
| 101 |
autorewrite with push_Zmul push_Zopp zsimplify zstrip_div. |
| 102 |
eexists; reflexivity. |
| 103 |
Qed. |
| 104 |
|
| 105 |
Lemma r_small : r < 2 * n. |
| 106 |
Proof using a_nonneg a_small k_good m_good n_pos n_reasonable q. |
| 107 |
Hint Rewrite (Z.mul_div_eq' a n) using lia : zstrip_div. |
| 108 |
assert (a mod n < n) by auto with zarith lia. |
| 109 |
unfold r; rewrite (proj2_sig q_nice); generalize (proj1_sig q_nice); intro; subst q m. |
| 110 |
autorewrite with push_Zmul zsimplify zstrip_div. |
| 111 |
break_match; auto with lia. |
| 112 |
Qed. |
| 113 |
|
| 114 |
(** In that case, we have *) |
| 115 |
Theorem barrett_reduction_small |
| 116 |
: a mod n = if r <? n |
| 117 |
then r |
| 118 |
else r - n. |
| 119 |
Proof using a_nonneg a_small k_good m_good n_pos n_reasonable q. |
| 120 |
pose proof r_small. pose proof qn_small. |
| 121 |
destruct (r <? n) eqn:rlt; Z.ltb_to_lt. |
| 122 |
{ symmetry; apply (Zmod_unique a n q); subst r; lia. } |
| 123 |
{ symmetry; apply (Zmod_unique a n (q + 1)); subst r; lia. } |
| 124 |
Qed. |
| 125 |
End barrett_algorithm. |
| 126 |
End barrett. |