| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import QArith. |
| 3 |
From Coq Require Import Qround. |
| 4 |
From Coq Require Import String. |
| 5 |
From Coq Require Import Derive. |
| 6 |
From Coq Require Import List. |
| 7 |
Require Import Crypto.Util.ZRange. |
| 8 |
Require Import Crypto.Arithmetic.Core. |
| 9 |
Require Import Crypto.Arithmetic.ModOps. |
| 10 |
Require Import Crypto.Arithmetic.Partition. |
| 11 |
Require Import Crypto.Arithmetic.UniformWeight. |
| 12 |
Require Import Crypto.Arithmetic.Saturated. |
| 13 |
Require Import (*hints*) Coq.btauto.Algebra. |
| 14 |
From Coq Require OrdersEx. |
| 15 |
Require Import Crypto.Util.ListUtil.StdlibCompat. |
| 16 |
Require Import Crypto.Util.ZUtil.ModInv. |
| 17 |
|
| 18 |
From Coq Require Import Lia. |
| 19 |
Require Import Crypto.Algebra.Ring. |
| 20 |
Require Import Crypto.Util.Decidable. |
| 21 |
Require Import Crypto.Util.LetIn. |
| 22 |
Require Import Crypto.Util.ListUtil. |
| 23 |
Require Import Crypto.Util.ListUtil.FoldBool. |
| 24 |
Require Import Crypto.Util.NatUtil. |
| 25 |
Require Import Crypto.Util.Prod. |
| 26 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 27 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 28 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 29 |
Require Import Crypto.Util.ZUtil.AddGetCarry Crypto.Util.ZUtil.MulSplit. |
| 30 |
Require Import Crypto.Util.ZUtil.Modulo Crypto.Util.ZUtil.Div. |
| 31 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 32 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 33 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. |
| 34 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 35 |
From Coq Require Import Znat. |
| 36 |
|
| 37 |
Require Import Crypto.Util.CPSUtil. |
| 38 |
Require Import Crypto.Util.CPSNotations. |
| 39 |
Local Open Scope cps_scope. |
| 40 |
Notation "x' <- v ; C" := (v (fun x' => C)) (only parsing). |
| 41 |
|
| 42 |
Require Import Crypto.Util.Notations. |
| 43 |
Local Open Scope string_scope. |
| 44 |
Local Open Scope list_scope. |
| 45 |
Import ListNotations. Local Open Scope Z_scope. |
| 46 |
|
| 47 |
Import Associational Positional. |
| 48 |
|
| 49 |
Local Coercion Z.of_nat : nat >-> Z. |
| 50 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 51 |
Local Coercion Z.pos : positive >-> Z. |
| 52 |
|
| 53 |
Module SolinasReduction. |
| 54 |
|
| 55 |
Import Core.Associational. |
| 56 |
Import Core.Positional. |
| 57 |
|
| 58 |
Section __. |
| 59 |
|
| 60 |
Context (machine_wordsize := 64) |
| 61 |
(weight := uweight machine_wordsize) |
| 62 |
(up_bound := 2 ^ (machine_wordsize / 4)) |
| 63 |
{wprops : @weight_properties weight}. |
| 64 |
|
| 65 |
Ltac weight_comp := |
| 66 |
unfold weight, uweight, ModOps.weight, machine_wordsize; |
| 67 |
try rewrite !Z.div_1_r; |
| 68 |
try rewrite !Z.opp_involutive; |
| 69 |
try rewrite !Nat2Z.inj_succ; |
| 70 |
try rewrite !OrdersEx.Z_as_OT.mul_succ_r; |
| 71 |
try rewrite !OrdersEx.Z_as_OT.pow_add_r; |
| 72 |
autorewrite with zsimplify_const; |
| 73 |
ring_simplify. |
| 74 |
|
| 75 |
Ltac solve_ineq := |
| 76 |
repeat |
| 77 |
match goal with |
| 78 |
| |- 0 <= _ + _ => apply OrdersEx.Z_as_OT.add_nonneg_nonneg |
| 79 |
| |- 0 < _ * _ => apply OrdersEx.Z_as_OT.mul_pos_pos |
| 80 |
| |- 0 <= _ * _ => apply OrdersEx.Z_as_OT.mul_nonneg_nonneg |
| 81 |
| |- 0 <= _ / _ => apply OrdersEx.Z_as_OT.div_pos |
| 82 |
| |- _ / _ < _ => apply OrdersEx.Z_as_OT.div_lt_upper_bound |
| 83 |
|
| 84 |
| |- _ + ?x < _ + ?x => apply OrdersEx.Z_as_OT.add_lt_mono_r |
| 85 |
| |- _ + _ < _ => apply OrdersEx.Z_as_OT.add_lt_mono |
| 86 |
| |- _ + _ <= _ => apply OrdersEx.Z_as_OT.add_le_mono |
| 87 |
| |- _ - ?x < _ - ?x => rewrite <-OrdersEx.Z_as_OT.sub_lt_mono_r |
| 88 |
| |- _ - ?x <= _ - ?x => rewrite <-OrdersEx.Z_as_OT.sub_le_mono_r |
| 89 |
|
| 90 |
| _ => apply Z.mod_small |
| 91 |
| |- _ mod (?x * ?y) < (?y * ?x) => rewrite Z.mul_comm with (n:=x) |
| 92 |
| _ => apply OrdersEx.Z_as_OT.mod_pos_bound |
| 93 |
(* | [ |- 0 <= _ mod _ ] => apply Z_mod_nonneg_nonneg *) |
| 94 |
| [ |- 0 <= weight _ ] => apply OrdersEx.Z_as_OT.lt_le_incl; auto |
| 95 |
|
| 96 |
| _ => split |
| 97 |
| _ => lia |
| 98 |
end. |
| 99 |
|
| 100 |
Ltac le_lt := |
| 101 |
(rewrite Le.Z.le_sub_1_iff || rewrite <-Le.Z.le_sub_1_iff). |
| 102 |
|
| 103 |
Hint Rewrite Nat.add_0_l : const_simpl. |
| 104 |
Hint Rewrite Nat.add_0_r : const_simpl. |
| 105 |
Hint Rewrite Z.add_0_l : const_simpl. |
| 106 |
Hint Rewrite Z.add_0_r : const_simpl. |
| 107 |
Lemma S_sub_1 : forall (n : nat), |
| 108 |
(n > 0)%nat -> |
| 109 |
S (n - 1)%nat = n. |
| 110 |
Proof using Type. lia. Qed. |
| 111 |
Hint Rewrite S_sub_1 using lia : const_simpl. |
| 112 |
Lemma Sn_sub_n : forall (n : nat), |
| 113 |
(S n - n)%nat = 1%nat. |
| 114 |
Proof using Type. lia. Qed. |
| 115 |
Hint Rewrite Sn_sub_n : const_simpl. |
| 116 |
Lemma n2_sub : forall (n : nat), |
| 117 |
(2 * n - n)%nat = n. |
| 118 |
Proof using Type. lia. Qed. |
| 119 |
Hint Rewrite n2_sub : const_simpl. |
| 120 |
Ltac const_simpl := |
| 121 |
autorewrite with const_simpl in *. |
| 122 |
|
| 123 |
Hint Rewrite eval_cons using auto : push_eval. |
| 124 |
Hint Rewrite eval_sat_mul using lia : push_eval. |
| 125 |
Hint Rewrite eval_sat_mul_const using lia : push_eval. |
| 126 |
Hint Rewrite eval_split using auto : push_eval. |
| 127 |
Hint Rewrite Rows.eval_from_associational using (auto || lia) : push_eval. |
| 128 |
Hint Rewrite Rows.flatten_mod using (eauto using Rows.length_from_associational) : push_eval. |
| 129 |
Hint Rewrite Rows.flatten_correct using (eauto using Rows.length_from_associational) : push_eval. |
| 130 |
Hint Rewrite eval_add_to_nth using auto : push_eval. |
| 131 |
|
| 132 |
Hint Rewrite @nil_length0 cons_length app_length seq_length map_length firstn_length @skipn_length length_partition length_add_to_nth : push_length. |
| 133 |
Hint Rewrite (@ListUtil.length_snoc) : push_length. |
| 134 |
Hint Rewrite Rows.length_flatten using (eauto using Rows.length_from_associational) : push_length. |
| 135 |
|
| 136 |
Hint Rewrite map_nil map_cons map_app map_map in_map_iff : push_misc. |
| 137 |
Hint Rewrite @combine_app_samelength : push_misc. |
| 138 |
Hint Rewrite @combine_nil_r @combine_cons : push_misc. |
| 139 |
Hint Rewrite @fold_right_cons fold_right_app : push_misc. |
| 140 |
Hint Rewrite seq_add : push_misc. |
| 141 |
Hint Rewrite split_app : push_misc. |
| 142 |
Hint Rewrite @nth_default_cons_S : push_misc. |
| 143 |
Hint Rewrite @firstn_map firstn_seq firstn_app : push_misc. |
| 144 |
Hint Rewrite @skipn_app @skipn_0 : push_misc. |
| 145 |
Hint Rewrite @fst_pair @snd_pair : push_misc. |
| 146 |
Hint Rewrite app_nil_r app_nil_l : push_misc. |
| 147 |
Hint Rewrite Nat.sub_diag : push_misc. |
| 148 |
|
| 149 |
Hint Resolve in_or_app : core. |
| 150 |
Hint Resolve in_eq : core. |
| 151 |
Hint Resolve in_cons : core. |
| 152 |
|
| 153 |
Hint Unfold eval : unfold_eval. |
| 154 |
Hint Unfold Associational.eval : unfold_eval. |
| 155 |
Hint Unfold to_associational : unfold_eval. |
| 156 |
|
| 157 |
Ltac push := |
| 158 |
autorewrite with push_eval push_length push_misc zsimplify_const; |
| 159 |
auto. |
| 160 |
|
| 161 |
Ltac push' H := |
| 162 |
autorewrite with push_eval push_length push_misc zsimplify_const in H; |
| 163 |
auto. |
| 164 |
|
| 165 |
Lemma seq_double : forall n, |
| 166 |
seq 0 (2 * n) = seq 0 n ++ seq n n. |
| 167 |
Proof using Type. |
| 168 |
intros n; replace (2*n)%nat with (n+n)%nat; push; lia. |
| 169 |
Qed. |
| 170 |
Hint Rewrite seq_double : push_misc. |
| 171 |
|
| 172 |
Lemma map_weight_seq : forall m p, |
| 173 |
map weight (seq 0 p) = map (fun t => t / (weight m)) (map weight (seq m p)). |
| 174 |
Proof using wprops. |
| 175 |
induction m as [| m IHm]; intros; push. |
| 176 |
erewrite map_ext. |
| 177 |
eauto. |
| 178 |
intros. |
| 179 |
cbn. |
| 180 |
rewrite Z.div_1_r. |
| 181 |
lia. |
| 182 |
|
| 183 |
rewrite IHm. |
| 184 |
rewrite <-seq_shift. |
| 185 |
push. |
| 186 |
apply map_ext_in. |
| 187 |
intros a H. |
| 188 |
rewrite in_seq in H. |
| 189 |
weight_comp; try lia. |
| 190 |
rewrite <-!Z.pow_add_r; try lia. |
| 191 |
rewrite <-!Z.pow_sub_r; try lia. |
| 192 |
f_equal. |
| 193 |
lia. |
| 194 |
Qed. |
| 195 |
Hint Rewrite <-map_weight_seq : push_misc. |
| 196 |
|
| 197 |
Lemma seq_shift_1 : forall len, |
| 198 |
map S (seq 0 len) = seq 1 len. |
| 199 |
Proof using Type. |
| 200 |
intros. |
| 201 |
apply seq_shift. |
| 202 |
Qed. |
| 203 |
Hint Rewrite <-seq_shift_1 : push_misc. |
| 204 |
|
| 205 |
(* SECTION CANONICAL_REPR *) |
| 206 |
|
| 207 |
Definition canonical_repr n (p : list Z) : Prop := |
| 208 |
length p = n /\ |
| 209 |
p = Partition.partition weight n (Positional.eval weight n p). |
| 210 |
|
| 211 |
Lemma canonical_pos n : forall (p : list Z), |
| 212 |
canonical_repr n p -> |
| 213 |
0 <= eval weight n p. |
| 214 |
Proof using wprops. |
| 215 |
intros; |
| 216 |
repeat match goal with |
| 217 |
| H : canonical_repr _ _ |- _ => |
| 218 |
unfold canonical_repr in H; |
| 219 |
destruct H as [ _ H ]; |
| 220 |
rewrite H; |
| 221 |
rewrite Partition.eval_partition |
| 222 |
| _ => apply Z.mod_pos_bound |
| 223 |
| _ => auto |
| 224 |
end. |
| 225 |
Qed. |
| 226 |
|
| 227 |
Lemma canonical_bounded n : forall (p : list Z), |
| 228 |
canonical_repr n p -> |
| 229 |
forall x, In x p -> 0 <= x < 2 ^ machine_wordsize. |
| 230 |
Proof using wprops. |
| 231 |
intros; |
| 232 |
repeat multimatch goal with |
| 233 |
| H : canonical_repr ?n ?p |- _ => |
| 234 |
pose proof (canonical_pos n p H); |
| 235 |
cbv [canonical_repr Partition.partition] in H; |
| 236 |
destruct H as [ Hlen Hpart ] |
| 237 |
| H1 : In _ ?p, H2 : ?p = _ |- _ => |
| 238 |
rewrite H2 in H1; |
| 239 |
autorewrite with push_misc in H1 |
| 240 |
| H : context[exists _, _] |- _ => destruct H |
| 241 |
| H : _ = ?x |- 0 <= ?x => rewrite <-H |
| 242 |
| H : _ = ?x |- ?x < _ => rewrite <-H |
| 243 |
| _ => unfold weight; rewrite uweight_S; fold weight |
| 244 |
| _ => solve_ineq |
| 245 |
| _ => progress intuition |
| 246 |
| _ => auto || lia |
| 247 |
end. |
| 248 |
Qed. |
| 249 |
|
| 250 |
Lemma canonical_iff p n : |
| 251 |
canonical_repr n p <-> |
| 252 |
length p = n /\ |
| 253 |
forall x, In x p -> 0 <= x < 2 ^ machine_wordsize. |
| 254 |
Proof using wprops. |
| 255 |
split; intros; |
| 256 |
repeat multimatch goal with |
| 257 |
| H : length _ = _ |- _ => rewrite H |
| 258 |
| |- length _ = _ => unfold canonical_repr in * |
| 259 |
| |- _ = Partition.partition _ _ _ => unfold canonical_repr in * |
| 260 |
| |- canonical_repr _ _ => unfold canonical_repr |
| 261 |
| _ => eapply canonical_bounded |
| 262 |
| _ => progress intuition |
| 263 |
| _ => eauto || lia |
| 264 |
end. |
| 265 |
apply uweight_partition_unique. |
| 266 |
lia. |
| 267 |
lia. |
| 268 |
intros. |
| 269 |
rewrite Le.Z.le_sub_1_iff. |
| 270 |
auto. |
| 271 |
Qed. |
| 272 |
|
| 273 |
Lemma canonical_cons n a p: |
| 274 |
canonical_repr (S n) (a :: p) -> |
| 275 |
canonical_repr n p. |
| 276 |
Proof using wprops. |
| 277 |
intros. |
| 278 |
rewrite canonical_iff in *. |
| 279 |
intuition; |
| 280 |
repeat multimatch goal with |
| 281 |
| H : context[_ <= _ < _] |- _ => apply H |
| 282 |
| _ => cbn |
| 283 |
| _ => auto |
| 284 |
end. |
| 285 |
Qed. |
| 286 |
|
| 287 |
Lemma canonical_app_l n n1 n2 l l1 l2 : |
| 288 |
canonical_repr n l -> |
| 289 |
length l1 = n1 -> |
| 290 |
length l2 = n2 -> |
| 291 |
n = (n1 + n2)%nat -> |
| 292 |
l = l1 ++ l2 -> |
| 293 |
canonical_repr n1 l1. |
| 294 |
Proof using wprops. |
| 295 |
intros. |
| 296 |
rewrite canonical_iff in *; intuition; |
| 297 |
repeat multimatch goal with |
| 298 |
| H : context[_ <= _ < _] |- _ => apply H |
| 299 |
| H : ?x = _ ++ _ |- In _ ?x => rewrite H |
| 300 |
| _ => cbn |
| 301 |
| _ => auto |
| 302 |
end. |
| 303 |
Qed. |
| 304 |
|
| 305 |
Lemma canonical_app_r n n1 n2 l l1 l2 : |
| 306 |
canonical_repr n l -> |
| 307 |
length l1 = n1 -> |
| 308 |
length l2 = n2 -> |
| 309 |
n = (n1 + n2)%nat -> |
| 310 |
l = l1 ++ l2 -> |
| 311 |
canonical_repr n2 l2. |
| 312 |
Proof using wprops. |
| 313 |
intros. |
| 314 |
rewrite canonical_iff in *; intuition; |
| 315 |
repeat multimatch goal with |
| 316 |
| H : context[_ <= _ < _] |- _ => apply H |
| 317 |
| H : ?x = _ ++ _ |- In _ ?x => rewrite H |
| 318 |
| _ => cbn |
| 319 |
| _ => auto |
| 320 |
end. |
| 321 |
Qed. |
| 322 |
|
| 323 |
Lemma fold_right_add : forall l x, |
| 324 |
fold_right Z.add x l = x + fold_right Z.add 0 l. |
| 325 |
Proof using Type. |
| 326 |
intros l x. |
| 327 |
induction l as [ | l' IHl ]; cbn; try rewrite IHl; lia. |
| 328 |
Qed. |
| 329 |
|
| 330 |
Definition eval_weight_P p := forall a b, |
| 331 |
Associational.eval (combine (map (fun x0 : nat => weight (S x0)) (seq a b)) p) = |
| 332 |
weight 1 * Associational.eval (combine (map weight (seq a b)) p). |
| 333 |
|
| 334 |
Lemma eval_weight_S' : forall p, |
| 335 |
eval_weight_P p. |
| 336 |
Proof using Type. |
| 337 |
apply (ListAux.list_length_induction Z). |
| 338 |
unfold eval_weight_P. |
| 339 |
intros l1 H n b. |
| 340 |
pose proof (@break_list_last Z l1). |
| 341 |
cbv [eval_weight_P eval Associational.eval to_associational] in *. |
| 342 |
intuition; |
| 343 |
repeat multimatch goal with |
| 344 |
| H : context[exists _, _] |- _ => destruct H |
| 345 |
| _ => autorewrite with push_eval push_misc |
| 346 |
| _ => progress subst |
| 347 |
| _ => lia || auto |
| 348 |
end. |
| 349 |
|
| 350 |
destruct (b <=? length x)%nat eqn:E. |
| 351 |
rewrite Nat.leb_le in E. |
| 352 |
rewrite combine_truncate_r. |
| 353 |
rewrite combine_truncate_r with (xs:=map weight (seq n b)). |
| 354 |
push. |
| 355 |
apply H. |
| 356 |
push. |
| 357 |
rewrite Nat.min_l; lia. |
| 358 |
|
| 359 |
rewrite Nat.leb_gt in E. |
| 360 |
rewrite combine_truncate_l. |
| 361 |
rewrite combine_truncate_l with (xs:=map weight (seq n b)). |
| 362 |
autorewrite with push_length push_misc. |
| 363 |
rewrite Nat.min_l; [|lia]. |
| 364 |
rewrite seq_snoc. |
| 365 |
autorewrite with push_misc. |
| 366 |
push. |
| 367 |
rewrite fold_right_add. |
| 368 |
symmetry. |
| 369 |
rewrite fold_right_add. |
| 370 |
symmetry. |
| 371 |
rewrite fold_right_add. |
| 372 |
rewrite H. |
| 373 |
ring_simplify. |
| 374 |
unfold weight, machine_wordsize. |
| 375 |
rewrite uweight_S; [|lia]. |
| 376 |
cbn; break_match; lia. |
| 377 |
push. |
| 378 |
push. |
| 379 |
push. |
| 380 |
Qed. |
| 381 |
|
| 382 |
Lemma eval_weight_S p n: |
| 383 |
eval (fun i : nat => weight (S i)) n p = |
| 384 |
(eval weight n p) * weight 1. |
| 385 |
Proof using Type. |
| 386 |
cbv [eval to_associational]. |
| 387 |
rewrite eval_weight_S'. |
| 388 |
lia. |
| 389 |
Qed. |
| 390 |
Hint Rewrite eval_weight_S : push_eval. |
| 391 |
|
| 392 |
Lemma eval_weight_S_gen p a b : |
| 393 |
Associational.eval (combine (map (fun x0 : nat => weight (S x0)) (seq a b)) p) = |
| 394 |
weight 1 * Associational.eval (combine (map weight (seq a b)) p). |
| 395 |
Proof using Type. |
| 396 |
apply eval_weight_S'. |
| 397 |
Qed. |
| 398 |
Hint Rewrite eval_weight_S_gen : push_eval. |
| 399 |
|
| 400 |
Lemma canonical_eval_bounded n : forall (p : list Z), |
| 401 |
canonical_repr n p -> |
| 402 |
eval weight n p < weight n. |
| 403 |
Proof using wprops. |
| 404 |
intros p. |
| 405 |
generalize dependent n. |
| 406 |
induction p as [| a p IHp]; intros n H; destruct n; push; try lia; |
| 407 |
assert (H' := H); unfold canonical_repr in H'; push' H'. |
| 408 |
lia. |
| 409 |
le_lt. |
| 410 |
etransitivity. |
| 411 |
solve_ineq. |
| 412 |
le_lt. |
| 413 |
eapply canonical_bounded; eauto. |
| 414 |
rewrite <-OrdersEx.Z_as_OT.mul_le_mono_pos_r; eauto. |
| 415 |
le_lt. |
| 416 |
apply IHp. |
| 417 |
eapply canonical_app_r with (l1:=[a]); eauto. |
| 418 |
all: try lia. |
| 419 |
weight_comp; lia. |
| 420 |
Qed. |
| 421 |
|
| 422 |
Definition dual_map {A B : Type} (f : A -> B -> bool) (l1 : list A) (l2 : list B) := |
| 423 |
map (fun x => (f (fst x) (snd x))) (combine l1 l2). |
| 424 |
Definition fold_andb_map' {A B : Type} (f : A -> B -> bool) (ls1 : list A) (ls2 : list B) := |
| 425 |
fold_right andb true (dual_map f ls1 ls2). |
| 426 |
Definition is_bounded_by bounds ls := |
| 427 |
fold_andb_map' (fun r v'' => (fst r <=? v'') && (v'' <=? snd r)) bounds ls. |
| 428 |
Hint Unfold is_bounded_by : core. |
| 429 |
Hint Unfold fold_andb_map' : core. |
| 430 |
Hint Unfold dual_map : core. |
| 431 |
|
| 432 |
Lemma canonical_is_bounded_by : forall n p, |
| 433 |
canonical_repr n p <-> |
| 434 |
length p = n /\ |
| 435 |
is_bounded_by (repeat (0, 2^machine_wordsize-1) n) p = true. |
| 436 |
Proof using wprops. |
| 437 |
intros n p. |
| 438 |
rewrite canonical_iff. |
| 439 |
repeat autounfold. |
| 440 |
split. |
| 441 |
intros H. |
| 442 |
destruct H as [H H1]. |
| 443 |
intuition. |
| 444 |
generalize dependent n. |
| 445 |
induction p as [| a p IHp]; intros; |
| 446 |
repeat multimatch goal with |
| 447 |
| H : length _ = ?x |- _ => progress cbn in H; subst x |
| 448 |
| _ => apply andb_true_intro |
| 449 |
| _ => rewrite Z.leb_le |
| 450 |
| _ => rewrite Le.Z.le_sub_1_iff |
| 451 |
| _ => apply H1 |
| 452 |
| _ => eapply IHp |
| 453 |
| _ => progress cbn || intuition |
| 454 |
| _ => progress intuition |
| 455 |
| _ => reflexivity || lia || auto |
| 456 |
| [ |- _ <= 18446744073709551615] => replace 18446744073709551615 with (18446744073709551616 - 1) by lia |
| 457 |
end. |
| 458 |
split. |
| 459 |
intuition. |
| 460 |
generalize dependent n. |
| 461 |
induction p; intros; |
| 462 |
repeat multimatch goal with |
| 463 |
| H : length _ = ?x |- _ => cbn in H; rewrite <-H in * |
| 464 |
| H : In _ _ |- _ => cbn in H |
| 465 |
| H : context[S _] |- _ => cbn in H |
| 466 |
| H : context[_ && _] |- _ => rewrite andb_true_iff in H |
| 467 |
| H : context[_ <=? _] |- _ => rewrite <-Zle_is_le_bool in H |
| 468 |
| _ => progress cbn || intuition || subst |
| 469 |
| _ => lia |
| 470 |
| _ => eapply IHp |
| 471 |
end. |
| 472 |
Qed. |
| 473 |
|
| 474 |
Lemma eval_is_bounded_by_pos n : forall p, |
| 475 |
is_bounded_by (repeat (0, 2 ^ machine_wordsize - 1) n) p = true -> |
| 476 |
0 <= eval weight n p. |
| 477 |
Proof. |
| 478 |
intros p. |
| 479 |
pose proof eval_weight_S as Heval. |
| 480 |
repeat autounfold with * in *. |
| 481 |
generalize dependent n; induction p; intros n; destruct n; |
| 482 |
repeat multimatch goal with |
| 483 |
| H : context[fold_right _ _ _] |- _ => cbn in H |
| 484 |
| H : context[_ && _] |- _ => rewrite andb_true_iff in H |
| 485 |
| H : context[_ <=? _] |- _ => rewrite <-Zle_is_le_bool in H |
| 486 |
| _ => solve_ineq |
| 487 |
| _ => rewrite Heval |
| 488 |
| _ => push |
| 489 |
| _ => cbn |
| 490 |
| _ => intuition |
| 491 |
| _ => break_match |
| 492 |
| _ => lia |
| 493 |
end. |
| 494 |
Qed. |
| 495 |
|
| 496 |
Lemma eval_is_bounded_by n : forall p, |
| 497 |
is_bounded_by (repeat (0, 2 ^ machine_wordsize - 1) n) p = true -> |
| 498 |
0 <= eval weight n p < weight n. |
| 499 |
Proof using wprops. |
| 500 |
intros p. |
| 501 |
split. |
| 502 |
apply eval_is_bounded_by_pos; auto. |
| 503 |
pose proof eval_weight_S as Heval. |
| 504 |
repeat autounfold with * in *. |
| 505 |
generalize dependent n; induction p; intros n; destruct n; |
| 506 |
repeat multimatch goal with |
| 507 |
| H : context[fold_right _ _ _] |- _ => progress cbn in H |
| 508 |
| H : context[_ && _] |- _ => rewrite andb_true_iff in H |
| 509 |
| H : context[_ <=? _] |- _ => rewrite Z.leb_le in H |
| 510 |
| _ => solve_ineq |
| 511 |
| _ => push |
| 512 |
| _ => rewrite Heval |
| 513 |
| _ => progress cbn || intuition |
| 514 |
| _ => lia || auto || reflexivity || discriminate |
| 515 |
end. |
| 516 |
le_lt. |
| 517 |
etransitivity. |
| 518 |
solve_ineq. |
| 519 |
break_match; eauto. |
| 520 |
apply Z.mul_le_mono_nonneg_r; try lia. |
| 521 |
le_lt. |
| 522 |
apply IHp; auto. |
| 523 |
weight_comp; lia. |
| 524 |
Qed. |
| 525 |
Hint Resolve eval_is_bounded_by : ibb. |
| 526 |
|
| 527 |
Lemma is_bounded_by_cons1 : forall b bounds p' p, |
| 528 |
is_bounded_by (b :: bounds) (p' :: p) = true -> |
| 529 |
is_bounded_by bounds p = true. |
| 530 |
Proof using Type. |
| 531 |
intros; repeat autounfold in *; match goal with | H : _ |- _ => push' H end. |
| 532 |
Qed. |
| 533 |
Hint Resolve is_bounded_by_cons1 : ibb. |
| 534 |
|
| 535 |
Lemma is_bounded_by_cons2 : forall b bounds p' p, |
| 536 |
is_bounded_by (b :: bounds) (p' :: p) = true -> |
| 537 |
fst b <= p' <= snd b. |
| 538 |
Proof using Type. |
| 539 |
intros; repeat autounfold in *; match goal with | H : _ |- _ => push' H end. |
| 540 |
Qed. |
| 541 |
Hint Resolve is_bounded_by_cons2 : ibb. |
| 542 |
|
| 543 |
Lemma is_bounded_by_cons : forall b bounds p' p, |
| 544 |
is_bounded_by (b :: bounds) (p' :: p) = true -> |
| 545 |
is_bounded_by bounds p = true /\ |
| 546 |
fst b <= p' <= snd b. |
| 547 |
Proof using Type. |
| 548 |
intros; repeat autounfold in *; match goal with | H : _ |- _ => push' H end. |
| 549 |
Qed. |
| 550 |
Hint Resolve is_bounded_by_cons : ibb. |
| 551 |
|
| 552 |
Lemma is_bounded_by_nth n : forall p bounds, |
| 553 |
is_bounded_by bounds p = true -> |
| 554 |
(n < length p)%nat -> |
| 555 |
(n < length bounds)%nat -> |
| 556 |
fst (nth_default (0,0) bounds n) <= nth_default 0 p n <= snd (nth_default (0,0) bounds n). |
| 557 |
Proof using Type. |
| 558 |
intros p bounds H H0 H1. |
| 559 |
generalize dependent n. |
| 560 |
generalize dependent p. |
| 561 |
induction bounds as [ | b bounds IHbounds ]; |
| 562 |
intros p ? n; destruct n; destruct p; intros; |
| 563 |
repeat multimatch goal with |
| 564 |
| H : _ |- _ => autorewrite with push_length in H |
| 565 |
| _ => apply IHbounds |
| 566 |
| _ => autorewrite with push_misc |
| 567 |
| _ => cbn |
| 568 |
| _ => eauto with ibb |
| 569 |
| _ => lia |
| 570 |
end. |
| 571 |
Qed. |
| 572 |
Hint Resolve is_bounded_by_nth : ibb. |
| 573 |
|
| 574 |
Lemma is_bounded_by_app_l : forall bound1 bound2 l1 l2, |
| 575 |
is_bounded_by (bound1 ++ bound2) (l1 ++ l2) = true -> |
| 576 |
length bound1 = length l1 -> |
| 577 |
is_bounded_by bound1 l1 = true. |
| 578 |
Proof using Type. |
| 579 |
intros b1 b2 l1 l2 H H1. |
| 580 |
generalize dependent b1. |
| 581 |
generalize dependent b2. |
| 582 |
generalize dependent l2. |
| 583 |
induction l1 as [ | ? ? IHl1 ]; intros; destruct b1; |
| 584 |
repeat multimatch goal with |
| 585 |
| _ => autounfold in * |
| 586 |
| _ => eapply IHl1 |
| 587 |
| _ => rewrite Z.leb_le |
| 588 |
| H : _ |- _ => rewrite <-!app_comm_cons in H; push' H |
| 589 |
| H : _ |- _ => rewrite andb_true_iff in H |
| 590 |
| _ => rewrite andb_true_iff |
| 591 |
| _ => intuition |
| 592 |
| _ => push |
| 593 |
| _ => lia |
| 594 |
| _ => eauto |
| 595 |
end. |
| 596 |
Qed. |
| 597 |
Hint Resolve is_bounded_by_app_l : ibb. |
| 598 |
|
| 599 |
Lemma fold_right_andb_default : forall d l, |
| 600 |
fold_right andb d l = true -> d = true. |
| 601 |
Proof using Type. |
| 602 |
intros; induction l; |
| 603 |
repeat multimatch goal with |
| 604 |
| H : context[fold_right _ _ _] |- _ => push' H |
| 605 |
end. |
| 606 |
Qed. |
| 607 |
Hint Resolve fold_right_andb_default : core. |
| 608 |
|
| 609 |
Lemma is_bounded_by_app_r : forall bound1 bound2 l1 l2, |
| 610 |
is_bounded_by (bound1 ++ bound2) (l1 ++ l2) = true -> |
| 611 |
length bound1 = length l1 -> |
| 612 |
is_bounded_by bound2 l2 = true. |
| 613 |
Proof using Type. |
| 614 |
intros b1 b2 l1 l2 H H1. |
| 615 |
generalize dependent b1. |
| 616 |
generalize dependent b2. |
| 617 |
generalize dependent l2. |
| 618 |
induction l1 as [ | ? ? IHl1 ]; |
| 619 |
intros l2 b2 b1; [ | specialize (IHl1 l2 b2 b1)]; destruct b1; |
| 620 |
repeat multimatch goal with |
| 621 |
| _ => autounfold in * |
| 622 |
| _ => rewrite Z.leb_le |
| 623 |
| H : context[length _] |- _ => autorewrite with push_length in H |
| 624 |
| H : context[(_ :: _) ++ _] |- _ => rewrite <-!app_comm_cons in H; push' H |
| 625 |
| H : _ |- _ => rewrite andb_true_iff in H |
| 626 |
| _ => rewrite andb_true_iff |
| 627 |
| _ => intuition |
| 628 |
| _ => eauto |
| 629 |
| _ => discriminate |
| 630 |
end. |
| 631 |
Qed. |
| 632 |
|
| 633 |
Lemma is_bounded_by_loosen : forall l bound1 bound2, |
| 634 |
length bound1 = length bound2 -> |
| 635 |
is_bounded_by bound1 l = true -> |
| 636 |
fold_andb_map' (fun x y => (fst y <=? fst x) && (snd x <=? snd y)) bound1 bound2 = true -> |
| 637 |
is_bounded_by bound2 l = true. |
| 638 |
Proof using Type. |
| 639 |
intros l bound1 bound2 H H0 H1. |
| 640 |
generalize dependent bound1. |
| 641 |
generalize dependent bound2. |
| 642 |
repeat autounfold. |
| 643 |
induction l as [ | ? ? IHl]; intros; destruct bound1; destruct bound2; |
| 644 |
repeat match goal with |
| 645 |
| H : context[length _] |- _ => progress autorewrite with push_length in H |
| 646 |
| H : context[_ :: _] |- _ => progress autorewrite with push_misc in H |
| 647 |
| _ => apply IHl |
| 648 |
| _ => rewrite Z.leb_le |
| 649 |
| H : _ |- _ => rewrite Z.leb_le in H |
| 650 |
| _ => rewrite andb_true_iff |
| 651 |
| H : _ |- _ => rewrite andb_true_iff in H |
| 652 |
| _ => progress intuition |
| 653 |
| _ => progress push |
| 654 |
| _ => lia |
| 655 |
| _ => eauto |
| 656 |
end. |
| 657 |
Qed. |
| 658 |
|
| 659 |
Lemma bounds_same : forall b, |
| 660 |
fold_andb_map' (fun x y => (fst y <=? fst x) && (snd x <=? snd y)) b b = true. |
| 661 |
Proof using Type. |
| 662 |
intros b. |
| 663 |
repeat autounfold. |
| 664 |
induction b; |
| 665 |
repeat match goal with |
| 666 |
| _ => progress push |
| 667 |
| _ => rewrite andb_true_iff |
| 668 |
| _ => rewrite Z.leb_le |
| 669 |
| _ => progress intuition |
| 670 |
| _ => lia |
| 671 |
end. |
| 672 |
Qed. |
| 673 |
|
| 674 |
(* END SECTION CANONICAL_REPR *) |
| 675 |
|
| 676 |
Ltac solve_length q := |
| 677 |
try match goal with |
| 678 |
| [ H : canonical_repr _ q |- _ ] => |
| 679 |
unfold canonical_repr in H; intuition |
| 680 |
end; |
| 681 |
try match goal with |
| 682 |
| [ H : length q = _ |- _] => |
| 683 |
rewrite !app_length in H; |
| 684 |
try rewrite !app_length; |
| 685 |
cbn [length] in H; cbn [length]; lia |
| 686 |
end; |
| 687 |
match goal with |
| 688 |
| [ H : q = _ |- _ ] => |
| 689 |
apply f_equal with (f:=fun l => length l) in H; |
| 690 |
rewrite !app_length in H; |
| 691 |
try rewrite !app_length; |
| 692 |
cbn [length] in H; cbn [length]; lia |
| 693 |
end. |
| 694 |
|
| 695 |
Ltac solve_in := |
| 696 |
repeat |
| 697 |
match goal with |
| 698 |
| [ |- In ?hi ?p ] => |
| 699 |
match goal with |
| 700 |
| [ H : p = _ ++ _ |- _ ] => |
| 701 |
rewrite H; apply in_or_app; simpl; auto |
| 702 |
| [ H : p = _ ++ _ ++ _ |- _ ] => |
| 703 |
rewrite app_assoc in H |
| 704 |
end |
| 705 |
end. |
| 706 |
|
| 707 |
Ltac apply_iff p := |
| 708 |
match goal with |
| 709 |
| [ H : canonical_repr _ p |- _ ] => |
| 710 |
rewrite canonical_iff in H; |
| 711 |
destruct H as [ _ Htmp ]; |
| 712 |
apply Htmp; |
| 713 |
solve_in |
| 714 |
end. |
| 715 |
Ltac solve_hi := |
| 716 |
match goal with |
| 717 |
| [ |- 0 <= ?hi ] => |
| 718 |
match goal with |
| 719 |
| [ H : ?p = _ ++ [hi] |- _ ] => apply_iff p |
| 720 |
| [ H : ?p = _ ++ [hi] ++ _ |- _ ] => apply_iff p |
| 721 |
| [ H : ?p = _ ++ _ ++ [hi] |- _ ] => apply_iff p |
| 722 |
end |
| 723 |
end. |
| 724 |
|
| 725 |
Ltac adjust_ineq_lt H := |
| 726 |
match type of H with |
| 727 |
| context[ ?x < ?y ] => |
| 728 |
match goal with |
| 729 |
| [ |- context[ x * ?z ] ] => |
| 730 |
apply Zmult_lt_compat_r with (p:=z) in H; eauto |
| 731 |
end |
| 732 |
end. |
| 733 |
Ltac adjust_ineq_le H := |
| 734 |
match type of H with |
| 735 |
| context[ ?x <= ?y ] => |
| 736 |
match goal with |
| 737 |
| [ |- context[ x * ?z ] ] => |
| 738 |
apply Zmult_le_compat_r with (p:=z) in H; eauto |
| 739 |
end |
| 740 |
end. |
| 741 |
Ltac adjust_ineq H := adjust_ineq_le H || adjust_ineq_lt H. |
| 742 |
|
| 743 |
Ltac canonical_app p := |
| 744 |
let H' := fresh "TEMP" in |
| 745 |
pose proof (eq_refl p) as H'; |
| 746 |
match goal with |
| 747 |
| [ H : p = ?lo ++ ?hi |- _ ] => |
| 748 |
let H1 := fresh "Hcanon_l" in |
| 749 |
let H2 := fresh "Hcanon_r" in |
| 750 |
match goal with |
| 751 |
| [ H' : canonical_repr _ p |- _ ] => |
| 752 |
eapply canonical_app_l with (l1:=lo) (n1:=length lo) (l2:=hi) (n2:=length hi) in H' as H1; |
| 753 |
eapply canonical_app_r with (l1:=lo) (n1:=length lo) (l2:=hi) (n2:=length hi) in H' as H2; |
| 754 |
try (solve_length p) |
| 755 |
end |
| 756 |
end; |
| 757 |
clear H'. |
| 758 |
|
| 759 |
Ltac subst_canon q := |
| 760 |
match goal with |
| 761 |
| [ H : canonical_repr ?n1 ?p |- canonical_repr ?n2 ?p ] => |
| 762 |
replace n2 with n1 by (solve_length q); |
| 763 |
auto |
| 764 |
end. |
| 765 |
|
| 766 |
Definition sat_reduce base s c n (p : list (Z * Z)) := |
| 767 |
let s' := fst (Saturated.Rows.adjust_s weight (S (S n)) s) in |
| 768 |
let lo_hi := Associational.split s' p in |
| 769 |
let coef := Saturated.Associational.sat_mul_const base [(1, s'/s)] c in |
| 770 |
let hi := Saturated.Associational.sat_mul_const base coef (snd lo_hi) in |
| 771 |
let r := (fst lo_hi) ++ hi in |
| 772 |
r. |
| 773 |
|
| 774 |
Lemma value_sat_reduce base s c n (p : list (Z * Z)) (basenz:base<>0): |
| 775 |
let s' := fst (Saturated.Rows.adjust_s weight (S (S n)) s) in |
| 776 |
let coef := Saturated.Associational.sat_mul_const base [(1, s'/s)] c in |
| 777 |
let lo_hi := Associational.split s' p in |
| 778 |
Associational.eval (sat_reduce base s c n p) = |
| 779 |
Associational.eval coef * Associational.eval (snd lo_hi) + Associational.eval (fst lo_hi). |
| 780 |
Proof. |
| 781 |
intros; cbv [sat_reduce] in *; cbv [s' lo_hi coef]. |
| 782 |
autorewrite with push_eval; lia. |
| 783 |
Qed. |
| 784 |
Hint Rewrite value_sat_reduce : push_eval. |
| 785 |
|
| 786 |
Lemma adjust_s_invariant fuel s (s_nz:s<>0) : |
| 787 |
fst (Saturated.Rows.adjust_s weight fuel s) mod s = 0 |
| 788 |
/\ fst (Saturated.Rows.adjust_s weight fuel s) <> 0. |
| 789 |
Proof using wprops. |
| 790 |
cbv [Saturated.Rows.adjust_s]; rewrite fold_right_map; generalize (List.rev (seq 0 fuel)); intro ls; induction ls as [|l ls IHls]; |
| 791 |
cbn. |
| 792 |
{ rewrite Z.mod_same by assumption; auto. } |
| 793 |
{ break_match; cbn in *; auto with zarith. } |
| 794 |
Qed. |
| 795 |
|
| 796 |
Lemma adjust_s_finished' fuel s w (s_nz:s<>0) : |
| 797 |
Rows.adjust_s weight fuel s = (w, true) -> |
| 798 |
Rows.adjust_s weight (S fuel) s = (w, true). |
| 799 |
Proof using Type. |
| 800 |
cbv [Rows.adjust_s]. |
| 801 |
rewrite !fold_right_map. |
| 802 |
replace (rev (seq 0 (S fuel))) with (fuel :: rev (seq 0 fuel)). |
| 803 |
generalize (rev (seq 0 fuel)). |
| 804 |
cbn in *. |
| 805 |
intros l. |
| 806 |
induction l; |
| 807 |
break_match; auto; discriminate. |
| 808 |
rewrite seq_snoc. |
| 809 |
rewrite rev_app_distr. |
| 810 |
reflexivity. |
| 811 |
Qed. |
| 812 |
|
| 813 |
Lemma adjust_s_finished fuel fuel' s w (s_nz:s<>0) : |
| 814 |
(fuel' > fuel)%nat -> |
| 815 |
Saturated.Rows.adjust_s weight fuel s = (w, true) -> |
| 816 |
Saturated.Rows.adjust_s weight fuel' s = (w, true). |
| 817 |
Proof using Type. |
| 818 |
induction 1; intros; apply adjust_s_finished'; auto. |
| 819 |
Qed. |
| 820 |
|
| 821 |
Lemma eval_sat_reduce base s c fuel p : |
| 822 |
base <> 0 |
| 823 |
-> s - Associational.eval c <> 0 |
| 824 |
-> s <> 0 |
| 825 |
-> Associational.eval (sat_reduce base s c fuel p) mod (s - Associational.eval c) |
| 826 |
= Associational.eval p mod (s - Associational.eval c). |
| 827 |
Proof using wprops. |
| 828 |
intros; cbv [sat_reduce]. |
| 829 |
lazymatch goal with |
| 830 |
| |- context[Saturated.Rows.adjust_s ?weight ?fuel ?s] => |
| 831 |
destruct (adjust_s_invariant fuel s ltac:(assumption)) as [Hmod ?] |
| 832 |
end. |
| 833 |
eta_expand; autorewrite with push_eval zsimplify_const; cbn [fst snd]. |
| 834 |
rewrite <- (Z.mul_comm (Associational.eval c)), <- !Z.mul_assoc, <-Associational.reduction_rule by auto. |
| 835 |
autorewrite with zsimplify_const; rewrite !Z.mul_assoc, Z.mul_div_eq_full, Hmod by auto. |
| 836 |
autorewrite with zsimplify_const push_eval; trivial. |
| 837 |
Qed. |
| 838 |
Hint Rewrite eval_sat_reduce using auto : push_eval. |
| 839 |
|
| 840 |
Definition mul_no_reduce base n (p q : list Z) := |
| 841 |
let p_a := Positional.to_associational weight n p in |
| 842 |
let q_a := Positional.to_associational weight n q in |
| 843 |
let pq_a := Saturated.Associational.sat_mul base p_a q_a in |
| 844 |
let pq_rows := Saturated.Rows.from_associational weight (2*n) pq_a in |
| 845 |
let pq := Saturated.Rows.flatten weight (2*n) pq_rows in |
| 846 |
let bound := (0, 2^machine_wordsize - 1) in |
| 847 |
if (is_bounded_by (repeat bound n) p) then |
| 848 |
if (is_bounded_by (repeat bound n) q) then |
| 849 |
fst pq |
| 850 |
else |
| 851 |
add_to_nth 0 (weight (2 * n) * snd pq) (fst pq) |
| 852 |
else |
| 853 |
add_to_nth 0 (weight (2 * n) * snd pq) (fst pq). |
| 854 |
|
| 855 |
Definition reduce1 base s c n m (p : list Z) := |
| 856 |
let bound := (0, 2^machine_wordsize - 1) in |
| 857 |
if (is_bounded_by (repeat bound n) p) then |
| 858 |
let p_a := Positional.to_associational weight n p in |
| 859 |
let r_a := sat_reduce base s c n p_a in |
| 860 |
let r_rows := Saturated.Rows.from_associational weight m r_a in |
| 861 |
let r_flat := Saturated.Rows.flatten weight m r_rows in |
| 862 |
fst r_flat |
| 863 |
else |
| 864 |
let p_a := Positional.to_associational weight n p in |
| 865 |
let r_a := sat_reduce base s c n p_a in |
| 866 |
let r_rows := Saturated.Rows.from_associational weight m r_a in |
| 867 |
let r_flat := Saturated.Rows.flatten weight m r_rows in |
| 868 |
add_to_nth 0 (weight (m) * snd r_flat) (fst r_flat). |
| 869 |
|
| 870 |
(* S n -> n limbs *) |
| 871 |
Definition reduce3 base s c n (p : list Z) := |
| 872 |
let bound := (0, 2^machine_wordsize-1) in |
| 873 |
let bounds := (repeat bound n) ++ [(0, 1)] in |
| 874 |
let s' := fst (Saturated.Rows.adjust_s weight (S (S n)) s) in |
| 875 |
let coef_a := Saturated.Associational.sat_mul_const base [(1, s'/s)] c in |
| 876 |
let coef := Associational.eval coef_a in |
| 877 |
dlet_nd hi := Z.zselect (nth_default 0 p n) 0 coef in |
| 878 |
let lo := Saturated.Rows.flatten weight 1 [ [hi]; [nth_default 0 p 0] ] in |
| 879 |
if (is_bounded_by bounds p) then |
| 880 |
(fst lo) ++ (skipn 1 (firstn n p)) |
| 881 |
else |
| 882 |
let hi' := coef * (nth_default 0 p n) in |
| 883 |
add_to_nth 0 hi' (firstn n p). |
| 884 |
|
| 885 |
Definition reduce_full base s c n (p : list Z) := |
| 886 |
let r1 := reduce1 base s c (2*n) (S n) p in |
| 887 |
let bound := (0, 2^machine_wordsize - 1) in |
| 888 |
let bounds := repeat bound n ++ [(0, up_bound-1)] in |
| 889 |
let r2 := reduce1 base s c (S n) (S n) r1 in |
| 890 |
let r3 := reduce3 base s c n r2 in |
| 891 |
if (is_bounded_by bounds r1) then r3 |
| 892 |
else add_to_nth 0 (weight n * nth_default 0 r1 n) (firstn n r1). |
| 893 |
|
| 894 |
Definition mulmod' base s c n (p q : list Z) := |
| 895 |
let prod := mul_no_reduce base n p q in |
| 896 |
let red := reduce_full base s c n prod in |
| 897 |
red. |
| 898 |
|
| 899 |
Definition reduce1_cps {T} base s c n m (p : list Z) (f : list Z -> T) := |
| 900 |
let bound := (0, 2^machine_wordsize - 1) in |
| 901 |
if (is_bounded_by (repeat bound n) p) then |
| 902 |
let p_a := Positional.to_associational weight n p in |
| 903 |
let r_a := sat_reduce base s c n p_a in |
| 904 |
let r_rows := Saturated.Rows.from_associational weight m r_a in |
| 905 |
let r_flat := Saturated.Rows.flatten weight m r_rows in |
| 906 |
f (fst r_flat) |
| 907 |
else |
| 908 |
let p_a := Positional.to_associational weight n p in |
| 909 |
let r_a := sat_reduce base s c n p_a in |
| 910 |
let r_rows := Saturated.Rows.from_associational weight m r_a in |
| 911 |
let r_flat := Saturated.Rows.flatten weight m r_rows in |
| 912 |
f (add_to_nth 0 (weight (m) * snd r_flat) (fst r_flat)). |
| 913 |
|
| 914 |
Lemma reduce1_cps_ok {T} base s c n m (f : list Z -> T) : forall p, |
| 915 |
reduce1_cps base s c n m p f = f (reduce1 base s c n m p). |
| 916 |
Proof using Type. |
| 917 |
intros. |
| 918 |
cbv [reduce1 reduce1_cps]. |
| 919 |
break_match; reflexivity. |
| 920 |
Qed. |
| 921 |
|
| 922 |
Definition reduce3_cps {T} base s c n (p : list Z) (f : list Z -> T) := |
| 923 |
let bound := (0, 2^machine_wordsize-1) in |
| 924 |
let bounds := (repeat bound n) ++ [(0, 1)] in |
| 925 |
let s' := fst (Saturated.Rows.adjust_s weight (S (S n)) s) in |
| 926 |
let coef_a := Saturated.Associational.sat_mul_const base [(1, s'/s)] c in |
| 927 |
let coef := Associational.eval coef_a in |
| 928 |
dlet_nd hi := Z.zselect (nth_default 0 p n) 0 coef in |
| 929 |
let lo := Saturated.Rows.flatten weight 1 [ [hi]; [nth_default 0 p 0] ] in |
| 930 |
if (is_bounded_by bounds p) then |
| 931 |
f ((fst lo) ++ (skipn 1 (firstn n p))) |
| 932 |
else |
| 933 |
let hi' := coef * (nth_default 0 p n) in |
| 934 |
f (add_to_nth 0 hi' (firstn n p)). |
| 935 |
|
| 936 |
Lemma reduce3_cps_ok {T} base s c n (f : list Z -> T) : forall p, |
| 937 |
reduce3_cps base s c n p f = f (reduce3 base s c n p). |
| 938 |
Proof. |
| 939 |
intros. |
| 940 |
cbv [reduce3 reduce3_cps]. |
| 941 |
break_match; reflexivity. |
| 942 |
Qed. |
| 943 |
|
| 944 |
Definition reduce_full_cps {T} base s c n (p : list Z) (f : list Z -> T):= |
| 945 |
(r1 <- reduce1_cps base s c (2*n) (S n) p; |
| 946 |
(let bound := (0, 2^machine_wordsize - 1) in |
| 947 |
let bounds := repeat bound n ++ [(0, up_bound-1)] in |
| 948 |
r2 <- reduce1_cps base s c (S n) (S n) r1; |
| 949 |
(if (is_bounded_by bounds r1) then |
| 950 |
reduce3_cps base s c n r2 f |
| 951 |
else |
| 952 |
f (add_to_nth 0 (weight n * nth_default 0 r1 n) (firstn n r1))))). |
| 953 |
|
| 954 |
Definition reduce_full' base s c n p := |
| 955 |
ltac:(let x := (eval cbv beta delta [reduce_full_cps reduce1_cps reduce3_cps id] in (@reduce_full_cps (list Z) base s c n p id)) in |
| 956 |
exact x). |
| 957 |
|
| 958 |
Lemma reduce_full_cps_ok {T} base s c n (f : list Z -> T) : forall p, |
| 959 |
reduce_full_cps base s c n p f = f (reduce_full base s c n p). |
| 960 |
Proof using Type. |
| 961 |
intros. |
| 962 |
cbv [reduce_full reduce_full_cps]. |
| 963 |
repeat (rewrite reduce1_cps_ok || |
| 964 |
rewrite reduce3_cps_ok || |
| 965 |
reflexivity || |
| 966 |
break_match). |
| 967 |
Qed. |
| 968 |
|
| 969 |
Definition mul_no_reduce_cps {T} base n (p q : list Z) (f : list Z -> T):= |
| 970 |
let p_a := Positional.to_associational weight n p in |
| 971 |
let q_a := Positional.to_associational weight n q in |
| 972 |
let pq_a := Saturated.Associational.sat_mul base p_a q_a in |
| 973 |
let pq_rows := Saturated.Rows.from_associational weight (2*n) pq_a in |
| 974 |
let pq := Saturated.Rows.flatten weight (2*n) pq_rows in |
| 975 |
let bound := (0, 2^machine_wordsize - 1) in |
| 976 |
if (is_bounded_by (repeat bound n) p) then |
| 977 |
if (is_bounded_by (repeat bound n) q) then |
| 978 |
f (fst pq) |
| 979 |
else |
| 980 |
f (add_to_nth 0 (weight (2 * n) * snd pq) (fst pq)) |
| 981 |
else |
| 982 |
f (add_to_nth 0 (weight (2 * n) * snd pq) (fst pq)). |
| 983 |
|
| 984 |
Lemma mul_no_reduce_cps_ok {T} base n (f : list Z -> T) : forall p q, |
| 985 |
mul_no_reduce_cps base n p q f = f (mul_no_reduce base n p q). |
| 986 |
Proof using Type. |
| 987 |
intros. |
| 988 |
cbv [mul_no_reduce mul_no_reduce_cps]. |
| 989 |
break_match; reflexivity. |
| 990 |
Qed. |
| 991 |
|
| 992 |
Definition mulmod_cps {T} base s c n (p q : list Z) (f : list Z -> T) := |
| 993 |
(mul <- mul_no_reduce_cps base n p q; |
| 994 |
reduce_full_cps base s c n mul f). |
| 995 |
|
| 996 |
Lemma mulmod_cps_ok {T} base s c n (f : list Z -> T) : forall p q, |
| 997 |
mulmod_cps base s c n p q f = f (mulmod' base s c n p q). |
| 998 |
Proof using Type. |
| 999 |
intros. |
| 1000 |
cbv [mulmod' mulmod_cps]. |
| 1001 |
rewrite mul_no_reduce_cps_ok, reduce_full_cps_ok. |
| 1002 |
reflexivity. |
| 1003 |
Qed. |
| 1004 |
|
| 1005 |
Definition mulmod base s c n (p q : list Z) := |
| 1006 |
ltac:(let x := (eval cbv beta delta [mulmod_cps mul_no_reduce_cps reduce_full_cps reduce1_cps reduce3_cps id] in (@mulmod_cps (list Z) base s c n p q id)) in |
| 1007 |
exact x). |
| 1008 |
|
| 1009 |
Lemma mulmod_unfold base s c n : forall p q, |
| 1010 |
mulmod' base s c n p q = mulmod_cps base s c n p q id. |
| 1011 |
Proof using Type. |
| 1012 |
intros. |
| 1013 |
rewrite mulmod_cps_ok. |
| 1014 |
reflexivity. |
| 1015 |
Qed. |
| 1016 |
|
| 1017 |
Lemma mulmod_cps_conv base s c n : forall p q, |
| 1018 |
mulmod base s c n p q = mulmod' base s c n p q. |
| 1019 |
Proof using Type. |
| 1020 |
intros. |
| 1021 |
rewrite mulmod_unfold. |
| 1022 |
reflexivity. |
| 1023 |
Qed. |
| 1024 |
|
| 1025 |
Hint Resolve length_partition : push_length. |
| 1026 |
Hint Resolve Rows.length_from_associational : push_length. |
| 1027 |
|
| 1028 |
Lemma split_lt w l1 l2: |
| 1029 |
(forall x, In x l1 -> 0 < x < w) -> |
| 1030 |
split w (combine l1 l2) = (combine l1 l2, []). |
| 1031 |
Proof using Type. |
| 1032 |
intros H. |
| 1033 |
generalize dependent l2. |
| 1034 |
induction l1 as [| ? ? IHl1]; intros l2; destruct l2; push; |
| 1035 |
match goal with |
| 1036 |
| [ |- context[ ?x :: ?y ] ] => replace (x :: y) with ([x] ++ y) by auto |
| 1037 |
end; |
| 1038 |
specialize (IHl1 ltac:(auto)); |
| 1039 |
specialize (H _ ltac:(auto)); |
| 1040 |
repeat multimatch goal with |
| 1041 |
| |- context[_ mod _] => rewrite Z.mod_small |
| 1042 |
| _ => rewrite IHl1 |
| 1043 |
| _ => push |
| 1044 |
| _ => cbn |
| 1045 |
| _ => lia |
| 1046 |
| _ => auto |
| 1047 |
| _ => break_match |
| 1048 |
end. |
| 1049 |
Qed. |
| 1050 |
|
| 1051 |
Lemma split_gt w l1 l2: |
| 1052 |
(forall x, In x l1 -> x mod w = 0) -> |
| 1053 |
split w (combine l1 l2) = ([], combine (map (fun t => t / w) l1) l2). |
| 1054 |
Proof using Type. |
| 1055 |
intros H. |
| 1056 |
generalize dependent l2. |
| 1057 |
induction l1 as [| ? ? IHl1]; intros l2; destruct l2; push; |
| 1058 |
match goal with |
| 1059 |
| [ |- context[ ?x :: ?y ] ] => replace (x :: y) with ([x] ++ y) by eauto |
| 1060 |
end; |
| 1061 |
specialize (IHl1 ltac:(auto)); |
| 1062 |
specialize (H _ ltac:(auto)); |
| 1063 |
repeat multimatch goal with |
| 1064 |
| H : ?x = 0, H1 : (?x =? 0) = false |- _ => rewrite H in H1 |
| 1065 |
| _ => rewrite IHl1 |
| 1066 |
| _ => push |
| 1067 |
| _ => cbn |
| 1068 |
| _ => lia |
| 1069 |
| _ => auto |
| 1070 |
| _ => break_match |
| 1071 |
| _ => discriminate |
| 1072 |
end. |
| 1073 |
Qed. |
| 1074 |
|
| 1075 |
Lemma weight_mono' x : |
| 1076 |
weight x < weight (S x). |
| 1077 |
Proof using Type. |
| 1078 |
weight_comp. |
| 1079 |
rewrite Zred_factor0 at 1. |
| 1080 |
rewrite Z.mul_comm. |
| 1081 |
apply Zmult_lt_compat_r. |
| 1082 |
apply Z.pow_pos_nonneg. |
| 1083 |
all: lia. |
| 1084 |
Qed. |
| 1085 |
|
| 1086 |
Lemma weight_mono'' x1 x2 : |
| 1087 |
(x2 > 0)%nat |
| 1088 |
-> weight x1 < weight (x2 + x1). |
| 1089 |
Proof using Type. |
| 1090 |
intros H. |
| 1091 |
induction H as [| ? ? IHle]; |
| 1092 |
repeat match goal with |
| 1093 |
| _ => apply IHle |
| 1094 |
| _ => apply weight_mono' |
| 1095 |
| _ => etransitivity |
| 1096 |
end. |
| 1097 |
Qed. |
| 1098 |
|
| 1099 |
Lemma weight_mono x1 x2 : |
| 1100 |
(x1 < x2)%nat -> |
| 1101 |
weight x1 < weight x2. |
| 1102 |
Proof using Type. |
| 1103 |
intros. |
| 1104 |
replace x2%nat with ((x2 - x1) + x1)%nat by lia. |
| 1105 |
apply weight_mono''; lia. |
| 1106 |
Qed. |
| 1107 |
|
| 1108 |
Lemma weight_mono_le x1 x2 : |
| 1109 |
(x1 <= x2)%nat -> |
| 1110 |
weight x1 <= weight x2. |
| 1111 |
Proof using Type. |
| 1112 |
intros H. |
| 1113 |
apply Nat.lt_eq_cases in H. |
| 1114 |
intuition. |
| 1115 |
pose proof (weight_mono x1 x2 ltac:(auto)); lia. |
| 1116 |
subst; lia. |
| 1117 |
Qed. |
| 1118 |
|
| 1119 |
Lemma map_seq_start : forall a b, |
| 1120 |
map weight (seq a b) = |
| 1121 |
map (fun t => t * weight a) (map weight (seq 0 b)). |
| 1122 |
Proof using Type. |
| 1123 |
intros a b. |
| 1124 |
induction b as [| ? IHb]; |
| 1125 |
repeat multimatch goal with |
| 1126 |
| _ => rewrite IHb |
| 1127 |
| _ => rewrite seq_snoc |
| 1128 |
| _ => f_equal |
| 1129 |
| _ => push |
| 1130 |
| _ => cbn |
| 1131 |
end. |
| 1132 |
weight_comp. |
| 1133 |
rewrite Nat2Z.inj_add. |
| 1134 |
rewrite Z.mul_add_distr_l. |
| 1135 |
rewrite Z.pow_add_r; lia. |
| 1136 |
Qed. |
| 1137 |
|
| 1138 |
Lemma eval_seq_start : forall a b p, |
| 1139 |
Associational.eval (combine (map weight (seq a b)) p) = |
| 1140 |
weight a * Associational.eval (combine (map weight (seq 0 b)) p). |
| 1141 |
Proof using wprops. |
| 1142 |
intros a b p. |
| 1143 |
generalize dependent a. |
| 1144 |
generalize dependent b. |
| 1145 |
induction p as [ | x p IHp ]; intros. |
| 1146 |
push. |
| 1147 |
destruct b. |
| 1148 |
push. |
| 1149 |
cbn [seq]. |
| 1150 |
rewrite <-seq_shift. |
| 1151 |
push. |
| 1152 |
rewrite IHp. |
| 1153 |
lia. |
| 1154 |
Qed. |
| 1155 |
|
| 1156 |
Lemma weight_dif_mono' : forall n, |
| 1157 |
weight (S n) - weight n < weight (S (S n)) - weight (S n). |
| 1158 |
Proof using Type. |
| 1159 |
intros n. |
| 1160 |
induction n. |
| 1161 |
weight_comp; lia. |
| 1162 |
cbv [weight]. |
| 1163 |
rewrite uweight_S; [ | lia]. |
| 1164 |
rewrite uweight_S with (n:=n) at 2; [ | lia]. |
| 1165 |
rewrite uweight_S with (n:=S (S n)); [ | lia]. |
| 1166 |
fold weight. |
| 1167 |
rewrite <-!Z.mul_sub_distr_l. |
| 1168 |
apply Zmult_lt_compat_l; unfold machine_wordsize; lia. |
| 1169 |
Qed. |
| 1170 |
|
| 1171 |
Lemma weight_dif_mono : forall n m, |
| 1172 |
(n < m)%nat -> |
| 1173 |
weight (S n) - weight n < weight (S m) - weight m. |
| 1174 |
Proof using Type. |
| 1175 |
intros n m H. |
| 1176 |
induction H; |
| 1177 |
repeat multimatch goal with |
| 1178 |
| _ => apply IHle |
| 1179 |
| _ => apply weight_dif_mono' |
| 1180 |
| _ => etransitivity |
| 1181 |
end. |
| 1182 |
Qed. |
| 1183 |
|
| 1184 |
Lemma weight_dif_lt : forall n m a, |
| 1185 |
(n < m)%nat -> |
| 1186 |
a < weight (S n) - weight n -> |
| 1187 |
a < weight (S m) - weight m. |
| 1188 |
Proof. |
| 1189 |
intros n m a H H0. |
| 1190 |
induction H. |
| 1191 |
etransitivity; [| apply weight_dif_mono']. |
| 1192 |
auto. |
| 1193 |
etransitivity; [| apply weight_dif_mono']. |
| 1194 |
auto. |
| 1195 |
Qed. |
| 1196 |
|
| 1197 |
Section mulmod. |
| 1198 |
|
| 1199 |
Context (base : Z) |
| 1200 |
(s : Z) |
| 1201 |
(c : list (Z * Z)) |
| 1202 |
(n : nat). |
| 1203 |
|
| 1204 |
Context (n_gt_1 : (n > 1)%nat) |
| 1205 |
(s_pos : s > 0) |
| 1206 |
(c_pos : Associational.eval c > 0) |
| 1207 |
(mod_nz : s - Associational.eval c <> 0) |
| 1208 |
(base_nz : base <> 0) |
| 1209 |
(solinas_property : Rows.adjust_s weight (S (S n)) s = (weight n, true)) |
| 1210 |
(coef_small : weight n / s * Associational.eval c < up_bound). |
| 1211 |
|
| 1212 |
(* SECTION MUL_NO_REDUCE *) |
| 1213 |
|
| 1214 |
Theorem eval_mul_no_reduce : forall p q, |
| 1215 |
eval weight (2 * n) (mul_no_reduce base n p q) = |
| 1216 |
eval weight n p * Positional.eval weight n q. |
| 1217 |
Proof using base_nz n_gt_1 wprops. |
| 1218 |
clear dependent s. |
| 1219 |
intros p q. |
| 1220 |
cbv [mul_no_reduce]. |
| 1221 |
break_match. |
| 1222 |
(* properly bounded *) |
| 1223 |
push. |
| 1224 |
apply Z.mod_small. |
| 1225 |
repeat match goal with |
| 1226 |
| H : context[_ && _] |- _ => rewrite andb_true_iff in Heqb |
| 1227 |
| H : is_bounded_by _ _ = true |- _ => apply eval_is_bounded_by in H |
| 1228 |
| _ => progress intuition |
| 1229 |
| _ => solve_ineq |
| 1230 |
end. |
| 1231 |
le_lt. |
| 1232 |
etransitivity. |
| 1233 |
apply OrdersEx.Z_as_OT.mul_le_mono_nonneg; eauto; rewrite Le.Z.le_sub_1_iff; eauto. |
| 1234 |
le_lt. |
| 1235 |
replace (weight (2 * n)) with (weight n * weight n). |
| 1236 |
solve_ineq. |
| 1237 |
weight_comp. |
| 1238 |
rewrite <-OrdersEx.Z_as_OT.pow_mul_r. |
| 1239 |
f_equal. |
| 1240 |
lia. |
| 1241 |
lia. |
| 1242 |
lia. |
| 1243 |
|
| 1244 |
(* not bounded *) |
| 1245 |
push. |
| 1246 |
rewrite <-Z_div_mod_eq_full. |
| 1247 |
auto. |
| 1248 |
push. |
| 1249 |
lia. |
| 1250 |
push. |
| 1251 |
|
| 1252 |
push. |
| 1253 |
rewrite <-Z_div_mod_eq_full. |
| 1254 |
auto. |
| 1255 |
push. |
| 1256 |
lia. |
| 1257 |
push. |
| 1258 |
Qed. |
| 1259 |
Hint Rewrite eval_mul_no_reduce : push_eval. |
| 1260 |
|
| 1261 |
Theorem length_mul_no_reduce : forall p q, |
| 1262 |
length (mul_no_reduce base n p q) = (2 * n)%nat. |
| 1263 |
Proof using base_nz n_gt_1 wprops. |
| 1264 |
clear dependent s. |
| 1265 |
intros; unfold mul_no_reduce; break_match; push. |
| 1266 |
Qed. |
| 1267 |
Hint Rewrite length_mul_no_reduce : push_length. |
| 1268 |
|
| 1269 |
(* END SECTION MUL_NO_REDUCE *) |
| 1270 |
|
| 1271 |
(* SECTION REDUCE1 *) |
| 1272 |
|
| 1273 |
Lemma reduce1_length : forall p m1 m2, |
| 1274 |
length (reduce1 base s c m1 m2 p) = m2. |
| 1275 |
Proof using wprops. |
| 1276 |
intros; cbv [reduce1]; break_match; push. |
| 1277 |
Qed. |
| 1278 |
Hint Rewrite reduce1_length : push_length. |
| 1279 |
|
| 1280 |
Lemma split_p_firstn : forall p, |
| 1281 |
n <= length p -> |
| 1282 |
split (weight n) (combine (map weight (seq 0 n)) (firstn n p)) = |
| 1283 |
(combine (map weight (seq 0 n)) (firstn n p), []). |
| 1284 |
Proof using wprops. |
| 1285 |
intros. |
| 1286 |
rewrite split_lt; |
| 1287 |
repeat multimatch goal with |
| 1288 |
| H : _ |- _ => autorewrite with push_misc in H |
| 1289 |
| H : _ |- _ => rewrite in_seq in H |
| 1290 |
| _ => rewrite min_l |
| 1291 |
| H : context[exists _, _] |- _ => destruct H |
| 1292 |
| H : _ = ?x |- context[?x] => rewrite <-H |
| 1293 |
| _ => push |
| 1294 |
| _ => apply weight_mono |
| 1295 |
| _ => intuition |
| 1296 |
| _ => auto || lia |
| 1297 |
end. |
| 1298 |
Qed. |
| 1299 |
Hint Rewrite split_p_firstn : push_misc. |
| 1300 |
|
| 1301 |
Lemma split_p_skipn : forall p m1, |
| 1302 |
n <= length p -> |
| 1303 |
split (weight n) (combine (map weight (seq n (m1 - n))) (skipn n p)) = |
| 1304 |
([], combine (map weight (seq 0 (m1 - n))) (skipn n p)). |
| 1305 |
Proof using wprops. |
| 1306 |
intros. |
| 1307 |
rewrite split_gt; |
| 1308 |
repeat multimatch goal with |
| 1309 |
| H : _ |- _ => autorewrite with push_misc in H |
| 1310 |
| H : _ |- _ => rewrite in_seq in H |
| 1311 |
| _ => apply Weight.weight_multiples_full |
| 1312 |
| H : context[exists _, _] |- _ => destruct H |
| 1313 |
| H : _ = ?x |- context[?x] => rewrite <-H |
| 1314 |
| _ => push |
| 1315 |
| _ => intuition |
| 1316 |
end. |
| 1317 |
Qed. |
| 1318 |
Hint Rewrite split_p_skipn : push_misc. |
| 1319 |
|
| 1320 |
Lemma split_p : forall m1 p, |
| 1321 |
(m1 >= n)%nat -> |
| 1322 |
n <= length p -> |
| 1323 |
split (weight n) (combine (map weight (seq 0 m1)) p) = |
| 1324 |
(combine (map weight (seq 0 n)) (firstn n p), |
| 1325 |
(combine (map weight (seq 0 (m1 - n))) (skipn n p))). |
| 1326 |
Proof using n_gt_1 wprops. |
| 1327 |
clear dependent s. |
| 1328 |
intros m1 p ? ?. |
| 1329 |
replace m1 with (n + (m1 - n))%nat at 1 by lia. |
| 1330 |
rewrite <-(firstn_skipn n p) at 1. |
| 1331 |
push. |
| 1332 |
push. |
| 1333 |
lia. |
| 1334 |
Qed. |
| 1335 |
Hint Rewrite split_p : push_misc. |
| 1336 |
|
| 1337 |
Hint Rewrite repeat_length : push_length. |
| 1338 |
|
| 1339 |
Ltac solve_ibb := |
| 1340 |
apply eval_is_bounded_by; |
| 1341 |
match goal with |
| 1342 |
| |- context [firstn _ _] => eapply is_bounded_by_app_l |
| 1343 |
| |- context [skipn _ _] => eapply is_bounded_by_app_r |
| 1344 |
end; eauto; push; try lia. |
| 1345 |
|
| 1346 |
Lemma value_reduce1 : forall p m1 m2, |
| 1347 |
(m1 >= n)%nat -> |
| 1348 |
(m2 > 0)%nat -> |
| 1349 |
n <= length p -> |
| 1350 |
up_bound * weight (m1 - n) + weight n < weight m2 -> |
| 1351 |
let s' := fst (Saturated.Rows.adjust_s weight (S (S n)) s) in |
| 1352 |
let coef := Associational.sat_mul_const base [(1, s'/s)] c in |
| 1353 |
eval weight m2 (reduce1 base s c m1 m2 p) = |
| 1354 |
Associational.eval coef * eval weight (m1 - n) (skipn n p) + eval weight n (firstn n p). |
| 1355 |
Proof using base_nz c_pos coef_small n_gt_1 s_pos solinas_property wprops. |
| 1356 |
intros p m1 m2 H. |
| 1357 |
intros. |
| 1358 |
assert (Rows.adjust_s weight (S (S m1)) s = |
| 1359 |
Rows.adjust_s weight (S (S n)) s) as Hadjust. |
| 1360 |
{ destruct H. |
| 1361 |
auto. |
| 1362 |
rewrite solinas_property. |
| 1363 |
eapply adjust_s_finished; try apply solinas_property. |
| 1364 |
lia. |
| 1365 |
lia. } |
| 1366 |
cbv [s' coef reduce1]. |
| 1367 |
destruct (is_bounded_by (repeat (0, 2 ^ machine_wordsize - 1) m1) p) eqn:Heqb; push. |
| 1368 |
rewrite Hadjust. |
| 1369 |
rewrite solinas_property. |
| 1370 |
cbv [to_associational]. |
| 1371 |
push. |
| 1372 |
rewrite <-(firstn_skipn n p) in Heqb. |
| 1373 |
replace m1 with (n + (m1 - n))%nat in Heqb by lia. |
| 1374 |
rewrite repeat_app in Heqb. |
| 1375 |
solve_ineq. |
| 1376 |
solve_ibb. |
| 1377 |
solve_ibb. |
| 1378 |
etransitivity. |
| 1379 |
solve_ineq. |
| 1380 |
apply Z.mul_lt_mono_nonneg. |
| 1381 |
solve_ineq. |
| 1382 |
eauto. |
| 1383 |
solve_ibb. |
| 1384 |
solve_ibb. |
| 1385 |
solve_ibb. |
| 1386 |
lia. |
| 1387 |
|
| 1388 |
rewrite Hadjust. |
| 1389 |
rewrite solinas_property. |
| 1390 |
cbv [to_associational]. |
| 1391 |
push. |
| 1392 |
rewrite <-Z_div_mod_eq_full. |
| 1393 |
reflexivity. |
| 1394 |
all: push. |
| 1395 |
Qed. |
| 1396 |
|
| 1397 |
Lemma eval_reduce1 : forall p m1 m2, |
| 1398 |
(m1 >= n)%nat -> |
| 1399 |
(m2 > 0)%nat -> |
| 1400 |
n <= length p -> |
| 1401 |
up_bound * weight (m1 - n) + weight n < weight m2 -> |
| 1402 |
let q := reduce1 base s c m1 m2 p in |
| 1403 |
(Positional.eval weight m1 p) mod (s - Associational.eval c) |
| 1404 |
= (Positional.eval weight m2 q) mod (s - Associational.eval c). |
| 1405 |
Proof using base_nz c_pos coef_small mod_nz n_gt_1 s_pos solinas_property wprops. |
| 1406 |
intros p m1 m2; intros. |
| 1407 |
cbv [q]. |
| 1408 |
rewrite value_reduce1; try lia. |
| 1409 |
push. |
| 1410 |
rewrite solinas_property. |
| 1411 |
cbn [fst snd]. |
| 1412 |
match goal with |
| 1413 |
| |- context[_ mod (_ - ?c)] => |
| 1414 |
lazymatch goal with |
| 1415 |
| |- context[?x * ?c * ?y] => replace (x * c * y) with (c * (x * y)) by lia |
| 1416 |
end |
| 1417 |
end. |
| 1418 |
rewrite Z.add_comm. |
| 1419 |
rewrite <-reduction_rule. |
| 1420 |
apply Z.elim_mod. |
| 1421 |
rewrite <-(firstn_skipn n p) at 1. |
| 1422 |
replace m1 with (n + (m1 - n))%nat by lia. |
| 1423 |
cbv [eval to_associational]. |
| 1424 |
push. |
| 1425 |
rewrite Z.mul_assoc. |
| 1426 |
rewrite <-Z_div_exact_2. |
| 1427 |
rewrite Z.add_cancel_l. |
| 1428 |
cbn. |
| 1429 |
replace (n + (m1 - n) - n)%nat with (m1 - n)%nat by lia. |
| 1430 |
rewrite eval_seq_start. |
| 1431 |
lia. |
| 1432 |
lia. |
| 1433 |
pose proof (adjust_s_invariant (S (S n)) s ltac:(lia)) as Hadj. |
| 1434 |
rewrite solinas_property in Hadj. |
| 1435 |
intuition. |
| 1436 |
push. |
| 1437 |
lia. |
| 1438 |
lia. |
| 1439 |
Qed. |
| 1440 |
|
| 1441 |
(* END SECTION REDUCE1 *) |
| 1442 |
|
| 1443 |
(* SECTION REDUCE3 *) |
| 1444 |
|
| 1445 |
Lemma value_reduce1' : forall p m, |
| 1446 |
m = n -> |
| 1447 |
length p = S m -> |
| 1448 |
nth_default 0 p n <= 1 -> |
| 1449 |
(weight n / s * Associational.eval c) * (nth_default 0 p n) + eval weight n (firstn n p) < weight n -> |
| 1450 |
let s' := fst (Saturated.Rows.adjust_s weight (S (S n)) s) in |
| 1451 |
let coef := Associational.sat_mul_const base [(1, s'/s)] c in |
| 1452 |
eval weight m (reduce1 base s c (S m) m p) = |
| 1453 |
Associational.eval coef * nth_default 0 p n + eval weight n (firstn n p). |
| 1454 |
Proof. |
| 1455 |
intros p m H H1 H2 H3. |
| 1456 |
cbv [reduce1]. |
| 1457 |
rewrite H. |
| 1458 |
push. |
| 1459 |
erewrite adjust_s_finished'; try apply solinas_property. |
| 1460 |
rewrite solinas_property. |
| 1461 |
cbv [to_associational]. |
| 1462 |
push. |
| 1463 |
const_simpl. |
| 1464 |
rewrite skipn_nth_default with (d:=0) by lia. |
| 1465 |
rewrite skipn_all. |
| 1466 |
cbn [seq map]. |
| 1467 |
push. |
| 1468 |
|
| 1469 |
break_match. |
| 1470 |
assert (0 <= nth_default 0 p n). |
| 1471 |
apply is_bounded_by_nth with (n:=n) in Heqb. |
| 1472 |
etransitivity. |
| 1473 |
2: apply Heqb. |
| 1474 |
rewrite nth_default_repeat. |
| 1475 |
break_match; try lia. |
| 1476 |
reflexivity. |
| 1477 |
lia. |
| 1478 |
push. |
| 1479 |
|
| 1480 |
push. |
| 1481 |
rewrite Z.mod_small. |
| 1482 |
reflexivity. |
| 1483 |
solve_ineq. |
| 1484 |
rewrite <-firstn_skipn with (l:=p) (n:=n) in Heqb. |
| 1485 |
replace (S n) with (n + 1)%nat in Heqb by lia. |
| 1486 |
rewrite repeat_app in Heqb. |
| 1487 |
solve_ibb. |
| 1488 |
auto. |
| 1489 |
push. |
| 1490 |
rewrite <-Z_div_mod_eq_full. |
| 1491 |
all: push; lia. |
| 1492 |
Qed. |
| 1493 |
|
| 1494 |
Lemma eval_reduce1' : forall p m, |
| 1495 |
m = n -> |
| 1496 |
length p = S m -> |
| 1497 |
nth_default 0 p n <= 1 -> |
| 1498 |
(weight n / s * Associational.eval c) * (nth_default 0 p n) + eval weight n (firstn n p) < weight n -> |
| 1499 |
let s' := fst (Saturated.Rows.adjust_s weight (S (S n)) s) in |
| 1500 |
let coef := Associational.sat_mul_const base [(1, s'/s)] c in |
| 1501 |
let q := reduce1 base s c (S m) m p in |
| 1502 |
(Positional.eval weight (S m) p) mod (s - Associational.eval c) |
| 1503 |
= (Positional.eval weight m q) mod (s - Associational.eval c). |
| 1504 |
Proof using base_nz c_pos coef_small mod_nz n_gt_1 s_pos solinas_property wprops. |
| 1505 |
intros p m H H1 H2 H3 s' coef q. |
| 1506 |
cbv [q]. |
| 1507 |
rewrite value_reduce1'; try lia. |
| 1508 |
push. |
| 1509 |
rewrite solinas_property. |
| 1510 |
cbn [fst snd]. |
| 1511 |
match goal with |
| 1512 |
| |- context[_ mod (_ - ?c)] => |
| 1513 |
lazymatch goal with |
| 1514 |
| |- context[?x * ?c * ?y] => replace (x * c * y) with (c * (x * y)) by lia |
| 1515 |
end |
| 1516 |
end. |
| 1517 |
rewrite Z.add_comm. |
| 1518 |
rewrite <-reduction_rule. |
| 1519 |
apply Z.elim_mod. |
| 1520 |
rewrite <-(firstn_skipn n p) at 1. |
| 1521 |
replace (S m) with (m+1)%nat by lia. |
| 1522 |
cbv [eval to_associational]. |
| 1523 |
push. |
| 1524 |
rewrite Z.mul_assoc. |
| 1525 |
rewrite <-Z_div_exact_2. |
| 1526 |
rewrite H. |
| 1527 |
rewrite Z.add_cancel_l. |
| 1528 |
const_simpl. |
| 1529 |
rewrite eval_seq_start. |
| 1530 |
f_equal. |
| 1531 |
rewrite skipn_nth_default with (d:=0). |
| 1532 |
rewrite skipn_all. |
| 1533 |
cbn. |
| 1534 |
break_match; lia. |
| 1535 |
|
| 1536 |
lia. |
| 1537 |
lia. |
| 1538 |
lia. |
| 1539 |
pose proof (adjust_s_invariant (S (S n)) s ltac:(lia)) as Hadj. |
| 1540 |
rewrite solinas_property in Hadj. |
| 1541 |
intuition. |
| 1542 |
push; lia. |
| 1543 |
lia. |
| 1544 |
Qed. |
| 1545 |
|
| 1546 |
Lemma firstn_nth_default_0 : forall p, |
| 1547 |
length p > 0 -> |
| 1548 |
firstn 1 p = [nth_default 0 p 0]. |
| 1549 |
Proof. |
| 1550 |
intros p H. |
| 1551 |
induction p as [| a p IHp]. |
| 1552 |
push' H. |
| 1553 |
lia. |
| 1554 |
push. |
| 1555 |
Qed. |
| 1556 |
|
| 1557 |
Lemma eval_smaller m p : |
| 1558 |
(length p <= m)%nat -> |
| 1559 |
eval weight m p = eval weight (length p) p. |
| 1560 |
Proof. |
| 1561 |
intros H. |
| 1562 |
destruct p using rev_ind. |
| 1563 |
push. |
| 1564 |
unfold eval at 1. |
| 1565 |
cbv [to_associational]. |
| 1566 |
replace m with ((length (p ++ [x])) + (m - length (p ++ [x])))%nat. |
| 1567 |
rewrite seq_app. |
| 1568 |
rewrite map_app. |
| 1569 |
rewrite combine_truncate_l. |
| 1570 |
rewrite firstn_app_inleft. |
| 1571 |
rewrite firstn_all. |
| 1572 |
reflexivity. |
| 1573 |
push. |
| 1574 |
push. |
| 1575 |
lia. |
| 1576 |
Qed. |
| 1577 |
|
| 1578 |
Lemma eval_reduce3 : forall p, |
| 1579 |
canonical_repr (S n) p -> |
| 1580 |
(nth_default 0 p (n-1) = 0 /\ nth_default 0 p n = 1 /\ nth_default 0 p 0 < up_bound * up_bound + 1) \/ nth_default 0 p n = 0 -> |
| 1581 |
let q := reduce3 base s c n p in |
| 1582 |
(Positional.eval weight (S n) p) mod (s - Associational.eval c) |
| 1583 |
= (Positional.eval weight n q) mod (s - Associational.eval c). |
| 1584 |
Proof. |
| 1585 |
intros. |
| 1586 |
rewrite eval_reduce1'. |
| 1587 |
rewrite value_reduce1'. |
| 1588 |
rewrite solinas_property. |
| 1589 |
push. |
| 1590 |
const_simpl. |
| 1591 |
cbv [q reduce3 Let_In]. |
| 1592 |
assert (Hcanon := H). |
| 1593 |
unfold canonical_repr in Hcanon. |
| 1594 |
destruct Hcanon. |
| 1595 |
break_match. |
| 1596 |
|
| 1597 |
(* bounded *) |
| 1598 |
pose proof (is_bounded_by_nth 0 _ _ Heqb ltac:(lia)) . |
| 1599 |
specialize (H3 ltac:(push; try lia)). |
| 1600 |
rewrite nth_default_app in H3. |
| 1601 |
destruct (lt_dec 0 (Datatypes.length (repeat (0, 2 ^ machine_wordsize - 1) n))). |
| 1602 |
rewrite nth_default_repeat in H3. |
| 1603 |
destruct (dec (0 < n)%nat). |
| 1604 |
push' H3. |
| 1605 |
|
| 1606 |
match goal with |
| 1607 |
| |- context[fst (Rows.flatten weight 1 [ [?x]; [?y] ])] => |
| 1608 |
assert (fst (Rows.flatten weight 1 [ [x]; [y] ]) = |
| 1609 |
[fst (Z.add_get_carry machine_wordsize x y)]) |
| 1610 |
end. |
| 1611 |
{ cbv [Z.add_get_carry Z.add_with_get_carry Z.add_with_carry Z.get_carry Let_In]. |
| 1612 |
rewrite solinas_property. |
| 1613 |
push. |
| 1614 |
rewrite !Rows.eval_cons. |
| 1615 |
rewrite Rows.eval_nil. |
| 1616 |
push. |
| 1617 |
rewrite Partition.partition_step. |
| 1618 |
push. |
| 1619 |
intros. |
| 1620 |
cbn in H4. |
| 1621 |
intuition. |
| 1622 |
rewrite <-H7; push. |
| 1623 |
rewrite <-H4; push. |
| 1624 |
rewrite <-H0; push. |
| 1625 |
rewrite <-H4; push. } |
| 1626 |
rewrite H4. |
| 1627 |
|
| 1628 |
cbv [Z.add_get_carry Z.add_with_get_carry Z.add_with_carry Z.get_carry Let_In Z.zselect]. |
| 1629 |
rewrite solinas_property. |
| 1630 |
push. |
| 1631 |
rewrite <-firstn_skipn with (l:=(firstn n p)) (n:=1%nat) at 1. |
| 1632 |
rewrite firstn_firstn. |
| 1633 |
rewrite firstn_nth_default_0. |
| 1634 |
intuition. |
| 1635 |
(* nth_default 0 p n = 1 *) |
| 1636 |
rewrite H3. |
| 1637 |
break_match; [lia|]. |
| 1638 |
push. |
| 1639 |
|
| 1640 |
f_equal. |
| 1641 |
rewrite Z.mod_small. |
| 1642 |
cbv [eval to_associational]. |
| 1643 |
destruct n eqn:E1. |
| 1644 |
lia. |
| 1645 |
cbn [seq map]. |
| 1646 |
replace (weight 0 :: map weight (seq 1 n0)) with ([weight 0] ++ map weight (seq 1 n0)) by auto. |
| 1647 |
rewrite !combine_app_samelength. |
| 1648 |
cbn [combine]. |
| 1649 |
rewrite !eval_app. |
| 1650 |
push. |
| 1651 |
lia. |
| 1652 |
cbn; lia. |
| 1653 |
cbn; lia. |
| 1654 |
solve_ineq. |
| 1655 |
etransitivity. |
| 1656 |
apply Z.add_lt_mono. |
| 1657 |
eauto. |
| 1658 |
eauto. |
| 1659 |
cbv [up_bound]; weight_comp; simpl; lia. |
| 1660 |
|
| 1661 |
(* nth_default 0 p n = 0 *) |
| 1662 |
rewrite H3. |
| 1663 |
break_match; [| lia]. |
| 1664 |
push. |
| 1665 |
f_equal. |
| 1666 |
rewrite Z.mod_small. |
| 1667 |
lia. |
| 1668 |
solve_ineq. |
| 1669 |
lia. |
| 1670 |
lia. |
| 1671 |
lia. |
| 1672 |
intuition. |
| 1673 |
push' n0. |
| 1674 |
lia. |
| 1675 |
push' n0. |
| 1676 |
lia. |
| 1677 |
|
| 1678 |
(* not bounded *) |
| 1679 |
rewrite solinas_property. |
| 1680 |
push. |
| 1681 |
push; lia. |
| 1682 |
push; lia. |
| 1683 |
lia. |
| 1684 |
solve_length p. |
| 1685 |
lia. |
| 1686 |
|
| 1687 |
intuition. |
| 1688 |
{ rewrite H1. |
| 1689 |
rewrite <-firstn_skipn with (n:=(n-1)%nat) (l:=firstn n p). |
| 1690 |
rewrite firstn_firstn by lia. |
| 1691 |
rewrite skipn_nth_default with (d:=0). |
| 1692 |
rewrite skipn_all. |
| 1693 |
rewrite nth_default_firstn. |
| 1694 |
destruct le_dec. |
| 1695 |
destruct lt_dec; [| lia]. |
| 1696 |
rewrite H0. |
| 1697 |
cbv [eval to_associational]. |
| 1698 |
destruct n eqn:E; [lia|]. |
| 1699 |
rewrite seq_snoc. |
| 1700 |
rewrite map_app, combine_app_samelength. |
| 1701 |
rewrite eval_app. |
| 1702 |
push. |
| 1703 |
pose proof (firstn_skipn n0 p). |
| 1704 |
symmetry in H2. |
| 1705 |
canonical_app p. |
| 1706 |
push' Hcanon_l. |
| 1707 |
rewrite min_l in Hcanon_l by lia. |
| 1708 |
pose proof (canonical_eval_bounded n0 (firstn n0 p) ltac:(auto)). |
| 1709 |
etransitivity. |
| 1710 |
cbv [eval to_associational] in H4. |
| 1711 |
replace (S n0 - 1)%nat with (n0) by lia. |
| 1712 |
apply Z.add_lt_le_mono. |
| 1713 |
eauto. |
| 1714 |
le_lt; eauto. |
| 1715 |
cbv [up_bound]. |
| 1716 |
rewrite Z.add_sub_assoc. |
| 1717 |
rewrite Z.add_sub_swap. |
| 1718 |
rewrite Z.lt_add_lt_sub_r. |
| 1719 |
apply weight_dif_lt with (n:=0%nat). |
| 1720 |
lia. |
| 1721 |
weight_comp; simpl; lia. |
| 1722 |
push. |
| 1723 |
lia. |
| 1724 |
push. |
| 1725 |
intuition. |
| 1726 |
exfalso. |
| 1727 |
apply n0. |
| 1728 |
unfold canonical_repr in H. |
| 1729 |
lia. |
| 1730 |
push. |
| 1731 |
lia. |
| 1732 |
push. |
| 1733 |
unfold canonical_repr in H. |
| 1734 |
lia. } |
| 1735 |
rewrite H1. |
| 1736 |
ring_simplify. |
| 1737 |
pose proof (firstn_skipn n p). |
| 1738 |
symmetry in H0. |
| 1739 |
canonical_app p. |
| 1740 |
push' Hcanon_l. |
| 1741 |
rewrite min_l in Hcanon_l; [|solve_length p]. |
| 1742 |
apply canonical_eval_bounded; auto. |
| 1743 |
lia. |
| 1744 |
solve_length p. |
| 1745 |
lia. |
| 1746 |
intuition. |
| 1747 |
{ rewrite H1. |
| 1748 |
rewrite <-firstn_skipn with (n:=(n-1)%nat) (l:=firstn n p). |
| 1749 |
rewrite firstn_firstn by lia. |
| 1750 |
rewrite skipn_nth_default with (d:=0). |
| 1751 |
rewrite skipn_all. |
| 1752 |
rewrite nth_default_firstn. |
| 1753 |
destruct le_dec. |
| 1754 |
destruct lt_dec; [| lia]. |
| 1755 |
rewrite H0. |
| 1756 |
cbv [eval to_associational]. |
| 1757 |
destruct n eqn:E; [lia|]. |
| 1758 |
rewrite seq_snoc. |
| 1759 |
rewrite map_app, combine_app_samelength. |
| 1760 |
rewrite eval_app. |
| 1761 |
push. |
| 1762 |
pose proof (firstn_skipn n0 p). |
| 1763 |
symmetry in H2. |
| 1764 |
canonical_app p. |
| 1765 |
push' Hcanon_l. |
| 1766 |
rewrite min_l in Hcanon_l by lia. |
| 1767 |
pose proof (canonical_eval_bounded n0 (firstn n0 p) ltac:(auto)). |
| 1768 |
etransitivity. |
| 1769 |
cbv [eval to_associational] in H4. |
| 1770 |
replace (S n0 - 1)%nat with (n0) by lia. |
| 1771 |
apply Z.add_lt_le_mono. |
| 1772 |
eauto. |
| 1773 |
le_lt; eauto. |
| 1774 |
cbv [up_bound]. |
| 1775 |
rewrite Z.add_sub_assoc. |
| 1776 |
rewrite Z.add_sub_swap. |
| 1777 |
rewrite Z.lt_add_lt_sub_r. |
| 1778 |
apply weight_dif_lt with (n:=0%nat). |
| 1779 |
lia. |
| 1780 |
weight_comp; simpl; lia. |
| 1781 |
push. |
| 1782 |
lia. |
| 1783 |
push. |
| 1784 |
intuition. |
| 1785 |
exfalso. |
| 1786 |
apply n0. |
| 1787 |
unfold canonical_repr in H. |
| 1788 |
lia. |
| 1789 |
push. |
| 1790 |
lia. |
| 1791 |
push. |
| 1792 |
unfold canonical_repr in H. |
| 1793 |
lia. } |
| 1794 |
rewrite H1. |
| 1795 |
ring_simplify. |
| 1796 |
pose proof (firstn_skipn n p). |
| 1797 |
symmetry in H0. |
| 1798 |
canonical_app p. |
| 1799 |
push' Hcanon_l. |
| 1800 |
rewrite min_l in Hcanon_l; [|solve_length p]. |
| 1801 |
apply canonical_eval_bounded; auto. |
| 1802 |
Qed. |
| 1803 |
|
| 1804 |
(* END SECTION REDUCE3 *) |
| 1805 |
|
| 1806 |
(* SECTION REDUCE_FIRST *) |
| 1807 |
|
| 1808 |
Lemma reduce_first_canonical : forall p, |
| 1809 |
length p = (2 * n)%nat -> |
| 1810 |
is_bounded_by (repeat (0, 2 ^ machine_wordsize - 1) (2 * n)) p = true-> |
| 1811 |
canonical_repr (S n) (reduce1 base s c (2*n) (S n) p). |
| 1812 |
Proof using base_nz c_pos coef_small n_gt_1 s_pos solinas_property wprops. |
| 1813 |
intros p Hlen H. |
| 1814 |
cbv [reduce1 canonical_repr]. |
| 1815 |
rewrite H. |
| 1816 |
push. |
| 1817 |
intuition. |
| 1818 |
erewrite adjust_s_finished; try apply solinas_property; try lia. |
| 1819 |
push. |
| 1820 |
f_equal. |
| 1821 |
rewrite Z.mod_small. |
| 1822 |
reflexivity. |
| 1823 |
cbv [to_associational]. |
| 1824 |
push. |
| 1825 |
rewrite <-(firstn_skipn n p) in H. |
| 1826 |
replace (2*n-n)%nat with n by lia. |
| 1827 |
replace (2 * n)%nat with (n + n)%nat in H by lia. |
| 1828 |
rewrite repeat_app in H. |
| 1829 |
solve_ineq. |
| 1830 |
|
| 1831 |
solve_ibb. |
| 1832 |
solve_ibb. |
| 1833 |
etransitivity. |
| 1834 |
solve_ineq. |
| 1835 |
apply Z.mul_lt_mono_nonneg. |
| 1836 |
solve_ineq. |
| 1837 |
eauto. |
| 1838 |
solve_ibb. |
| 1839 |
solve_ibb. |
| 1840 |
solve_ibb. |
| 1841 |
cbv [up_bound machine_wordsize]. |
| 1842 |
weight_comp. |
| 1843 |
rewrite <-Z.mul_succ_l. |
| 1844 |
apply Zmult_lt_compat_r. |
| 1845 |
apply Z.pow_pos_nonneg; lia. |
| 1846 |
all: cbn; break_match; lia. |
| 1847 |
Qed. |
| 1848 |
|
| 1849 |
(* END SECTION REDUCE_FIRST *) |
| 1850 |
|
| 1851 |
(* SECTION REDUCE_SECOND *) |
| 1852 |
|
| 1853 |
Lemma reduce_second_canonical : forall p, |
| 1854 |
canonical_repr (S n) p -> |
| 1855 |
canonical_repr (S n) (reduce1 base s c (S n) (S n) p). |
| 1856 |
Proof using base_nz c_pos coef_small n_gt_1 s_pos solinas_property wprops. |
| 1857 |
intros p H. |
| 1858 |
cbv [canonical_repr]. |
| 1859 |
push. |
| 1860 |
assert (Hcanon := H). |
| 1861 |
cbv [canonical_repr] in H. |
| 1862 |
intuition. |
| 1863 |
rewrite value_reduce1. |
| 1864 |
rewrite solinas_property. |
| 1865 |
push. |
| 1866 |
cbv [reduce1]. |
| 1867 |
break_match. |
| 1868 |
push. |
| 1869 |
erewrite adjust_s_finished'; try eapply solinas_property. |
| 1870 |
cbv [to_associational]. |
| 1871 |
rewrite split_p. |
| 1872 |
push. |
| 1873 |
lia. |
| 1874 |
lia. |
| 1875 |
lia. |
| 1876 |
|
| 1877 |
rewrite canonical_is_bounded_by in Hcanon. |
| 1878 |
intuition. |
| 1879 |
match goal with |
| 1880 |
| H : ?x = true, H1 : ?x = false |- _ => rewrite H in H1; discriminate |
| 1881 |
end. |
| 1882 |
lia. |
| 1883 |
lia. |
| 1884 |
lia. |
| 1885 |
replace (S n - n)%nat with 1%nat by lia. |
| 1886 |
cbv [up_bound machine_wordsize]. |
| 1887 |
rewrite Z.lt_add_lt_sub_r. |
| 1888 |
etransitivity; [ | apply weight_dif_mono with (n:=1%nat); lia ]. |
| 1889 |
weight_comp; cbn; lia. |
| 1890 |
Qed. |
| 1891 |
|
| 1892 |
Lemma up_bound_weight1 : forall m, |
| 1893 |
(m > 1)%nat -> |
| 1894 |
up_bound * weight 1 < weight (S m) - weight m. |
| 1895 |
Proof. |
| 1896 |
intros m H. |
| 1897 |
induction H. |
| 1898 |
cbv [up_bound]. |
| 1899 |
weight_comp; try lia. |
| 1900 |
simpl; break_match; lia. |
| 1901 |
etransitivity. |
| 1902 |
2: apply weight_dif_mono'. |
| 1903 |
auto. |
| 1904 |
Qed. |
| 1905 |
|
| 1906 |
Hint Rewrite nth_default_partition : push_misc. |
| 1907 |
Lemma reduce_second_bounds : forall p, |
| 1908 |
canonical_repr (S n) p -> |
| 1909 |
(nth_default 0 p n) < up_bound -> |
| 1910 |
let q := reduce1 base s c (S n) (S n) p in |
| 1911 |
(nth_default 0 q (n-1) = 0 /\ nth_default 0 q n = 1 /\ nth_default 0 q 0 < up_bound * up_bound + 1) \/ |
| 1912 |
nth_default 0 q n = 0. |
| 1913 |
Proof using base_nz c_pos coef_small n_gt_1 s_pos solinas_property wprops. |
| 1914 |
intros p ? ? q. |
| 1915 |
pose proof (reduce_second_canonical p ltac:(auto)) as Hcanonq. |
| 1916 |
fold q in Hcanonq. |
| 1917 |
pose proof (firstn_skipn n p) as Hp; symmetry in Hp. |
| 1918 |
pose proof (firstn_skipn n q) as Hq; symmetry in Hq. |
| 1919 |
canonical_app p. |
| 1920 |
push' Hcanon_l. |
| 1921 |
push' Hcanon_r. |
| 1922 |
canonical_app q; push' Hcanon_l0; push' Hcanon_r0. |
| 1923 |
replace (length p) with (S n) in * by (solve_length p). |
| 1924 |
replace (length q) with (S n) in * by (solve_length q). |
| 1925 |
rewrite min_l in *; [| lia | solve_length q]. |
| 1926 |
const_simpl. |
| 1927 |
|
| 1928 |
assert (0 <= nth_default 0 q n < 2). |
| 1929 |
assert (Hcanonq' := Hcanonq). |
| 1930 |
cbv [canonical_repr] in Hcanonq'. |
| 1931 |
destruct Hcanonq as [ _ Hpartq ]. |
| 1932 |
rewrite Hpartq. |
| 1933 |
push. |
| 1934 |
solve_ineq; auto. |
| 1935 |
rewrite Z.mod_small. |
| 1936 |
cbv [q]. |
| 1937 |
rewrite value_reduce1. |
| 1938 |
const_simpl. |
| 1939 |
rewrite solinas_property. |
| 1940 |
push. |
| 1941 |
rewrite <-Zplus_diag_eq_mult_2. |
| 1942 |
solve_ineq. |
| 1943 |
etransitivity. |
| 1944 |
apply Z.mul_lt_mono_nonneg. |
| 1945 |
solve_ineq. |
| 1946 |
eauto. |
| 1947 |
apply canonical_pos; auto. |
| 1948 |
rewrite skipn_nth_default with (d:=0). |
| 1949 |
rewrite skipn_all. |
| 1950 |
push; eauto. |
| 1951 |
solve_length p. |
| 1952 |
solve_length p. |
| 1953 |
cbv [up_bound machine_wordsize]. |
| 1954 |
weight_comp. |
| 1955 |
rewrite <-OrdersEx.Z_as_OT.pow_mul_r. |
| 1956 |
apply Z.pow_lt_mono_r; cbn; break_match; lia. |
| 1957 |
cbn; lia. |
| 1958 |
lia. |
| 1959 |
apply canonical_eval_bounded; auto. |
| 1960 |
lia. |
| 1961 |
lia. |
| 1962 |
solve_length p. |
| 1963 |
const_simpl. |
| 1964 |
cbv [up_bound machine_wordsize]. |
| 1965 |
rewrite Z.lt_add_lt_sub_r. |
| 1966 |
etransitivity; [ | apply weight_dif_mono with (n:=1%nat); lia ]. |
| 1967 |
weight_comp; cbn; lia. |
| 1968 |
solve_ineq; [apply canonical_pos | apply canonical_eval_bounded]; auto. |
| 1969 |
|
| 1970 |
assert (Hnth : nth_default 0 q n = 0 \/ nth_default 0 q n = 1) by lia. |
| 1971 |
destruct Hnth as [Hnth1 | Hnth2]. |
| 1972 |
intuition. |
| 1973 |
left. |
| 1974 |
|
| 1975 |
intuition. |
| 1976 |
assert (Hcanonq' := Hcanonq). |
| 1977 |
destruct Hcanonq' as [_ Hpart]. |
| 1978 |
rewrite Hpart. |
| 1979 |
push. |
| 1980 |
assert (H' : Associational.eval (combine (map weight (seq 0 n)) (firstn n q)) = eval weight (S n) q - weight n). |
| 1981 |
rewrite Hq at 2. |
| 1982 |
cbv [eval to_associational]. |
| 1983 |
rewrite seq_snoc. |
| 1984 |
push. |
| 1985 |
rewrite skipn_nth_default with (d:=0). |
| 1986 |
rewrite skipn_all. |
| 1987 |
const_simpl. |
| 1988 |
cbn [seq]. |
| 1989 |
push. |
| 1990 |
lia. |
| 1991 |
solve_length q. |
| 1992 |
solve_length q. |
| 1993 |
push. |
| 1994 |
rewrite min_l; [lia | solve_length q]. |
| 1995 |
rewrite <-Z.add_move_l in H'. |
| 1996 |
rewrite <-H'. |
| 1997 |
const_simpl. |
| 1998 |
rewrite Zplus_mod, Z.mod_same, Z.add_0_l, Z.mod_mod. |
| 1999 |
rewrite Z.add_move_l in H'. |
| 2000 |
apply Z.div_small. |
| 2001 |
rewrite Z.mod_small. |
| 2002 |
solve_ineq. |
| 2003 |
apply canonical_pos; auto. |
| 2004 |
rewrite H'. |
| 2005 |
rewrite Z.lt_sub_lt_add_l. |
| 2006 |
cbv [q]. |
| 2007 |
rewrite value_reduce1. |
| 2008 |
rewrite solinas_property. |
| 2009 |
push. |
| 2010 |
const_simpl. |
| 2011 |
rewrite Z.add_comm. |
| 2012 |
solve_ineq. |
| 2013 |
apply canonical_eval_bounded; auto. |
| 2014 |
rewrite skipn_nth_default with (d:=0). |
| 2015 |
rewrite skipn_all. |
| 2016 |
push. |
| 2017 |
etransitivity. |
| 2018 |
apply Z.mul_lt_mono_nonneg. |
| 2019 |
solve_ineq. |
| 2020 |
eauto. |
| 2021 |
apply (canonical_bounded (S n) p). |
| 2022 |
auto. |
| 2023 |
rewrite Hp at 2. |
| 2024 |
apply in_or_app. |
| 2025 |
right. |
| 2026 |
rewrite skipn_nth_default with (d:=0). |
| 2027 |
rewrite skipn_all. |
| 2028 |
push. |
| 2029 |
solve_length p. |
| 2030 |
solve_length p. |
| 2031 |
eauto. |
| 2032 |
cbv [up_bound machine_wordsize]. |
| 2033 |
rewrite <-Le.Z.le_sub_1_iff. |
| 2034 |
etransitivity; [| rewrite <-Z.sub_le_mono_r; apply (weight_mono_le 1)]. |
| 2035 |
weight_comp; cbn; lia. |
| 2036 |
lia. |
| 2037 |
solve_length p. |
| 2038 |
solve_length p. |
| 2039 |
lia. |
| 2040 |
lia. |
| 2041 |
solve_length p. |
| 2042 |
const_simpl. |
| 2043 |
cbv [up_bound machine_wordsize]. |
| 2044 |
rewrite Z.lt_add_lt_sub_r. |
| 2045 |
etransitivity; [| apply (weight_dif_mono 1)]. |
| 2046 |
weight_comp; cbn; lia. |
| 2047 |
lia. |
| 2048 |
solve_ineq. |
| 2049 |
apply canonical_pos; auto. |
| 2050 |
apply canonical_eval_bounded; auto. |
| 2051 |
auto. |
| 2052 |
auto. |
| 2053 |
lia. |
| 2054 |
|
| 2055 |
assert (Hcanonq' := Hcanonq). |
| 2056 |
destruct Hcanonq as [ _ Hpartq]. |
| 2057 |
rewrite Hpartq. |
| 2058 |
rewrite nth_default_partition. |
| 2059 |
rewrite weight_0. |
| 2060 |
rewrite Z.div_1_r. |
| 2061 |
assert (eval weight (S n) q = eval weight n (firstn n q) + weight n). |
| 2062 |
{ rewrite Hq at 1. |
| 2063 |
rewrite skipn_nth_default with (d:=0). |
| 2064 |
rewrite skipn_all. |
| 2065 |
rewrite eval_snoc_S. |
| 2066 |
lia. |
| 2067 |
push. |
| 2068 |
rewrite min_l. |
| 2069 |
lia. |
| 2070 |
solve_length q. |
| 2071 |
solve_length q. |
| 2072 |
solve_length q. } |
| 2073 |
assert (eval weight (S n) q = weight n / s * Associational.eval c * nth_default 0 p n + eval weight n (firstn n p)). |
| 2074 |
{ unfold q at 1. |
| 2075 |
rewrite value_reduce1. |
| 2076 |
rewrite solinas_property. |
| 2077 |
push. |
| 2078 |
const_simpl. |
| 2079 |
unfold eval at 1. |
| 2080 |
unfold to_associational at 1. |
| 2081 |
rewrite skipn_nth_default with (d:=0). |
| 2082 |
rewrite skipn_all. |
| 2083 |
cbn [seq map combine]. |
| 2084 |
push. |
| 2085 |
solve_length p. |
| 2086 |
solve_length p. |
| 2087 |
lia. |
| 2088 |
lia. |
| 2089 |
solve_length p. |
| 2090 |
const_simpl. |
| 2091 |
rewrite Z.lt_add_lt_sub_r. |
| 2092 |
apply up_bound_weight1; lia. } |
| 2093 |
rewrite H1 in H4. |
| 2094 |
apply LinearSubstitute.Z.move_R_pX in H4. |
| 2095 |
rewrite H1. |
| 2096 |
rewrite PullPush.Z.add_mod_r. |
| 2097 |
rewrite Weight.weight_multiples_full. |
| 2098 |
const_simpl. |
| 2099 |
rewrite Z.mod_small. |
| 2100 |
rewrite H4. |
| 2101 |
etransitivity. |
| 2102 |
apply Z.add_lt_mono_r. |
| 2103 |
apply Z.add_lt_mono. |
| 2104 |
apply Z.mul_lt_mono_nonneg. |
| 2105 |
solve_ineq. |
| 2106 |
eauto. |
| 2107 |
eapply canonical_bounded with (n:=S n) (p:=p). |
| 2108 |
auto. |
| 2109 |
rewrite Hp. |
| 2110 |
rewrite nth_default_app. |
| 2111 |
break_match. |
| 2112 |
push' H5. |
| 2113 |
rewrite min_l in H5. |
| 2114 |
lia. |
| 2115 |
lia. |
| 2116 |
push. |
| 2117 |
rewrite min_l. |
| 2118 |
rewrite Nat.sub_diag. |
| 2119 |
rewrite skipn_nth_default with (d:=0). |
| 2120 |
rewrite skipn_all. |
| 2121 |
apply in_or_app. |
| 2122 |
right. |
| 2123 |
push. |
| 2124 |
solve_length p. |
| 2125 |
solve_length p. |
| 2126 |
solve_length p. |
| 2127 |
eauto. |
| 2128 |
apply canonical_eval_bounded. |
| 2129 |
eauto. |
| 2130 |
lia. |
| 2131 |
solve_ineq. |
| 2132 |
apply canonical_pos; auto. |
| 2133 |
rewrite H4. |
| 2134 |
etransitivity. |
| 2135 |
apply Z.add_lt_mono_r. |
| 2136 |
apply Z.add_lt_mono. |
| 2137 |
apply Z.mul_lt_mono_nonneg. |
| 2138 |
solve_ineq. |
| 2139 |
eauto. |
| 2140 |
eapply canonical_bounded with (n:=S n) (p:=p). |
| 2141 |
auto. |
| 2142 |
rewrite Hp. |
| 2143 |
rewrite nth_default_app. |
| 2144 |
break_match. |
| 2145 |
push' H5. |
| 2146 |
rewrite min_l in H5. |
| 2147 |
lia. |
| 2148 |
lia. |
| 2149 |
push. |
| 2150 |
rewrite min_l. |
| 2151 |
rewrite Nat.sub_diag. |
| 2152 |
rewrite skipn_nth_default with (d:=0). |
| 2153 |
rewrite skipn_all. |
| 2154 |
apply in_or_app. |
| 2155 |
right. |
| 2156 |
push. |
| 2157 |
solve_length p. |
| 2158 |
solve_length p. |
| 2159 |
solve_length p. |
| 2160 |
eauto. |
| 2161 |
apply canonical_eval_bounded. |
| 2162 |
eauto. |
| 2163 |
rewrite <-Z.add_assoc. |
| 2164 |
rewrite Z.add_opp_diag_r. |
| 2165 |
const_simpl. |
| 2166 |
cbv [up_bound]. |
| 2167 |
weight_comp. |
| 2168 |
simpl; break_match; lia. |
| 2169 |
lia. |
| 2170 |
lia. |
| 2171 |
auto. |
| 2172 |
auto. |
| 2173 |
lia. |
| 2174 |
auto. |
| 2175 |
lia. |
| 2176 |
Qed. |
| 2177 |
|
| 2178 |
(* END SECTION REDUCE_SECOND *) |
| 2179 |
|
| 2180 |
(* (* SECTION REDUCE_THIRD *) *) |
| 2181 |
|
| 2182 |
(* Lemma eval_reduce_third' : forall p, *) |
| 2183 |
(* (canonical_repr (S n) p) -> *) |
| 2184 |
(* let q := reduce3 base s c n p in *) |
| 2185 |
(* ((nth_default 0 p (n-1) = 0 /\ nth_default 0 q n = 1 /\ nth_default 0 q 0 < up_bound * up_bound + 1) \/ nth_default 0 q n = 0) -> *) |
| 2186 |
(* (Positional.eval weight (S n) p) mod (s - Associational.eval c) *) |
| 2187 |
(* = (Positional.eval weight n q) mod (s - Associational.eval c). *) |
| 2188 |
(* Proof. *) |
| 2189 |
(* intros p ? q ?. *) |
| 2190 |
(* cbv [q]. *) |
| 2191 |
(* rewrite eval_reduce3. *) |
| 2192 |
(* lia. *) |
| 2193 |
(* lia. *) |
| 2194 |
(* solve_length p. *) |
| 2195 |
(* Qed. *) |
| 2196 |
|
| 2197 |
(* (* END SECTION REDUCE_THIRD *) *) |
| 2198 |
|
| 2199 |
(* SECTION REDUCE_FULL] *) |
| 2200 |
Theorem reduce_full_correct : forall (p : list Z), |
| 2201 |
n <= length p -> |
| 2202 |
let r := reduce_full base s c n p in |
| 2203 |
(Positional.eval weight (2 * n) p) mod (s - Associational.eval c) |
| 2204 |
= (Positional.eval weight n r) mod (s - Associational.eval c). |
| 2205 |
Proof. |
| 2206 |
intros p ? r; cbv [r reduce_full]; break_match. |
| 2207 |
pose proof (is_bounded_by_nth n _ _ Heqb ltac:(push) ltac:(push)) as Hnth. |
| 2208 |
repeat match goal with |
| 2209 |
| H : context[nth_default _ (_ ++ _) _] |- _ => rewrite nth_default_app in H |
| 2210 |
| H : context[snd (nth_default _ _ _)] |- _ => progress cbn in H |
| 2211 |
| H : _ |- _ => progress push' H |
| 2212 |
| _ => progress destruct lt_dec |
| 2213 |
| _ => progress intuition |
| 2214 |
| _ => lia |
| 2215 |
end. |
| 2216 |
apply is_bounded_by_loosen with (bound2:=repeat (0, 2^machine_wordsize-1) (S n)) in Heqb. |
| 2217 |
assert (canonical_repr (S n) (reduce1 base s c (2*n) (S n) p)). |
| 2218 |
rewrite canonical_is_bounded_by. |
| 2219 |
intuition; push. |
| 2220 |
rewrite <-eval_reduce3. |
| 2221 |
rewrite <-eval_reduce1. |
| 2222 |
rewrite <-eval_reduce1. |
| 2223 |
auto. |
| 2224 |
pose proof (firstn_skipn n p) as Hp; symmetry in Hp. |
| 2225 |
|
| 2226 |
all: |
| 2227 |
repeat multimatch goal with |
| 2228 |
| _ => apply reduce_second_canonical |
| 2229 |
| _ => apply reduce_second_bounds |
| 2230 |
| _ => solve_length p |
| 2231 |
| _ => const_simpl |
| 2232 |
| _ => cbv [up_bound] |
| 2233 |
| _ => push |
| 2234 |
| _ => auto |
| 2235 |
| _ => lia |
| 2236 |
end. |
| 2237 |
weight_comp; try lia. |
| 2238 |
rewrite <-Z.mul_succ_l. |
| 2239 |
apply Zmult_lt_compat_r. |
| 2240 |
apply Z.pow_pos_nonneg; cbn; break_match; lia. |
| 2241 |
cbn; lia. |
| 2242 |
rewrite Z.lt_add_lt_sub_r. |
| 2243 |
etransitivity; [| apply (weight_dif_mono 1); lia]. |
| 2244 |
weight_comp; cbn; break_match; lia. |
| 2245 |
autounfold. |
| 2246 |
replace (S n) with (n+1)%nat. |
| 2247 |
cbn. |
| 2248 |
const_simpl. |
| 2249 |
replace (n+1)%nat with (S n) by lia. |
| 2250 |
lia. |
| 2251 |
lia. |
| 2252 |
cbv [fold_andb_map' dual_map]. |
| 2253 |
cbn [repeat]. |
| 2254 |
rewrite repeat_cons. |
| 2255 |
rewrite combine_app_samelength. |
| 2256 |
rewrite map_app. |
| 2257 |
rewrite fold_right_app. |
| 2258 |
cbn. |
| 2259 |
pose proof (bounds_same (repeat (0, 18446744073709551615) n)). |
| 2260 |
auto. |
| 2261 |
auto. |
| 2262 |
|
| 2263 |
(* not canonical *) |
| 2264 |
rewrite eval_reduce1 with (m2:=S n). |
| 2265 |
rewrite <-(firstn_skipn n (reduce1 base s c (2 * n) (S n) p)) at 1. |
| 2266 |
unfold eval at 1. |
| 2267 |
unfold to_associational. |
| 2268 |
rewrite seq_snoc. |
| 2269 |
rewrite skipn_nth_default with (d:=0). |
| 2270 |
rewrite skipn_all. |
| 2271 |
push. |
| 2272 |
apply Z.elim_mod. |
| 2273 |
const_simpl. |
| 2274 |
rewrite Z.add_comm at 1. |
| 2275 |
auto. |
| 2276 |
all: |
| 2277 |
repeat multimatch goal with |
| 2278 |
| _ => push |
| 2279 |
| _ => lia |
| 2280 |
end. |
| 2281 |
const_simpl. |
| 2282 |
cbv [up_bound]. |
| 2283 |
weight_comp; try lia. |
| 2284 |
rewrite <-Z.mul_succ_l. |
| 2285 |
apply Zmult_lt_compat_r. |
| 2286 |
apply Z.pow_pos_nonneg; cbn; break_match; lia. |
| 2287 |
cbn; lia. |
| 2288 |
Qed. |
| 2289 |
|
| 2290 |
(* END SECTION REDUCE_FULL *) |
| 2291 |
|
| 2292 |
(* SECTION MULMOD *) |
| 2293 |
Theorem mulmod'_correct : forall p q, |
| 2294 |
Positional.eval weight n (mulmod' base s c n p q) mod (s - Associational.eval c) = |
| 2295 |
(Positional.eval weight n p * Positional.eval weight n q) mod (s - Associational.eval c). |
| 2296 |
Proof using base_nz c_pos coef_small mod_nz n_gt_1 s_pos solinas_property wprops. |
| 2297 |
intros. |
| 2298 |
cbv [mulmod']. |
| 2299 |
rewrite <-reduce_full_correct; push; lia. |
| 2300 |
Qed. |
| 2301 |
|
| 2302 |
Theorem mulmod_correct : forall p q, |
| 2303 |
Positional.eval weight n (mulmod base s c n p q) mod (s - Associational.eval c) = |
| 2304 |
(Positional.eval weight n p * Positional.eval weight n q) mod (s - Associational.eval c). |
| 2305 |
Proof using base_nz c_pos coef_small mod_nz n_gt_1 s_pos solinas_property wprops. |
| 2306 |
intros. |
| 2307 |
rewrite mulmod_cps_conv. |
| 2308 |
apply mulmod'_correct. |
| 2309 |
Qed. |
| 2310 |
(* END SECTION MULMOD *) |
| 2311 |
|
| 2312 |
End mulmod. |
| 2313 |
|
| 2314 |
Section squaremod. |
| 2315 |
|
| 2316 |
Definition sqr_indiv' base (state : list (Z * Z)) (p : list (Z * Z)) := |
| 2317 |
fold_right (fun a b => b ++ Associational.sat_mul base [a] [a]) state p. |
| 2318 |
|
| 2319 |
Definition sqr_indiv base (p : list (Z * Z)) := |
| 2320 |
sqr_indiv' base [] p. |
| 2321 |
|
| 2322 |
Definition square1 base (p : list (Z * Z)) := |
| 2323 |
let prod0 := Saturated.Associational.sat_mul base (firstn 1 p) (skipn 1 p) in |
| 2324 |
let prod1 := Saturated.Associational.sat_mul base (skipn 3 p) (firstn 2 (skipn 1 p)) in |
| 2325 |
let carry1_a := prod0 ++ prod1 in |
| 2326 |
let carry1_rows := Saturated.Rows.from_associational weight 7 carry1_a in |
| 2327 |
let carry1 := Saturated.Rows.flatten weight 7 carry1_rows in |
| 2328 |
let prod2 := Saturated.Associational.sat_mul base (firstn 1 (skipn 1 p)) (firstn 1 (skipn 2 p)) in |
| 2329 |
let carry2_rows := Saturated.Rows.from_associational weight 7 prod2 in |
| 2330 |
let carry2 := Saturated.Rows.flatten' weight carry1 carry2_rows in |
| 2331 |
let carry2 := (fst carry2) ++ [snd carry2] in |
| 2332 |
carry2. |
| 2333 |
|
| 2334 |
Definition square_no_reduce base n (p : list Z) := |
| 2335 |
let p_a := Positional.to_associational weight 4 p in |
| 2336 |
let carry2 := square1 base p_a in |
| 2337 |
let double := Saturated.Rows.flatten weight 8 [carry2; carry2] in |
| 2338 |
let square_a := sqr_indiv base p_a in |
| 2339 |
let square_rows := Saturated.Rows.from_associational weight 8 square_a in |
| 2340 |
let square := Saturated.Rows.flatten' weight double square_rows in |
| 2341 |
let bound := (0, 2^machine_wordsize-1) in |
| 2342 |
if ((n =? 4)%nat) then |
| 2343 |
if ((length p =? 4)%nat) then |
| 2344 |
if (is_bounded_by (repeat bound 4) p) then |
| 2345 |
fst square |
| 2346 |
else |
| 2347 |
mul_no_reduce base n p p |
| 2348 |
else |
| 2349 |
mul_no_reduce base n p p |
| 2350 |
else |
| 2351 |
mul_no_reduce base n p p. |
| 2352 |
|
| 2353 |
Definition square_no_reduce_cps {T} base n (p : list Z) (f : list Z -> T) := |
| 2354 |
let p_a := Positional.to_associational weight 4 p in |
| 2355 |
let carry2 := square1 base p_a in |
| 2356 |
let double := Saturated.Rows.flatten weight 8 [carry2; carry2] in |
| 2357 |
let square_a := sqr_indiv base p_a in |
| 2358 |
let square_rows := Saturated.Rows.from_associational weight 8 square_a in |
| 2359 |
let square := Saturated.Rows.flatten' weight double square_rows in |
| 2360 |
let bound := (0, 2^machine_wordsize-1) in |
| 2361 |
if ((n =? 4)%nat) then |
| 2362 |
if ((length p =? 4)%nat) then |
| 2363 |
if (is_bounded_by (repeat bound 4) p) then |
| 2364 |
f (fst square) |
| 2365 |
else |
| 2366 |
mul_no_reduce_cps base n p p f |
| 2367 |
else |
| 2368 |
mul_no_reduce_cps base n p p f |
| 2369 |
else |
| 2370 |
mul_no_reduce_cps base n p p f. |
| 2371 |
|
| 2372 |
Definition squaremod' base s c n (p : list Z) := |
| 2373 |
let sqr := square_no_reduce base n p in |
| 2374 |
let r := reduce_full base s c n sqr in |
| 2375 |
r. |
| 2376 |
|
| 2377 |
Definition squaremod_cps {T} base s c n (p : list Z) (f : list Z -> T) := |
| 2378 |
(sqr <- square_no_reduce_cps base n p; |
| 2379 |
reduce_full_cps base s c n sqr f). |
| 2380 |
|
| 2381 |
Definition squaremod base s c n (p : list Z) := |
| 2382 |
ltac:(let x := (eval cbv beta delta [squaremod_cps square_no_reduce_cps mul_no_reduce_cps reduce_full_cps reduce1_cps reduce3_cps id] in (@squaremod_cps (list Z) base s c n p id)) in |
| 2383 |
exact x). |
| 2384 |
|
| 2385 |
Context (base : Z) |
| 2386 |
(s : Z) |
| 2387 |
(c : list (Z * Z)) |
| 2388 |
(n : nat). |
| 2389 |
|
| 2390 |
Context (n_gt_1 : (n > 1)%nat) |
| 2391 |
(s_pos : s > 0) |
| 2392 |
(c_pos : Associational.eval c > 0) |
| 2393 |
(mod_nz : s - Associational.eval c <> 0) |
| 2394 |
(base_nz : base <> 0) |
| 2395 |
(solinas_property : Rows.adjust_s weight (S (S n)) s = (weight n, true)) |
| 2396 |
(coef_small : weight n / s * Associational.eval c < up_bound). |
| 2397 |
|
| 2398 |
Lemma square_no_reduce_cps_ok {T} (f : list Z -> T) : forall p, |
| 2399 |
square_no_reduce_cps base n p f = f (square_no_reduce base n p). |
| 2400 |
Proof. |
| 2401 |
intros. |
| 2402 |
cbv [square_no_reduce square_no_reduce_cps]. |
| 2403 |
break_match. |
| 2404 |
reflexivity. |
| 2405 |
apply mul_no_reduce_cps_ok. |
| 2406 |
apply mul_no_reduce_cps_ok. |
| 2407 |
apply mul_no_reduce_cps_ok. |
| 2408 |
Qed. |
| 2409 |
|
| 2410 |
Lemma squaremod_cps_ok : forall {T} p (f : list Z -> T), |
| 2411 |
squaremod_cps base s c n p f = f (squaremod' base s c n p). |
| 2412 |
Proof. |
| 2413 |
intros. |
| 2414 |
cbv [squaremod' squaremod_cps]. |
| 2415 |
rewrite square_no_reduce_cps_ok, reduce_full_cps_ok. |
| 2416 |
reflexivity. |
| 2417 |
Qed. |
| 2418 |
|
| 2419 |
Lemma squaremod_unfold : forall p, |
| 2420 |
squaremod' base s c n p = squaremod_cps base s c n p id. |
| 2421 |
Proof. |
| 2422 |
intros. |
| 2423 |
rewrite squaremod_cps_ok. |
| 2424 |
reflexivity. |
| 2425 |
Qed. |
| 2426 |
|
| 2427 |
Lemma squaremod_cps_conv : forall p, |
| 2428 |
squaremod base s c n p = squaremod' base s c n p. |
| 2429 |
Proof. |
| 2430 |
intros. |
| 2431 |
rewrite squaremod_unfold. |
| 2432 |
reflexivity. |
| 2433 |
Qed. |
| 2434 |
|
| 2435 |
Lemma sat_mul_comm (p q : list (Z * Z)) : |
| 2436 |
Associational.eval (Associational.sat_mul base p q) = |
| 2437 |
Associational.eval (Associational.sat_mul base q p). |
| 2438 |
Proof using base_nz n_gt_1. clear dependent s. push; lia. Qed. |
| 2439 |
|
| 2440 |
Lemma sat_mul_distr (p q1 q2 : list (Z * Z)) : |
| 2441 |
Associational.eval (Associational.sat_mul base p (q1 ++ q2)) = |
| 2442 |
Associational.eval (Associational.sat_mul base p q1) + |
| 2443 |
Associational.eval (Associational.sat_mul base p q2). |
| 2444 |
Proof using base_nz n_gt_1. clear dependent s. push; lia. Qed. |
| 2445 |
|
| 2446 |
Lemma cons_to_app {A} a (p : list A) : |
| 2447 |
a :: p = [a] ++ p. |
| 2448 |
Proof. reflexivity. Qed. |
| 2449 |
|
| 2450 |
Lemma flatten'_mod state (inp : list (list Z)) (m : nat) : |
| 2451 |
inp <> [] -> |
| 2452 |
Datatypes.length (fst state) = m -> |
| 2453 |
(forall row : list Z, In row inp -> Datatypes.length row = m) -> |
| 2454 |
eval weight m (fst (Rows.flatten' weight state inp)) = |
| 2455 |
(Rows.eval weight m inp + eval weight m (fst state) + weight m * snd state) mod weight m. |
| 2456 |
Proof using n_gt_1 wprops. |
| 2457 |
clear dependent s. |
| 2458 |
intros. |
| 2459 |
rewrite Rows.flatten'_correct with (n:=m) by auto. |
| 2460 |
push. |
| 2461 |
f_equal. |
| 2462 |
lia. |
| 2463 |
Qed. |
| 2464 |
|
| 2465 |
Hint Rewrite Nat.sub_diag : const_simpl. |
| 2466 |
Hint Rewrite Z.sub_diag : const_simpl. |
| 2467 |
|
| 2468 |
Lemma sum_one x : |
| 2469 |
sum [x] = x. |
| 2470 |
Proof. clear dependent s; cbn; lia. Qed. |
| 2471 |
|
| 2472 |
Lemma square_indiv_cons (p : list (Z * Z)) (a : Z * Z) : |
| 2473 |
Associational.eval (sqr_indiv base (a :: p)) = |
| 2474 |
Associational.eval (sqr_indiv base [a]) + |
| 2475 |
Associational.eval (sqr_indiv base p). |
| 2476 |
Proof using base_nz n_gt_1. |
| 2477 |
clear dependent s. |
| 2478 |
cbv [sqr_indiv sqr_indiv']. |
| 2479 |
cbn [fold_right]. |
| 2480 |
push. |
| 2481 |
lia. |
| 2482 |
Qed. |
| 2483 |
|
| 2484 |
Lemma square_indiv_app (p q : list (Z * Z)) : |
| 2485 |
Associational.eval (sqr_indiv base (p ++ q)) = |
| 2486 |
Associational.eval (sqr_indiv base p) + Associational.eval (sqr_indiv base q). |
| 2487 |
Proof using base_nz n_gt_1. |
| 2488 |
clear dependent s. |
| 2489 |
generalize dependent q. |
| 2490 |
induction p as [| a p IHp] using rev_ind; intros q. |
| 2491 |
push. |
| 2492 |
rewrite <-app_assoc. |
| 2493 |
rewrite !IHp. |
| 2494 |
rewrite <-cons_to_app. |
| 2495 |
rewrite square_indiv_cons. |
| 2496 |
lia. |
| 2497 |
Qed. |
| 2498 |
|
| 2499 |
Lemma eval_square_indiv (p : list Z) : forall x x0 x1 x2 q, |
| 2500 |
p = x :: x0 :: x1 :: x2 :: q -> |
| 2501 |
Associational.eval (sqr_indiv base (to_associational weight 4 p)) = (Associational.eval (sat_mul base [(weight 0, x)] [(weight 0, x)]) + |
| 2502 |
(Associational.eval (sat_mul base [(weight 1, x0)] [(weight 1, x0)]) + |
| 2503 |
(Associational.eval (sat_mul base [(weight 2, x1)] [(weight 2, x1)]) + |
| 2504 |
Associational.eval (sat_mul base [(weight 3, x2)] [(weight 3, x2)])))). |
| 2505 |
Proof using base_nz wprops n_gt_1. |
| 2506 |
clear dependent s. |
| 2507 |
intros x x0 x1 x2 q H. |
| 2508 |
rewrite H. |
| 2509 |
cbv [to_associational]. |
| 2510 |
cbn [seq map weight combine]. |
| 2511 |
repeat multimatch goal with |
| 2512 |
| |- _ => rewrite app_comm_cons |
| 2513 |
| |- context[?x :: ?y :: ?z] => |
| 2514 |
rewrite cons_to_app with (a:=x) (p:=y::z) |
| 2515 |
| |- context[?x ++ ?y :: ?z] => |
| 2516 |
rewrite cons_to_app with (a:=y) (p:=z); |
| 2517 |
rewrite app_nil_r |
| 2518 |
end. |
| 2519 |
rewrite !square_indiv_app. |
| 2520 |
cbv [sqr_indiv sqr_indiv']. |
| 2521 |
cbn [fold_right]. |
| 2522 |
push. |
| 2523 |
Qed. |
| 2524 |
|
| 2525 |
Lemma length_square1 (p : list Z) : forall x x0 x1 x2 q, |
| 2526 |
p = x :: x0 :: x1 :: x2 :: q -> |
| 2527 |
length (square1 base (to_associational weight 4 p)) = 8%nat. |
| 2528 |
Proof using base_nz wprops n_gt_1. |
| 2529 |
clear dependent s. |
| 2530 |
intros x x0 x1 x2 q H. |
| 2531 |
cbv [square1]. |
| 2532 |
push. |
| 2533 |
rewrite Rows.flatten'_correct with (n:=7%nat). |
| 2534 |
push. |
| 2535 |
auto. |
| 2536 |
push. |
| 2537 |
intros. |
| 2538 |
eapply Rows.length_from_associational. |
| 2539 |
eauto. |
| 2540 |
apply Rows.from_associational_nonnil. |
| 2541 |
lia. |
| 2542 |
rewrite H. |
| 2543 |
discriminate. |
| 2544 |
Qed. |
| 2545 |
|
| 2546 |
Lemma eval_square1 (p : list Z) : forall x x0 x1 x2 q, |
| 2547 |
let bound := (0, 2^machine_wordsize-1) in |
| 2548 |
is_bounded_by (repeat bound 4) p = true -> |
| 2549 |
p = x :: x0 :: x1 :: x2 :: q -> |
| 2550 |
eval weight 8 (square1 base (Positional.to_associational weight 4 p)) = |
| 2551 |
Associational.eval (sat_mul base [(weight 1, x0)] [(weight 2, x1)]) + |
| 2552 |
(Associational.eval (sat_mul base [(weight 0, x)] [(weight 1, x0)]) + |
| 2553 |
(Associational.eval (sat_mul base [(weight 0, x)] [(weight 2, x1)]) + |
| 2554 |
Associational.eval (sat_mul base [(weight 0, x)] [(weight 3, x2)])) + |
| 2555 |
(Associational.eval (sat_mul base [(weight 3, x2)] [(weight 1, x0)]) + |
| 2556 |
Associational.eval (sat_mul base [(weight 3, x2)] [(weight 2, x1)]))). |
| 2557 |
Proof using base_nz wprops n_gt_1. |
| 2558 |
clear dependent s. |
| 2559 |
intros x x0 x1 x2 q bound H H1. |
| 2560 |
rewrite H1. |
| 2561 |
cbv [to_associational]. |
| 2562 |
cbn [seq map combine]. |
| 2563 |
cbv [square1]. |
| 2564 |
cbn [firstn skipn]. |
| 2565 |
|
| 2566 |
rewrite H1 in H. |
| 2567 |
cbv [is_bounded_by fold_andb_map' dual_map bound] in H. |
| 2568 |
cbn [repeat combine map fold_right fst snd] in H. |
| 2569 |
repeat match goal with |
| 2570 |
| H : _ && _ = true |- _ => apply andb_prop in H |
| 2571 |
| H : _ /\ _ |- _ => destruct H |
| 2572 |
| H : _ <=? _ = true |- _ => rewrite Z.leb_le in H |
| 2573 |
end. |
| 2574 |
|
| 2575 |
repeat multimatch goal with |
| 2576 |
| |- _ => rewrite app_comm_cons |
| 2577 |
| |- context[?x :: ?y :: ?z] => |
| 2578 |
rewrite cons_to_app with (a:=x) (p:=y::z) by discriminate |
| 2579 |
end. |
| 2580 |
repeat multimatch goal with |
| 2581 |
| _ => rewrite eval_snoc_S |
| 2582 |
| _ => rewrite Rows.flatten_mod |
| 2583 |
| _ => rewrite flatten'_mod |
| 2584 |
| _ => rewrite Rows.flatten'_correct with (n:=7%nat); cbn [snd] |
| 2585 |
| _ => rewrite Rows.flatten_correct; cbn [snd] |
| 2586 |
| _ => rewrite Rows.eval_from_associational |
| 2587 |
| _ => rewrite eval_app |
| 2588 |
| _ => rewrite sat_mul_distr |
| 2589 |
| _ => cbn [fst] |
| 2590 |
end. |
| 2591 |
all: repeat match goal with |
| 2592 |
| _ => assumption |
| 2593 |
| _ => lia |
| 2594 |
| |- Rows.from_associational _ _ _ <> [] => |
| 2595 |
apply Rows.from_associational_nonnil |
| 2596 |
| |- context[length (Partition.partition _ _ _)] => |
| 2597 |
autorewrite with push_length |
| 2598 |
| |- forall _ : _, In _ _ -> _ => |
| 2599 |
intros; eapply Rows.length_from_associational; eassumption |
| 2600 |
| _ => discriminate |
| 2601 |
end. |
| 2602 |
|
| 2603 |
repeat rewrite Z.div_small. |
| 2604 |
all: repeat match goal with |
| 2605 |
| |- context[_ mod _] => rewrite Z.mod_small |
| 2606 |
end. |
| 2607 |
all: const_simpl; try lia. |
| 2608 |
all: push; solve_ineq; le_lt; replace x with (weight 0 * x) by (weight_comp; lia); etransitivity; [ |
| 2609 |
repeat match goal with |
| 2610 |
| |- _ + _ <= _ => apply OrdersEx.Z_as_DT.add_le_mono |
| 2611 |
| |- _ * _ * _ <= _ => apply Z.mul_le_mono_nonneg |
| 2612 |
| |- _ * _ <= _ => apply OrdersEx.Z_as_DT.mul_le_mono_nonneg_l |
| 2613 |
| |- 0 <= _ => solve_ineq |
| 2614 |
| H : ?x <= _ |- ?x <= _ => eassumption |
| 2615 |
end | (weight_comp; lia) ]. |
| 2616 |
Qed. |
| 2617 |
|
| 2618 |
Lemma eval_square1_bounded (p : list Z) : forall x x0 x1 x2 q, |
| 2619 |
let bound := (0, 2^machine_wordsize-1) in |
| 2620 |
is_bounded_by (repeat bound 4) p = true -> |
| 2621 |
p = x :: x0 :: x1 :: x2 :: q -> |
| 2622 |
0 <= eval weight 8 (square1 base (to_associational weight 4 p)) < weight 7. |
| 2623 |
Proof using base_nz wprops n_gt_1. |
| 2624 |
clear dependent s. |
| 2625 |
intros x x0 x1 x2 q bound H H0. |
| 2626 |
erewrite eval_square1; [| eauto | eauto]. |
| 2627 |
rewrite H0 in H. |
| 2628 |
cbv [is_bounded_by fold_andb_map' dual_map bound] in H. |
| 2629 |
cbn [repeat combine map fold_right fst snd] in H. |
| 2630 |
repeat match goal with |
| 2631 |
| H : _ && _ = true |- _ => apply andb_prop in H |
| 2632 |
| H : _ /\ _ |- _ => destruct H |
| 2633 |
| H : _ <=? _ = true |- _ => rewrite Z.leb_le in H |
| 2634 |
end. |
| 2635 |
push; solve_ineq; le_lt; replace x with (weight 0 * x) by (weight_comp; lia); etransitivity; |
| 2636 |
[repeat match goal with |
| 2637 |
| |- _ + _ <= _ => apply OrdersEx.Z_as_DT.add_le_mono |
| 2638 |
| |- _ * _ * _ <= _ => apply Z.mul_le_mono_nonneg |
| 2639 |
| |- _ * _ <= _ => apply OrdersEx.Z_as_DT.mul_le_mono_nonneg_l |
| 2640 |
| |- 0 <= _ => solve_ineq |
| 2641 |
| H : ?x <= _ |- ?x <= _ => eassumption |
| 2642 |
end | (weight_comp; lia) ]. |
| 2643 |
Qed. |
| 2644 |
|
| 2645 |
Theorem eval_square_no_reduce (p : list Z) : |
| 2646 |
eval weight (2 * n) (square_no_reduce base n p) = (eval weight n p) * (eval weight n p). |
| 2647 |
Proof using base_nz wprops n_gt_1. |
| 2648 |
clear dependent s. |
| 2649 |
rewrite <-eval_mul_no_reduce with (base:=base) by lia. |
| 2650 |
cbv [square_no_reduce]. |
| 2651 |
break_match. |
| 2652 |
|
| 2653 |
rewrite Nat.eqb_eq in Heqb. |
| 2654 |
rewrite Heqb. |
| 2655 |
assert (exists p1 p2 p3 p4, p = p1 :: p2 :: p3 :: p4 :: nil). |
| 2656 |
{ repeat (destruct p; [cbn in Heqb0; lia|]). |
| 2657 |
destruct p; [| cbn in Heqb0; lia]. |
| 2658 |
eauto. } |
| 2659 |
destruct H; destruct H; destruct H; destruct H. |
| 2660 |
|
| 2661 |
pose proof (eval_square1_bounded p x x0 x1 x2 nil ltac:(auto) ltac:(auto)). |
| 2662 |
|
| 2663 |
rewrite flatten'_mod. |
| 2664 |
rewrite Rows.flatten_mod. |
| 2665 |
rewrite Rows.eval_from_associational. |
| 2666 |
|
| 2667 |
rewrite Zplus_mod. |
| 2668 |
rewrite PullPush.Z.mul_mod_full. |
| 2669 |
rewrite Z.mod_same. |
| 2670 |
const_simpl. |
| 2671 |
rewrite Zmod_mod. |
| 2672 |
rewrite Zplus_mod. |
| 2673 |
rewrite Zmod_mod. |
| 2674 |
rewrite <-Zplus_mod. |
| 2675 |
|
| 2676 |
rewrite Rows.eval_cons. |
| 2677 |
cbv [Rows.eval map]. |
| 2678 |
rewrite sum_one. |
| 2679 |
erewrite eval_square1; try eapply H. |
| 2680 |
erewrite eval_square_indiv; try eapply H. |
| 2681 |
|
| 2682 |
rewrite H. |
| 2683 |
cbv [mul_no_reduce]. |
| 2684 |
break_match. |
| 2685 |
replace (2*4)%nat with 8%nat by lia. |
| 2686 |
cbv [to_associational]. |
| 2687 |
rewrite combine_firstn_l. |
| 2688 |
cbn [seq map length]. |
| 2689 |
cbn [firstn seq map combine]. |
| 2690 |
repeat multimatch goal with |
| 2691 |
| |- _ => rewrite app_comm_cons |
| 2692 |
| |- context[?x :: ?y :: ?z] => |
| 2693 |
rewrite cons_to_app with (a:=x) (p:=y::z) |
| 2694 |
end. |
| 2695 |
rewrite Rows.flatten_mod. |
| 2696 |
rewrite Rows.eval_from_associational. |
| 2697 |
rewrite !sat_mul_distr. |
| 2698 |
repeat multimatch goal with |
| 2699 |
| |- context[sat_mul _ (?y ++ ?z) ?x] => rewrite sat_mul_comm with (p:=(y ++ z)) (q:=x) |
| 2700 |
end. |
| 2701 |
rewrite !sat_mul_distr. |
| 2702 |
push. |
| 2703 |
f_equal. |
| 2704 |
lia. |
| 2705 |
auto. |
| 2706 |
lia. |
| 2707 |
auto. |
| 2708 |
|
| 2709 |
all: repeat match goal with |
| 2710 |
| |- forall _ : _, In _ _ -> _ => |
| 2711 |
intros; eapply Rows.length_from_associational; eassumption |
| 2712 |
| _ => auto |
| 2713 |
end. |
| 2714 |
rewrite H in Heqb1. |
| 2715 |
rewrite Heqb1 in Heqb2. |
| 2716 |
lia. |
| 2717 |
repeat match goal with |
| 2718 |
| H : In _ (_ :: _) |- _ => |
| 2719 |
apply in_inv in H |
| 2720 |
| H : In _ [] |- _ => apply in_nil in H; lia |
| 2721 |
| H : _ = ?x |- length ?x = _ => rewrite <-H |
| 2722 |
| _ => eapply length_square1; eauto |
| 2723 |
| _ => intuition |
| 2724 |
end. |
| 2725 |
apply Rows.from_associational_nonnil. |
| 2726 |
lia. |
| 2727 |
rewrite H. |
| 2728 |
discriminate. |
| 2729 |
push. |
| 2730 |
repeat match goal with |
| 2731 |
| H : In _ (_ :: _) |- _ => |
| 2732 |
apply in_inv in H |
| 2733 |
| H : In _ [] |- _ => apply in_nil in H; lia |
| 2734 |
| H : _ = ?x |- length ?x = _ => rewrite <-H |
| 2735 |
| _ => eapply length_square1; eauto |
| 2736 |
| _ => intuition |
| 2737 |
end. |
| 2738 |
Qed. |
| 2739 |
|
| 2740 |
Theorem length_square_no_reduce (p : list Z): |
| 2741 |
length (square_no_reduce base n p) = (2 * n)%nat. |
| 2742 |
Proof using base_nz wprops n_gt_1. |
| 2743 |
clear dependent s. |
| 2744 |
cbv [square_no_reduce]. |
| 2745 |
break_match. |
| 2746 |
rewrite Nat.eqb_eq in Heqb. |
| 2747 |
assert (exists p1 p2 p3 p4, p = p1 :: p2 :: p3 :: p4 :: nil). |
| 2748 |
{ repeat (destruct p; [cbn in Heqb0; lia|]). |
| 2749 |
destruct p; [| cbn in Heqb0; lia]. |
| 2750 |
eauto. } |
| 2751 |
destruct H; destruct H; destruct H; destruct H. |
| 2752 |
rewrite Rows.flatten'_correct with (n:=8%nat). |
| 2753 |
push. |
| 2754 |
lia. |
| 2755 |
repeat match goal with |
| 2756 |
| H : In _ (_ :: _) |- _ => |
| 2757 |
apply in_inv in H |
| 2758 |
| H : In _ [] |- _ => apply in_nil in H; lia |
| 2759 |
| H : _ = ?x |- length ?x = _ => rewrite <-H |
| 2760 |
| _ => eapply length_square1; eauto |
| 2761 |
| _ => intuition |
| 2762 |
end. |
| 2763 |
auto. |
| 2764 |
push. |
| 2765 |
repeat match goal with |
| 2766 |
| H : In _ (_ :: _) |- _ => |
| 2767 |
apply in_inv in H |
| 2768 |
| H : In _ [] |- _ => apply in_nil in H; lia |
| 2769 |
| H : _ = ?x |- length ?x = _ => rewrite <-H |
| 2770 |
| _ => eapply length_square1; eauto |
| 2771 |
| _ => intuition |
| 2772 |
end. |
| 2773 |
intros; eapply Rows.length_from_associational; eauto. |
| 2774 |
apply Rows.from_associational_nonnil. |
| 2775 |
lia. |
| 2776 |
rewrite H. |
| 2777 |
discriminate. |
| 2778 |
apply length_mul_no_reduce; auto. |
| 2779 |
apply length_mul_no_reduce; auto. |
| 2780 |
apply length_mul_no_reduce; auto. |
| 2781 |
Qed. |
| 2782 |
|
| 2783 |
Lemma squaremod'_correct : forall p, |
| 2784 |
Positional.eval weight n (squaremod' base s c n p) mod (s - Associational.eval c) = |
| 2785 |
(Positional.eval weight n p * Positional.eval weight n p) mod (s - Associational.eval c). |
| 2786 |
Proof using base_nz c_pos coef_small mod_nz n_gt_1 s_pos solinas_property wprops. |
| 2787 |
intros. |
| 2788 |
cbv [squaremod']. |
| 2789 |
rewrite <-reduce_full_correct. |
| 2790 |
rewrite eval_square_no_reduce. |
| 2791 |
all: try lia. |
| 2792 |
assumption. |
| 2793 |
rewrite length_square_no_reduce. |
| 2794 |
lia. |
| 2795 |
Qed. |
| 2796 |
|
| 2797 |
Theorem squaremod_correct : forall p , |
| 2798 |
Positional.eval weight n (squaremod base s c n p) mod (s - Associational.eval c) = |
| 2799 |
(Positional.eval weight n p * Positional.eval weight n p) mod (s - Associational.eval c). |
| 2800 |
Proof using base_nz c_pos coef_small mod_nz n_gt_1 s_pos solinas_property wprops. |
| 2801 |
intros. |
| 2802 |
rewrite squaremod_cps_conv. |
| 2803 |
apply squaremod'_correct. |
| 2804 |
Qed. |
| 2805 |
|
| 2806 |
End squaremod. |
| 2807 |
|
| 2808 |
End __. |
| 2809 |
|
| 2810 |
End SolinasReduction. |