| 1 |
From Coq Require Import Bool. |
| 2 |
From Coq Require Import Lia. |
| 3 |
From Coq Require Import ZArith. |
| 4 |
From Coq Require Import Znumtheory. |
| 5 |
Require Import Crypto.Util.Loops. |
| 6 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 7 |
Require Import Crypto.Util.ZUtil. |
| 8 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 9 |
Require Import Crypto.Util.ZUtil.Modulo.PullPush. |
| 10 |
|
| 11 |
Local Open Scope Z_scope. |
| 12 |
|
| 13 |
Module BinaryExtendedGCD. |
| 14 |
Section BinaryExtendedGCD. |
| 15 |
(** This section implements a constant-time version of the binary extended |
| 16 |
GCD algorithm, for computing modular inverses. It is the fully general |
| 17 |
version, supporting even moduli. In most cases, more specific algorithms |
| 18 |
should be used. For instance, inverting by a prime is simpler to do with |
| 19 |
Fermat's Little Theorem. However, RSA key generation requires the most |
| 20 |
general version. |
| 21 |
|
| 22 |
This algorithm is adapted from the Handbook of Applied Cryptography, |
| 23 |
section 14.4.3, algorithm 14.61. It was modified slightly to better |
| 24 |
bound coefficients, then recast into constant-time selects: |
| 25 |
|
| 26 |
1. Negate [B] and [C] so they are positive. The invariant now involves a |
| 27 |
subtraction. |
| 28 |
|
| 29 |
2. If step 2 (both [x] and [y] are even) runs, abort immediately. This |
| 30 |
algorithm only cares about [x] and [y] relatively prime. |
| 31 |
|
| 32 |
3. Subtract copies of [x] and [y] as needed in step 6 (both [u] and [v] |
| 33 |
are odd) so coefficients stay in bounds. |
| 34 |
|
| 35 |
4. Replace the [u >= v] check with [u > v]. This changes the end |
| 36 |
condition to [v = 0] rather than [u = 0]. This saves an extra |
| 37 |
subtraction due to which coefficients were negated. |
| 38 |
|
| 39 |
5. Rename x and y to a and n, to capture that one is a modulus. |
| 40 |
|
| 41 |
6. Rearrange steps 4 through 6 slightly. Merge the loops in steps 4 and |
| 42 |
5 into the main loop (step 7's goto), and move step 6 to the start of |
| 43 |
the loop iteration, ensuring each loop iteration halves at least one |
| 44 |
value. |
| 45 |
|
| 46 |
Note this algorithm does not handle either input being zero. *) |
| 47 |
|
| 48 |
(** This is a constant-time algorithm, so first define fixed-width words to |
| 49 |
compute over. In particular, we want to capture the non-trivial bounds on |
| 50 |
the intermediate values. A [word] is an integer with specified bit |
| 51 |
width. The actual implementation will represent this with an array of |
| 52 |
smaller machine words. *) |
| 53 |
Inductive word (bits : Z) := |
| 54 |
| Word : Z -> word. |
| 55 |
|
| 56 |
Definition wrap (bits val : Z) : Z := val mod 2^bits. |
| 57 |
|
| 58 |
Definition to_Z {bits : Z} (a : word bits) : Z := |
| 59 |
match a with Word v => v end. |
| 60 |
Definition to_word {bits : Z} (val : Z) := |
| 61 |
Word bits (wrap bits val). |
| 62 |
|
| 63 |
(** Our algorithm is implemented over the following constant-time |
| 64 |
primitives. [add] and [sub] return a carry and borrow bit, |
| 65 |
respectively. *) |
| 66 |
Definition add {bits : Z} (a b : word bits) : bool * word bits := |
| 67 |
let a' := to_Z a in |
| 68 |
let b' := to_Z b in |
| 69 |
(2^bits <=? a' + b', to_word (a' + b')). |
| 70 |
Definition sub {bits : Z} (a b : word bits) : bool * word bits := |
| 71 |
let a' := to_Z a in |
| 72 |
let b' := to_Z b in |
| 73 |
(a' <? b', to_word (a' - b')). |
| 74 |
Definition rshift1 {bits : Z} (a : word bits) : word bits := |
| 75 |
to_word ((to_Z a) / 2). |
| 76 |
Definition select {bits : Z} (t : bool) (a b : word bits) : word bits := |
| 77 |
if t then a else b. |
| 78 |
Definition odd {bits : Z} (a : word bits) : bool := Z.odd (to_Z a). |
| 79 |
Definition is_one {bits: Z} (a : word bits) : bool := to_Z a =? 1. |
| 80 |
|
| 81 |
|
| 82 |
(** These helpers are implemented with temporaries. They return the final |
| 83 |
values of the temporaries, to model clobbering them in imperative |
| 84 |
code. *) |
| 85 |
Definition maybe_rshift1_carry {bits : Z} |
| 86 |
(t carry : bool) (a : word bits) : word bits * word bits := |
| 87 |
let tmp := rshift1 a in |
| 88 |
let a := select t tmp a in |
| 89 |
let a := if t && carry then to_word ((to_Z a) + 2^(bits - 1)) |
| 90 |
else a in |
| 91 |
(a, tmp). |
| 92 |
|
| 93 |
Definition maybe_rshift1 {bits : Z} (t : bool) := |
| 94 |
@maybe_rshift1_carry bits t false. |
| 95 |
|
| 96 |
Definition maybe_add {bits : Z} |
| 97 |
(t : bool) (a b : word bits) : bool * word bits * word bits := |
| 98 |
let (carry, tmp) := add a b in |
| 99 |
let a := select t tmp a in |
| 100 |
(carry && t, a, tmp). |
| 101 |
|
| 102 |
(** We ultimately wish to compute [a] inverse mod [n]. Parameterize over |
| 103 |
both values' public widths. ([a]'s width may be smaller than [n]'s if it |
| 104 |
publicly-bounded. For instance, RSA key generation involves a public |
| 105 |
small [a], typically 65537, and a large secret modulus, the totient. *) |
| 106 |
Context {abits nbits : Z}. |
| 107 |
Context {abits_positive : 0 < abits}. |
| 108 |
Context {nbits_positive : 0 < nbits}. |
| 109 |
|
| 110 |
(** This is our loop state. *) |
| 111 |
Definition state : Set := |
| 112 |
word abits * word nbits * (* a, n *) |
| 113 |
word nbits * word abits * word nbits * word abits * (* A, B, C, D *) |
| 114 |
word nbits * word nbits (* u, v *). |
| 115 |
|
| 116 |
Definition step (s : state) : state := |
| 117 |
let '(a, n, A, B, C, D, u, v) := s in |
| 118 |
(* If both |u| and |v| are odd, subtract the smaller from the larger. *) |
| 119 |
let both_odd := odd u && odd v in |
| 120 |
let (v_less_than_u, tmp) := sub v u in |
| 121 |
let v := select (both_odd && negb v_less_than_u) tmp v in |
| 122 |
let (_, tmp) := sub u v in |
| 123 |
let u := select (both_odd && v_less_than_u) tmp u in |
| 124 |
|
| 125 |
(* If we updated one of the values, update the corresponding |
| 126 |
coefficient. *) |
| 127 |
let (carry, tmp) := add A C in |
| 128 |
let (borrow, tmp2) := sub tmp n in |
| 129 |
let borrow := borrow && negb carry in |
| 130 |
let tmp := select borrow tmp tmp2 in |
| 131 |
let A := select (both_odd && v_less_than_u) tmp A in |
| 132 |
let C := select (both_odd && negb v_less_than_u) tmp C in |
| 133 |
|
| 134 |
let (_, tmp) := add B D in |
| 135 |
let (_, tmp2) := sub tmp a in |
| 136 |
let tmp := select borrow tmp tmp2 in |
| 137 |
let B := select (both_odd && v_less_than_u) tmp B in |
| 138 |
let D := select (both_odd && negb v_less_than_u) tmp D in |
| 139 |
|
| 140 |
(* Our loop invariants hold at this point. Additionally, exactly one of |
| 141 |
[u] and [v] is now even. *) |
| 142 |
let u_even := negb (odd u) in |
| 143 |
let v_even := negb (odd v) in |
| 144 |
|
| 145 |
(* Halve the even one and adjust the corresponding coefficient. *) |
| 146 |
let (u, tmp) := maybe_rshift1 u_even u in |
| 147 |
let A_or_B_odd := odd A || odd B in |
| 148 |
let '(A_carry, A, tmp) := maybe_add (A_or_B_odd && u_even) A n in |
| 149 |
let '(B_carry, B, tmp) := maybe_add (A_or_B_odd && u_even) B a in |
| 150 |
let (A, tmp) := maybe_rshift1_carry u_even A_carry A in |
| 151 |
let (B, tmp) := maybe_rshift1_carry u_even B_carry B in |
| 152 |
|
| 153 |
let (v, tmp) := maybe_rshift1 v_even v in |
| 154 |
let C_or_D_odd := odd C || odd D in |
| 155 |
let '(C_carry, C, tmp) := maybe_add (C_or_D_odd && v_even) C n in |
| 156 |
let '(D_carry, D, tmp) := maybe_add (C_or_D_odd && v_even) D a in |
| 157 |
let (C, tmp) := maybe_rshift1_carry v_even C_carry C in |
| 158 |
let (D, tmp) := maybe_rshift1_carry v_even D_carry D in |
| 159 |
(a, n, A, B, C, D, u, v). |
| 160 |
|
| 161 |
Definition mod_inverse_consttime |
| 162 |
(a : word abits) (n : word nbits) : option (word nbits) := |
| 163 |
if negb (odd a) && negb (odd n) |
| 164 |
then None |
| 165 |
else |
| 166 |
let u := to_word (to_Z a) in |
| 167 |
let v := n in |
| 168 |
let A := to_word 1 in |
| 169 |
let B := to_word 0 in |
| 170 |
let C := to_word 0 in |
| 171 |
let D := to_word 1 in |
| 172 |
let i := 0 in |
| 173 |
let '(a, n, A, B, C, D, u, v, _) := |
| 174 |
(while (fun '(_, i) => i <? abits + nbits) |
| 175 |
(fun '(s, i) => (step s, i + 1)) |
| 176 |
(Z.to_nat (abits + nbits)) |
| 177 |
(a, n, A, B, C, D, u, v, i)) in |
| 178 |
if is_one u |
| 179 |
then Some A |
| 180 |
else None. |
| 181 |
|
| 182 |
|
| 183 |
(** Proofs *) |
| 184 |
|
| 185 |
(** To simplify analysis, we split each loop in the middle, where the loop |
| 186 |
invariant also holds. *) |
| 187 |
|
| 188 |
Definition odd_step (s : state) : state := |
| 189 |
let '(a, n, A, B, C, D, u, v) := s in |
| 190 |
|
| 191 |
(* If both |u| and |v| are odd, subtract the smaller from the larger. *) |
| 192 |
let both_odd := odd u && odd v in |
| 193 |
let (v_less_than_u, tmp) := sub v u in |
| 194 |
let v := select (both_odd && negb v_less_than_u) tmp v in |
| 195 |
let (_, tmp) := sub u v in |
| 196 |
let u := select (both_odd && v_less_than_u) tmp u in |
| 197 |
|
| 198 |
(* If we updated one of the values, update the corresponding |
| 199 |
coefficient. *) |
| 200 |
let (carry, tmp) := add A C in |
| 201 |
let (borrow, tmp2) := sub tmp n in |
| 202 |
let borrow := borrow && negb carry in |
| 203 |
let tmp := select borrow tmp tmp2 in |
| 204 |
let A := select (both_odd && v_less_than_u) tmp A in |
| 205 |
let C := select (both_odd && negb v_less_than_u) tmp C in |
| 206 |
|
| 207 |
let (_, tmp) := add B D in |
| 208 |
let (_, tmp2) := sub tmp a in |
| 209 |
let tmp := select borrow tmp tmp2 in |
| 210 |
let B := select (both_odd && v_less_than_u) tmp B in |
| 211 |
let D := select (both_odd && negb v_less_than_u) tmp D in |
| 212 |
(a, n, A, B, C, D, u, v). |
| 213 |
|
| 214 |
Definition even_step (s : state) : state := |
| 215 |
let '(a, n, A, B, C, D, u, v) := s in |
| 216 |
|
| 217 |
(* This step should be called after [odd_step], which ensures at least one |
| 218 |
of [u] and [v] are even. *) |
| 219 |
let u_even := negb (odd u) in |
| 220 |
let v_even := negb (odd v) in |
| 221 |
|
| 222 |
(* Halve the even one and adjust the corresponding coefficient. *) |
| 223 |
let (u, tmp) := maybe_rshift1 u_even u in |
| 224 |
let A_or_B_odd := odd A || odd B in |
| 225 |
let '(A_carry, A, tmp) := maybe_add (A_or_B_odd && u_even) A n in |
| 226 |
let '(B_carry, B, tmp) := maybe_add (A_or_B_odd && u_even) B a in |
| 227 |
let (A, tmp) := maybe_rshift1_carry u_even A_carry A in |
| 228 |
let (B, tmp) := maybe_rshift1_carry u_even B_carry B in |
| 229 |
|
| 230 |
let (v, tmp) := maybe_rshift1 v_even v in |
| 231 |
let C_or_D_odd := odd C || odd D in |
| 232 |
let '(C_carry, C, tmp) := maybe_add (C_or_D_odd && v_even) C n in |
| 233 |
let '(D_carry, D, tmp) := maybe_add (C_or_D_odd && v_even) D a in |
| 234 |
let (C, tmp) := maybe_rshift1_carry v_even C_carry C in |
| 235 |
let (D, tmp) := maybe_rshift1_carry v_even D_carry D in |
| 236 |
(a, n, A, B, C, D, u, v). |
| 237 |
|
| 238 |
Ltac destruct_state s := |
| 239 |
destruct s as [[[[[[[[a] [n]] [A]] [B]] [C]] [D]] [u]] [v]]. |
| 240 |
|
| 241 |
Lemma step_even_odd (s : state) : |
| 242 |
even_step (odd_step s) = step s. |
| 243 |
Proof. |
| 244 |
destruct_state s. reflexivity. Qed. |
| 245 |
|
| 246 |
(** Next, we establish a core GCD invariant that the loop will maintain. *) |
| 247 |
|
| 248 |
Definition invariant (s : state) := |
| 249 |
match s with |
| 250 |
| (Word a, Word n, Word A, Word B, Word C, Word D, Word u, Word v) => |
| 251 |
(* Bounds on all values. Note [B] and [D] have slightly looser bounds |
| 252 |
than [A] and [C]. *) |
| 253 |
0 < n < 2^nbits /\ |
| 254 |
0 < a < 2^abits /\ |
| 255 |
a < n /\ |
| 256 |
0 < u <= a /\ |
| 257 |
0 <= v <= n /\ |
| 258 |
0 <= A < n /\ |
| 259 |
0 <= B <= a /\ |
| 260 |
0 <= C < n /\ |
| 261 |
0 <= D <= a /\ |
| 262 |
(* The both even case is discarded early; [a] cannot have an inverse mod |
| 263 |
[n] if both are even. *) |
| 264 |
(Z.odd u = true \/ Z.odd v = true) /\ |
| 265 |
(Z.odd a = true \/ Z.odd n = true) /\ |
| 266 |
(* The extended GCD invariant. *) |
| 267 |
u = A*a - B*n /\ |
| 268 |
v = D*n - C*a /\ |
| 269 |
Z.gcd u v = Z.gcd a n |
| 270 |
end. |
| 271 |
|
| 272 |
Lemma if_func_distr (X Y : Type) (f : X -> Y) (t : bool) (a b : X) : |
| 273 |
(if t then f a else f b) = f (if t then a else b). |
| 274 |
Proof. destruct t; reflexivity. Qed. |
| 275 |
|
| 276 |
Lemma if_mod_distr (t : bool) (a b n : Z) : |
| 277 |
(if t then a mod n else b mod n) = (if t then a else b) mod n. |
| 278 |
Proof. destruct t; reflexivity. Qed. |
| 279 |
|
| 280 |
Ltac simplify_words := |
| 281 |
cbv [select odd add sub rshift1 maybe_add maybe_rshift1 |
| 282 |
maybe_rshift1_carry to_Z to_word wrap is_one]; |
| 283 |
try rewrite !if_func_distr. |
| 284 |
|
| 285 |
Ltac bool_to_prop_once := |
| 286 |
first [rewrite andb_true_iff in * | |
| 287 |
rewrite andb_false_iff in * | |
| 288 |
rewrite orb_true_iff in * | |
| 289 |
rewrite orb_false_iff in * | |
| 290 |
rewrite negb_true_iff in * | |
| 291 |
rewrite negb_false_iff in * | |
| 292 |
rewrite Z.eqb_eq in * | |
| 293 |
rewrite Z.eqb_neq in * | |
| 294 |
rewrite Z.ltb_lt in * | |
| 295 |
rewrite Z.ltb_ge in * | |
| 296 |
rewrite Z.leb_le in * | |
| 297 |
rewrite Z.leb_gt in *]. |
| 298 |
|
| 299 |
(** Consider all possible parities for [u] and [v], but discard the one where |
| 300 |
[u] and [v] are both even. *) |
| 301 |
Ltac destruct_odd_u_v u v := |
| 302 |
destruct (Z.odd u) eqn:Hu; |
| 303 |
destruct (Z.odd v) eqn:Hv; |
| 304 |
[ | | | |
| 305 |
(* [u] and [v] cannot both be even by [invariant]. *) |
| 306 |
destruct_head'_and; |
| 307 |
destruct_head'_or; |
| 308 |
match goal with |
| 309 |
| H : false = true |- _ => inversion H |
| 310 |
end ]. |
| 311 |
|
| 312 |
(** This is a roundabout way of saying [n < A + C] that comes out of [add] |
| 313 |
and [sub] in [odd_step]. *) |
| 314 |
Lemma add_then_sub_gt (bits n A C : Z) |
| 315 |
(Hn : 0 <= n < 2^bits) |
| 316 |
(HA : 0 <= A < 2^bits) |
| 317 |
(HC : 0 <= C < 2^bits) : |
| 318 |
((A + C) mod 2^bits <? n) && negb (2^bits <=? A + C) = (A + C <? n). |
| 319 |
Proof. |
| 320 |
destruct (A + C <? n) eqn:HACn; |
| 321 |
destruct (A + C <? 2^bits) eqn:HAC2bits; |
| 322 |
repeat first [bool_to_prop_once | split]; |
| 323 |
repeat match goal with |
| 324 |
| H : ?a < ?m |- context[?a mod ?m] => |
| 325 |
rewrite (Z.mod_small a m) by lia |
| 326 |
| _ => lia |
| 327 |
end. |
| 328 |
Qed. |
| 329 |
|
| 330 |
(** Prove the left version of [Z.gcd_sub_diag_r]. *) |
| 331 |
Lemma Zgcd_sub_diag_l (n m : Z) : Z.gcd (n - m) m = Z.gcd n m. |
| 332 |
Proof. |
| 333 |
rewrite (Z.gcd_comm (n - m) m). |
| 334 |
rewrite (Z.gcd_comm n m). |
| 335 |
apply Z.gcd_sub_diag_r. |
| 336 |
Qed. |
| 337 |
|
| 338 |
(** The following bounds on [B + D] come out of [nia], but [nia] takes a |
| 339 |
long time to discover this in the context of the full proof. *) |
| 340 |
Lemma AC_lt_BD_le (a n A B C D : Z) : |
| 341 |
0 <= a < n -> |
| 342 |
0 < A*a - B*n <= a -> |
| 343 |
0 <= D*n - C*a <= n -> |
| 344 |
A + C < n -> |
| 345 |
B + D <= a. |
| 346 |
Proof. nia. Qed. |
| 347 |
|
| 348 |
Lemma AC_ge_BD_ge (a n A B C D : Z) : |
| 349 |
0 <= a < n -> |
| 350 |
0 < A*a - B*n <= a -> |
| 351 |
0 <= D*n - C*a <= n -> |
| 352 |
n <= A + C -> |
| 353 |
a <= B + D. |
| 354 |
Proof. nia. Qed. |
| 355 |
|
| 356 |
Lemma odd_step_invariant |
| 357 |
(s : state) |
| 358 |
(H : invariant s) : |
| 359 |
invariant (odd_step s). |
| 360 |
Proof. |
| 361 |
destruct_state s. |
| 362 |
unfold odd_step, invariant in *. |
| 363 |
simplify_words. |
| 364 |
|
| 365 |
rewrite add_then_sub_gt by lia. |
| 366 |
|
| 367 |
(* Expand all cases and simplify. While doing so, help [lia] with bounds |
| 368 |
on [B + D] to go with the [A + C] bounds. *) |
| 369 |
destruct (A + C <? n) eqn:HACn; |
| 370 |
[ assert (B + D <= a) by (apply (AC_lt_BD_le a n A B C D); |
| 371 |
repeat bool_to_prop_once; lia) | |
| 372 |
assert (a <= B + D) by (apply (AC_ge_BD_ge a n A B C D); |
| 373 |
repeat bool_to_prop_once; lia) ]; |
| 374 |
destruct_odd_u_v u v; |
| 375 |
destruct (v <? u) eqn:Huv; |
| 376 |
cbn; |
| 377 |
|
| 378 |
(* Discard the cases where [odd_step] is a no-op. [Hu] and [Hv] must be |
| 379 |
re-applied due to some simplification. *) |
| 380 |
try rewrite Hu; |
| 381 |
try rewrite Hv; |
| 382 |
try apply H; |
| 383 |
|
| 384 |
destruct_head'_and; |
| 385 |
repeat match goal with |
| 386 |
| _ => bool_to_prop_once |
| 387 |
| _ => rewrite Zminus_mod_idemp_l |
| 388 |
(* Note this relies both on [destruct] and [cbn] above clearing |
| 389 |
all dead branches, as well as [Zminus_mod_idemp_l] above |
| 390 |
simplifying some expressions. Some operations intentionally |
| 391 |
overflow or underflow, but are either discarded by [select] or |
| 392 |
cancelled out by a later operation. *) |
| 393 |
| _ => rewrite Z.mod_small |
| 394 |
| _ => split |
| 395 |
| _ => apply Z.mod_pos_bound |
| 396 |
| _ => rewrite Zgcd_sub_diag_l |
| 397 |
| _ => rewrite Z.gcd_sub_diag_r |
| 398 |
| _ => progress auto |
| 399 |
| _ => lia |
| 400 |
end. |
| 401 |
Qed. |
| 402 |
|
| 403 |
(** [even_step] writes [(x + y) / 2] slightly funny. *) |
| 404 |
Lemma add_carry_div2 (bits x y : Z) |
| 405 |
(Hbits : 0 < bits) |
| 406 |
(Hx : 0 <= x < 2^bits) |
| 407 |
(Hy : 0 <= y < 2^bits) : |
| 408 |
(if 2^bits <=? x + y |
| 409 |
then ((x + y) mod 2^bits / 2 + 2^(bits-1)) |
| 410 |
else (x + y) mod 2^bits / 2) = (x + y) / 2. |
| 411 |
Proof. |
| 412 |
destruct (2^bits <=? x + y) eqn:Hxy; repeat bool_to_prop_once. |
| 413 |
{ (* 2^bits <= x + y *) |
| 414 |
rewrite Z.mod_eq by lia. |
| 415 |
rewrite (Div.Z.div_between_1 (x + y) (2^bits)), Z.mul_1_r by lia. |
| 416 |
replace (2^bits) with (2^(bits-1)*2) by (rewrite Z.mul_comm, <- Z.pow_succ_r; f_equal; lia). |
| 417 |
rewrite Div.Z.div_sub by lia. |
| 418 |
lia. } |
| 419 |
{ (* x + y < 2^bits *) |
| 420 |
rewrite Z.mod_small by lia. |
| 421 |
lia. } |
| 422 |
Qed. |
| 423 |
|
| 424 |
Lemma pos_even_le (z : Z) (Heven : Z.odd z = false) (Hpos : 0 < z) : |
| 425 |
2 <= z. |
| 426 |
Proof. |
| 427 |
destruct (z =? 1) eqn:H1; repeat bool_to_prop_once. |
| 428 |
{ rewrite H1 in Heven. |
| 429 |
cbn in Heven. |
| 430 |
inversion Heven. } |
| 431 |
{ lia. } |
| 432 |
Qed. |
| 433 |
|
| 434 |
Lemma gcd_div_rel_prime_l (a b c : Z) |
| 435 |
(Hc : 0 < c) |
| 436 |
(Hdiv : (c | a)) |
| 437 |
(Hrel : rel_prime b c) : |
| 438 |
Z.gcd (a / c) b = Z.gcd a b. |
| 439 |
Proof. |
| 440 |
symmetry. |
| 441 |
apply Z.gcd_unique. |
| 442 |
- apply Z.gcd_nonneg. |
| 443 |
- apply (Z.divide_transitive (Z.gcd (a / c) b) (a / c) a). |
| 444 |
+ apply Z.gcd_divide_l. |
| 445 |
+ exists c; apply Zdivide_Zdiv_eq; trivial. |
| 446 |
- apply Z.gcd_divide_r. |
| 447 |
- intros q Hqa Hqb. |
| 448 |
destruct Hdiv as [x Hx]. |
| 449 |
rewrite Hx in *. |
| 450 |
rewrite Z_div_mult by lia. |
| 451 |
apply Z.gcd_greatest; |
| 452 |
try apply (Gauss q c x); |
| 453 |
try apply (rel_prime_div b c q); |
| 454 |
try rewrite Z.mul_comm; |
| 455 |
trivial. |
| 456 |
Qed. |
| 457 |
|
| 458 |
Lemma gcd_div_rel_prime_r (a b c : Z) |
| 459 |
(Hc : 0 < c) |
| 460 |
(Hrel : rel_prime a c) |
| 461 |
(Hdiv : (c | b)) : |
| 462 |
Z.gcd a (b / c) = Z.gcd a b. |
| 463 |
Proof. |
| 464 |
rewrite (Z.gcd_comm a (b/c)). |
| 465 |
rewrite (Z.gcd_comm a b). |
| 466 |
apply gcd_div_rel_prime_l; trivial. |
| 467 |
Qed. |
| 468 |
|
| 469 |
(** Some easy lemmas bridging [Z.odd] with [Z.modulo], which |
| 470 |
[Z.div_mod_to_quot_rem] can handle. *) |
| 471 |
Lemma odd_mod_2 : forall z : Z, |
| 472 |
Z.odd z = true <-> z mod 2 = 1. |
| 473 |
Proof. |
| 474 |
intros z; split; intros H. |
| 475 |
- rewrite Zmod_odd; rewrite H; reflexivity. |
| 476 |
- rewrite Zmod_odd in H; |
| 477 |
destruct (Z.odd z); try trivial; lia. |
| 478 |
Qed. |
| 479 |
Lemma even_mod_2 : forall z : Z, |
| 480 |
Z.odd z = false <-> z mod 2 = 0. |
| 481 |
Proof. |
| 482 |
intros z; split; intros H. |
| 483 |
- rewrite Zmod_odd; rewrite H; reflexivity. |
| 484 |
- rewrite Zmod_odd in H; |
| 485 |
destruct (Z.odd z); try trivial; lia. |
| 486 |
Qed. |
| 487 |
|
| 488 |
Ltac fail_if_mod t m := match t with |
| 489 |
| _ mod m => fail 1 |
| 490 |
| _ => idtac |
| 491 |
end. |
| 492 |
|
| 493 |
Ltac push_mod_down_in t m := |
| 494 |
match t with |
| 495 |
| context[(?x - ?y) mod m] => |
| 496 |
first [fail_if_mod x m; rewrite (Z.sub_mod_l x y m) in * | |
| 497 |
fail_if_mod y m; rewrite (Z.sub_mod_r x y m) in *] |
| 498 |
| context[(?x + ?y) mod m] => |
| 499 |
first [fail_if_mod x m; rewrite (Z.add_mod_l x y m) in * | |
| 500 |
fail_if_mod y m; rewrite (Z.add_mod_r x y m) in *] |
| 501 |
| context[(?x * ?y) mod m] => |
| 502 |
first [fail_if_mod x m; rewrite (Z.mul_mod_l x y m) in * | |
| 503 |
fail_if_mod y m; rewrite (Z.mul_mod_r x y m) in *] |
| 504 |
end. |
| 505 |
|
| 506 |
Ltac brute_force_mod_2 := |
| 507 |
solve [ |
| 508 |
(* The easy cases are solved immediately. *) |
| 509 |
auto | |
| 510 |
(* Otherwise, brute force all possible even/odd assignments. Rewrite |
| 511 |
all hypotheses, variables, and goals as equations mod 2. *) |
| 512 |
repeat match goal with |
| 513 |
| H : @eq Z ?a ?b |- _ => |
| 514 |
let H' := fresh in |
| 515 |
fail_if_mod a 2; |
| 516 |
fail_if_mod b 2; |
| 517 |
assert (H' : a mod 2 = b mod 2) by (rewrite H; reflexivity); |
| 518 |
clear H |
| 519 |
| _ : ?H |- _ => push_mod_down_in H 2 |
| 520 |
| [ |- ?G ] => push_mod_down_in G 2 |
| 521 |
end; |
| 522 |
(* Apply any existing hypotheses. *) |
| 523 |
repeat match goal with |
| 524 |
| z : Z |- _ => |
| 525 |
match goal with |
| 526 |
| H : z mod 2 = _ |- _ => |
| 527 |
rewrite H in * |
| 528 |
end |
| 529 |
end; |
| 530 |
(* For each [Z], check both possible parities. *) |
| 531 |
repeat match goal with |
| 532 |
(* Try to solve it right now. *) |
| 533 |
| H : false = true \/ false = true |- _ => |
| 534 |
(* [lia] doesn't figure this one out. *) |
| 535 |
destruct H as [H' | H']; inversion H' |
| 536 |
| _ => progress cbn in * |
| 537 |
| _ => lia |
| 538 |
(* Pick a variable to branch on. *) |
| 539 |
| z : Z |- _ => |
| 540 |
match z with |
| 541 |
| nbits => fail 1 | abits => fail 1 |
| 542 |
| _ => |
| 543 |
match goal with |
| 544 |
| H : z mod 2 = _ |- _ => fail 2 |
| 545 |
| _ => |
| 546 |
let Hz := fresh in |
| 547 |
destruct (Z.odd z) eqn:Hz; |
| 548 |
[ apply odd_mod_2 in Hz | apply even_mod_2 in Hz]; |
| 549 |
try rewrite Hz in * |
| 550 |
end |
| 551 |
end |
| 552 |
end |
| 553 |
]. |
| 554 |
|
| 555 |
Lemma even_step_invariant |
| 556 |
(s : state) |
| 557 |
(H : invariant s) : |
| 558 |
invariant (even_step s). |
| 559 |
Proof. |
| 560 |
destruct_state s. |
| 561 |
unfold even_step, invariant in *. |
| 562 |
simplify_words. |
| 563 |
|
| 564 |
destruct_odd_u_v u v; |
| 565 |
[ (* The odd/odd case is a no-op. *) |
| 566 |
rewrite !andb_false_r; cbn; rewrite Hu; rewrite Hv; |
| 567 |
apply H |
| 568 |
| destruct (Z.odd C) eqn:HC; |
| 569 |
destruct (Z.odd D) eqn:HD |
| 570 |
| destruct (Z.odd A) eqn:HA; |
| 571 |
destruct (Z.odd B) eqn:HB ]; |
| 572 |
try rewrite !andb_false_r; |
| 573 |
try rewrite !andb_true_r; |
| 574 |
cbn; |
| 575 |
|
| 576 |
destruct_head'_and; |
| 577 |
|
| 578 |
repeat match goal with |
| 579 |
| _ => rewrite Zplus_mod_idemp_l |
| 580 |
| _ => rewrite if_mod_distr |
| 581 |
| _ => rewrite add_carry_div2 |
| 582 |
end; |
| 583 |
|
| 584 |
repeat split; |
| 585 |
(* Keep only the relevant half of the problem in the context. *) |
| 586 |
match goal with |
| 587 |
| [ |- context[u]] => idtac |
| 588 |
| [ |- context[A]] => idtac |
| 589 |
| [ |- context[B]] => idtac |
| 590 |
| _ => clear dependent u; clear dependent A; clear dependent B |
| 591 |
end; |
| 592 |
match goal with |
| 593 |
| [ |- context[v]] => idtac |
| 594 |
| [ |- context[C]] => idtac |
| 595 |
| [ |- context[D]] => idtac |
| 596 |
| _ => clear dependent v; clear dependent C; clear dependent D |
| 597 |
end; |
| 598 |
|
| 599 |
repeat match goal with |
| 600 |
| [ |- _ /\ _ ] => split |
| 601 |
| _ => rewrite Z.mod_small |
| 602 |
| _ => apply Z.div_pos |
| 603 |
| _ => apply Z.div_str_pos |
| 604 |
| _ => apply Z.div_le_upper_bound |
| 605 |
| _ => apply Z.div_lt_upper_bound |
| 606 |
| _ => apply pos_even_le |
| 607 |
| [ |- context[?a / 2 * ?b]] => rewrite (Z.mul_comm (a/2) b) |
| 608 |
| _ => rewrite <- Z.divide_div_mul_exact |
| 609 |
| _ => progress auto |
| 610 |
| H : Z.odd _ = true |- _ => apply odd_mod_2 in H |
| 611 |
| H : Z.odd _ = false |- _ => apply even_mod_2 in H |
| 612 |
| [ |- (2 | _) ] => |
| 613 |
rewrite <- Z.mod_divide by lia; brute_force_mod_2 |
| 614 |
| [ |- ~ (2 | _) ] => |
| 615 |
let H := fresh in |
| 616 |
rewrite <- Z.mod_divide by lia; |
| 617 |
intros H; brute_force_mod_2 |
| 618 |
| _ => rewrite gcd_div_rel_prime_l |
| 619 |
| _ => rewrite gcd_div_rel_prime_r |
| 620 |
| [ |- rel_prime _ 2 ] => apply rel_prime_sym |
| 621 |
| [ |- rel_prime 2 _ ] => apply prime_rel_prime |
| 622 |
| _ => apply prime_2 |
| 623 |
| _ => progress Z.div_mod_to_quot_rem |
| 624 |
| _ => lia |
| 625 |
end. |
| 626 |
Qed. |
| 627 |
|
| 628 |
Lemma step_invariant |
| 629 |
(s : state) |
| 630 |
(H : invariant s) : |
| 631 |
invariant (step s). |
| 632 |
Proof. |
| 633 |
rewrite <- step_even_odd. |
| 634 |
apply even_step_invariant. |
| 635 |
apply odd_step_invariant. |
| 636 |
trivial. |
| 637 |
Qed. |
| 638 |
|
| 639 |
(** Next, we show that [step] causes [u * v] to decrease by two each step. |
| 640 |
That means that, after enough steps, the product must be zero. *) |
| 641 |
|
| 642 |
Definition u_v_prod (s : state) : Z := |
| 643 |
match s with |
| 644 |
| (_, Word u, Word v) => u * v |
| 645 |
end. |
| 646 |
|
| 647 |
Lemma odd_step_non_increasing (s : state) (H : invariant s) : |
| 648 |
u_v_prod (odd_step s) <= u_v_prod s. |
| 649 |
Proof. |
| 650 |
destruct_state s. |
| 651 |
unfold odd_step, u_v_prod, invariant in *. |
| 652 |
simplify_words. |
| 653 |
|
| 654 |
destruct_odd_u_v u v; |
| 655 |
destruct (v <? u) eqn:Huv; |
| 656 |
repeat bool_to_prop_once; |
| 657 |
cbn; |
| 658 |
repeat match goal with |
| 659 |
| _ => rewrite Z.mod_small |
| 660 |
| _ => nia |
| 661 |
end. |
| 662 |
Qed. |
| 663 |
|
| 664 |
Definition u_or_v_even (s : state) : Prop := |
| 665 |
match s with |
| 666 |
| (_, Word u, Word v) => |
| 667 |
Z.odd u = false \/ Z.odd v = false |
| 668 |
end. |
| 669 |
|
| 670 |
Lemma odd_step_u_or_v_even (s : state) (H : invariant s) : |
| 671 |
u_or_v_even (odd_step s). |
| 672 |
Proof. |
| 673 |
destruct_state s. |
| 674 |
unfold odd_step, invariant, u_or_v_even in *. |
| 675 |
simplify_words. |
| 676 |
|
| 677 |
(* Throw away unnecessary bits. *) |
| 678 |
destruct_head'_and. |
| 679 |
clear dependent a; clear dependent n; clear dependent A; |
| 680 |
clear dependent B; clear dependent C; clear dependent D. |
| 681 |
|
| 682 |
destruct_odd_u_v u v; |
| 683 |
destruct (v <? u) eqn:Huv; |
| 684 |
repeat bool_to_prop_once; |
| 685 |
cbn; |
| 686 |
try rewrite !even_mod_2 in *; |
| 687 |
try rewrite !odd_mod_2 in *; |
| 688 |
try rewrite <- !Zmod_div_mod by (try apply Z.pow_pos_nonneg; |
| 689 |
try apply Zpow_facts.Zpower_divide; |
| 690 |
lia); |
| 691 |
(* Pick the subtraction, if there is one. Otherwise [auto] will figure |
| 692 |
it out. *) |
| 693 |
try match goal with |
| 694 |
| [ |- (_ - _) mod 2 = 0 \/ _ ] => left |
| 695 |
| [ |- _ \/ (_ - _) mod 2 = 0 ] => right |
| 696 |
end; |
| 697 |
brute_force_mod_2. |
| 698 |
Qed. |
| 699 |
|
| 700 |
Lemma even_step_shrinks |
| 701 |
(s : state) |
| 702 |
(H : invariant s) |
| 703 |
(Heven : u_or_v_even s) : |
| 704 |
u_v_prod (even_step s) <= (u_v_prod s) / 2. |
| 705 |
Proof. |
| 706 |
destruct_state s. |
| 707 |
unfold even_step, u_v_prod, invariant, u_or_v_even in *. |
| 708 |
simplify_words. |
| 709 |
|
| 710 |
destruct_odd_u_v u v; |
| 711 |
[ (* [u] and [v] cannot both be even by [Heven]. *) |
| 712 |
destruct_head'_and; |
| 713 |
destruct_head'_or; |
| 714 |
match goal with |
| 715 |
| H : false = true |- _ => inversion H |
| 716 |
| H : true = false |- _ => inversion H |
| 717 |
end | | ]; |
| 718 |
cbn; |
| 719 |
repeat match goal with |
| 720 |
| H : Z.odd _ = true |- _ => apply odd_mod_2 in H |
| 721 |
| H : Z.odd _ = false |- _ => apply even_mod_2 in H |
| 722 |
| _ => split |
| 723 |
| _ => rewrite Z.mod_small |
| 724 |
| _ => Z.div_mod_to_quot_rem; nia |
| 725 |
end. |
| 726 |
Qed. |
| 727 |
|
| 728 |
Lemma step_shrinks (s : state) (H : invariant s) : |
| 729 |
u_v_prod (step s) <= (u_v_prod s) / 2. |
| 730 |
Proof. |
| 731 |
repeat first [ rewrite <- step_even_odd |
| 732 |
| rewrite even_step_shrinks |
| 733 |
| apply Z.div_le_mono |
| 734 |
| apply odd_step_non_increasing |
| 735 |
| apply odd_step_invariant |
| 736 |
| apply odd_step_u_or_v_even |
| 737 |
| solve [trivial | lia]]. |
| 738 |
Qed. |
| 739 |
|
| 740 |
(** Help the main proof learn that [step] leaves [a] and [n] unchanged. *) |
| 741 |
Definition state_get_a (s : state) := |
| 742 |
match s with |
| 743 |
| (Word a, _, _, _, _, _, _, _) => a |
| 744 |
end. |
| 745 |
Definition state_get_n (s : state) := |
| 746 |
match s with |
| 747 |
| (_, Word n, _, _, _, _, _, _) => n |
| 748 |
end. |
| 749 |
|
| 750 |
Lemma step_preserve_a_n (s : state) : |
| 751 |
state_get_a (step s) = state_get_a s /\ |
| 752 |
state_get_n (step s) = state_get_n s. |
| 753 |
Proof. |
| 754 |
destruct_state s. |
| 755 |
unfold step, state_get_a, state_get_n. |
| 756 |
simplify_words. |
| 757 |
auto. |
| 758 |
Qed. |
| 759 |
|
| 760 |
Lemma gcd_even_even (a b : Z) |
| 761 |
(Ha : Z.odd a = false) |
| 762 |
(Hb : Z.odd b = false) : |
| 763 |
Z.gcd a b <> 1. |
| 764 |
Proof. |
| 765 |
intros Hgcd. |
| 766 |
rewrite even_mod_2 in Ha. |
| 767 |
rewrite even_mod_2 in Hb. |
| 768 |
rewrite Z.mod_divide in Ha by lia. |
| 769 |
rewrite Z.mod_divide in Hb by lia. |
| 770 |
pose proof (Z.gcd_greatest a b 2 Ha Hb) as H. |
| 771 |
rewrite Hgcd in H. |
| 772 |
apply Z.divide_1_r in H. |
| 773 |
lia. |
| 774 |
Qed. |
| 775 |
|
| 776 |
(** Finally, our correctness result. If [mod_inverse_consttime] returns |
| 777 |
something, it must be a modular inverse. If it does not, the inverse did |
| 778 |
not exist. |
| 779 |
|
| 780 |
Note the proof doesn't hold for [a = 0] or [n = 0], which should be |
| 781 |
handled by the caller. *) |
| 782 |
Theorem mod_inverse_consttime_spec |
| 783 |
(a n : Z) |
| 784 |
(Ha : 0 < a < 2^abits) |
| 785 |
(Hn : 0 < n < 2^nbits) |
| 786 |
(Hreduced : a < n) : |
| 787 |
match mod_inverse_consttime (to_word a) (to_word n) with |
| 788 |
| Some (Word ainv) => |
| 789 |
0 <= ainv < n /\ |
| 790 |
(ainv * a) mod n = 1 |
| 791 |
| None => Z.gcd a n <> 1 |
| 792 |
end. |
| 793 |
Proof. |
| 794 |
(* Tidy up the loop. *) |
| 795 |
unfold mod_inverse_consttime. |
| 796 |
simplify_words. |
| 797 |
rewrite !Z.mod_small by |
| 798 |
(repeat first [split | apply Z.pow_gt_1 | lia]). |
| 799 |
|
| 800 |
(* Discard the both even case. *) |
| 801 |
destruct (negb (Z.odd a) && negb (Z.odd n)) eqn:both_even; |
| 802 |
repeat bool_to_prop_once; |
| 803 |
[ destruct_head'_and; apply gcd_even_even; trivial | ]. |
| 804 |
|
| 805 |
lazymatch goal with |- context [while ?t ?b ?l ?i] => pattern (while t b l i) end. |
| 806 |
remember ((fun '(_,i) => Z.to_nat (abits + nbits - i)) : state * Z -> nat) as measure. |
| 807 |
lazymatch goal with |- ?P (while ?t ?b ?l ?s) => |
| 808 |
replace l with (measure s) |
| 809 |
end. |
| 810 |
eapply (while.by_invariant |
| 811 |
(fun '(s, i) => |
| 812 |
invariant s /\ |
| 813 |
u_v_prod s <= a * n / 2^i /\ |
| 814 |
0 <= i /\ |
| 815 |
state_get_a s = a /\ |
| 816 |
state_get_n s = n)); subst measure. |
| 817 |
{ (* Invariant holds in the beginning. *) |
| 818 |
unfold invariant, u_v_prod. |
| 819 |
repeat split; try rewrite Z.div_1_r; try solve [trivial | lia]. } |
| 820 |
{ intros [s i]. |
| 821 |
intros [Hinv [Hprod [Hi [Hpreserve_a Hpreserve_n]]]]. |
| 822 |
destruct (i <? abits + nbits) eqn:Hbranch; repeat bool_to_prop_once. |
| 823 |
{ (* If the loop continued, the invariant is preserved. *) |
| 824 |
destruct_head'_and. |
| 825 |
repeat split. |
| 826 |
- apply step_invariant; trivial. |
| 827 |
- rewrite step_shrinks by trivial. |
| 828 |
rewrite Z.pow_add_r by lia. |
| 829 |
rewrite Z.pow_1_r. |
| 830 |
rewrite <- Z.div_div by (try apply Z.pow_nonzero; lia). |
| 831 |
apply Z.div_le_mono; lia. |
| 832 |
- lia. |
| 833 |
- rewrite <- Hpreserve_a; apply step_preserve_a_n. |
| 834 |
- rewrite <- Hpreserve_n; apply step_preserve_a_n. |
| 835 |
- lia. } |
| 836 |
{ (* If the loop exited, the invariant implies the postcondition. *) |
| 837 |
destruct s as [[[[[[[[a'] [n']] [A]] [B]] [C]] [D]] [u]] [v]]. |
| 838 |
unfold invariant, state_get_a, state_get_n, u_v_prod in *. |
| 839 |
rewrite Hpreserve_a in *; rewrite Hpreserve_n in *. |
| 840 |
clear dependent a'; clear dependent n'. |
| 841 |
|
| 842 |
destruct_head'_and. |
| 843 |
clear dependent C; clear dependent D. |
| 844 |
|
| 845 |
(* First, show [u = Z.gcd a n], by way of [v = 0]. *) |
| 846 |
assert (Hzero : u * v = 0). |
| 847 |
{ |
| 848 |
assert (a * n / 2^i = 0). |
| 849 |
{ apply (Z.pow_le_mono_r 2) in Hbranch; try lia. |
| 850 |
rewrite Z.pow_add_r in Hbranch by lia. |
| 851 |
apply Z.div_small; nia. } |
| 852 |
nia. } |
| 853 |
apply Z.mul_eq_0 in Hzero. |
| 854 |
destruct Hzero as [Hu | Hv]; try lia. |
| 855 |
rewrite Hv in *; clear dependent v. |
| 856 |
rewrite Z.gcd_0_r_nonneg in * by lia. |
| 857 |
match goal with |
| 858 |
| H : u = Z.gcd a n |- _ => rewrite H in *; clear dependent u |
| 859 |
end. |
| 860 |
|
| 861 |
destruct (Z.gcd a n =? 1) eqn:Hgcd; repeat bool_to_prop_once. |
| 862 |
{ (* There is a mod inverse *) |
| 863 |
rewrite Hgcd in *. |
| 864 |
split; try lia. |
| 865 |
rewrite <- (Z.mod_add (A*a) (-B) n) by lia. |
| 866 |
rewrite <- (Z.mod_1_l n) by lia. |
| 867 |
f_equal; nia. } |
| 868 |
{ (* No mod inverse *) trivial. } } } |
| 869 |
{ (* fuel <= measure *) |
| 870 |
subst; lia. } |
| 871 |
Qed. |
| 872 |
|
| 873 |
End BinaryExtendedGCD. |
| 874 |
End BinaryExtendedGCD. |