| 1 |
From Coq Require Import ZArith Lia. |
| 2 |
From Coq Require Import List. |
| 3 |
Require Import Crypto.Algebra.Ring. |
| 4 |
Require Import Crypto.Arithmetic.Core. |
| 5 |
Require Import Crypto.Arithmetic.Partition. |
| 6 |
Require Import Crypto.Util.Decidable. |
| 7 |
Require Import Crypto.Util.LetIn. |
| 8 |
Require Import Crypto.Util.ListUtil. |
| 9 |
Require Import Crypto.Util.NatUtil. |
| 10 |
Require Import Crypto.Util.Prod. |
| 11 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 12 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 13 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 14 |
Require Import Crypto.Util.ZUtil.AddGetCarry Crypto.Util.ZUtil.MulSplit. |
| 15 |
Require Import Crypto.Util.ZUtil.Modulo Crypto.Util.ZUtil.Div. |
| 16 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 17 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 18 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. |
| 19 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 20 |
|
| 21 |
Require Import Crypto.Util.Notations. |
| 22 |
Import ListNotations. Local Open Scope Z_scope. |
| 23 |
|
| 24 |
Module Associational. |
| 25 |
Section Associational. |
| 26 |
|
| 27 |
Definition sat_multerm s (t t' : (Z * Z)) : list (Z * Z) := |
| 28 |
dlet_nd xy := Z.mul_split s (snd t) (snd t') in |
| 29 |
[(fst t * fst t', fst xy); (fst t * fst t' * s, snd xy)]. |
| 30 |
|
| 31 |
Definition sat_mul s (p q : list (Z * Z)) : list (Z * Z) := |
| 32 |
flat_map (fun t => flat_map (fun t' => sat_multerm s t t') q) p. |
| 33 |
|
| 34 |
Lemma eval_map_sat_multerm s a q (s_nonzero:s<>0): |
| 35 |
Associational.eval (flat_map (sat_multerm s a) q) = fst a * snd a * Associational.eval q. |
| 36 |
Proof using Type. |
| 37 |
cbv [sat_multerm Let_In]; induction q; |
| 38 |
repeat match goal with |
| 39 |
| _ => progress autorewrite with cancel_pair push_eval to_div_mod in * |
| 40 |
| _ => progress simpl flat_map |
| 41 |
| _ => rewrite IHq |
| 42 |
| _ => rewrite Z.mod_eq by assumption |
| 43 |
| _ => ring_simplify; lia |
| 44 |
end. |
| 45 |
Qed. |
| 46 |
Hint Rewrite eval_map_sat_multerm using (lia || assumption) : push_eval. |
| 47 |
|
| 48 |
Lemma eval_sat_mul s p q (s_nonzero:s<>0): |
| 49 |
Associational.eval (sat_mul s p q) = Associational.eval p * Associational.eval q. |
| 50 |
Proof using Type. |
| 51 |
cbv [sat_mul]; induction p; [reflexivity|]. |
| 52 |
repeat match goal with |
| 53 |
| _ => progress (autorewrite with push_flat_map push_eval in * ) |
| 54 |
| _ => rewrite IHp |
| 55 |
| _ => ring_simplify; lia |
| 56 |
end. |
| 57 |
Qed. |
| 58 |
Hint Rewrite eval_sat_mul : push_eval. |
| 59 |
|
| 60 |
Definition sat_multerm_const s (t t' : (Z * Z)) : list (Z * Z) := |
| 61 |
if snd t =? 1 |
| 62 |
then [(fst t * fst t', snd t')] |
| 63 |
else if snd t =? -1 |
| 64 |
then [(fst t * fst t', - snd t')] |
| 65 |
else if snd t =? 0 |
| 66 |
then nil |
| 67 |
else dlet_nd xy := Z.mul_split s (snd t) (snd t') in |
| 68 |
[(fst t * fst t', fst xy); (fst t * fst t' * s, snd xy)]. |
| 69 |
|
| 70 |
Definition sat_mul_const s (p q : list (Z * Z)) : list (Z * Z) := |
| 71 |
flat_map (fun t => flat_map (fun t' => sat_multerm_const s t t') q) p. |
| 72 |
|
| 73 |
Lemma eval_map_sat_multerm_const s a q (s_nonzero:s<>0): |
| 74 |
Associational.eval (flat_map (sat_multerm_const s a) q) = fst a * snd a * Associational.eval q. |
| 75 |
Proof using Type. |
| 76 |
cbv [sat_multerm_const Let_In]; induction q; |
| 77 |
repeat match goal with |
| 78 |
| _ => progress autorewrite with cancel_pair push_eval to_div_mod in * |
| 79 |
| _ => progress simpl flat_map |
| 80 |
| H : _ = 1 |- _ => rewrite H |
| 81 |
| H : _ = -1 |- _ => rewrite H |
| 82 |
| H : _ = 0 |- _ => rewrite H |
| 83 |
| _ => progress break_match; Z.ltb_to_lt |
| 84 |
| _ => rewrite IHq |
| 85 |
| _ => rewrite Z.mod_eq by assumption |
| 86 |
| _ => ring_simplify; lia |
| 87 |
end. |
| 88 |
Qed. |
| 89 |
Hint Rewrite eval_map_sat_multerm_const using (lia || assumption) : push_eval. |
| 90 |
|
| 91 |
Lemma eval_sat_mul_const s p q (s_nonzero:s<>0): |
| 92 |
Associational.eval (sat_mul_const s p q) = Associational.eval p * Associational.eval q. |
| 93 |
Proof using Type. |
| 94 |
cbv [sat_mul_const]; induction p; [reflexivity|]. |
| 95 |
repeat match goal with |
| 96 |
| _ => progress (autorewrite with push_flat_map push_eval in * ) |
| 97 |
| _ => rewrite IHp |
| 98 |
| _ => ring_simplify; lia |
| 99 |
end. |
| 100 |
Qed. |
| 101 |
Hint Rewrite eval_sat_mul_const : push_eval. |
| 102 |
End Associational. |
| 103 |
End Associational. |
| 104 |
|
| 105 |
Module Columns. |
| 106 |
Import Weight. |
| 107 |
Section Columns. |
| 108 |
Context weight {wprops : @weight_properties weight}. |
| 109 |
|
| 110 |
Definition eval n (x : list (list Z)) : Z := Positional.eval weight n (map sum x). |
| 111 |
|
| 112 |
Lemma eval_nil n : eval n [] = 0. |
| 113 |
Proof using Type. cbv [eval]; simpl. apply Positional.eval_nil. Qed. |
| 114 |
Hint Rewrite eval_nil : push_eval. |
| 115 |
Lemma eval_snoc n x y : n = length x -> eval (S n) (x ++ [y]) = eval n x + weight n * sum y. |
| 116 |
Proof using Type. |
| 117 |
cbv [eval]; intros; subst. rewrite map_app. simpl map. |
| 118 |
apply Positional.eval_snoc; distr_length. |
| 119 |
Qed. Hint Rewrite eval_snoc using (solve [distr_length]) : push_eval. |
| 120 |
|
| 121 |
Ltac cases := |
| 122 |
match goal with |
| 123 |
| |- _ /\ _ => split |
| 124 |
| H: _ /\ _ |- _ => destruct H |
| 125 |
| H: _ \/ _ |- _ => destruct H |
| 126 |
| _ => progress break_match; try discriminate |
| 127 |
end. |
| 128 |
|
| 129 |
Section Flatten. |
| 130 |
Context (add_split : Z -> Z -> Z -> Z * Z). |
| 131 |
Context (add_split_mod : |
| 132 |
forall s x y, fst (add_split s x y) = (x + y) mod s) |
| 133 |
(add_split_div : |
| 134 |
forall s x y, snd (add_split s x y) = (x + y) / s). |
| 135 |
Hint Rewrite add_split_mod add_split_div : to_div_mod. |
| 136 |
|
| 137 |
Section flatten_column. |
| 138 |
Context (fw : Z). (* maximum size of the result *) |
| 139 |
|
| 140 |
(* Outputs (sum, carry) *) |
| 141 |
Definition flatten_column (digit: list Z) : (Z * Z) := |
| 142 |
list_rect (fun _ => (Z * Z)%type) (0,0) |
| 143 |
(fun x tl flatten_column_tl => |
| 144 |
list_case |
| 145 |
(fun _ => (Z * Z)%type) (x mod fw, x / fw) |
| 146 |
(fun y tl' => |
| 147 |
list_case |
| 148 |
(fun _ => (Z * Z)%type) (add_split fw x y) |
| 149 |
(fun _ _ => |
| 150 |
dlet_nd rec := flatten_column_tl in (* recursively get the sum and carry *) |
| 151 |
dlet_nd sum_carry := add_split fw x (fst rec) in (* add the new value to the sum *) |
| 152 |
dlet_nd carry' := snd sum_carry + snd rec in (* add the two carries together *) |
| 153 |
(fst sum_carry, carry')) |
| 154 |
tl') |
| 155 |
tl) |
| 156 |
digit. |
| 157 |
End flatten_column. |
| 158 |
|
| 159 |
Definition flatten_step (digit:list Z) (acc_carry:list Z * Z) : list Z * Z := |
| 160 |
let acc := fst acc_carry in |
| 161 |
let carry := snd acc_carry in |
| 162 |
let fw := weight (S (length acc)) / weight (length acc) in |
| 163 |
dlet sum_carry := flatten_column fw (digit ++ [snd acc_carry]) in |
| 164 |
(acc ++ fst sum_carry :: nil, snd sum_carry). |
| 165 |
|
| 166 |
Definition flatten (xs : list (list Z)) : list Z * Z := |
| 167 |
fold_right (fun a b => flatten_step a b) (nil,0) (rev xs). |
| 168 |
|
| 169 |
Ltac push_fast := |
| 170 |
repeat match goal with |
| 171 |
| _ => progress cbv [Let_In list_case] |
| 172 |
| |- context [list_rect _ _ _ ?ls] => rewrite single_list_rect_to_match; destruct ls |
| 173 |
| _ => progress (unfold flatten_step in *; fold flatten_step in * ) |
| 174 |
| _ => rewrite Nat.add_1_r |
| 175 |
| _ => rewrite Z.mul_div_eq_full by (auto with zarith; lia) |
| 176 |
| _ => rewrite weight_multiples |
| 177 |
| _ => reflexivity |
| 178 |
| _ => solve [repeat (f_equal; try ring)] |
| 179 |
| _ => congruence |
| 180 |
| _ => progress cases |
| 181 |
end. |
| 182 |
Ltac push := |
| 183 |
repeat match goal with |
| 184 |
| _ => progress push_fast |
| 185 |
| _ => progress autorewrite with cancel_pair to_div_mod |
| 186 |
| _ => progress autorewrite with push_sum push_fold_right push_nth_default in * |
| 187 |
| _ => progress autorewrite with pull_Zmod pull_Zdiv zsimplify_fast |
| 188 |
| _ => progress autorewrite with list distr_length push_eval |
| 189 |
end. |
| 190 |
|
| 191 |
Lemma flatten_column_mod fw (xs : list Z) : |
| 192 |
fst (flatten_column fw xs) = sum xs mod fw. |
| 193 |
Proof using add_split_mod. |
| 194 |
induction xs; simpl flatten_column; cbv [Let_In]; |
| 195 |
repeat match goal with |
| 196 |
| _ => rewrite IHxs |
| 197 |
| _ => progress push |
| 198 |
end. |
| 199 |
Qed. Hint Rewrite flatten_column_mod : to_div_mod. |
| 200 |
|
| 201 |
Lemma flatten_column_div fw (xs : list Z) (fw_nz : fw <> 0) : |
| 202 |
snd (flatten_column fw xs) = sum xs / fw. |
| 203 |
Proof using add_split_div add_split_mod. |
| 204 |
(* this hint is already in the database but Z.div_add_l' is triggered first and that screws things up *) |
| 205 |
Hint Rewrite <- Z.div_add' using zutil_arith : pull_Zdiv. |
| 206 |
induction xs; simpl flatten_column; cbv [Let_In]; |
| 207 |
repeat match goal with |
| 208 |
| _ => rewrite IHxs |
| 209 |
| _ => rewrite <-Z.div_add' by zutil_arith |
| 210 |
| _ => rewrite Z.mul_div_eq_full by lia |
| 211 |
| _ => progress push |
| 212 |
end. |
| 213 |
Qed. Hint Rewrite flatten_column_div using auto with zarith : to_div_mod. |
| 214 |
|
| 215 |
Hint Rewrite Positional.eval_nil : push_eval. |
| 216 |
|
| 217 |
Lemma length_flatten_step digit state : |
| 218 |
length (fst (flatten_step digit state)) = S (length (fst state)). |
| 219 |
Proof using add_split_mod. cbv [flatten_step]; push. Qed. |
| 220 |
Hint Rewrite length_flatten_step : distr_length. |
| 221 |
Lemma length_flatten inp : length (fst (flatten inp)) = length inp. |
| 222 |
Proof using add_split_mod. |
| 223 |
cbv [flatten]. induction inp using rev_ind; push. |
| 224 |
Qed. |
| 225 |
Hint Rewrite length_flatten : distr_length. |
| 226 |
|
| 227 |
Lemma flatten_snoc x inp : flatten (inp ++ [x]) = flatten_step x (flatten inp). |
| 228 |
Proof using Type. cbv [flatten]. rewrite rev_unit. reflexivity. Qed. |
| 229 |
|
| 230 |
Lemma flatten_correct inp: |
| 231 |
forall n, |
| 232 |
length inp = n -> |
| 233 |
flatten inp = (Partition.partition weight n (eval n inp), |
| 234 |
eval n inp / (weight n)). |
| 235 |
Proof using wprops add_split_mod add_split_div. |
| 236 |
induction inp using rev_ind; intros; |
| 237 |
destruct n; distr_length; [ reflexivity | ]. |
| 238 |
rewrite flatten_snoc. |
| 239 |
rewrite partition_step. |
| 240 |
erewrite IHinp with (n:=n) by distr_length. |
| 241 |
push. |
| 242 |
pose proof (@weight_positive _ wprops n). |
| 243 |
repeat match goal with |
| 244 |
| |- pair _ _ = pair _ _ => f_equal |
| 245 |
| |- _ ++ _ = _ ++ _ => f_equal |
| 246 |
| |- _ :: _ = _ :: _ => f_equal |
| 247 |
| _ => apply (@partition_eq_mod _ wprops) |
| 248 |
| _ => rewrite length_partition |
| 249 |
| _ => rewrite weight_mod_pull_div by auto |
| 250 |
| _ => rewrite weight_div_pull_div by auto |
| 251 |
| _ => f_equal; ring |
| 252 |
| _ => progress autorewrite with zsimplify |
| 253 |
end. |
| 254 |
Qed. |
| 255 |
|
| 256 |
Lemma flatten_div_mod n inp : |
| 257 |
length inp = n -> |
| 258 |
(Positional.eval weight n (fst (flatten inp)) |
| 259 |
= (eval n inp) mod (weight n)) |
| 260 |
/\ (snd (flatten inp) = eval n inp / weight n). |
| 261 |
Proof using wprops add_split_mod add_split_div. |
| 262 |
intros. |
| 263 |
rewrite flatten_correct with (n:=n) by auto. |
| 264 |
cbn [fst snd]. |
| 265 |
rewrite eval_partition; auto. |
| 266 |
Qed. |
| 267 |
|
| 268 |
Lemma flatten_mod {n} inp : |
| 269 |
length inp = n -> |
| 270 |
(Positional.eval weight n (fst (flatten inp)) = (eval n inp) mod (weight n)). |
| 271 |
Proof using wprops add_split_mod add_split_div. |
| 272 |
apply flatten_div_mod. |
| 273 |
Qed. |
| 274 |
Hint Rewrite @flatten_mod : push_eval. |
| 275 |
|
| 276 |
Lemma flatten_div {n} inp : |
| 277 |
length inp = n -> snd (flatten inp) = eval n inp / weight n. |
| 278 |
Proof using wprops add_split_mod add_split_div. |
| 279 |
apply flatten_div_mod. |
| 280 |
Qed. |
| 281 |
Hint Rewrite @flatten_div : push_eval. |
| 282 |
|
| 283 |
Lemma flatten_same_sum p q : |
| 284 |
Forall2 (fun x y => sum x = sum y) p q -> |
| 285 |
flatten p = flatten q. |
| 286 |
Proof using wprops add_split_mod add_split_div. |
| 287 |
cbv [flatten]. |
| 288 |
let H := fresh in |
| 289 |
intro H; apply Forall2_rev in H; |
| 290 |
induction H; [ reflexivity | ]. |
| 291 |
push. |
| 292 |
Qed. |
| 293 |
End Flatten. |
| 294 |
|
| 295 |
Section FromAssociational. |
| 296 |
(* nils *) |
| 297 |
Definition nils n : list (list Z) := repeat nil n. |
| 298 |
Lemma length_nils n : length (nils n) = n. Proof using Type. cbv [nils]. distr_length. Qed. |
| 299 |
Hint Rewrite length_nils : distr_length. |
| 300 |
Lemma eval_nils n : eval n (nils n) = 0. |
| 301 |
Proof using Type. |
| 302 |
erewrite <-Positional.eval_zeros by eauto. |
| 303 |
cbv [eval nils]; rewrite List.map_repeat; reflexivity. |
| 304 |
Qed. Hint Rewrite eval_nils : push_eval. |
| 305 |
|
| 306 |
(* cons_to_nth *) |
| 307 |
Definition cons_to_nth i x (xs : list (list Z)) : list (list Z) := |
| 308 |
ListUtil.update_nth i (fun y => cons x y) xs. |
| 309 |
Lemma length_cons_to_nth i x xs : length (cons_to_nth i x xs) = length xs. |
| 310 |
Proof using Type. cbv [cons_to_nth]. distr_length. Qed. |
| 311 |
Hint Rewrite length_cons_to_nth : distr_length. |
| 312 |
Lemma cons_to_nth_add_to_nth xs : forall i x, |
| 313 |
map sum (cons_to_nth i x xs) = Positional.add_to_nth i x (map sum xs). |
| 314 |
Proof using Type. |
| 315 |
cbv [cons_to_nth]; induction xs as [|? ? IHxs]; |
| 316 |
intros i x; destruct i; simpl; rewrite ?IHxs; reflexivity. |
| 317 |
Qed. |
| 318 |
Lemma eval_cons_to_nth n i x xs : (i < length xs)%nat -> length xs = n -> |
| 319 |
eval n (cons_to_nth i x xs) = weight i * x + eval n xs. |
| 320 |
Proof using Type. |
| 321 |
cbv [eval]; intros. rewrite cons_to_nth_add_to_nth. |
| 322 |
apply Positional.eval_add_to_nth; distr_length. |
| 323 |
Qed. Hint Rewrite eval_cons_to_nth using (solve [distr_length]) : push_eval. |
| 324 |
|
| 325 |
Hint Rewrite Positional.eval_zeros : push_eval. |
| 326 |
Hint Rewrite Positional.eval_add_to_nth using (solve [distr_length]): push_eval. |
| 327 |
|
| 328 |
(* from_associational *) |
| 329 |
Definition from_associational n (p:list (Z*Z)) : list (list Z) := |
| 330 |
List.fold_right (fun t ls => |
| 331 |
dlet_nd p := Positional.place weight t (pred n) in |
| 332 |
cons_to_nth (fst p) (snd p) ls ) (nils n) p. |
| 333 |
Lemma length_from_associational n p : length (from_associational n p) = n. |
| 334 |
Proof using Type. cbv [from_associational Let_In]. apply fold_right_invariant; intros; distr_length. Qed. |
| 335 |
Hint Rewrite length_from_associational: distr_length. |
| 336 |
Lemma eval_from_associational n p (n_nonzero:n<>0%nat\/p=nil) : |
| 337 |
eval n (from_associational n p) = Associational.eval p. |
| 338 |
Proof using wprops. |
| 339 |
erewrite <-Positional.eval_from_associational by eauto with zarith. |
| 340 |
induction p; [ autorewrite with push_eval; solve [auto] |]. |
| 341 |
cbv [from_associational Positional.from_associational]; autorewrite with push_fold_right. |
| 342 |
fold (from_associational n p); fold (Positional.from_associational weight n p). |
| 343 |
cbv [Let_In]. |
| 344 |
match goal with |- context [Positional.place _ ?x ?n] => |
| 345 |
pose proof (Positional.place_in_range weight x n) end. |
| 346 |
repeat match goal with |
| 347 |
| _ => rewrite Nat.succ_pred in * by auto |
| 348 |
| _ => rewrite IHp by auto |
| 349 |
| _ => progress autorewrite with push_eval |
| 350 |
| _ => progress cases |
| 351 |
| _ => congruence |
| 352 |
end. |
| 353 |
Qed. |
| 354 |
|
| 355 |
Lemma from_associational_step n t p : |
| 356 |
from_associational n (t :: p) = |
| 357 |
cons_to_nth (fst (Positional.place weight t (Nat.pred n))) |
| 358 |
(snd (Positional.place weight t (Nat.pred n))) |
| 359 |
(from_associational n p). |
| 360 |
Proof using Type. reflexivity. Qed. |
| 361 |
End FromAssociational. |
| 362 |
|
| 363 |
Section Reverse. |
| 364 |
Definition reverse (p : list (list Z)) : list (list Z) := |
| 365 |
map (@rev Z) p. |
| 366 |
|
| 367 |
Lemma eval_reverse n p : |
| 368 |
eval n (reverse p) = eval n p. |
| 369 |
Proof. |
| 370 |
cbv [eval reverse]. rewrite map_map. |
| 371 |
f_equal. apply map_ext; intros. |
| 372 |
autorewrite with push_sum. reflexivity. |
| 373 |
Qed. Hint Rewrite @eval_reverse : push_eval. |
| 374 |
|
| 375 |
Lemma length_reverse p : |
| 376 |
length (reverse p) = length p. |
| 377 |
Proof. cbv [reverse]; distr_length. Qed. |
| 378 |
Hint Rewrite @length_reverse : distr_length. |
| 379 |
|
| 380 |
Lemma reverse_same_sum p : |
| 381 |
Forall2 (fun x y => sum x = sum y) (reverse p) p. |
| 382 |
Proof. |
| 383 |
cbv [reverse]. |
| 384 |
induction p; cbn [rev map]; constructor; |
| 385 |
autorewrite with push_sum; auto. |
| 386 |
Qed. |
| 387 |
End Reverse. |
| 388 |
End Columns. |
| 389 |
End Columns. |
| 390 |
|
| 391 |
Module Rows. |
| 392 |
Import Weight. |
| 393 |
Section Rows. |
| 394 |
Context weight {wprops : @weight_properties weight}. |
| 395 |
Local Notation rows := (list (list Z)) (only parsing). |
| 396 |
Local Notation cols := (list (list Z)) (only parsing). |
| 397 |
|
| 398 |
Hint Rewrite Positional.eval_nil Positional.eval0 @Positional.eval_snoc |
| 399 |
Positional.eval_to_associational |
| 400 |
Columns.eval_nil Columns.eval_snoc using (auto; solve [distr_length]) : push_eval. |
| 401 |
Hint Resolve in_eq in_cons : core. |
| 402 |
|
| 403 |
Definition eval n (inp : rows) := |
| 404 |
sum (map (Positional.eval weight n) inp). |
| 405 |
Lemma eval_nil n : eval n nil = 0. |
| 406 |
Proof using Type. cbv [eval]. rewrite map_nil, sum_nil; reflexivity. Qed. |
| 407 |
Hint Rewrite eval_nil : push_eval. |
| 408 |
Lemma eval0 x : eval 0 x = 0. |
| 409 |
Proof using Type. cbv [eval]. induction x; autorewrite with push_map push_sum push_eval; lia. Qed. |
| 410 |
Hint Rewrite eval0 : push_eval. |
| 411 |
Lemma eval_cons n r inp : eval n (r :: inp) = Positional.eval weight n r + eval n inp. |
| 412 |
Proof using Type. cbv [eval]; autorewrite with push_map push_sum; reflexivity. Qed. |
| 413 |
Hint Rewrite eval_cons : push_eval. |
| 414 |
Lemma eval_app n x y : eval n (x ++ y) = eval n x + eval n y. |
| 415 |
Proof using Type. cbv [eval]; autorewrite with push_map push_sum; reflexivity. Qed. |
| 416 |
Hint Rewrite eval_app : push_eval. |
| 417 |
|
| 418 |
Ltac In_cases := |
| 419 |
repeat match goal with |
| 420 |
| H: In _ (_ ++ _) |- _ => apply in_app_or in H; destruct H |
| 421 |
| H: In _ (_ :: _) |- _ => apply in_inv in H; destruct H |
| 422 |
| H: In _ nil |- _ => contradiction H |
| 423 |
| H: forall x, In x (?y :: ?ls) -> ?P |- _ => |
| 424 |
unique pose proof (H y ltac:(apply in_eq)); |
| 425 |
unique assert (forall x, In x ls -> P) by auto |
| 426 |
| H: forall x, In x (?ls ++ ?y :: nil) -> ?P |- _ => |
| 427 |
unique pose proof (H y ltac:(auto using in_or_app, in_eq)); |
| 428 |
unique assert (forall x, In x ls -> P) by eauto using in_or_app |
| 429 |
end. |
| 430 |
|
| 431 |
Section FromAssociational. |
| 432 |
(* extract row *) |
| 433 |
Definition extract_row (inp : cols) : cols * list Z := (map (fun c => tl c) inp, map (fun c => hd 0 c) inp). |
| 434 |
|
| 435 |
Lemma eval_extract_row (inp : cols): forall n, |
| 436 |
length inp = n -> |
| 437 |
Positional.eval weight n (snd (extract_row inp)) = Columns.eval weight n inp - Columns.eval weight n (fst (extract_row inp)) . |
| 438 |
Proof using Type. |
| 439 |
cbv [extract_row]. |
| 440 |
induction inp using rev_ind; [ | destruct n ]; |
| 441 |
repeat match goal with |
| 442 |
| _ => progress intros |
| 443 |
| _ => progress distr_length |
| 444 |
| _ => rewrite Positional.eval_snoc with (n:=n) by distr_length |
| 445 |
| _ => progress autorewrite with cancel_pair push_eval push_map in * |
| 446 |
| _ => ring |
| 447 |
end. |
| 448 |
rewrite IHinp by distr_length. |
| 449 |
destruct x; cbn [hd tl]; rewrite ?sum_nil, ?sum_cons; ring. |
| 450 |
Qed. Hint Rewrite eval_extract_row using (solve [distr_length]) : push_eval. |
| 451 |
|
| 452 |
Lemma length_fst_extract_row (inp : cols) : |
| 453 |
length (fst (extract_row inp)) = length inp. |
| 454 |
Proof using Type. cbv [extract_row]; autorewrite with cancel_pair; distr_length. Qed. |
| 455 |
Hint Rewrite length_fst_extract_row : distr_length. |
| 456 |
|
| 457 |
Lemma length_snd_extract_row (inp : cols) : |
| 458 |
length (snd (extract_row inp)) = length inp. |
| 459 |
Proof using Type. cbv [extract_row]; autorewrite with cancel_pair; distr_length. Qed. |
| 460 |
Hint Rewrite length_snd_extract_row : distr_length. |
| 461 |
|
| 462 |
(* max column size *) |
| 463 |
Definition max_column_size (x:cols) := fold_right (fun a b => Nat.max a b) 0%nat (map (fun c => length c) x). |
| 464 |
|
| 465 |
(* TODO: move to where list is defined *) |
| 466 |
Hint Rewrite @app_nil_l : list. |
| 467 |
Hint Rewrite <-@app_comm_cons: list. |
| 468 |
|
| 469 |
Lemma max_column_size_nil : max_column_size nil = 0%nat. |
| 470 |
Proof using Type. reflexivity. Qed. Hint Rewrite max_column_size_nil : push_max_column_size. |
| 471 |
Lemma max_column_size_cons col (inp : cols) : |
| 472 |
max_column_size (col :: inp) = Nat.max (length col) (max_column_size inp). |
| 473 |
Proof using Type. reflexivity. Qed. Hint Rewrite max_column_size_cons : push_max_column_size. |
| 474 |
Lemma max_column_size_app (x y : cols) : |
| 475 |
max_column_size (x ++ y) = Nat.max (max_column_size x) (max_column_size y). |
| 476 |
Proof using Type. induction x; autorewrite with list push_max_column_size; lia. Qed. |
| 477 |
Hint Rewrite max_column_size_app : push_max_column_size. |
| 478 |
Lemma max_column_size0 (inp : cols) : |
| 479 |
forall n, |
| 480 |
length inp = n -> (* this is not needed to make the lemma true, but prevents reliance on the implementation of Columns.eval*) |
| 481 |
max_column_size inp = 0%nat -> Columns.eval weight n inp = 0. |
| 482 |
Proof using Type. |
| 483 |
induction inp as [|x inp] using rev_ind; destruct n; try destruct x; intros; |
| 484 |
autorewrite with push_max_column_size push_eval push_sum distr_length in *; try lia. |
| 485 |
rewrite IHinp; distr_length; lia. |
| 486 |
Qed. |
| 487 |
|
| 488 |
(* from_columns *) |
| 489 |
Definition from_columns' n start_state : cols * rows := |
| 490 |
fold_right (fun _ (state : cols * rows) => |
| 491 |
let cols'_row := extract_row (fst state) in |
| 492 |
(fst cols'_row, snd state ++ [snd cols'_row]) |
| 493 |
) start_state (repeat 0 n). |
| 494 |
|
| 495 |
Definition from_columns (inp : cols) : rows := snd (from_columns' (max_column_size inp) (inp, [])). |
| 496 |
|
| 497 |
Local Ltac eval_from_columns'_with_length_t := |
| 498 |
cbv [from_columns']; |
| 499 |
first [ intros; apply fold_right_invariant; intros |
| 500 |
| apply fold_right_invariant ]; |
| 501 |
repeat match goal with |
| 502 |
| _ => progress (intros; subst) |
| 503 |
| _ => progress autorewrite with cancel_pair push_eval in * |
| 504 |
| _ => progress In_cases |
| 505 |
| _ => split; try lia |
| 506 |
| H: _ /\ _ |- _ => destruct H |
| 507 |
| _ => progress distr_length |
| 508 |
| _ => solve [auto] |
| 509 |
end. |
| 510 |
Lemma length_from_columns' m st n: |
| 511 |
(length (fst st) = n) -> |
| 512 |
length (fst (from_columns' m st)) = n /\ |
| 513 |
((forall r, In r (snd st) -> length r = n) -> |
| 514 |
forall r, In r (snd (from_columns' m st)) -> length r = n). |
| 515 |
Proof using Type. eval_from_columns'_with_length_t. Qed. |
| 516 |
Lemma eval_from_columns'_with_length m st n: |
| 517 |
(length (fst st) = n) -> |
| 518 |
length (fst (from_columns' m st)) = n /\ |
| 519 |
((forall r, In r (snd st) -> length r = n) -> |
| 520 |
forall r, In r (snd (from_columns' m st)) -> length r = n) /\ |
| 521 |
eval n (snd (from_columns' m st)) = Columns.eval weight n (fst st) + eval n (snd st) |
| 522 |
- Columns.eval weight n (fst (from_columns' m st)). |
| 523 |
Proof using Type. eval_from_columns'_with_length_t. Qed. |
| 524 |
Lemma length_fst_from_columns' m st : |
| 525 |
length (fst (from_columns' m st)) = length (fst st). |
| 526 |
Proof using Type. apply length_from_columns'; reflexivity. Qed. |
| 527 |
Hint Rewrite length_fst_from_columns' : distr_length. |
| 528 |
Lemma length_snd_from_columns' m st : |
| 529 |
(forall r, In r (snd st) -> length r = length (fst st)) -> |
| 530 |
forall r, In r (snd (from_columns' m st)) -> length r = length (fst st). |
| 531 |
Proof using Type. apply length_from_columns'; reflexivity. Qed. |
| 532 |
Hint Rewrite length_snd_from_columns' : distr_length. |
| 533 |
Lemma eval_from_columns' m st n : |
| 534 |
(length (fst st) = n) -> |
| 535 |
eval n (snd (from_columns' m st)) = Columns.eval weight n (fst st) + eval n (snd st) |
| 536 |
- Columns.eval weight n (fst (from_columns' m st)). |
| 537 |
Proof using Type. apply eval_from_columns'_with_length. Qed. |
| 538 |
Hint Rewrite eval_from_columns' using (auto; solve [distr_length]) : push_eval. |
| 539 |
|
| 540 |
Lemma max_column_size_extract_row inp : |
| 541 |
max_column_size (fst (extract_row inp)) = (max_column_size inp - 1)%nat. |
| 542 |
Proof using Type. |
| 543 |
cbv [extract_row]. autorewrite with cancel_pair. |
| 544 |
induction inp; [ reflexivity | ]. |
| 545 |
autorewrite with push_max_column_size push_map distr_length. |
| 546 |
rewrite IHinp. auto using Nat.sub_max_distr_r. |
| 547 |
Qed. |
| 548 |
Hint Rewrite max_column_size_extract_row : push_max_column_size. |
| 549 |
|
| 550 |
Lemma max_column_size_from_columns' m st : |
| 551 |
max_column_size (fst (from_columns' m st)) = (max_column_size (fst st) - m)%nat. |
| 552 |
Proof using Type. |
| 553 |
cbv [from_columns']; induction m; intros; cbn - [max_column_size extract_row]; |
| 554 |
autorewrite with push_max_column_size; lia. |
| 555 |
Qed. |
| 556 |
Hint Rewrite max_column_size_from_columns' : push_max_column_size. |
| 557 |
|
| 558 |
Lemma eval_from_columns (inp : cols) : |
| 559 |
forall n, length inp = n -> eval n (from_columns inp) = Columns.eval weight n inp. |
| 560 |
Proof using Type. |
| 561 |
intros; cbv [from_columns]; |
| 562 |
repeat match goal with |
| 563 |
| _ => progress autorewrite with cancel_pair push_eval push_max_column_size |
| 564 |
| _ => rewrite max_column_size0 with (inp := fst (from_columns' _ _)) by |
| 565 |
(autorewrite with push_max_column_size; distr_length) |
| 566 |
| _ => lia |
| 567 |
end. |
| 568 |
Qed. |
| 569 |
Hint Rewrite eval_from_columns using (auto; solve [distr_length]) : push_eval. |
| 570 |
|
| 571 |
Lemma length_from_columns inp: |
| 572 |
forall r, In r (from_columns inp) -> length r = length inp. |
| 573 |
Proof using Type. |
| 574 |
cbv [from_columns]; intros. |
| 575 |
change inp with (fst (inp, @nil (list Z))). |
| 576 |
eapply length_snd_from_columns'; eauto. |
| 577 |
autorewrite with cancel_pair; intros; In_cases. |
| 578 |
Qed. |
| 579 |
Hint Rewrite length_from_columns using eassumption : distr_length. |
| 580 |
|
| 581 |
(* from associational *) |
| 582 |
Definition from_associational n (p : list (Z * Z)) := from_columns (Columns.from_associational weight n p). |
| 583 |
|
| 584 |
Lemma eval_from_associational n p: (n <> 0%nat \/ p = nil) -> |
| 585 |
eval n (from_associational n p) = Associational.eval p. |
| 586 |
Proof using wprops. |
| 587 |
intros. cbv [from_associational]. |
| 588 |
rewrite eval_from_columns by auto using Columns.length_from_associational. |
| 589 |
auto using Columns.eval_from_associational. |
| 590 |
Qed. |
| 591 |
|
| 592 |
Lemma length_from_associational n p : |
| 593 |
forall r, In r (from_associational n p) -> length r = n. |
| 594 |
Proof using Type. |
| 595 |
cbv [from_associational]; intros. |
| 596 |
match goal with H: _ |- _ => apply length_from_columns in H end. |
| 597 |
rewrite Columns.length_from_associational in *; auto. |
| 598 |
Qed. |
| 599 |
|
| 600 |
Lemma max_column_size_zero_iff x : |
| 601 |
max_column_size x = 0%nat <-> (forall c, In c x -> c = nil). |
| 602 |
Proof using Type. |
| 603 |
cbv [max_column_size]; induction x; intros; [ cbn; tauto | ]. |
| 604 |
autorewrite with push_fold_right push_map. |
| 605 |
rewrite max_0_iff, IHx. |
| 606 |
split; intros; [ | rewrite length_zero_iff_nil; solve [auto] ]. |
| 607 |
match goal with H : _ /\ _ |- _ => destruct H end. |
| 608 |
In_cases; subst; auto using length0_nil. |
| 609 |
Qed. |
| 610 |
|
| 611 |
Lemma max_column_size_Columns_from_associational n p : |
| 612 |
n <> 0%nat -> p <> nil -> |
| 613 |
max_column_size (Columns.from_associational weight n p) <> 0%nat. |
| 614 |
Proof using Type. |
| 615 |
intros. |
| 616 |
rewrite max_column_size_zero_iff. |
| 617 |
intro. destruct p; [congruence | ]. |
| 618 |
rewrite Columns.from_associational_step in *. |
| 619 |
cbv [Columns.cons_to_nth] in *. |
| 620 |
match goal with H : forall c, In c (update_nth ?n ?f ?ls) -> _ |- _ => |
| 621 |
assert (n < length (update_nth n f ls))%nat; |
| 622 |
[ | specialize (H (nth n (update_nth n f ls) nil) ltac:(auto using nth_In)) ] |
| 623 |
end. |
| 624 |
{ distr_length. |
| 625 |
rewrite Columns.length_from_associational. |
| 626 |
remember (Nat.pred n) as m. replace n with (S m) by lia. |
| 627 |
apply Positional.place_in_range. } |
| 628 |
rewrite <-nth_default_eq in *. |
| 629 |
autorewrite with push_nth_default in *. |
| 630 |
rewrite eq_nat_dec_refl in *. |
| 631 |
congruence. |
| 632 |
Qed. |
| 633 |
|
| 634 |
Lemma from_associational_nonnil n p : |
| 635 |
n <> 0%nat -> p <> nil -> |
| 636 |
from_associational n p <> nil. |
| 637 |
Proof using Type. |
| 638 |
intros; cbv [from_associational from_columns from_columns']. |
| 639 |
pose proof (max_column_size_Columns_from_associational n p ltac:(auto) ltac:(auto)). |
| 640 |
case_eq (max_column_size (Columns.from_associational weight n p)); [lia|]. |
| 641 |
intros; cbn. |
| 642 |
rewrite <-length_zero_iff_nil. distr_length. |
| 643 |
Qed. |
| 644 |
End FromAssociational. |
| 645 |
|
| 646 |
Section Flatten. |
| 647 |
Local Notation fw := (fun i => weight (S i) / weight i) (only parsing). |
| 648 |
|
| 649 |
Section SumRows. |
| 650 |
Definition sum_rows' start_state (row1 row2 : list Z) : list Z * Z * nat := |
| 651 |
fold_right (fun next (state : list Z * Z * nat) => |
| 652 |
let i := snd state in |
| 653 |
let low_high' := |
| 654 |
let low_high := fst state in |
| 655 |
let low := fst low_high in |
| 656 |
let high := snd low_high in |
| 657 |
dlet_nd sum_carry := Z.add_with_get_carry_full (fw i) high (fst next) (snd next) in |
| 658 |
(low ++ [fst sum_carry], snd sum_carry) in |
| 659 |
(low_high', S i)) start_state (rev (combine row1 row2)). |
| 660 |
Definition sum_rows row1 row2 := fst (sum_rows' (nil, 0, 0%nat) row1 row2). |
| 661 |
|
| 662 |
Ltac push := |
| 663 |
repeat match goal with |
| 664 |
| _ => progress intros |
| 665 |
| _ => progress cbv [Let_In] |
| 666 |
| _ => rewrite Nat.add_1_r |
| 667 |
| _ => erewrite Positional.eval_snoc by eauto |
| 668 |
| H : length _ = _ |- _ => rewrite H |
| 669 |
| H: 0%nat = _ |- _ => rewrite <-H |
| 670 |
| [p := _ |- _] => subst p |
| 671 |
| _ => progress autorewrite with cancel_pair natsimplify push_sum_rows list |
| 672 |
| _ => progress autorewrite with cancel_pair in * |
| 673 |
| _ => progress distr_length |
| 674 |
| _ => progress break_match |
| 675 |
| _ => ring |
| 676 |
| _ => solve [ repeat (f_equal; try ring) ] |
| 677 |
| _ => tauto |
| 678 |
| _ => solve [eauto] |
| 679 |
end. |
| 680 |
|
| 681 |
Lemma sum_rows'_cons state x1 row1 x2 row2 : |
| 682 |
sum_rows' state (x1 :: row1) (x2 :: row2) = |
| 683 |
sum_rows' (fst (fst state) ++ [(snd (fst state) + x1 + x2) mod (fw (snd state))], |
| 684 |
(snd (fst state) + x1 + x2) / fw (snd state), |
| 685 |
S (snd state)) row1 row2. |
| 686 |
Proof using Type. |
| 687 |
cbv [sum_rows' Let_In]; autorewrite with push_combine. |
| 688 |
rewrite !fold_left_rev_right. cbn [fold_left]. |
| 689 |
autorewrite with cancel_pair to_div_mod. congruence. |
| 690 |
Qed. |
| 691 |
|
| 692 |
Lemma sum_rows'_nil state : |
| 693 |
sum_rows' state nil nil = state. |
| 694 |
Proof using Type. reflexivity. Qed. |
| 695 |
|
| 696 |
Hint Rewrite sum_rows'_cons sum_rows'_nil : push_sum_rows. |
| 697 |
|
| 698 |
Lemma sum_rows'_correct row1 : |
| 699 |
forall start_state nm row2 row1' row2', |
| 700 |
let m := snd start_state in |
| 701 |
let n := length row1 in |
| 702 |
length row2 = n -> |
| 703 |
length row1' = m -> |
| 704 |
length row2' = m -> |
| 705 |
length (fst (fst start_state)) = m -> |
| 706 |
nm = (n + m)%nat -> |
| 707 |
let eval := Positional.eval weight in |
| 708 |
snd (fst start_state) = (eval m row1' + eval m row2') / weight m -> |
| 709 |
(fst (fst start_state) = Partition.partition weight m (eval m row1' + eval m row2')) -> |
| 710 |
let sum := eval nm (row1' ++ row1) + eval nm (row2' ++ row2) in |
| 711 |
sum_rows' start_state row1 row2 |
| 712 |
= (Partition.partition weight nm sum, sum / weight nm, nm) . |
| 713 |
Proof using wprops. |
| 714 |
destruct start_state as [ [acc rem] m]. |
| 715 |
cbn [fst snd]. revert acc rem m. |
| 716 |
induction row1 as [|x1 row1]; |
| 717 |
destruct row2 as [|x2 row2]; intros; |
| 718 |
subst nm; push; [ congruence | ]. |
| 719 |
rewrite (app_cons_app_app _ row1'), (app_cons_app_app _ row2'). |
| 720 |
subst rem acc. |
| 721 |
apply IHrow1; clear IHrow1; |
| 722 |
repeat match goal with |
| 723 |
| _ => rewrite <-(Z.add_assoc _ x1 x2) |
| 724 |
| _ => rewrite div_step by auto using Z.gt_lt |
| 725 |
| _ => rewrite Z.mul_div_eq_full by auto |
| 726 |
| _ => rewrite weight_multiples by auto |
| 727 |
| _ => rewrite partition_step by auto |
| 728 |
| _ => rewrite weight_div_pull_div by auto |
| 729 |
| _ => rewrite weight_mod_pull_div by auto |
| 730 |
| _ => rewrite <-Z.div_add' by auto with zarith |
| 731 |
| _ => progress push |
| 732 |
end. |
| 733 |
f_equal; push; [ ]. |
| 734 |
apply (@partition_eq_mod _ wprops). |
| 735 |
push_Zmod. |
| 736 |
autorewrite with zsimplify_fast; reflexivity. |
| 737 |
Qed. |
| 738 |
|
| 739 |
Lemma sum_rows_correct row1: forall row2 n, |
| 740 |
length row1 = n -> length row2 = n -> |
| 741 |
let sum := Positional.eval weight n row1 + Positional.eval weight n row2 in |
| 742 |
sum_rows row1 row2 = (Partition.partition weight n sum, sum / weight n). |
| 743 |
Proof using wprops. |
| 744 |
cbv [sum_rows]; intros. |
| 745 |
erewrite sum_rows'_correct with (nm:=n) (row1':=nil) (row2':=nil)by (cbn; distr_length; reflexivity). |
| 746 |
reflexivity. |
| 747 |
Qed. |
| 748 |
|
| 749 |
Lemma sum_rows_mod n row1 row2 : |
| 750 |
length row1 = n -> length row2 = n -> |
| 751 |
Positional.eval weight n (fst (sum_rows row1 row2)) |
| 752 |
= (Positional.eval weight n row1 + Positional.eval weight n row2) mod (weight n). |
| 753 |
Proof using wprops. |
| 754 |
intros; erewrite sum_rows_correct by eauto. |
| 755 |
cbn [fst]. auto using eval_partition. |
| 756 |
Qed. |
| 757 |
|
| 758 |
Lemma length_sum_rows row1 row2 n: |
| 759 |
length row1 = n -> length row2 = n -> |
| 760 |
length (fst (sum_rows row1 row2)) = n. |
| 761 |
Proof using wprops. |
| 762 |
intros; erewrite sum_rows_correct by eauto. |
| 763 |
cbn [fst]. distr_length. |
| 764 |
Qed. Hint Rewrite length_sum_rows : distr_length. |
| 765 |
End SumRows. |
| 766 |
Hint Resolve length_sum_rows : core. |
| 767 |
Hint Rewrite sum_rows_mod using (auto; solve [distr_length; auto]) : push_eval. |
| 768 |
|
| 769 |
Definition flatten' (start_state : list Z * Z) (inp : rows) : list Z * Z := |
| 770 |
fold_right (fun next_row (state : list Z * Z)=> |
| 771 |
let out_carry := sum_rows (fst state) next_row in |
| 772 |
(fst out_carry, snd state + snd out_carry)) start_state inp. |
| 773 |
|
| 774 |
(* In order for the output to have the right length and bounds, |
| 775 |
we insert rows of zeroes if there are fewer than two rows. *) |
| 776 |
Definition flatten n (inp : rows) : list Z * Z := |
| 777 |
let default := Positional.zeros n in |
| 778 |
flatten' (hd default inp, 0) (hd default (tl inp) :: tl (tl inp)). |
| 779 |
|
| 780 |
Lemma flatten'_cons state r inp : |
| 781 |
flatten' state (r :: inp) = (fst (sum_rows (fst (flatten' state inp)) r), snd (flatten' state inp) + snd (sum_rows (fst (flatten' state inp)) r)). |
| 782 |
Proof using Type. cbv [flatten']; autorewrite with list push_fold_right. reflexivity. Qed. |
| 783 |
Lemma flatten'_snoc state r inp : |
| 784 |
flatten' state (inp ++ r :: nil) = flatten' (fst (sum_rows (fst state) r), snd state + snd (sum_rows (fst state) r)) inp. |
| 785 |
Proof using Type. cbv [flatten']; autorewrite with list push_fold_right. reflexivity. Qed. |
| 786 |
Lemma flatten'_nil state : flatten' state [] = state. Proof using Type. reflexivity. Qed. |
| 787 |
Hint Rewrite flatten'_cons flatten'_snoc flatten'_nil : push_flatten. |
| 788 |
|
| 789 |
Ltac push := |
| 790 |
repeat match goal with |
| 791 |
| _ => progress intros |
| 792 |
| _ => erewrite sum_rows_correct by (eassumption || distr_length; reflexivity) |
| 793 |
| _ => rewrite eval_partition by auto |
| 794 |
| H: length _ = _ |- _ => rewrite H |
| 795 |
| _ => progress autorewrite with cancel_pair push_flatten push_eval distr_length zsimplify_fast |
| 796 |
| _ => progress In_cases |
| 797 |
| |- _ /\ _ => split |
| 798 |
| |- context [?x mod ?y] => unique pose proof (Z.mul_div_eq_full x y ltac:(auto)); lia |
| 799 |
| _ => apply length_sum_rows |
| 800 |
| _ => solve [repeat (ring_simplify; f_equal; try ring)] |
| 801 |
| _ => congruence |
| 802 |
| _ => solve [eauto] |
| 803 |
end. |
| 804 |
|
| 805 |
Lemma flatten'_correct n inp : forall start_state, |
| 806 |
length (fst start_state) = n -> |
| 807 |
(forall row, In row inp -> length row = n) -> |
| 808 |
inp <> nil -> |
| 809 |
let sum := Positional.eval weight n (fst start_state) + eval n inp + weight n * snd start_state in |
| 810 |
flatten' start_state inp = (Partition.partition weight n sum, sum / weight n). |
| 811 |
Proof using wprops. |
| 812 |
induction inp using rev_ind; push. subst sum. |
| 813 |
destruct (dec (inp = nil)); [ subst inp; cbn | ]; |
| 814 |
repeat match goal with |
| 815 |
| _ => rewrite IHinp by push; clear IHinp |
| 816 |
| |- pair _ _ = pair _ _ => f_equal |
| 817 |
| _ => apply (@partition_eq_mod _ wprops) |
| 818 |
| _ => rewrite <-Z.div_add_l' by auto with zarith |
| 819 |
| _ => rewrite Z.mod_add'_full by lia |
| 820 |
| _ => rewrite Z.mul_div_eq_full by auto with zarith |
| 821 |
| _ => progress (push_Zmod; pull_Zmod) |
| 822 |
| _ => progress push |
| 823 |
end. |
| 824 |
Qed. |
| 825 |
|
| 826 |
Hint Rewrite (@Positional.length_zeros) : distr_length. |
| 827 |
Hint Rewrite (@Positional.eval_zeros) using auto : push_eval. |
| 828 |
|
| 829 |
Lemma flatten_correct inp n : |
| 830 |
(forall row, In row inp -> length row = n) -> |
| 831 |
flatten n inp = (Partition.partition weight n (eval n inp), eval n inp / weight n). |
| 832 |
Proof using wprops. |
| 833 |
intros; cbv [flatten]. |
| 834 |
destruct inp; [|destruct inp]; cbn [hd tl]; |
| 835 |
[ | | erewrite ?flatten'_correct ]; push. |
| 836 |
Qed. |
| 837 |
|
| 838 |
Lemma flatten_mod inp n : |
| 839 |
(forall row, In row inp -> length row = n) -> |
| 840 |
Positional.eval weight n (fst (flatten n inp)) = (eval n inp) mod (weight n). |
| 841 |
Proof using wprops. intros; rewrite flatten_correct; push. Qed. |
| 842 |
|
| 843 |
Lemma length_flatten n inp : |
| 844 |
(forall row, In row inp -> length row = n) -> |
| 845 |
length (fst (flatten n inp)) = n. |
| 846 |
Proof using wprops. intros; rewrite flatten_correct by assumption; push. Qed. |
| 847 |
End Flatten. |
| 848 |
Hint Rewrite length_flatten : distr_length. |
| 849 |
|
| 850 |
Section Ops. |
| 851 |
Definition add n p q := flatten n [p; q]. |
| 852 |
|
| 853 |
(* TODO: Although cleaner, using Positional.negate snd inserts |
| 854 |
dlets which prevent add-opp=>sub transformation in partial |
| 855 |
evaluation. Should probably either make partial evaluation |
| 856 |
handle that or remove the dlet in Positional.from_associational. |
| 857 |
|
| 858 |
NOTE(from jgross): I think partial evaluation now handles that |
| 859 |
fine; we should check this. *) |
| 860 |
Definition sub n p q := flatten n [p; map (fun x => dlet y := x in Z.opp y) q]. |
| 861 |
|
| 862 |
Definition conditional_add n mask cond (p q:list Z) := |
| 863 |
let qq := Positional.zselect mask cond q in |
| 864 |
add n p qq. |
| 865 |
|
| 866 |
(* Subtract q if and only if p >= q. *) |
| 867 |
Definition conditional_sub n (p q:list Z) := |
| 868 |
let '(v, c) := sub n p q in |
| 869 |
Positional.select (-c) v p. |
| 870 |
|
| 871 |
(* the carry will be 0 unless we underflow--we do the addition only |
| 872 |
in the underflow case *) |
| 873 |
Definition sub_then_maybe_add n mask (p q r:list Z) := |
| 874 |
let '(p_minus_q, c) := sub n p q in |
| 875 |
let rr := Positional.zselect mask (-c) r in |
| 876 |
let '(res, c') := add n p_minus_q rr in |
| 877 |
(res, c' - c). |
| 878 |
|
| 879 |
Hint Rewrite eval_cons eval_nil using solve [auto] : push_eval. |
| 880 |
|
| 881 |
Definition mul base n m (p q : list Z) := |
| 882 |
let p_a := Positional.to_associational weight n p in |
| 883 |
let q_a := Positional.to_associational weight n q in |
| 884 |
let pq_a := Associational.sat_mul base p_a q_a in |
| 885 |
flatten m (from_associational m pq_a). |
| 886 |
|
| 887 |
(* if [s] is not exactly equal to a weight, we must adjust it to |
| 888 |
be a weight, so that rather than dividing by s and |
| 889 |
multiplying by c, we divide by w and multiply by c*(w/s). |
| 890 |
See |
| 891 |
https://github.com/mit-plv/fiat-crypto/issues/326#issuecomment-404135131 |
| 892 |
for a bit more discussion *) |
| 893 |
Definition adjust_s fuel s : Z * bool := |
| 894 |
fold_right |
| 895 |
(fun w_i res |
| 896 |
=> let '(v, found_adjustment) := res in |
| 897 |
let res := (v, found_adjustment) in |
| 898 |
if found_adjustment:bool |
| 899 |
then res |
| 900 |
else if w_i mod s =? 0 |
| 901 |
then (w_i, true) |
| 902 |
else res) |
| 903 |
(s, false) |
| 904 |
(map weight (List.rev (seq 0 fuel))). |
| 905 |
|
| 906 |
(* TODO : move sat_reduce and repeat_sat_reduce to Saturated.Associational *) |
| 907 |
Definition sat_reduce base s c n (p : list (Z * Z)) := |
| 908 |
let '(s', _) := adjust_s (S (S n)) s in |
| 909 |
let lo_hi := Associational.split s' p in |
| 910 |
fst lo_hi ++ (Associational.sat_mul_const base [(1, s'/s)] (Associational.sat_mul_const base c (snd lo_hi))). |
| 911 |
|
| 912 |
Definition repeat_sat_reduce base s c (p : list (Z * Z)) n := |
| 913 |
fold_right (fun _ q => sat_reduce base s c n q) p (seq 0 n). |
| 914 |
|
| 915 |
Definition mulmod base s c n nreductions (p q : list Z) := |
| 916 |
let p_a := Positional.to_associational weight n p in |
| 917 |
let q_a := Positional.to_associational weight n q in |
| 918 |
let pq_a := Associational.sat_mul base p_a q_a in |
| 919 |
let r_a := repeat_sat_reduce base s c pq_a nreductions in |
| 920 |
flatten n (from_associational n r_a). |
| 921 |
|
| 922 |
Hint Rewrite Associational.eval_sat_mul_const Associational.eval_sat_mul Associational.eval_split using solve [auto] : push_eval. |
| 923 |
Hint Rewrite eval_from_associational using solve [auto] : push_eval. |
| 924 |
Ltac solver := |
| 925 |
intros; cbv [sub add mul mulmod sat_reduce]; |
| 926 |
rewrite ?flatten_correct by (intros; In_cases; subst; distr_length; eauto using length_from_associational); |
| 927 |
autorewrite with push_eval; ring_simplify_subterms; |
| 928 |
try reflexivity. |
| 929 |
|
| 930 |
Lemma add_partitions n p q : |
| 931 |
length p = n -> length q = n -> |
| 932 |
fst (add n p q) = Partition.partition weight n (Positional.eval weight n p + Positional.eval weight n q). |
| 933 |
Proof using wprops. solver. Qed. |
| 934 |
|
| 935 |
Lemma add_div n p q : |
| 936 |
length p = n -> length q = n -> |
| 937 |
snd (add n p q) = (Positional.eval weight n p + Positional.eval weight n q) / weight n. |
| 938 |
Proof using wprops. solver. Qed. |
| 939 |
|
| 940 |
Lemma conditional_add_partitions n mask cond p q : |
| 941 |
length p = n -> length q = n -> map (Z.land mask) q = q -> |
| 942 |
fst (conditional_add n mask cond p q) |
| 943 |
= Partition.partition weight n (Positional.eval weight n p + (if dec (cond = 0) then 0 else Positional.eval weight n q)). |
| 944 |
Proof using wprops. |
| 945 |
cbv [conditional_add]; intros; rewrite add_partitions by (distr_length; auto). |
| 946 |
autorewrite with push_eval; reflexivity. |
| 947 |
Qed. |
| 948 |
|
| 949 |
Lemma conditional_add_div n mask cond p q : |
| 950 |
length p = n -> length q = n -> map (Z.land mask) q = q -> |
| 951 |
snd (conditional_add n mask cond p q) = (Positional.eval weight n p + (if dec (cond = 0) then 0 else Positional.eval weight n q)) / weight n. |
| 952 |
Proof using wprops. |
| 953 |
cbv [conditional_add]; intros; rewrite add_div by (distr_length; auto). |
| 954 |
autorewrite with push_eval; auto. |
| 955 |
Qed. |
| 956 |
|
| 957 |
Lemma eval_map_opp q : |
| 958 |
forall n, length q = n -> |
| 959 |
Positional.eval weight n (map Z.opp q) = - Positional.eval weight n q. |
| 960 |
Proof using Type. |
| 961 |
induction q using rev_ind; intros; |
| 962 |
repeat match goal with |
| 963 |
| _ => progress autorewrite with push_map push_eval |
| 964 |
| _ => erewrite !Positional.eval_snoc with (n:=length q) by distr_length |
| 965 |
| _ => rewrite IHq by auto |
| 966 |
| _ => ring |
| 967 |
end. |
| 968 |
Qed. Hint Rewrite eval_map_opp using solve [auto]: push_eval. |
| 969 |
|
| 970 |
Lemma sub_partitions n p q : |
| 971 |
length p = n -> length q = n -> |
| 972 |
fst (sub n p q) = Partition.partition weight n (Positional.eval weight n p - Positional.eval weight n q). |
| 973 |
Proof using wprops. solver. Qed. |
| 974 |
|
| 975 |
Lemma sub_div n p q : |
| 976 |
length p = n -> length q = n -> |
| 977 |
snd (sub n p q) = (Positional.eval weight n p - Positional.eval weight n q) / weight n. |
| 978 |
Proof using wprops. solver. Qed. |
| 979 |
|
| 980 |
Lemma conditional_sub_partitions n p q |
| 981 |
(Hp : p = Partition.partition weight n (Positional.eval weight n p)) : |
| 982 |
length q = n -> |
| 983 |
0 <= Positional.eval weight n q < weight n -> |
| 984 |
conditional_sub n p q = Partition.partition weight n (if Positional.eval weight n q <=? Positional.eval weight n p then Positional.eval weight n p - Positional.eval weight n q else Positional.eval weight n p). |
| 985 |
Proof using wprops. |
| 986 |
cbv [conditional_sub]; intros. |
| 987 |
rewrite (surjective_pairing (sub _ _ _)). |
| 988 |
assert (length p = n) by (rewrite Hp; distr_length). |
| 989 |
assert (0 <= Positional.eval weight n p < weight n) by |
| 990 |
(rewrite Hp; autorewrite with push_eval; auto using Z.mod_pos_bound). |
| 991 |
rewrite sub_partitions, sub_div; distr_length. |
| 992 |
erewrite Positional.select_eq by (distr_length; eauto). |
| 993 |
rewrite Z.div_sub_small, Z.ltb_antisym by lia. |
| 994 |
destruct (Positional.eval weight n q <=? Positional.eval weight n p); |
| 995 |
cbn [negb]; autorewrite with zsimplify_fast; |
| 996 |
break_match; try lia; congruence. |
| 997 |
Qed. |
| 998 |
|
| 999 |
Let sub_then_maybe_add_Z a b c := |
| 1000 |
a - b + (if (a - b <? 0) then c else 0). |
| 1001 |
|
| 1002 |
Lemma sub_then_maybe_add_partitions n mask p q r : |
| 1003 |
length p = n -> length q = n -> length r = n -> |
| 1004 |
map (Z.land mask) r = r -> |
| 1005 |
0 <= Positional.eval weight n p < weight n -> |
| 1006 |
0 <= Positional.eval weight n q < weight n -> |
| 1007 |
fst (sub_then_maybe_add n mask p q r) = Partition.partition weight n (sub_then_maybe_add_Z (Positional.eval weight n p) (Positional.eval weight n q) (Positional.eval weight n r)). |
| 1008 |
Proof using wprops. |
| 1009 |
cbv [sub_then_maybe_add]. subst sub_then_maybe_add_Z. |
| 1010 |
intros. |
| 1011 |
rewrite (surjective_pairing (sub _ _ _)). |
| 1012 |
rewrite (surjective_pairing (add _ _ _)). |
| 1013 |
cbn [fst snd]. |
| 1014 |
rewrite sub_partitions, add_partitions, sub_div by distr_length. |
| 1015 |
autorewrite with push_eval. |
| 1016 |
Z.rewrite_mod_small. |
| 1017 |
rewrite Z.div_sub_small by lia. |
| 1018 |
break_innermost_match; Z.ltb_to_lt; try lia; |
| 1019 |
auto using partition_eq_mod with zarith. |
| 1020 |
Qed. |
| 1021 |
|
| 1022 |
Lemma mul_partitions base n m p q : |
| 1023 |
base <> 0 -> m <> 0%nat -> length p = n -> length q = n -> |
| 1024 |
fst (mul base n m p q) = Partition.partition weight m (Positional.eval weight n p * Positional.eval weight n q). |
| 1025 |
Proof using wprops. solver. Qed. |
| 1026 |
|
| 1027 |
Lemma mul_div base n m p q : |
| 1028 |
base <> 0 -> m <> 0%nat -> length p = n -> length q = n -> |
| 1029 |
snd (mul base n m p q) = (Positional.eval weight n p * Positional.eval weight n q) / weight m. |
| 1030 |
Proof using wprops. solver. Qed. |
| 1031 |
|
| 1032 |
Lemma length_mul base n m p q : |
| 1033 |
length p = n -> length q = n -> |
| 1034 |
length (fst (mul base n m p q)) = m. |
| 1035 |
Proof using wprops. solver; cbn [fst snd]; distr_length. Qed. |
| 1036 |
|
| 1037 |
Lemma adjust_s_invariant fuel s (s_nz:s<>0) : |
| 1038 |
fst (adjust_s fuel s) mod s = 0 |
| 1039 |
/\ fst (adjust_s fuel s) <> 0. |
| 1040 |
Proof using wprops. |
| 1041 |
cbv [adjust_s]; rewrite fold_right_map; generalize (List.rev (seq 0 fuel)); intro ls; induction ls as [|l ls IHls]; |
| 1042 |
cbn. |
| 1043 |
{ rewrite Z.mod_same by assumption; auto. } |
| 1044 |
{ break_match; cbn in *; auto with zarith. } |
| 1045 |
Qed. |
| 1046 |
|
| 1047 |
Lemma eval_sat_reduce base s c n p : |
| 1048 |
base <> 0 -> s - Associational.eval c <> 0 -> s <> 0 -> |
| 1049 |
Associational.eval (sat_reduce base s c n p) mod (s - Associational.eval c) |
| 1050 |
= Associational.eval p mod (s - Associational.eval c). |
| 1051 |
Proof using wprops. |
| 1052 |
intros; cbv [sat_reduce]. |
| 1053 |
lazymatch goal with |- context[adjust_s ?fuel ?s] => destruct (adjust_s_invariant fuel s ltac:(assumption)) as [Hmod ?] end. |
| 1054 |
eta_expand; autorewrite with push_eval zsimplify_const; cbn [fst snd]. |
| 1055 |
rewrite !Z.mul_assoc, <- (Z.mul_comm (Associational.eval c)), <- !Z.mul_assoc, <-Associational.reduction_rule by auto. |
| 1056 |
autorewrite with zsimplify_const; rewrite !Z.mul_assoc, Z.mul_div_eq_full, Hmod by auto. |
| 1057 |
autorewrite with zsimplify_const push_eval; trivial. |
| 1058 |
Qed. |
| 1059 |
Hint Rewrite eval_sat_reduce using auto : push_eval. |
| 1060 |
|
| 1061 |
Lemma eval_repeat_sat_reduce base s c p n : |
| 1062 |
base <> 0 -> s - Associational.eval c <> 0 -> s <> 0 -> |
| 1063 |
Associational.eval (repeat_sat_reduce base s c p n) mod (s - Associational.eval c) |
| 1064 |
= Associational.eval p mod (s - Associational.eval c). |
| 1065 |
Proof using wprops. |
| 1066 |
intros; cbv [repeat_sat_reduce]. |
| 1067 |
apply fold_right_invariant; intros; autorewrite with push_eval; auto. |
| 1068 |
Qed. |
| 1069 |
Hint Rewrite eval_repeat_sat_reduce using auto : push_eval. |
| 1070 |
|
| 1071 |
Lemma eval_mulmod base s c n nreductions p q : |
| 1072 |
base <> 0 -> s <> 0 -> s - Associational.eval c <> 0 -> |
| 1073 |
n <> 0%nat -> length p = n -> length q = n -> |
| 1074 |
(Positional.eval weight n (fst (mulmod base s c n nreductions p q)) |
| 1075 |
+ weight n * (snd (mulmod base s c n nreductions p q))) mod (s - Associational.eval c) |
| 1076 |
= (Positional.eval weight n p * Positional.eval weight n q) mod (s - Associational.eval c). |
| 1077 |
Proof using wprops. |
| 1078 |
solver. cbn [fst snd]. |
| 1079 |
rewrite eval_partition by auto. |
| 1080 |
rewrite <-Z.div_mod'' by auto with zarith. |
| 1081 |
autorewrite with push_eval; reflexivity. |
| 1082 |
Qed. |
| 1083 |
|
| 1084 |
(* returns all-but-lowest-limb and lowest limb *) |
| 1085 |
Definition divmod (p : list Z) : list Z * Z |
| 1086 |
:= (tl p, hd 0 p). |
| 1087 |
End Ops. |
| 1088 |
End Rows. |
| 1089 |
#[global] |
| 1090 |
Hint Rewrite length_from_columns using eassumption : distr_length. |
| 1091 |
#[global] |
| 1092 |
Hint Rewrite length_sum_rows using solve [ reflexivity | eassumption | distr_length; eauto ] : distr_length. |
| 1093 |
#[global] |
| 1094 |
Hint Rewrite length_fst_extract_row length_snd_extract_row length_flatten length_fst_from_columns' length_snd_from_columns' : distr_length. |
| 1095 |
End Rows. |