| 1 |
(*** Barrett Reduction *) |
| 2 |
(** This file implements a slightly-generalized version of Barrett |
| 3 |
Reduction on [Z]. This version follows the Handbook of Applied |
| 4 |
Cryptography (Algorithm 14.42) rather closely; the only deviations |
| 5 |
are that we generalize from [k ± 1] to [k ± offset] for an |
| 6 |
arbitrary offset, and we weaken the conditions on the base [b] in |
| 7 |
[bᵏ] slightly. Contrasted with some other versions, this version |
| 8 |
does reduction modulo [b^(k+offset)] early (ensuring that we don't |
| 9 |
have to carry around extra precision), but requires more stringint |
| 10 |
conditions on the base ([b]), exponent ([k]), and the [offset]. *) |
| 11 |
From Coq Require Import ZArith Lia. |
| 12 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 13 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 14 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 15 |
Require Import Crypto.Util.ZUtil.Div. |
| 16 |
Require Import Crypto.Util.ZUtil.Modulo. |
| 17 |
Require Import Crypto.Util.ZUtil.Hints. |
| 18 |
Require Import Crypto.Util.ZUtil.ZSimplify. |
| 19 |
|
| 20 |
Local Open Scope Z_scope. |
| 21 |
|
| 22 |
Section barrett. |
| 23 |
(** Quoting the Handbook of Applied Cryptography <http://cacr.uwaterloo.ca/hac/about/chap14.pdf>: *) |
| 24 |
(** Barrett reduction (Algorithm 14.42) computes [r = x mod m] given |
| 25 |
[x] and [m]. The algorithm requires the precomputation of the |
| 26 |
quantity [µ = ⌊b²ᵏ/m⌋]; it is advantageous if many reductions |
| 27 |
are performed with a single modulus. For example, each RSA |
| 28 |
encryption for one entity requires reduction modulo that |
| 29 |
entity’s public key modulus. The precomputation takes a fixed |
| 30 |
amount of work, which is negligible in comparison to modular |
| 31 |
exponentiation cost. Typically, the radix [b] is chosen to be |
| 32 |
close to the word-size of the processor. Hence, assume [b > 3] in |
| 33 |
Algorithm 14.42 (see Note 14.44 (ii)). *) |
| 34 |
|
| 35 |
(** * Barrett modular reduction *) |
| 36 |
Section barrett_modular_reduction. |
| 37 |
Context (m b x k μ offset : Z) |
| 38 |
(m_pos : 0 < m) |
| 39 |
(base_pos : 0 < b) |
| 40 |
(k_good : m < b^k) |
| 41 |
(μ_good : μ = b^(2*k) / m) (* [/] is [Z.div], which is truncated *) |
| 42 |
(x_nonneg : 0 <= x) |
| 43 |
(offset_nonneg : 0 <= offset) |
| 44 |
(k_big_enough : offset <= k) |
| 45 |
(x_small : x < b^(2*k)) |
| 46 |
(m_small : 3 * m <= b^(k+offset)) |
| 47 |
(** We also need that [m] is large enough; [m] larger than |
| 48 |
[bᵏ⁻¹] works, but we ask for something more precise. *) |
| 49 |
(m_large : x mod b^(k-offset) <= m). |
| 50 |
|
| 51 |
Let q1 := x / b^(k-offset). Let q2 := q1 * μ. Let q3 := q2 / b^(k+offset). |
| 52 |
Let r1 := x mod b^(k+offset). Let r2 := (q3 * m) mod b^(k+offset). |
| 53 |
(** At this point, the HAC says "If [r < 0] then [r ← r + bᵏ⁺¹]". |
| 54 |
This is equivalent to reduction modulo [b^(k+offset)], as we |
| 55 |
prove below. The version involving modular reduction has the |
| 56 |
benefit of being cheaper to implement, and making the proofs |
| 57 |
simpler, so we primarily use that version. *) |
| 58 |
Let r_mod_3m := (r1 - r2) mod b^(k+offset). |
| 59 |
Let r_mod_3m_orig := let r := r1 - r2 in |
| 60 |
if r <? 0 then r + b^(k+offset) else r. |
| 61 |
|
| 62 |
Lemma r_mod_3m_eq_orig : r_mod_3m = r_mod_3m_orig. |
| 63 |
Proof using base_pos k_big_enough m_pos m_small offset_nonneg r1 r2. |
| 64 |
clear μ_good x_nonneg. |
| 65 |
assert (0 <= r1 < b^(k+offset)) by (subst r1; auto with zarith). |
| 66 |
assert (0 <= r2 < b^(k+offset)) by (subst r2; auto with zarith). |
| 67 |
subst r_mod_3m r_mod_3m_orig; cbv zeta. |
| 68 |
break_match; Z.ltb_to_lt. |
| 69 |
{ symmetry; apply (Zmod_unique (r1 - r2) _ (-1)); lia. } |
| 70 |
{ symmetry; apply (Zmod_unique (r1 - r2) _ 0); lia. } |
| 71 |
Qed. |
| 72 |
|
| 73 |
(** 14.43 Fact By the division algorithm (Definition 2.82), there |
| 74 |
exist integers [Q] and [R] such that [x = Qm + R] and [0 ≤ R < |
| 75 |
m]. In step 1 of Algorithm 14.42 (Barrett modular reduction), |
| 76 |
the following inequality is satisfied: [Q - 2 ≤ q₃ ≤ Q]. *) |
| 77 |
(** We prove this by providing a more useful form for [q₃]. *) |
| 78 |
Let Q := x / m. |
| 79 |
Let R := x mod m. |
| 80 |
Lemma q3_nice : { b : bool * bool | q3 = Q + (if fst b then -1 else 0) + (if snd b then -1 else 0) }. |
| 81 |
Proof using base_pos k_big_enough m_large m_pos m_small offset_nonneg x_nonneg x_small μ_good. |
| 82 |
assert (0 < b^(k+offset)) by Z.zero_bounds. |
| 83 |
assert (0 < b^(k-offset)) by Z.zero_bounds. |
| 84 |
assert (x / b^(k-offset) <= b^(2*k) / b^(k-offset)) by auto with zarith lia. |
| 85 |
assert (x / b^(k-offset) <= b^(k+offset)) by (autorewrite with pull_Zpow zsimplify in *; assumption). |
| 86 |
subst q1 q2 q3 Q r_mod_3m r_mod_3m_orig r1 r2 R μ. |
| 87 |
rewrite (Z.div_mul_diff_exact' (b^(2*k)) m (x/b^(k-offset))) by auto with lia zero_bounds. |
| 88 |
rewrite (Z_div_mod_eq_full (_ * b^(2*k) / m) (b^(k+offset))). |
| 89 |
autorewrite with push_Zmul push_Zopp zsimplify zstrip_div zdiv_to_mod. |
| 90 |
rewrite Z.div_sub_mod_cond, !Z.div_sub_small; auto with zero_bounds zarith. |
| 91 |
eexists (_, _); reflexivity. |
| 92 |
Qed. |
| 93 |
|
| 94 |
Fact q3_in_range : Q - 2 <= q3 <= Q. |
| 95 |
Proof using base_pos k_big_enough m_large m_pos m_small offset_nonneg q2 x_nonneg x_small μ_good. |
| 96 |
rewrite (proj2_sig q3_nice). |
| 97 |
break_match; lia. |
| 98 |
Qed. |
| 99 |
|
| 100 |
(** 14.44 Note (partial justification of correctness of Barrett reduction) *) |
| 101 |
(** (i) Algorithm 14.42 is based on the observation that [⌊x/m⌋] |
| 102 |
can be written as [Q = |
| 103 |
⌊(x/bᵏ⁻¹)(b²ᵏ/m)(1/bᵏ⁺¹)⌋]. Moreover, [Q] can be |
| 104 |
approximated by the quantity [q₃ = ⌊⌊x/bᵏ⁻¹⌋µ/bᵏ⁺¹⌋]. |
| 105 |
Fact 14.43 guarantees that [q₃] is never larger than the |
| 106 |
true quotient [Q], and is at most 2 smaller. *) |
| 107 |
Lemma x_minus_q3_m_in_range : 0 <= x - q3 * m < 3 * m. |
| 108 |
Proof using base_pos k_big_enough m_large m_pos m_small offset_nonneg q2 x_nonneg x_small μ_good. |
| 109 |
clear k_good. |
| 110 |
pose proof q3_in_range. |
| 111 |
assert (0 <= R < m) by (subst R; auto with zarith). |
| 112 |
assert (0 <= (Q - q3) * m + R < 3 * m) by nia. |
| 113 |
subst Q R; autorewrite with push_Zmul zdiv_to_mod in *; lia. |
| 114 |
Qed. |
| 115 |
|
| 116 |
Lemma r_mod_3m_eq_alt : r_mod_3m = x - q3 * m. |
| 117 |
Proof using base_pos k_big_enough m_large m_pos m_small offset_nonneg q2 x_nonneg x_small μ_good. |
| 118 |
pose proof x_minus_q3_m_in_range. |
| 119 |
subst r_mod_3m r_mod_3m_orig r1 r2. |
| 120 |
autorewrite with pull_Zmod zsimplify; reflexivity. |
| 121 |
Qed. |
| 122 |
|
| 123 |
(** This version uses reduction modulo [b^(k+offset)]. *) |
| 124 |
Theorem barrett_reduction_equivalent |
| 125 |
: r_mod_3m mod m = x mod m. |
| 126 |
Proof using base_pos k_big_enough m_large m_pos m_small offset_nonneg r1 r2 x_nonneg x_small μ_good. |
| 127 |
rewrite r_mod_3m_eq_alt. |
| 128 |
autorewrite with zsimplify push_Zmod; reflexivity. |
| 129 |
Qed. |
| 130 |
|
| 131 |
(** This version, which matches the original in the HAC, uses |
| 132 |
conditional addition of [b^(k+offset)]. *) |
| 133 |
Theorem barrett_reduction_orig_equivalent |
| 134 |
: r_mod_3m_orig mod m = x mod m. |
| 135 |
Proof using base_pos k_big_enough m_large m_pos m_small offset_nonneg r_mod_3m x_nonneg x_small μ_good. rewrite <- r_mod_3m_eq_orig; apply barrett_reduction_equivalent. Qed. |
| 136 |
|
| 137 |
Lemma r_small : 0 <= r_mod_3m < 3 * m. |
| 138 |
Proof using Q R base_pos k_big_enough m_large m_pos m_small offset_nonneg q3 x_nonneg x_small μ_good. |
| 139 |
pose proof x_minus_q3_m_in_range. |
| 140 |
subst Q R r_mod_3m r_mod_3m_orig r1 r2. |
| 141 |
autorewrite with pull_Zmod zsimplify; lia. |
| 142 |
Qed. |
| 143 |
|
| 144 |
|
| 145 |
(** This version uses reduction modulo [b^(k+offset)]. *) |
| 146 |
Theorem barrett_reduction_small (r := r_mod_3m) |
| 147 |
: x mod m = let r := if r <? m then r else r-m in |
| 148 |
let r := if r <? m then r else r-m in |
| 149 |
r. |
| 150 |
Proof using base_pos k_big_enough m_large m_pos m_small offset_nonneg r1 r2 x_nonneg x_small μ_good. |
| 151 |
pose proof r_small. cbv zeta. |
| 152 |
destruct (r <? m) eqn:Hr, (r-m <? m) eqn:?; subst r; rewrite !r_mod_3m_eq_alt, ?Hr in *; Z.ltb_to_lt; try lia. |
| 153 |
{ symmetry; eapply (Zmod_unique x m q3); lia. } |
| 154 |
{ symmetry; eapply (Zmod_unique x m (q3 + 1)); lia. } |
| 155 |
{ symmetry; eapply (Zmod_unique x m (q3 + 2)); lia. } |
| 156 |
Qed. |
| 157 |
|
| 158 |
(** This version, which matches the original in the HAC, uses |
| 159 |
conditional addition of [b^(k+offset)]. *) |
| 160 |
Theorem barrett_reduction_small_orig (r := r_mod_3m_orig) |
| 161 |
: x mod m = let r := if r <? m then r else r-m in |
| 162 |
let r := if r <? m then r else r-m in |
| 163 |
r. |
| 164 |
Proof using base_pos k_big_enough m_large m_pos m_small offset_nonneg r_mod_3m x_nonneg x_small μ_good. subst r; rewrite <- r_mod_3m_eq_orig; apply barrett_reduction_small. Qed. |
| 165 |
End barrett_modular_reduction. |
| 166 |
End barrett. |