| 1 |
Require Import Crypto.Util.Notations. |
| 2 |
Require Import Crypto.Util.ZUtil.Hints.ZArith. |
| 3 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 4 |
From Coq Require Import ZArith. |
| 5 |
From Coq Require Import Lia. |
| 6 |
|
| 7 |
Open Scope Z_scope. |
| 8 |
|
| 9 |
|
| 10 |
(** This file proves an implementation of the variant of Barrett reduction |
| 11 |
described in |
| 12 |
http://www.ridiculousfish.com/blog/posts/labor-of-division-episode-i.html *) |
| 13 |
|
| 14 |
|
| 15 |
(** To simulate C behavior with fixed-width words, we introduce a word data type |
| 16 |
and associated operations that automatically take a modulus. We'll then |
| 17 |
prove the moduli are all no-ops (save one which is a subtraction). |
| 18 |
|
| 19 |
TODO(davidben): This does not perfectly match C's behavior. In C, operations |
| 20 |
on types smaller than [int], notably [uint16_t], promote both types to [int] |
| 21 |
first. Assuming a standard 32-bit [int], multiplying two [uint32_t]s is |
| 22 |
always defined, but multiplying two [uint16_t]s may not be! However, we |
| 23 |
never multiply [uint16_t]s, and we would otherwise need an unreasonably |
| 24 |
large expression to overflow [int] by way of promoted [uint16_t] |
| 25 |
additions. *) |
| 26 |
|
| 27 |
(** First, the C data types will ultimately compile down to a bunch of extra |
| 28 |
modulus operations, which we must remove. Define a primitive [wrap] |
| 29 |
operation operations which we'll instruct [cbn] to leave alone and match on |
| 30 |
in the proof script. *) |
| 31 |
Definition wrap (bits val : Z) : Z := val mod 2^bits. |
| 32 |
|
| 33 |
(** A [word] is a C data type with a specified bit size. *) |
| 34 |
Inductive word (bits : Z) := |
| 35 |
| Word (v : Z). |
| 36 |
|
| 37 |
Definition to_Z {bits : Z} (a : word bits) : Z := |
| 38 |
match a with Word v => v end. |
| 39 |
Definition to_word {bits : Z} (val : Z) := |
| 40 |
Word bits (wrap bits val). |
| 41 |
|
| 42 |
Definition word_binop |
| 43 |
(aw bw : Z) |
| 44 |
(op : Z -> Z -> Z) |
| 45 |
(a : word aw) (b : word bw) : word (Z.max aw bw) := |
| 46 |
to_word (op (to_Z a) (to_Z b)). |
| 47 |
|
| 48 |
Definition word_add {aw bw : Z} := word_binop aw bw Z.add. |
| 49 |
Definition word_sub {aw bw : Z} := word_binop aw bw Z.sub. |
| 50 |
Definition word_mul {aw bw : Z} := word_binop aw bw Z.mul. |
| 51 |
Definition word_div {aw bw : Z} := word_binop aw bw Z.div. |
| 52 |
|
| 53 |
(** The type of a shift expression in C is the type of the first argument, not |
| 54 |
the larger of the two type. *) |
| 55 |
Definition word_shiftl {aw bw : Z} (a : word aw) (b : word bw) : word aw := |
| 56 |
to_word (Z.shiftl (to_Z a) (to_Z b)). |
| 57 |
Definition word_shiftr {aw bw : Z} (a : word aw) (b : word bw) : word aw := |
| 58 |
to_word (Z.shiftr (to_Z a) (to_Z b)). |
| 59 |
|
| 60 |
Definition word_cast (from to : Z) (a : word from) : word to := |
| 61 |
to_word (to_Z a). |
| 62 |
|
| 63 |
Definition u16 := word 16. |
| 64 |
Definition u32 := word 32. |
| 65 |
Definition u64 := word 64. |
| 66 |
|
| 67 |
Definition to_u16 {bits : Z} := word_cast bits 16. |
| 68 |
Definition to_u32 {bits : Z} := word_cast bits 32. |
| 69 |
Definition to_u64 {bits : Z} := word_cast bits 64. |
| 70 |
|
| 71 |
Definition Z_to_u16 := @to_word 16. |
| 72 |
Definition Z_to_u32 := @to_word 32. |
| 73 |
Definition Z_to_u64 := @to_word 64. |
| 74 |
|
| 75 |
(** [to_u32'] is identical to [to_u32], but it is expected to be a subtraction |
| 76 |
of 2^32, rather than a no-op. *) |
| 77 |
Definition to_u32' {bits : Z} (a : word bits) : u32 := |
| 78 |
Z_to_u32 ((to_Z a) - 2^32). |
| 79 |
|
| 80 |
Declare Scope word_scope. |
| 81 |
Delimit Scope word_scope with word. |
| 82 |
Bind Scope word_scope with word. |
| 83 |
Infix "+" := word_add : word_scope. |
| 84 |
Infix "-" := word_sub : word_scope. |
| 85 |
Infix "*" := word_mul : word_scope. |
| 86 |
Infix "/" := word_div : word_scope. |
| 87 |
Infix "<<" := word_shiftl : word_scope. |
| 88 |
Infix ">>" := word_shiftr : word_scope. |
| 89 |
Notation "1" := (Z_to_u32 1) : word_scope. |
| 90 |
Notation "32" := (Z_to_u32 32) : word_scope. |
| 91 |
|
| 92 |
|
| 93 |
Definition num_bits (a : Z) : Z := |
| 94 |
match a with |
| 95 |
| Z0 => 0 |
| 96 |
| Zpos p => Zpos (Pos.size p) |
| 97 |
| Zneg _ => 0 (* A [u32] is always non-negative, so this does not come up. *) |
| 98 |
end. |
| 99 |
|
| 100 |
Definition BN_num_bits_word (a : u32) : u32 := |
| 101 |
Z_to_u32 (num_bits (to_Z a)). |
| 102 |
|
| 103 |
(** See [mod_u16] in |
| 104 |
https://boringssl-review.googlesource.com/#/c/boringssl/+/25887. *) |
| 105 |
Definition mod_u16 (n : u32) (d : u16) : u16 := |
| 106 |
let p := to_u32 (BN_num_bits_word (to_u32 (d - 1))) in |
| 107 |
let m := to_u32' ((((to_u64 1) << (32 + p)) + d - 1) / d) in |
| 108 |
let q := to_u32 (((to_u64 m) * n) >> 32) in |
| 109 |
let t := to_u32 (((n - q) >> 1) + q) in |
| 110 |
let t := to_u32 (t >> (p - 1)) in |
| 111 |
let n := to_u32 (n - d * t) in |
| 112 |
to_u16 n. |
| 113 |
|
| 114 |
|
| 115 |
(** Proofs *) |
| 116 |
|
| 117 |
Lemma num_bits_log2_up (a : Z) : |
| 118 |
num_bits (a - 1) = Z.log2_up a. |
| 119 |
Proof. |
| 120 |
unfold Z.log2_up, Z.log2, num_bits. |
| 121 |
destruct (1 ?= a) eqn:H. |
| 122 |
{ (* [1 = a] *) |
| 123 |
rewrite Z.compare_eq_iff in H. |
| 124 |
rewrite <- H. |
| 125 |
reflexivity. } |
| 126 |
{ (* [1 < a] *) |
| 127 |
rewrite Z.compare_lt_iff in H. |
| 128 |
assert (0 < a - 1) by lia. |
| 129 |
rewrite Z.sub_1_r in *. |
| 130 |
destruct (Z.pred a); try lia. |
| 131 |
destruct p; cbn; lia. } |
| 132 |
{ (* [1 > a] *) |
| 133 |
rewrite Z.compare_gt_iff in H. |
| 134 |
assert (a - 1 < 0) by lia. |
| 135 |
destruct (a - 1); lia. } |
| 136 |
Qed. |
| 137 |
|
| 138 |
Lemma wrap_bound (a b : Z) (H : 0 < a) : |
| 139 |
0 <= wrap a b < 2^a. |
| 140 |
Proof. |
| 141 |
unfold wrap. |
| 142 |
Z.div_mod_to_quot_rem_in_goal. |
| 143 |
lia. |
| 144 |
Qed. |
| 145 |
|
| 146 |
Lemma remove_outer_wrap (a a' b : Z) (H : 0 < a' <= a) : |
| 147 |
wrap a (wrap a' b) = wrap a' b. |
| 148 |
Proof. |
| 149 |
unfold wrap. |
| 150 |
apply Z.mod_small. |
| 151 |
Z.div_mod_to_quot_rem_in_goal. |
| 152 |
assert (2^a' <= 2^a) by (apply Z.pow_le_mono_r; lia). |
| 153 |
lia. |
| 154 |
Qed. |
| 155 |
|
| 156 |
Lemma divide_pow (a b c : Z) (Ha : 0 < a) (Hb : 0 <= b <= c) : |
| 157 |
(a^b | a^c). |
| 158 |
Proof. |
| 159 |
exists (a^(c - b)). |
| 160 |
rewrite <- Z.pow_add_r; try f_equal; lia. |
| 161 |
Qed. |
| 162 |
|
| 163 |
Lemma remove_inner_wrap (a a' b : Z) (H : 0 < a <= a') : |
| 164 |
wrap a (wrap a' b) = wrap a b. |
| 165 |
Proof. |
| 166 |
unfold wrap. |
| 167 |
rewrite (Znumtheory.Zmod_div_mod (2^a) (2^a') b); |
| 168 |
try apply Z.pow_pos_nonneg; |
| 169 |
try apply divide_pow; |
| 170 |
lia. |
| 171 |
Qed. |
| 172 |
|
| 173 |
Lemma double_wrap (a a' b : Z) (Ha : 0 < a) (Ha' : 0 < a') : |
| 174 |
wrap a (wrap a' b) = wrap (Z.min a a') b. |
| 175 |
Proof. |
| 176 |
destruct (a <? a') eqn:H. |
| 177 |
{ apply Z.ltb_lt in H. |
| 178 |
rewrite Z.min_l by lia. |
| 179 |
apply remove_inner_wrap. |
| 180 |
lia. } |
| 181 |
{ apply Z.ltb_ge in H. |
| 182 |
rewrite Z.min_r by lia. |
| 183 |
apply remove_outer_wrap. |
| 184 |
lia. } |
| 185 |
Qed. |
| 186 |
|
| 187 |
Lemma a_minus_b_div_2_plus_b (a b : Z) (H : a >= b) : |
| 188 |
(a - b) / 2 + b = (a + b) / 2. |
| 189 |
Proof. Z.div_mod_to_quot_rem_in_goal; lia. Qed. |
| 190 |
|
| 191 |
Lemma p_bound (d bits : Z) (Hbits : 0 < bits) (H : 1 < d < 2^bits) : |
| 192 |
0 < Z.log2_up d <= bits. |
| 193 |
Proof. |
| 194 |
split. |
| 195 |
- apply Z.log2_up_pos; lia. |
| 196 |
- apply Z.log2_up_le_pow2; lia. |
| 197 |
Qed. |
| 198 |
|
| 199 |
Lemma pow2_log2_up_bounds (d : Z) (H : 1 < d) : |
| 200 |
d <= 2^(Z.log2_up d) < d + d. |
| 201 |
Proof. |
| 202 |
pose proof (Z.log2_up_spec d H). |
| 203 |
assert (2 ^ Z.log2_up d = 2 * (2 ^ Z.pred (Z.log2_up d))). |
| 204 |
{ rewrite <- Z.pow_succ_r by (apply Z.lt_le_pred; apply Z.log2_up_pos; lia). |
| 205 |
f_equal. |
| 206 |
lia. } |
| 207 |
lia. |
| 208 |
Qed. |
| 209 |
|
| 210 |
Lemma m_bound (d bits : Z) (Hbits : 0 < bits) (H : 1 < d < 2^bits) : |
| 211 |
2^bits <= (2^(bits + Z.log2_up d) + d - 1) / d < 2 * 2^bits. |
| 212 |
Proof. |
| 213 |
rewrite !Z.pow_add_r by (auto with zarith). |
| 214 |
pose proof (pow2_log2_up_bounds d (proj1 H)). |
| 215 |
split. |
| 216 |
- apply Z.div_le_lower_bound; nia. |
| 217 |
- apply Z.div_lt_upper_bound; nia. |
| 218 |
Qed. |
| 219 |
|
| 220 |
Lemma division_shrinks (a b c : Z) (Hb : 0 < b) (H : 0 <= a < c) : |
| 221 |
a / b < c. |
| 222 |
Proof. Z.div_mod_to_quot_rem_in_goal; nia. Qed. |
| 223 |
|
| 224 |
Lemma mul_div_ceil_ge (a b : Z) (Hb : 0 < b) : |
| 225 |
a <= b * ((a + b - 1) / b). |
| 226 |
Proof. Z.div_mod_to_quot_rem_in_goal; nia. Qed. |
| 227 |
|
| 228 |
(** This is the main theorem this algorithm relies on. *) |
| 229 |
Lemma division_algorithm (bits n d k : Z) |
| 230 |
(Hbits : 0 < bits) |
| 231 |
(Hn : 0 <= n < 2^bits) |
| 232 |
(Hd : 1 < d) |
| 233 |
(Hk : bits + Z.log2_up d <= k) : |
| 234 |
n * ((2^k + d - 1) / d) / 2^k = n / d. |
| 235 |
Proof. |
| 236 |
(* Help [lia] and [nia] out. *) |
| 237 |
pose proof (Z.log2_up_nonneg d). |
| 238 |
pose proof (pow2_log2_up_bounds d Hd). |
| 239 |
assert (0 < 2^k) by (apply Z.pow_pos_nonneg; lia). |
| 240 |
|
| 241 |
assert (n/d <= n * ((2^k + d - 1) / d) / 2^k). |
| 242 |
{ (* This direction is true independent of [Hk]. *) |
| 243 |
assert (2^k <= d * ((2 ^ k + d - 1) / d)) by (apply mul_div_ceil_ge; lia). |
| 244 |
apply Z.div_le_lower_bound; try lia. |
| 245 |
rewrite Z.div_mul_le by (try auto with zarith; lia). |
| 246 |
apply Z.div_le_upper_bound; try lia. |
| 247 |
nia. } |
| 248 |
assert (n * ((2^k + d - 1) / d) / 2^k <= n / d). |
| 249 |
{ assert (div_mul_le_inner : n * ((2^k + d - 1) / d) / 2^k <= |
| 250 |
(n * (2^k + d - 1) / d) / 2^k). |
| 251 |
{ apply Z.div_le_mono; try apply Z.div_mul_le; lia. } |
| 252 |
rewrite div_mul_le_inner. |
| 253 |
replace (n * (2^k + d - 1)) with (n * 2^k + n * (d - 1)) by nia. |
| 254 |
(* Swap [d] and [2^k] in the denominator. *) |
| 255 |
rewrite Z.div_div by lia. |
| 256 |
rewrite (Z.mul_comm d (2^k)). |
| 257 |
rewrite <- Z.div_div by lia. |
| 258 |
(* We now can extract the [n * 2^k] term from the inner division. *) |
| 259 |
rewrite Z.div_add_l by lia. |
| 260 |
(* We show the error term is zero, which allows the inequality to hold. *) |
| 261 |
assert (zero_error : n * (d - 1) / 2 ^ k = 0). |
| 262 |
{ apply Z.div_small. |
| 263 |
assert (2^bits * 2^(Z.log2_up d) <= 2^k) by |
| 264 |
(rewrite <- Z.pow_add_r; try apply Z.pow_le_mono_r; lia). |
| 265 |
nia. } |
| 266 |
rewrite zero_error. |
| 267 |
auto with zarith. } |
| 268 |
lia. |
| 269 |
Qed. |
| 270 |
|
| 271 |
(** Peel off one innermost [wrap], and replace it with the expected |
| 272 |
bounds. Before doing so, introduce a lemma into the context that the value |
| 273 |
is bounded. This is allows later goals to make use of earlier bounds. |
| 274 |
|
| 275 |
TODO(davidben): Most of the slowness comes from these intermediate |
| 276 |
hypotheses. The proofs need them, but only those one or two layers down. Can |
| 277 |
we be smarter about retaining them? *) |
| 278 |
Local Ltac replace_innermost_wrap expr := |
| 279 |
match expr with |
| 280 |
| context[wrap ?x ?y] => |
| 281 |
first [ replace_innermost_wrap y | |
| 282 |
assert (0 <= (wrap x y) < 2^x) by (apply wrap_bound; lia); |
| 283 |
replace (wrap x y) with y in * ] |
| 284 |
end. |
| 285 |
|
| 286 |
(** Finally, the correctness proof. *) |
| 287 |
Theorem mod_u16_spec (n d : Z) (Hn : 0 <= n < 2^32) (Hd : 1 < d < 2^16) : |
| 288 |
to_Z (mod_u16 (Z_to_u32 n) (Z_to_u16 d)) = n mod d. |
| 289 |
Proof. |
| 290 |
unfold mod_u16. |
| 291 |
(* Evaluate away everything except for exponentations, which are |
| 292 |
still useful, and our [wrap] metadata. *) |
| 293 |
cbn -[Z.pow num_bits wrap]. |
| 294 |
|
| 295 |
(* The solver needs some help getting these in the context. *) |
| 296 |
assert (Hlog : 0 < Z.log2_up d <= 16) by (apply p_bound; lia). |
| 297 |
assert (d <= 2^(Z.log2_up d) < d + d) by (apply pow2_log2_up_bounds; lia). |
| 298 |
assert (2^(Z.log2_up d) < 2 ^ 17) by lia. |
| 299 |
assert (2^32 <= (2^32 * 2^(Z.log2_up d) + d - 1) / d < 2 * 2^32) by |
| 300 |
(rewrite <- Z.pow_add_r by lia; apply m_bound; lia). |
| 301 |
|
| 302 |
(* Convert shifts to multiplication and divison. *) |
| 303 |
rewrite !Z.shiftl_mul_pow2 by (apply wrap_bound; lia). |
| 304 |
rewrite !Z.shiftr_div_pow2 by (apply wrap_bound; lia). |
| 305 |
rewrite !Z.mul_1_l. |
| 306 |
rewrite !Z.pow_1_r. |
| 307 |
|
| 308 |
(* Replace all the [to_word] calls with their expected bounds. *) |
| 309 |
repeat match goal with |
| 310 |
| [ |- ?x = n mod d ] => |
| 311 |
match goal with |
| 312 |
(* Evaluate away the [Z.min]s. *) |
| 313 |
| _ => progress cbn -[Z.pow Z.add Z.mul wrap] |
| 314 |
(* Remove obviously pointless casts. The solver could handle them, |
| 315 |
but this cuts down on the number of generated hypotheses. *) |
| 316 |
| [ |- context[wrap ?x (wrap ?x' ?y)] ] => |
| 317 |
replace (wrap x (wrap x' y)) with (wrap (Z.min x x') y) by |
| 318 |
(symmetry; apply double_wrap; lia) |
| 319 |
| [ |- context[wrap ?x n] ] => |
| 320 |
replace (wrap x n) with n by (unfold wrap; rewrite Z.mod_small; lia) |
| 321 |
| [ |- context[wrap ?x d] ] => |
| 322 |
replace (wrap x d) with d by (unfold wrap; rewrite Z.mod_small; lia) |
| 323 |
| [ |- context[wrap ?x (Zpos ?p)] ] => |
| 324 |
replace (wrap x (Zpos p)) with (Zpos p) by reflexivity |
| 325 |
(* Perform rewrites as soon as possible, so the generated hypotheses |
| 326 |
match too. *) |
| 327 |
| _ => rewrite a_minus_b_div_2_plus_b |
| 328 |
| _ => rewrite num_bits_log2_up |
| 329 |
| _ => rewrite Z.pow_add_r by lia |
| 330 |
| _ => replace_innermost_wrap x |
| 331 |
end |
| 332 |
end; |
| 333 |
|
| 334 |
repeat unfold wrap in *; |
| 335 |
|
| 336 |
repeat match goal with |
| 337 |
(* Normalize a few things. *) |
| 338 |
| _ => apply Z.le_ge |
| 339 |
| _ => apply Z.lt_gt |
| 340 |
| [ |- _ = _ mod _ ] => symmetry |
| 341 |
(* Apply these transforms everywhere. *) |
| 342 |
| _ => apply Z.mod_small |
| 343 |
| _ => split |
| 344 |
(* Everything is positive, so these tend to be easy. *) |
| 345 |
| [ |- 0 <= _ * _ ] => nia |
| 346 |
(* Don't touch the goals that involve the final division |
| 347 |
expression. We'll dispatch them separately. *) |
| 348 |
| [ |- context[d * (_ / 2^_)] ] => fail 1 |
| 349 |
(* The solver needs some help for this one, otherwise the [Z.div_*] |
| 350 |
theorems below break apart the numerator term. *) |
| 351 |
| [ _ : 0 <= ?x < ?y |- ?x / _ < ?y ] => apply division_shrinks |
| 352 |
(* Simplify the various inequalities. *) |
| 353 |
| _ => apply Z.div_le_upper_bound |
| 354 |
| _ => apply Z.div_lt_upper_bound |
| 355 |
| _ => apply Z.div_le_lower_bound |
| 356 |
| _ => apply Z.pow_nonneg |
| 357 |
| _ => apply Z.pow_pos_nonneg |
| 358 |
(* Only apply in one direction. *) |
| 359 |
| [ |- 0 <= _ - _ ] => apply Z.le_0_sub |
| 360 |
| _ => apply Z.mul_le_mono_nonneg_r |
| 361 |
(* [nia] is rather slow, and only one case needs it. *) |
| 362 |
| [ _ : 0 <= ?x < _, _ : 0 <= ?y < _ |- ?x * ?y < ?z ] => nia |
| 363 |
| _ => lia |
| 364 |
end; |
| 365 |
|
| 366 |
(* Clear the now unnecessary intermediate hypotheses. *) |
| 367 |
clear - Hn Hd Hlog; |
| 368 |
|
| 369 |
(* Invert the various transforms to get to the main theorem. *) |
| 370 |
repeat match goal with |
| 371 |
| _ => rewrite Z.div_div by (try apply Z.pow_pos_nonneg; lia) |
| 372 |
| _ => rewrite <- Z.pow_succ_r by lia |
| 373 |
| _ => rewrite <- Z.add_1_r |
| 374 |
| _ => rewrite Z.sub_add |
| 375 |
| _ => rewrite Zplus_minus |
| 376 |
| _ => rewrite <- Z.pow_add_r by lia |
| 377 |
| _ => rewrite <- Z.mul_add_distr_l |
| 378 |
| _ => rewrite <- Z.div_add_l by lia |
| 379 |
| [ |- context[?x * ?y + ?z * ?x] ] => rewrite (Z.mul_comm z x) |
| 380 |
end; |
| 381 |
|
| 382 |
(* And finish with the main theorem. *) |
| 383 |
rewrite (division_algorithm 32) by lia; |
| 384 |
Z.div_mod_to_quot_rem_in_goal; |
| 385 |
nia. |
| 386 |
Qed. |