| 1 |
From Coq Require Import ZArith Lia. |
| 2 |
Require Import Crypto.Algebra.Nsatz. |
| 3 |
From Coq Require Import Derive. |
| 4 |
From Coq Require Import List. |
| 5 |
Require Import Crypto.Algebra.Ring. |
| 6 |
Require Import Crypto.Arithmetic.BaseConversion. |
| 7 |
Require Import Crypto.Arithmetic.Core. |
| 8 |
Require Import Crypto.Arithmetic.Partition. |
| 9 |
Require Import Crypto.Arithmetic.Saturated. |
| 10 |
Require Import Crypto.Arithmetic.UniformWeight. |
| 11 |
Require Import Crypto.Util.Decidable. |
| 12 |
Require Import Crypto.Util.LetIn. |
| 13 |
Require Import Crypto.Util.ListUtil. |
| 14 |
Require Import Crypto.Arithmetic.BarrettReduction.Generalized. |
| 15 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 16 |
Require Import Crypto.Util.ZUtil.AddModulo. |
| 17 |
Require Import Crypto.Util.ZUtil.CC Crypto.Util.ZUtil.Rshi. |
| 18 |
Require Import Crypto.Util.ZUtil.Modulo Crypto.Util.ZUtil.Div. |
| 19 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 20 |
Require Import Crypto.Util.ZUtil.Modulo.PullPush. |
| 21 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 22 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. |
| 23 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 24 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 25 |
|
| 26 |
Require Import Crypto.Util.Notations. |
| 27 |
Import ListNotations. Local Open Scope Z_scope. |
| 28 |
|
| 29 |
Section Generic. |
| 30 |
Context (b k M mu width : Z) (n : nat) |
| 31 |
(b_ok : 1 < b) |
| 32 |
(k_pos : 0 < k) |
| 33 |
(bk_eq : b^k = 2^(width * Z.of_nat n)) |
| 34 |
(M_range : b ^ (k - 1) < M < b ^ k) |
| 35 |
(mu_eq : mu = b ^ (2 * k) / M) |
| 36 |
(width_pos : 0 < width) |
| 37 |
(strong_bound : b ^ 1 * (b ^ (2 * k) mod M) <= b ^ (k + 1) - mu). |
| 38 |
Local Notation weight := (uweight width). |
| 39 |
Local Notation partition := (Partition.partition weight). |
| 40 |
Context (q1 : list Z -> list Z) |
| 41 |
(q1_correct : |
| 42 |
forall x, |
| 43 |
0 <= x < b ^ (2 * k) -> |
| 44 |
q1 (partition (n*2)%nat x) = partition (n+1)%nat (x / b ^ (k - 1))) |
| 45 |
(q3 : list Z -> list Z -> list Z) |
| 46 |
(q3_correct : |
| 47 |
forall x q1, |
| 48 |
0 <= x < b ^ (2 * k) -> |
| 49 |
q1 = x / b ^ (k - 1) -> |
| 50 |
q3 (partition (n*2) x) (partition (n+1) q1) = partition (n+1) ((mu * q1) / b ^ (k + 1))) |
| 51 |
(r : list Z -> list Z -> list Z) |
| 52 |
(r_correct : |
| 53 |
forall x q3, |
| 54 |
0 <= x < M * b ^ k -> |
| 55 |
0 <= q3 -> |
| 56 |
(exists b : bool, q3 = x / M + (if b then -1 else 0)) -> |
| 57 |
r (partition (n*2) x) (partition (n+1) q3) = partition n (x mod M)). |
| 58 |
|
| 59 |
Context (x : Z) (x_range : 0 <= x < M * b ^ k) |
| 60 |
(xt : list Z) (xt_correct : xt = partition (n*2) x). |
| 61 |
|
| 62 |
Local Lemma M_pos : 0 < M. |
| 63 |
Proof. assert (0 <= b ^ (k - 1)) by Z.zero_bounds. lia. Qed. |
| 64 |
Local Lemma M_upper : M < weight n. |
| 65 |
Proof. rewrite uweight_eq_alt'. lia. Qed. |
| 66 |
Local Lemma x_upper : x < b ^ (2 * k). |
| 67 |
Proof. |
| 68 |
assert (0 < b ^ k) by Z.zero_bounds. |
| 69 |
apply Z.lt_le_trans with (m:= M * b^k); [ lia | ]. |
| 70 |
transitivity (b^k * b^k); [ nia | ]. |
| 71 |
rewrite <-Z.pow_2_r, <-Z.pow_mul_r by lia. |
| 72 |
rewrite (Z.mul_comm k 2); reflexivity. |
| 73 |
Qed. |
| 74 |
Local Lemma xmod_lt_M : x mod b ^ (k - 1) <= M. |
| 75 |
Proof. pose proof (Z.mod_pos_bound x (b ^ (k - 1)) ltac:(Z.zero_bounds)). lia. Qed. |
| 76 |
Local Hint Resolve M_pos x_upper xmod_lt_M : core. |
| 77 |
|
| 78 |
Definition reduce := |
| 79 |
dlet_nd q1t := q1 xt in |
| 80 |
dlet_nd q3t := q3 xt q1t in |
| 81 |
r xt q3t. |
| 82 |
|
| 83 |
Lemma q1_range : 0 <= x / b^(k-1) < b^(k+1). |
| 84 |
Proof. |
| 85 |
split; [ solve [Z.zero_bounds] | ]. |
| 86 |
assert (0 < b ^ (k - 1)) by Z.zero_bounds. |
| 87 |
assert (0 < b ^ k) by Z.zero_bounds. |
| 88 |
apply Z.div_lt_upper_bound; [ solve [Z.zero_bounds] | ]. |
| 89 |
eapply Z.lt_le_trans with (m:=b^k * b^k); |
| 90 |
[ nia | autorewrite with pull_Zpow; apply Z.pow_le_mono; lia ]. |
| 91 |
Qed. |
| 92 |
|
| 93 |
Lemma q3_range : 0 <= mu * (x / b ^ (k - 1)) / b ^ (k + 1). |
| 94 |
Proof. |
| 95 |
assert (0 < b ^ (k - 1)) by Z.zero_bounds. |
| 96 |
subst mu; Z.zero_bounds. |
| 97 |
Qed. |
| 98 |
|
| 99 |
Lemma reduce_correct : reduce = partition n (x mod M). |
| 100 |
Proof. |
| 101 |
cbv [reduce Let_In]. pose proof q3_range. |
| 102 |
rewrite xt_correct, q1_correct, q3_correct by auto with lia. |
| 103 |
assert (exists cond : bool, ((mu * (x / b^(k-1))) / b^(k+1)) = x / M + (if cond then -1 else 0)) as Hq3. |
| 104 |
{ destruct q_nice_strong with (b:=b) (k:=k) (m:=mu) (offset:=1) (a:=x) (n:=M) as [cond Hcond]; |
| 105 |
eauto 2 using Z.lt_gt with zarith. } |
| 106 |
eauto using r_correct with lia. |
| 107 |
Qed. |
| 108 |
End Generic. |
| 109 |
|
| 110 |
(* Non-standard implementation -- uses specialized instructions and b=2 *) |
| 111 |
Module Fancy. |
| 112 |
Section Fancy. |
| 113 |
Context (M mu width k : Z) |
| 114 |
(sz : nat) (sz_nz : sz <> 0%nat) |
| 115 |
(width_ok : 1 < width) |
| 116 |
(k_pos : 0 < k) (* this can be inferred from other arguments but is easier to put here for tactics *) |
| 117 |
(k_eq : k = width * Z.of_nat sz). |
| 118 |
(* sz = 1, width = k = 256 *) |
| 119 |
Local Notation w := (uweight width). Local Notation eval := (Positional.eval w). |
| 120 |
Context (mut Mt : list Z) (mut_correct : mut = Partition.partition w (sz+1) mu) (Mt_correct : Mt = Partition.partition w sz M). |
| 121 |
Context (mu_eq : mu = 2 ^ (2 * k) / M) (muHigh_one : mu / w sz = 1) (M_range : 2^(k-1) < M < 2^k). |
| 122 |
Local Lemma wprops : @weight_properties w. Proof using width_ok. clear - width_ok. apply uwprops ; lia. Qed. |
| 123 |
Local Hint Resolve wprops : core. |
| 124 |
Hint Rewrite mut_correct Mt_correct : pull_partition. |
| 125 |
|
| 126 |
Lemma w_eq_2k : w sz = 2^k. Proof. rewrite uweight_eq_alt' by auto. congruence. Qed. |
| 127 |
Lemma mu_range : 2^k <= mu < 2^(k+1). |
| 128 |
Proof. |
| 129 |
rewrite mu_eq. assert (0 < 2^(k-1)) by Z.zero_bounds. |
| 130 |
assert (2^k < M * 2). |
| 131 |
{ replace (2^k) with (2^(k-1+1)) by (f_equal; lia). |
| 132 |
rewrite Z.pow_add_r, Z.pow_1_r by lia. |
| 133 |
lia. } |
| 134 |
replace (2 ^ (2 * k)) with (2^(k+k)) by (f_equal; lia). |
| 135 |
rewrite !Z.pow_add_r, Z.pow_1_r by lia. split. |
| 136 |
{ apply Z.div_le_lower_bound; nia. } |
| 137 |
{ apply Z.div_lt_upper_bound; nia. } |
| 138 |
Qed. |
| 139 |
Lemma mu_range' : 0 <= mu < 2 * w sz. |
| 140 |
Proof. |
| 141 |
pose proof mu_range. assert (0 < 2^k) by auto with zarith. |
| 142 |
assert (2^(k+1) = 2 * w sz); [ | lia]. |
| 143 |
rewrite k_eq, uweight_eq_alt'. |
| 144 |
rewrite Z.pow_add_r, Z.pow_1_r by lia. lia. |
| 145 |
Qed. |
| 146 |
Lemma M_range' : 0 <= M < w sz. (* more convenient form, especially for mod_small *) |
| 147 |
Proof. assert (0 <= 2 ^ (k-1)) by Z.zero_bounds. pose proof w_eq_2k; lia. Qed. |
| 148 |
|
| 149 |
Definition shiftr' (m : nat) (t : list Z) (n : Z) : list Z := |
| 150 |
map (fun i => Z.rshi (2^width) (nth_default 0 t (S i)) (nth_default 0 t i) n) (seq 0 m). |
| 151 |
|
| 152 |
Definition shiftr (m : nat) (t : list Z) (n : Z) : list Z := |
| 153 |
(* if width <= n, drop limbs first *) |
| 154 |
if dec (width <= n) |
| 155 |
then shiftr' m (skipn (Z.to_nat (n / width)) t) (n mod width) |
| 156 |
else shiftr' m t n. |
| 157 |
|
| 158 |
Definition wideadd t1 t2 := fst (Rows.add w (sz*2) t1 t2). |
| 159 |
Definition widesub t1 t2 := fst (Rows.sub w (sz*2) t1 t2). |
| 160 |
Definition widemul := BaseConversion.widemul_inlined width sz 2. |
| 161 |
(* widemul_inlined takes the following argument order : (width of limbs in input) (# limbs in input) (# parts to split each limb into before multiplying) *) |
| 162 |
|
| 163 |
Definition fill (n : nat) (a : list Z) := a ++ Positional.zeros (n - length a). |
| 164 |
Definition low : list Z -> list Z := firstn sz. |
| 165 |
Definition high : list Z -> list Z := skipn sz. |
| 166 |
Definition mul_high (a b : list Z) a0b1 : list Z := |
| 167 |
dlet_nd a0b0 := widemul (low a) (low b) in |
| 168 |
dlet_nd ab := wideadd (high a0b0 ++ high b) (fill (sz*2) (low b)) in |
| 169 |
wideadd ab a0b1. |
| 170 |
|
| 171 |
(* select based on the most significant bit of xHigh *) |
| 172 |
Definition muSelect xt := |
| 173 |
let xHigh := nth_default 0 xt (sz*2 - 1) in |
| 174 |
Positional.select (Z.cc_m (2 ^ width) xHigh) (Positional.zeros sz) (low mut). |
| 175 |
|
| 176 |
Definition cond_sub (a y : list Z) : list Z := |
| 177 |
let cond := Z.cc_l (nth_default 0 (high a) 0) in (* a[k] = least significant bit of (high a) *) |
| 178 |
dlet_nd maybe_y := Positional.select cond (Positional.zeros sz) y in |
| 179 |
dlet_nd diff := Rows.sub w sz (low a) maybe_y in (* (a mod (w sz) - y) mod (w sz)) = (a - y) mod (w sz); since we know a - y is < w sz this is okay by mod_small *) |
| 180 |
fst diff. |
| 181 |
|
| 182 |
Definition cond_subM x := |
| 183 |
if Nat.eq_dec sz 1 |
| 184 |
then [Z.add_modulo (nth_default 0 x 0) 0 M] (* use the special instruction if we can *) |
| 185 |
else Rows.conditional_sub w sz x Mt. |
| 186 |
|
| 187 |
Definition q1 (xt : list Z) := shiftr (sz+1) xt (k - 1). |
| 188 |
|
| 189 |
Definition q3 (xt q1t : list Z) := |
| 190 |
dlet_nd muSelect := muSelect xt in (* make sure muSelect is not inlined in the output *) |
| 191 |
dlet_nd twoq := mul_high (fill (sz*2) mut) (fill (sz*2) q1t) (fill (sz*2) muSelect) in |
| 192 |
shiftr (sz+1) twoq 1. |
| 193 |
|
| 194 |
Definition r (xt q3t : list Z) := |
| 195 |
dlet_nd r2 := widemul (low q3t) Mt in |
| 196 |
dlet_nd rt := widesub xt r2 in |
| 197 |
dlet_nd rt := cond_sub rt Mt in |
| 198 |
cond_subM rt. |
| 199 |
|
| 200 |
Section Proofs. |
| 201 |
Lemma shiftr'_correct m n : |
| 202 |
forall t tn, |
| 203 |
(m <= tn)%nat -> 0 <= t < w tn -> 0 <= n < width -> |
| 204 |
shiftr' m (Partition.partition w tn t) n = Partition.partition w m (t / 2 ^ n). |
| 205 |
Proof using width_ok. |
| 206 |
clear -width_ok. |
| 207 |
cbv [shiftr']. induction m; intros; [ reflexivity | ]. |
| 208 |
rewrite !partition_step, seq_snoc. |
| 209 |
autorewrite with distr_length natsimplify push_map push_nth_default. |
| 210 |
rewrite IHm, Z.rshi_correct, !uweight_S by auto with zarith. |
| 211 |
rewrite <-!Z.mod_pull_div by auto with zarith. |
| 212 |
destruct (Nat.eq_dec (S m) tn); [subst tn | ]. |
| 213 |
{ rewrite uweight_S in * by auto with zarith. |
| 214 |
break_innermost_match; try lia. |
| 215 |
autorewrite with zsimplify_fast. Z.rewrite_mod_small. |
| 216 |
rewrite Z.div_div_comm by auto with zarith; reflexivity. } |
| 217 |
{ break_innermost_match; try lia. |
| 218 |
rewrite <-!uweight_S by auto with zarith. |
| 219 |
repeat match goal with |
| 220 |
| _ => rewrite uweight_pull_mod by auto with zarith |
| 221 |
| _ => rewrite Z.mod_mod_small by auto with zarith |
| 222 |
| _ => rewrite <-Znumtheory.Zmod_div_mod by (Z.zero_bounds; auto with zarith) |
| 223 |
| _ => rewrite uweight_eq_alt with (n:=1%nat) by auto with zarith |
| 224 |
| |- context [(t / w (S m)) mod 2^width * 2^width] => |
| 225 |
replace (t / w (S m)) with (t / w m / 2^width) by |
| 226 |
(rewrite uweight_S, Z.div_div by auto with zarith; f_equal; lia); |
| 227 |
rewrite Z.mod_pull_div with (b:=2^width) by auto with zarith; |
| 228 |
rewrite Z.mul_div_eq' by auto with zarith |
| 229 |
| _ => progress autorewrite with natsimplify zsimplify_fast zsimplify |
| 230 |
end. |
| 231 |
replace (2^width*2^width) with (2^width*2^(width-n)*2^n) by (autorewrite with pull_Zpow; f_equal; lia). |
| 232 |
rewrite <-Z.mod_pull_div, <-Znumtheory.Zmod_div_mod by (Z.zero_bounds; auto with zarith). |
| 233 |
rewrite Z.div_div_comm by Z.zero_bounds. reflexivity. } |
| 234 |
Qed. |
| 235 |
Lemma shiftr_correct m n : |
| 236 |
forall t tn, |
| 237 |
(Z.to_nat (n / width) <= tn)%nat -> |
| 238 |
(m <= tn - Z.to_nat (n / width))%nat -> 0 <= t < w tn -> 0 <= n -> |
| 239 |
shiftr m (Partition.partition w tn t) n = Partition.partition w m (t / 2 ^ n). |
| 240 |
Proof. |
| 241 |
cbv [shiftr]; intros. |
| 242 |
break_innermost_match; [ | solve [auto using shiftr'_correct with zarith] ]. |
| 243 |
pose proof (Z.mod_pos_bound n width ltac:(lia)). |
| 244 |
assert (t / 2 ^ (n - n mod width) < w (tn - Z.to_nat (n / width))). |
| 245 |
{ apply Z.div_lt_upper_bound; [solve [Z.zero_bounds] | ]. |
| 246 |
rewrite uweight_eq_alt' in *. |
| 247 |
rewrite <-Z.pow_add_r, Nat2Z.inj_sub, Z2Nat.id, <-Z.mul_div_eq by auto with zarith. |
| 248 |
autorewrite with push_Zmul zsimplify. auto with zarith. } |
| 249 |
repeat match goal with |
| 250 |
| _ => progress rewrite ?uweight_skipn_partition, ?uweight_eq_alt' by auto with lia |
| 251 |
| _ => rewrite Z2Nat.id by Z.zero_bounds |
| 252 |
| _ => rewrite Z.mul_div_eq_full by auto with zarith |
| 253 |
| _ => rewrite shiftr'_correct by auto with zarith |
| 254 |
| _ => progress rewrite ?Z.div_div, <-?Z.pow_add_r by auto with zarith |
| 255 |
end. |
| 256 |
autorewrite with zsimplify. reflexivity. |
| 257 |
Qed. |
| 258 |
Hint Rewrite shiftr_correct using (solve [auto with lia]) : pull_partition. |
| 259 |
|
| 260 |
(* 2 ^ (k + 1) bits fit in sz + 1 limbs because we know 2^k bits fit in sz and 1 <= width *) |
| 261 |
Lemma q1_correct x : |
| 262 |
0 <= x < w (sz * 2) -> |
| 263 |
q1 (Partition.partition w (sz*2)%nat x) = Partition.partition w (sz+1)%nat (x / 2 ^ (k - 1)). |
| 264 |
Proof. |
| 265 |
cbv [q1]; intros. assert (1 <= Z.of_nat sz) by (destruct sz; lia). |
| 266 |
assert (Z.to_nat ((k-1) / width) < sz)%nat. { |
| 267 |
subst k. rewrite <-Z.add_opp_r. autorewrite with zsimplify. |
| 268 |
apply Nat2Z.inj_lt. rewrite Z2Nat.id by lia. lia. } |
| 269 |
assert (0 <= k - 1) by nia. |
| 270 |
autorewrite with pull_partition. reflexivity. |
| 271 |
Qed. |
| 272 |
|
| 273 |
Lemma low_correct n a : (sz <= n)%nat -> low (Partition.partition w n a) = Partition.partition w sz a. |
| 274 |
Proof. cbv [low]; auto using uweight_firstn_partition with lia. Qed. |
| 275 |
Lemma high_correct a : high (Partition.partition w (sz*2) a) = Partition.partition w sz (a / w sz). |
| 276 |
Proof. cbv [high]. rewrite uweight_skipn_partition by lia. f_equal; lia. Qed. |
| 277 |
Lemma fill_correct n m a : |
| 278 |
(n <= m)%nat -> |
| 279 |
fill m (Partition.partition w n a) = Partition.partition w m (a mod w n). |
| 280 |
Proof. |
| 281 |
cbv [fill]; intros. distr_length. |
| 282 |
rewrite <-partition_0 with (weight:=w). |
| 283 |
rewrite uweight_partition_app by lia. |
| 284 |
f_equal; lia. |
| 285 |
Qed. |
| 286 |
Hint Rewrite low_correct high_correct fill_correct using lia : pull_partition. |
| 287 |
|
| 288 |
Lemma wideadd_correct a b : |
| 289 |
wideadd (Partition.partition w (sz*2) a) (Partition.partition w (sz*2) b) = Partition.partition w (sz*2) (a + b). |
| 290 |
Proof. |
| 291 |
cbv [wideadd]. rewrite Rows.add_partitions by (distr_length; auto). |
| 292 |
autorewrite with push_eval. |
| 293 |
apply partition_eq_mod; auto with zarith. |
| 294 |
Qed. |
| 295 |
Lemma widesub_correct a b : |
| 296 |
widesub (Partition.partition w (sz*2) a) (Partition.partition w (sz*2) b) = Partition.partition w (sz*2) (a - b). |
| 297 |
Proof. |
| 298 |
cbv [widesub]. rewrite Rows.sub_partitions by (distr_length; auto). |
| 299 |
autorewrite with push_eval. |
| 300 |
apply partition_eq_mod; auto with zarith. |
| 301 |
Qed. |
| 302 |
Lemma widemul_correct a b : |
| 303 |
widemul (Partition.partition w sz a) (Partition.partition w sz b) = Partition.partition w (sz*2) ((a mod w sz) * (b mod w sz)). |
| 304 |
Proof. |
| 305 |
cbv [widemul]. rewrite BaseConversion.widemul_inlined_correct; (distr_length; auto). |
| 306 |
autorewrite with push_eval. reflexivity. |
| 307 |
Qed. |
| 308 |
Hint Rewrite widemul_correct widesub_correct wideadd_correct using lia : pull_partition. |
| 309 |
|
| 310 |
Lemma mul_high_idea d a b a0 a1 b0 b1 : |
| 311 |
d <> 0 -> |
| 312 |
a = d * a1 + a0 -> |
| 313 |
b = d * b1 + b0 -> |
| 314 |
(a * b) / d = a0 * b0 / d + d * a1 * b1 + a1 * b0 + a0 * b1. |
| 315 |
Proof. |
| 316 |
intros. subst a b. autorewrite with push_Zmul. |
| 317 |
ring_simplify_subterms. rewrite Z.pow_2_r. |
| 318 |
rewrite Z.div_add_exact by (push_Zmod; rewrite ?Zmod_0_l; lia). |
| 319 |
repeat match goal with |
| 320 |
| |- context [d * ?a * ?b * ?c] => |
| 321 |
replace (d * a * b * c) with (a * b * c * d) by ring |
| 322 |
| |- context [d * ?a * ?b] => |
| 323 |
replace (d * a * b) with (a * b * d) by ring |
| 324 |
end. |
| 325 |
rewrite !Z.div_add by lia. |
| 326 |
autorewrite with zsimplify. |
| 327 |
rewrite (Z.mul_comm a0 b0). |
| 328 |
ring_simplify. ring. |
| 329 |
Qed. |
| 330 |
|
| 331 |
Lemma mul_high_correct a b |
| 332 |
(Ha : a / w sz = 1) |
| 333 |
a0b1 (Ha0b1 : a0b1 = a mod w sz * (b / w sz)) : |
| 334 |
mul_high (Partition.partition w (sz*2) a) (Partition.partition w (sz*2) b) (Partition.partition w (sz*2) a0b1) = |
| 335 |
Partition.partition w (sz*2) (a * b / w sz). |
| 336 |
Proof. |
| 337 |
cbv [mul_high Let_In]. |
| 338 |
erewrite mul_high_idea by auto using Z.div_mod with zarith. |
| 339 |
repeat match goal with |
| 340 |
| _ => progress autorewrite with pull_partition |
| 341 |
| _ => progress rewrite ?Ha, ?Ha0b1 |
| 342 |
| _ => rewrite uweight_partition_app by lia; |
| 343 |
replace (sz+sz)%nat with (sz*2)%nat by lia |
| 344 |
| _ => rewrite Z.mod_pull_div by auto with zarith |
| 345 |
| _ => progress Z.rewrite_mod_small |
| 346 |
| _ => f_equal; ring |
| 347 |
end. |
| 348 |
Qed. |
| 349 |
|
| 350 |
Hint Rewrite uweight_S uweight_eq_alt' using lia : weight_to_pow. |
| 351 |
Hint Rewrite <-uweight_S uweight_eq_alt' using lia : pow_to_weight. |
| 352 |
|
| 353 |
Lemma q1_range x : |
| 354 |
0 <= x < w (sz * 2) -> |
| 355 |
0 <= x / 2 ^ (k-1) < 2 * w sz. |
| 356 |
Proof. |
| 357 |
intros; split; [ solve [Z.zero_bounds] | ]. |
| 358 |
apply Z.div_lt_upper_bound; [ solve [Z.zero_bounds] | ]. |
| 359 |
assert (w (sz * 2) <= 2 ^ (k-1) * (2 * w sz)); [ | lia ]. |
| 360 |
autorewrite with weight_to_pow pull_Zpow. |
| 361 |
apply Z.pow_le_mono_r; lia. |
| 362 |
Qed. |
| 363 |
|
| 364 |
Lemma muSelect_correct x : |
| 365 |
0 <= x < w (sz * 2) -> |
| 366 |
muSelect (Partition.partition w (sz*2) x) = Partition.partition w sz (mu mod (w sz) * (x / 2 ^ (k - 1) / (w sz))). |
| 367 |
Proof. |
| 368 |
cbv [muSelect]; intros; |
| 369 |
repeat match goal with |
| 370 |
| _ => progress autorewrite with pull_partition natsimplify |
| 371 |
| _ => progress rewrite ?Z.cc_m_eq by auto with zarith |
| 372 |
| _ => erewrite Positional.select_eq by (distr_length; eauto) |
| 373 |
| _ => rewrite nth_default_partition by lia |
| 374 |
| _ => progress replace (S (sz * 2 - 1)) with (sz * 2)%nat by lia |
| 375 |
| H : 0 <= ?x < ?m |- context [?x mod ?m] => rewrite (Z.mod_small x m) by auto with zarith |
| 376 |
end. |
| 377 |
replace (x / (w (sz * 2 - 1)) / (2 ^ width / 2)) with (x / (2 ^ (k - 1)) / w sz) by |
| 378 |
(autorewrite with weight_to_pow pull_Zpow; |
| 379 |
rewrite !Z.div_div, <-!Z.pow_add_r by (Core.zutil_arith || Z.zero_bounds); do 2 f_equal; nia). |
| 380 |
rewrite Z.div_between_0_if with (a:=x / 2^(k-1)) by (Z.zero_bounds; auto using q1_range). |
| 381 |
break_innermost_match; try lia; autorewrite with zsimplify_fast; [ | ]. |
| 382 |
{ apply partition_eq_mod; auto with zarith. } |
| 383 |
{ rewrite partition_0; reflexivity. } |
| 384 |
Qed. |
| 385 |
Hint Rewrite muSelect_correct using lia : pull_partition. |
| 386 |
|
| 387 |
Lemma mu_q1_range x (Hx : 0 <= x < w (sz * 2)) : mu * (x / 2^(k-1)) < w sz * w (sz * 2). |
| 388 |
Proof. |
| 389 |
pose proof mu_range'. pose proof q1_range x ltac:(lia). |
| 390 |
replace (w (sz * 2)) with (w sz * w sz) by |
| 391 |
(autorewrite with weight_to_pow pull_Zpow; f_equal; lia). |
| 392 |
apply Z.lt_le_trans with (m:= 2 * w sz * (2 * w sz)); [ nia | ]. |
| 393 |
assert (4 <= w sz); [ | nia ]. change 4 with (Z.pow 2 2). |
| 394 |
autorewrite with weight_to_pow. apply Z.pow_le_mono_r; nia. |
| 395 |
Qed. |
| 396 |
|
| 397 |
Lemma q3_correct x (Hx : 0 <= x < w (sz * 2)) q1 (Hq1 : q1 = x / 2 ^ (k - 1)) : |
| 398 |
q3 (Partition.partition w (sz*2) x) (Partition.partition w (sz+1) q1) = Partition.partition w (sz+1) ((mu*q1) / 2 ^ (k + 1)). |
| 399 |
Proof. |
| 400 |
cbv [q3 Let_In]. intros. pose proof mu_q1_range x ltac:(lia). |
| 401 |
pose proof mu_range'. pose proof q1_range x ltac:(lia). |
| 402 |
autorewrite with pull_partition pull_Zmod. |
| 403 |
assert (2 * w sz < w (sz + 1)) by (autorewrite with weight_to_pow pull_Zpow; auto with zarith lia). |
| 404 |
Z.rewrite_mod_small. rewrite <-Hq1 in *. |
| 405 |
rewrite mul_high_correct by |
| 406 |
(try lia; rewrite Z.div_between_0_if with (a:=q1) by lia; |
| 407 |
break_innermost_match; autorewrite with zsimplify; reflexivity). |
| 408 |
rewrite shiftr_correct by (rewrite ?Z.div_small, ?Z2Nat.inj_0 by lia; auto with zarith lia). |
| 409 |
autorewrite with weight_to_pow pull_Zpow pull_Zdiv. |
| 410 |
rewrite ?Z.div_div, <-?Z.pow_add_r by (Core.zutil_arith || Z.zero_bounds). |
| 411 |
congruence. |
| 412 |
Qed. |
| 413 |
|
| 414 |
Lemma cond_sub_correct a b : |
| 415 |
cond_sub (Partition.partition w (sz*2) a) (Partition.partition w sz b) |
| 416 |
= Partition.partition w sz (if dec ((a / w sz) mod 2 = 0) |
| 417 |
then a |
| 418 |
else a - b). |
| 419 |
Proof. |
| 420 |
intros; cbv [cond_sub Let_In Z.cc_l]. autorewrite with pull_partition. |
| 421 |
rewrite nth_default_partition by lia. |
| 422 |
rewrite weight_0 by auto. autorewrite with zsimplify_fast. |
| 423 |
rewrite uweight_eq_alt' with (n:=1%nat). autorewrite with push_Zof_nat zsimplify. |
| 424 |
rewrite <-Znumtheory.Zmod_div_mod by auto using Zpow_facts.Zpower_divide with zarith. |
| 425 |
rewrite Positional.select_eq with (n:=sz) by (distr_length; apply w). |
| 426 |
rewrite Rows.sub_partitions by (break_innermost_match; distr_length; auto). |
| 427 |
break_innermost_match; autorewrite with push_eval zsimplify_fast; |
| 428 |
apply partition_eq_mod; auto with zarith. |
| 429 |
Qed. |
| 430 |
Hint Rewrite cond_sub_correct : pull_partition. |
| 431 |
Lemma cond_subM_correct a : |
| 432 |
cond_subM (Partition.partition w sz a) |
| 433 |
= Partition.partition w sz (if dec (a mod w sz < M) |
| 434 |
then a |
| 435 |
else a - M). |
| 436 |
Proof. |
| 437 |
cbv [cond_subM]. autorewrite with pull_partition. pose proof M_range'. |
| 438 |
rewrite Rows.conditional_sub_partitions by |
| 439 |
(distr_length; auto; autorewrite with push_eval; try apply partition_eq_mod; auto with zarith). |
| 440 |
rewrite nth_default_partition, weight_0, Z.add_modulo_correct by auto with lia. |
| 441 |
autorewrite with zsimplify_fast push_eval. Z.rewrite_mod_small. |
| 442 |
pose proof Z.mod_pos_bound a (w 1) ltac:(auto). |
| 443 |
break_innermost_match; Z.ltb_to_lt; |
| 444 |
repeat match goal with |
| 445 |
| _ => lia |
| 446 |
| _ => reflexivity |
| 447 |
| _ => apply partition_eq_mod; solve [auto with zarith] |
| 448 |
| _ => rewrite partition_step, weight_0 by auto |
| 449 |
| _ => progress autorewrite with zsimplify_fast |
| 450 |
| _ => progress Z.rewrite_mod_small |
| 451 |
| _ => rewrite Z.sub_mod_l with (a:=a) |
| 452 |
end. |
| 453 |
Qed. |
| 454 |
Hint Rewrite cond_subM_correct : pull_partition. |
| 455 |
|
| 456 |
Lemma w_eq_22k : w (sz * 2) = 2 ^ (2 * k). |
| 457 |
Proof. |
| 458 |
replace (sz * 2)%nat with (sz + sz)%nat by lia. |
| 459 |
rewrite uweight_sum_indices, w_eq_2k, <-Z.pow_add_r by lia. |
| 460 |
f_equal; lia. |
| 461 |
Qed. |
| 462 |
|
| 463 |
Lemma r_idea x q3 (b:bool) : |
| 464 |
0 <= x < M * 2 ^ k -> |
| 465 |
0 <= q3 -> |
| 466 |
q3 = x / M + (if b then -1 else 0) -> |
| 467 |
x - q3 mod w sz * M = x mod M + (if b then M else 0). |
| 468 |
Proof. |
| 469 |
intros. assert (0 < 2^(k-1)) by Z.zero_bounds. |
| 470 |
assert (q3 < w sz). |
| 471 |
{ apply Z.le_lt_trans with (m:=x/M); [ subst q3; break_innermost_match; lia | ]. |
| 472 |
autorewrite with weight_to_pow. rewrite <-k_eq. auto with zarith. } |
| 473 |
Z.rewrite_mod_small. |
| 474 |
repeat match goal with |
| 475 |
| _ => progress autorewrite with push_Zmul |
| 476 |
| H : q3 = ?e |- _ => progress replace (q3 * M) with (e * M) by (rewrite H; reflexivity) |
| 477 |
| _ => rewrite (Z.mul_div_eq' x M) by lia |
| 478 |
end. |
| 479 |
break_innermost_match; Z.ltb_to_lt; lia. |
| 480 |
Qed. |
| 481 |
|
| 482 |
Lemma r_correct x q3 : |
| 483 |
0 <= x < M * 2 ^ k -> |
| 484 |
0 <= q3 -> |
| 485 |
(exists b : bool, q3 = x / M + (if b then -1 else 0)) -> |
| 486 |
r (Partition.partition w (sz*2) x) (Partition.partition w (sz+1) q3) = Partition.partition w sz (x mod M). |
| 487 |
Proof. |
| 488 |
intros; cbv [r Let_In]. pose proof M_range'. assert (0 < 2^(k-1)) by Z.zero_bounds. |
| 489 |
autorewrite with pull_partition. Z.rewrite_mod_small. |
| 490 |
match goal with H : exists _, q3 = _ |- _ => destruct H end. |
| 491 |
erewrite r_idea by eassumption. |
| 492 |
pose proof (Z.mod_pos_bound x M ltac:(lia)). |
| 493 |
rewrite Z.div_between_0_if with (b:=w sz) by (break_innermost_match; auto with zarith). |
| 494 |
rewrite Z.mod_small with (b:=2) by (break_innermost_match; lia). |
| 495 |
break_innermost_match; Z.ltb_to_lt; try lia; autorewrite with zsimplify_fast; |
| 496 |
repeat match goal with |
| 497 |
| |- exists e, _ /\ _ /\ ?f ?x = ?f e => exists x; split; [ | split ] |
| 498 |
| _ => rewrite Z.mod_small in * by lia |
| 499 |
| _ => progress Z.rewrite_mod_small |
| 500 |
| _ => progress (push_Zmod; pull_Zmod); autorewrite with zsimplify_fast |
| 501 |
| _ => lia |
| 502 |
| _ => reflexivity |
| 503 |
end. |
| 504 |
Qed. |
| 505 |
End Proofs. |
| 506 |
|
| 507 |
Section Def. |
| 508 |
Context (sz_eq_1 : sz = 1%nat). (* this is needed to get rid of branches in the templates; a different definition would be needed for sizes other than 1, but would be able to use the same proofs. *) |
| 509 |
Local Hint Resolve q1_correct q3_correct r_correct : core. |
| 510 |
|
| 511 |
(* muselect relies on an initially-set flag, so pull it out of q3 *) |
| 512 |
Definition fancy_reduce_muSelect_first xt := |
| 513 |
dlet_nd muSelect := muSelect xt in |
| 514 |
dlet_nd q1t := q1 xt in |
| 515 |
dlet_nd twoq := mul_high (fill (sz * 2) mut) (fill (sz * 2) q1t) (fill (sz * 2) muSelect) in |
| 516 |
dlet_nd q3t := shiftr (sz+1) twoq 1 in |
| 517 |
r xt q3t. |
| 518 |
|
| 519 |
Lemma fancy_reduce_muSelect_first_correct x : |
| 520 |
0 <= x < M * 2^k -> |
| 521 |
2 * (2 ^ (2 * k) mod M) <= 2 ^ (k + 1) - mu -> |
| 522 |
fancy_reduce_muSelect_first (Partition.partition w (sz*2) x) = Partition.partition w sz (x mod M). |
| 523 |
Proof. |
| 524 |
intros. pose proof w_eq_22k. |
| 525 |
erewrite <-reduce_correct with (b:=2) (k:=k) (mu:=mu) by |
| 526 |
(eauto with nia; intros; try rewrite q3'_correct; try rewrite <-k_eq; eauto with nia ). |
| 527 |
reflexivity. |
| 528 |
Qed. |
| 529 |
|
| 530 |
Derive fancy_reduce' |
| 531 |
SuchThat ( |
| 532 |
forall x, |
| 533 |
0 <= x < M * 2^k -> |
| 534 |
2 * (2 ^ (2 * k) mod M) <= 2 ^ (k + 1) - mu -> |
| 535 |
fancy_reduce' (Partition.partition w (sz*2) x) = Partition.partition w sz (x mod M)) |
| 536 |
As fancy_reduce'_correct. |
| 537 |
Proof. |
| 538 |
intros. assert (k = width) as width_eq_k by nia. |
| 539 |
erewrite <-fancy_reduce_muSelect_first_correct by nia. |
| 540 |
cbv [fancy_reduce_muSelect_first q1 q3 shiftr r cond_subM]. |
| 541 |
break_match; try solve [exfalso; lia]. |
| 542 |
match goal with |- ?g ?x = ?rhs => |
| 543 |
let f := (match (eval pattern x in rhs) with ?f _ => f end) in |
| 544 |
assert (f = g); subst fancy_reduce'; reflexivity |
| 545 |
end. |
| 546 |
Qed. |
| 547 |
|
| 548 |
Definition fancy_reduce xLow xHigh := hd 0 (fancy_reduce' [xLow;xHigh]). |
| 549 |
|
| 550 |
Lemma partition_2 xLow xHigh : |
| 551 |
0 <= xLow < 2 ^ k -> |
| 552 |
0 <= xHigh < M -> |
| 553 |
Partition.partition w 2 (xLow + 2^k * xHigh) = [xLow;xHigh]. |
| 554 |
Proof. |
| 555 |
replace k with width in M_range |- * by nia; intros. cbv [Partition.partition map seq]. |
| 556 |
rewrite !uweight_S, !weight_0 by auto with zarith lia. |
| 557 |
autorewrite with zsimplify. |
| 558 |
rewrite <-Z.mod_pull_div by Z.zero_bounds. |
| 559 |
autorewrite with zsimplify. reflexivity. |
| 560 |
Qed. |
| 561 |
|
| 562 |
Lemma fancy_reduce_correct xLow xHigh : |
| 563 |
0 <= xLow < 2 ^ k -> |
| 564 |
0 <= xHigh < M -> |
| 565 |
2 * (2 ^ (2 * k) mod M) <= 2 ^ (k + 1) - mu -> |
| 566 |
fancy_reduce xLow xHigh = (xLow + 2^k * xHigh) mod M. |
| 567 |
Proof. |
| 568 |
assert (M < 2^width) by (replace width with k by nia; lia). |
| 569 |
assert (0 < 2 ^ (k - 1)) by Z.zero_bounds. |
| 570 |
pose proof (Z.mod_pos_bound (xLow + 2^k * xHigh) M ltac:(lia)). |
| 571 |
intros. cbv [fancy_reduce]. rewrite <-partition_2 by lia. |
| 572 |
replace 2%nat with (sz*2)%nat by lia. |
| 573 |
rewrite fancy_reduce'_correct by nia. |
| 574 |
rewrite sz_eq_1; cbv [Partition.partition map seq hd]. |
| 575 |
rewrite !uweight_S, !weight_0 by auto with zarith lia. |
| 576 |
autorewrite with zsimplify. reflexivity. |
| 577 |
Qed. |
| 578 |
End Def. |
| 579 |
End Fancy. |
| 580 |
End Fancy. |