| 1 |
(*** Montgomery Multiplication *) |
| 2 |
(** This file implements the proofs for Montgomery Form, Montgomery |
| 3 |
Reduction, and Montgomery Multiplication on [Z]. We follow |
| 4 |
Wikipedia. *) |
| 5 |
From Coq Require Import ZArith Lia Equalities. |
| 6 |
Require Import Crypto.Arithmetic.MontgomeryReduction.Definition. |
| 7 |
Require Import Crypto.Util.ZUtil.EquivModulo. |
| 8 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 9 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 10 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 11 |
Require Import Crypto.Util.Tactics.SimplifyRepeatedIfs. |
| 12 |
Require Import Crypto.Util.Notations. |
| 13 |
|
| 14 |
Declare Module Nop : Nop. |
| 15 |
Module Import ImportEquivModuloInstances := Z.EquivModuloInstances Nop. |
| 16 |
|
| 17 |
Local Existing Instance eq_Reflexive. (* speed up setoid_rewrite as per https://coq.inria.fr/bugs/show_bug.cgi?id=4978 *) |
| 18 |
|
| 19 |
Local Open Scope Z_scope. |
| 20 |
|
| 21 |
Section montgomery. |
| 22 |
Context (N : Z) |
| 23 |
(N_reasonable : N <> 0) |
| 24 |
(R : Z) |
| 25 |
(R_good : Z.gcd N R = 1). |
| 26 |
Local Notation "x ≡ y" := (Z.equiv_modulo N x y) : type_scope. |
| 27 |
Local Notation "x ≡ᵣ y" := (Z.equiv_modulo R x y) : type_scope. |
| 28 |
Context (R' : Z) |
| 29 |
(R'_good : R * R' ≡ 1). |
| 30 |
|
| 31 |
Lemma R'_good' : R' * R ≡ 1. |
| 32 |
Proof using R'_good. rewrite <- R'_good; apply f_equal2; lia. Qed. |
| 33 |
|
| 34 |
Local Notation to_montgomery_naive := (to_montgomery_naive R) (only parsing). |
| 35 |
Local Notation from_montgomery_naive := (from_montgomery_naive R') (only parsing). |
| 36 |
|
| 37 |
Lemma to_from_montgomery_naive x : to_montgomery_naive (from_montgomery_naive x) ≡ x. |
| 38 |
Proof using R'_good. |
| 39 |
unfold to_montgomery_naive, from_montgomery_naive. |
| 40 |
rewrite <- Z.mul_assoc, R'_good'. |
| 41 |
autorewrite with zsimplify; reflexivity. |
| 42 |
Qed. |
| 43 |
Lemma from_to_montgomery_naive x : from_montgomery_naive (to_montgomery_naive x) ≡ x. |
| 44 |
Proof using R'_good. |
| 45 |
unfold to_montgomery_naive, from_montgomery_naive. |
| 46 |
rewrite <- Z.mul_assoc, R'_good. |
| 47 |
autorewrite with zsimplify; reflexivity. |
| 48 |
Qed. |
| 49 |
|
| 50 |
(** * Modular arithmetic and Montgomery form *) |
| 51 |
Section general. |
| 52 |
Local Infix "+" := add : montgomery_scope. |
| 53 |
Local Infix "-" := sub : montgomery_scope. |
| 54 |
Local Infix "*" := (mul_naive R') : montgomery_scope. |
| 55 |
|
| 56 |
Lemma add_correct_naive x y : from_montgomery_naive (x + y) = from_montgomery_naive x + from_montgomery_naive y. |
| 57 |
Proof using Type. unfold from_montgomery_naive, add; lia. Qed. |
| 58 |
Lemma add_correct_naive_to x y : to_montgomery_naive (x + y) = (to_montgomery_naive x + to_montgomery_naive y)%montgomery. |
| 59 |
Proof using Type. unfold to_montgomery_naive, add; autorewrite with push_Zmul; reflexivity. Qed. |
| 60 |
Lemma sub_correct_naive x y : from_montgomery_naive (x - y) = from_montgomery_naive x - from_montgomery_naive y. |
| 61 |
Proof using Type. unfold from_montgomery_naive, sub; lia. Qed. |
| 62 |
Lemma sub_correct_naive_to x y : to_montgomery_naive (x - y) = (to_montgomery_naive x - to_montgomery_naive y)%montgomery. |
| 63 |
Proof using Type. unfold to_montgomery_naive, sub; autorewrite with push_Zmul; reflexivity. Qed. |
| 64 |
|
| 65 |
Theorem mul_correct_naive x y : from_montgomery_naive (x * y) = from_montgomery_naive x * from_montgomery_naive y. |
| 66 |
Proof using Type. unfold from_montgomery_naive, mul_naive; lia. Qed. |
| 67 |
Theorem mul_correct_naive_to x y : to_montgomery_naive (x * y) ≡ (to_montgomery_naive x * to_montgomery_naive y)%montgomery. |
| 68 |
Proof using R'_good. |
| 69 |
unfold to_montgomery_naive, mul_naive. |
| 70 |
rewrite <- !Z.mul_assoc, R'_good. |
| 71 |
autorewrite with zsimplify; apply (f_equal2 Z.modulo); lia. |
| 72 |
Qed. |
| 73 |
End general. |
| 74 |
|
| 75 |
(** * The REDC algorithm *) |
| 76 |
Section redc. |
| 77 |
Context (N' : Z) |
| 78 |
(N'_in_range : 0 <= N' < R) |
| 79 |
(N'_good : N * N' ≡ᵣ -1). |
| 80 |
|
| 81 |
Lemma N'_good' : N' * N ≡ᵣ -1. |
| 82 |
Proof using N'_good. rewrite <- N'_good; apply f_equal2; lia. Qed. |
| 83 |
|
| 84 |
Lemma N'_good'_alt x : (((x mod R) * (N' mod R)) mod R) * (N mod R) ≡ᵣ x * -1. |
| 85 |
Proof using N'_good. |
| 86 |
rewrite <- N'_good', Z.mul_assoc. |
| 87 |
unfold Z.equiv_modulo; push_Zmod. |
| 88 |
reflexivity. |
| 89 |
Qed. |
| 90 |
|
| 91 |
Section redc. |
| 92 |
Context (T : Z). |
| 93 |
|
| 94 |
Local Notation m := (((T mod R) * N') mod R). |
| 95 |
Local Notation prereduce := (prereduce N R N'). |
| 96 |
|
| 97 |
Local Ltac t_fin_correct := |
| 98 |
unfold Z.equiv_modulo; push_Zmod; autorewrite with zsimplify; reflexivity. |
| 99 |
|
| 100 |
Lemma prereduce_correct : prereduce T ≡ T * R'. |
| 101 |
Proof using N'_good N'_in_range N_reasonable R'_good. |
| 102 |
transitivity ((T + m * N) * R'). |
| 103 |
{ unfold prereduce. |
| 104 |
autorewrite with zstrip_div; push_Zmod. |
| 105 |
rewrite N'_good'_alt. |
| 106 |
autorewrite with zsimplify pull_Zmod. |
| 107 |
reflexivity. } |
| 108 |
t_fin_correct. |
| 109 |
Qed. |
| 110 |
|
| 111 |
Lemma reduce_correct : reduce N R N' T ≡ T * R'. |
| 112 |
Proof using N'_good N'_in_range N_reasonable R'_good. |
| 113 |
unfold reduce. |
| 114 |
break_match; rewrite prereduce_correct; t_fin_correct. |
| 115 |
Qed. |
| 116 |
|
| 117 |
Lemma partial_reduce_correct : partial_reduce N R N' T ≡ T * R'. |
| 118 |
Proof using N'_good N'_in_range N_reasonable R'_good. |
| 119 |
unfold partial_reduce. |
| 120 |
break_match; rewrite prereduce_correct; t_fin_correct. |
| 121 |
Qed. |
| 122 |
|
| 123 |
Lemma reduce_via_partial_correct : reduce_via_partial N R N' T ≡ T * R'. |
| 124 |
Proof using N'_good N'_in_range N_reasonable R'_good. |
| 125 |
unfold reduce_via_partial. |
| 126 |
break_match; rewrite partial_reduce_correct; t_fin_correct. |
| 127 |
Qed. |
| 128 |
|
| 129 |
Let m_small : 0 <= m < R. Proof. auto with zarith. Qed. |
| 130 |
|
| 131 |
Section generic. |
| 132 |
Lemma prereduce_in_range_gen B |
| 133 |
: 0 <= N |
| 134 |
-> 0 <= T <= R * B |
| 135 |
-> 0 <= prereduce T < B + N. |
| 136 |
Proof using N_reasonable m_small. unfold prereduce; auto with zarith nia. Qed. |
| 137 |
End generic. |
| 138 |
|
| 139 |
Section N_very_small. |
| 140 |
Context (N_very_small : 0 <= 4 * N < R). |
| 141 |
|
| 142 |
Lemma prereduce_in_range_very_small |
| 143 |
: 0 <= T <= (2 * N - 1) * (2 * N - 1) |
| 144 |
-> 0 <= prereduce T < 2 * N. |
| 145 |
Proof using N_reasonable N_very_small m_small. pose proof (prereduce_in_range_gen N); nia. Qed. |
| 146 |
End N_very_small. |
| 147 |
|
| 148 |
Section N_small. |
| 149 |
Context (N_small : 0 <= 2 * N < R). |
| 150 |
|
| 151 |
Lemma prereduce_in_range_small |
| 152 |
: 0 <= T <= (2 * N - 1) * (N - 1) |
| 153 |
-> 0 <= prereduce T < 2 * N. |
| 154 |
Proof using N_reasonable N_small m_small. pose proof (prereduce_in_range_gen N); nia. Qed. |
| 155 |
|
| 156 |
Lemma prereduce_in_range_small_fully_reduced |
| 157 |
: 0 <= T <= 2 * N |
| 158 |
-> 0 <= prereduce T <= N. |
| 159 |
Proof using N_reasonable N_small m_small. pose proof (prereduce_in_range_gen 1); nia. Qed. |
| 160 |
End N_small. |
| 161 |
|
| 162 |
Section N_small_enough. |
| 163 |
Context (N_small_enough : 0 <= N < R). |
| 164 |
|
| 165 |
Lemma prereduce_in_range_small_enough |
| 166 |
: 0 <= T <= R * R |
| 167 |
-> 0 <= prereduce T < R + N. |
| 168 |
Proof using N_reasonable N_small_enough m_small. pose proof (prereduce_in_range_gen R); nia. Qed. |
| 169 |
|
| 170 |
Lemma reduce_in_range_R |
| 171 |
: 0 <= T <= R * R |
| 172 |
-> 0 <= reduce N R N' T < R. |
| 173 |
Proof using N_reasonable N_small_enough m_small. |
| 174 |
intro H; pose proof (prereduce_in_range_small_enough H). |
| 175 |
unfold reduce, prereduce in *; break_match; Z.ltb_to_lt; nia. |
| 176 |
Qed. |
| 177 |
|
| 178 |
Lemma partial_reduce_in_range_R |
| 179 |
: 0 <= T <= R * R |
| 180 |
-> 0 <= partial_reduce N R N' T < R. |
| 181 |
Proof using N_reasonable N_small_enough m_small. |
| 182 |
intro H; pose proof (prereduce_in_range_small_enough H). |
| 183 |
unfold partial_reduce, prereduce in *; break_match; Z.ltb_to_lt; nia. |
| 184 |
Qed. |
| 185 |
|
| 186 |
Lemma reduce_via_partial_in_range_R |
| 187 |
: 0 <= T <= R * R |
| 188 |
-> 0 <= reduce_via_partial N R N' T < R. |
| 189 |
Proof using N_reasonable N_small_enough m_small. |
| 190 |
intro H; pose proof (prereduce_in_range_small_enough H). |
| 191 |
unfold reduce_via_partial, partial_reduce, prereduce in *; break_match; Z.ltb_to_lt; nia. |
| 192 |
Qed. |
| 193 |
End N_small_enough. |
| 194 |
|
| 195 |
Section unconstrained. |
| 196 |
Lemma prereduce_in_range |
| 197 |
: 0 <= T <= R * N |
| 198 |
-> 0 <= prereduce T < 2 * N. |
| 199 |
Proof using N_reasonable m_small. pose proof (prereduce_in_range_gen N); nia. Qed. |
| 200 |
|
| 201 |
Lemma reduce_in_range |
| 202 |
: 0 <= T <= R * N |
| 203 |
-> 0 <= reduce N R N' T < N. |
| 204 |
Proof using N_reasonable m_small. |
| 205 |
intro H; pose proof (prereduce_in_range H). |
| 206 |
unfold reduce, prereduce in *; break_match; Z.ltb_to_lt; nia. |
| 207 |
Qed. |
| 208 |
|
| 209 |
Lemma partial_reduce_in_range |
| 210 |
: 0 <= T <= R * N |
| 211 |
-> Z.min 0 (R - N) <= partial_reduce N R N' T < 2 * N. |
| 212 |
Proof using N_reasonable m_small. |
| 213 |
intro H; pose proof (prereduce_in_range H). |
| 214 |
unfold partial_reduce, prereduce in *; break_match; Z.ltb_to_lt; |
| 215 |
apply Z.min_case_strong; nia. |
| 216 |
Qed. |
| 217 |
|
| 218 |
Lemma reduce_via_partial_in_range |
| 219 |
: 0 <= T <= R * N |
| 220 |
-> Z.min 0 (R - N) <= reduce_via_partial N R N' T < N. |
| 221 |
Proof using N_reasonable m_small. |
| 222 |
intro H; pose proof (partial_reduce_in_range H). |
| 223 |
unfold reduce_via_partial in *; break_match; Z.ltb_to_lt; lia. |
| 224 |
Qed. |
| 225 |
End unconstrained. |
| 226 |
|
| 227 |
Section alt. |
| 228 |
Context (N_in_range : 0 <= N < R) |
| 229 |
(T_representable : 0 <= T < R * R). |
| 230 |
Lemma partial_reduce_alt_eq : partial_reduce_alt N R N' T = partial_reduce N R N' T. |
| 231 |
Proof using N_in_range N_reasonable T_representable m_small. |
| 232 |
assert (0 <= T + m * N < 2 * (R * R)) by nia. |
| 233 |
assert (0 <= T + m * N < R * (R + N)) by nia. |
| 234 |
assert (0 <= (T + m * N) / R < R + N) by auto with zarith. |
| 235 |
assert ((T + m * N) / R - N < R) by lia. |
| 236 |
assert (R * R <= T + m * N -> R <= (T + m * N) / R) by auto with zarith. |
| 237 |
assert (T + m * N < R * R -> (T + m * N) / R < R) by auto with zarith. |
| 238 |
assert (H' : (T + m * N) mod (R * R) = if R * R <=? T + m * N then T + m * N - R * R else T + m * N) |
| 239 |
by (break_match; Z.ltb_to_lt; autorewrite with zsimplify; lia). |
| 240 |
unfold partial_reduce, partial_reduce_alt, prereduce. |
| 241 |
rewrite H'; clear H'. |
| 242 |
simplify_repeated_ifs. |
| 243 |
set (m' := m) in *. |
| 244 |
autorewrite with zsimplify; push_Zmod; autorewrite with zsimplify; pull_Zmod. |
| 245 |
break_match; Z.ltb_to_lt; autorewrite with zsimplify; try reflexivity; lia. |
| 246 |
Qed. |
| 247 |
|
| 248 |
Lemma reduce_via_partial_alt_eq : reduce_via_partial_alt N R N' T = reduce_via_partial N R N' T. |
| 249 |
Proof. |
| 250 |
cbv [reduce_via_partial_alt reduce_via_partial]. |
| 251 |
rewrite partial_reduce_alt_eq by lia. reflexivity. |
| 252 |
Qed. |
| 253 |
End alt. |
| 254 |
End redc. |
| 255 |
|
| 256 |
(** * Arithmetic in Montgomery form *) |
| 257 |
Section arithmetic. |
| 258 |
Local Infix "*" := (mul N R N') : montgomery_scope. |
| 259 |
|
| 260 |
Local Notation to_montgomery := (to_montgomery N R N'). |
| 261 |
Local Notation from_montgomery := (from_montgomery N R N'). |
| 262 |
Lemma to_from_montgomery a : to_montgomery (from_montgomery a) ≡ a. |
| 263 |
Proof using N'_good N'_in_range N_reasonable R'_good. |
| 264 |
unfold to_montgomery, from_montgomery. |
| 265 |
transitivity ((a * 1) * 1); [ | apply f_equal2; lia ]. |
| 266 |
rewrite <- !R'_good, !reduce_correct. |
| 267 |
unfold Z.equiv_modulo; push_Zmod; pull_Zmod. |
| 268 |
apply f_equal2; lia. |
| 269 |
Qed. |
| 270 |
Lemma from_to_montgomery a : from_montgomery (to_montgomery a) ≡ a. |
| 271 |
Proof using N'_good N'_in_range N_reasonable R'_good. |
| 272 |
unfold to_montgomery, from_montgomery. |
| 273 |
rewrite !reduce_correct. |
| 274 |
transitivity (a * ((R * (R * R' mod N) * R') mod N)). |
| 275 |
{ unfold Z.equiv_modulo; push_Zmod; pull_Zmod. |
| 276 |
apply f_equal2; lia. } |
| 277 |
{ repeat first [ rewrite R'_good |
| 278 |
| reflexivity |
| 279 |
| push_Zmod; pull_Zmod; progress autorewrite with zsimplify |
| 280 |
| progress unfold Z.equiv_modulo ]. } |
| 281 |
Qed. |
| 282 |
|
| 283 |
Theorem mul_correct x y : from_montgomery (x * y) ≡ from_montgomery x * from_montgomery y. |
| 284 |
Proof using N'_good N'_in_range N_reasonable R'_good. |
| 285 |
unfold from_montgomery, mul. |
| 286 |
rewrite !reduce_correct; apply f_equal2; lia. |
| 287 |
Qed. |
| 288 |
Theorem mul_correct_to x y : to_montgomery (x * y) ≡ (to_montgomery x * to_montgomery y)%montgomery. |
| 289 |
Proof using N'_good N'_in_range N_reasonable R'_good. |
| 290 |
unfold to_montgomery, mul. |
| 291 |
rewrite !reduce_correct. |
| 292 |
transitivity (x * y * R * 1 * 1 * 1); |
| 293 |
[ rewrite <- R'_good at 1 |
| 294 |
| rewrite <- R'_good at 1 2 3 ]; |
| 295 |
autorewrite with zsimplify; |
| 296 |
unfold Z.equiv_modulo; push_Zmod; pull_Zmod. |
| 297 |
{ apply f_equal2; lia. } |
| 298 |
{ apply f_equal2; lia. } |
| 299 |
Qed. |
| 300 |
End arithmetic. |
| 301 |
End redc. |
| 302 |
End montgomery. |
| 303 |
|
| 304 |
Module Import LocalizeEquivModuloInstances := Z.RemoveEquivModuloInstances Nop. |