| 1 |
Require Import bedrock2.NotationsCustomEntry. |
| 2 |
|
| 3 |
Import Syntax BinInt String List.ListNotations. |
| 4 |
Local Open Scope string_scope. Local Open Scope Z_scope. Local Open Scope list_scope. |
| 5 |
From coqutil.Word Require Import Interface Properties. |
| 6 |
|
| 7 |
From bedrock2 Require Import WeakestPrecondition WeakestPreconditionProperties |
| 8 |
Syntax Semantics BasicC64Semantics ProgramLogic Scalars Array Loops ZnWords. |
| 9 |
From bedrock2.Map Require Import Separation SeparationLogic. |
| 10 |
Require Import coqutil.Map.Interface. |
| 11 |
|
| 12 |
|
| 13 |
From coqutil.Tactics Require Import Tactics letexists eabstract. |
| 14 |
From Coq Require Import ZArith. |
| 15 |
From coqutil.Z Require Import div_mod_to_equations Lia. |
| 16 |
From Coq.Program Require Import Tactics. |
| 17 |
|
| 18 |
Require Import Crypto.Arithmetic.WordByWordMontgomery. |
| 19 |
Import Markers. |
| 20 |
|
| 21 |
Require Import Crypto.Util.ZUtil.Modulo.PullPush. |
| 22 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
Section WithParameters. |
| 27 |
|
| 28 |
Import List coqutil.Datatypes.List. |
| 29 |
Import WordByWordMontgomery. |
| 30 |
|
| 31 |
Context {prime: Z} (r := 64) {ri : Z}. |
| 32 |
Context {ri_correct: (ri * 2^r) mod prime = 1} {prime_reasonable: 1 < prime}. |
| 33 |
(* prime is the modulus; r is the word size; ri is the inverse of 2^r mod prime *) |
| 34 |
|
| 35 |
Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing) (* experiment*). |
| 36 |
|
| 37 |
(* redc_alt ought to take in small arrays A and B, and output an array S *) |
| 38 |
(* S should be small, and should evaluate mod the prime to the same thing as |
| 39 |
A * B * ri *) |
| 40 |
|
| 41 |
Instance spec_of_redc_alt : spec_of "redc_alt" := |
| 42 |
fnspec! "redc_alt" Astart Bstart Sstart len / A (aval: Z) B (bval: Z) S R, |
| 43 |
{ requires t m := |
| 44 |
m =* array scalar (word.of_Z 8) Astart A * |
| 45 |
array scalar (word.of_Z 8) Bstart B * |
| 46 |
array scalar (word.of_Z 8) Sstart S * R /\ |
| 47 |
word.unsigned len = Z.of_nat (List.length A) /\ |
| 48 |
word.unsigned len = Z.of_nat (List.length B) /\ |
| 49 |
word.unsigned len = Z.of_nat (List.length S) /\ |
| 50 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned A) = aval /\ |
| 51 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned B) = bval; |
| 52 |
ensures t' m' := t=t' /\ exists S', |
| 53 |
m' =* |
| 54 |
array scalar (word.of_Z 8) Astart A * |
| 55 |
array scalar (word.of_Z 8) Bstart B * |
| 56 |
array scalar (word.of_Z 8) Sstart S' * R /\ |
| 57 |
( aval * bval * ri^(word.unsigned len) ) mod prime = |
| 58 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned S') mod prime |
| 59 |
}. |
| 60 |
|
| 61 |
(* redc_step ought to take in small arrays B and S, and value a, and output an array S' *) |
| 62 |
(* S' should be small, and should eval to the same as (a * B + S) * ri modulo the prime *) |
| 63 |
|
| 64 |
Instance spec_of_redc_step : spec_of "redc_step" := |
| 65 |
fnspec! "redc_step" a Bstart Sstart len / B (bval: Z) S (sval: Z) R, |
| 66 |
{ requires t m := |
| 67 |
m =* array scalar (word.of_Z 8) Bstart B * |
| 68 |
array scalar (word.of_Z 8) Sstart S * R /\ |
| 69 |
word.unsigned len = Z.of_nat (List.length B) /\ |
| 70 |
word.unsigned len = Z.of_nat (List.length S) /\ |
| 71 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned B) = bval /\ |
| 72 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned S) = sval; |
| 73 |
ensures t' m' := t=t' /\ exists S', |
| 74 |
m' =* array scalar (word.of_Z 8) Bstart B * |
| 75 |
array scalar (word.of_Z 8) Sstart S' * R /\ |
| 76 |
word.unsigned len = Z.of_nat (List.length S') /\ |
| 77 |
((word.unsigned a) * bval + sval) * ri mod prime = |
| 78 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned S') mod prime |
| 79 |
}. |
| 80 |
|
| 81 |
Definition redc_alt := |
| 82 |
func! (Astart, Bstart, Sstart, len) { |
| 83 |
i = $0; |
| 84 |
while (i < len) { |
| 85 |
store(Sstart + $8*i, $0); |
| 86 |
i = i + $1 |
| 87 |
}; |
| 88 |
i = $0; |
| 89 |
while (i < len) { |
| 90 |
redc_step ( load(Astart + $8*i), Bstart, Sstart, len ); |
| 91 |
i = i + $1 |
| 92 |
} |
| 93 |
}. |
| 94 |
|
| 95 |
Import Coq.Lists.List. |
| 96 |
|
| 97 |
Let zeros (n: Z) := |
| 98 |
repeat (@word.of_Z _ word 0) (Z.to_nat n). |
| 99 |
|
| 100 |
Theorem eval_firstn: |
| 101 |
forall modulus depth number, |
| 102 |
@Core.Positional.eval (UniformWeight.uweight modulus) depth number = @Core.Positional.eval (UniformWeight.uweight modulus) depth (firstn depth number) . |
| 103 |
Proof. |
| 104 |
intros. cbv [Core.Positional.eval Core.Positional.to_associational Core.Associational.eval]. |
| 105 |
rewrite combine_firstn_l. rewrite map_length. rewrite seq_length. reflexivity. |
| 106 |
Qed. |
| 107 |
|
| 108 |
Theorem eval_single: |
| 109 |
forall modulus x, |
| 110 |
Core.Positional.eval (UniformWeight.uweight modulus) (length [x]) [x] = x. |
| 111 |
Proof. |
| 112 |
intros. cbv [length]. |
| 113 |
cbv [Core.Positional.eval Core.Positional.to_associational Core.Associational.eval]. |
| 114 |
cbv [seq map UniformWeight.uweight ModOps.weight]. simpl. |
| 115 |
rewrite Z.mul_0_r. rewrite Zdiv_0_l. replace (-0) with 0 by Lia.lia. |
| 116 |
rewrite Z.pow_0_r. Lia.lia. |
| 117 |
Qed. |
| 118 |
|
| 119 |
Theorem eval_one_further: |
| 120 |
forall modulus depth number, Z.of_nat (length number) > Z.of_nat depth -> 0 <= modulus -> |
| 121 |
@eval modulus (depth + 1) number = |
| 122 |
@eval modulus depth number + ((2 ^ modulus) ^ Z.of_nat depth) * hd 0 (skipn depth number). |
| 123 |
Proof. |
| 124 |
intros. cbv [eval]. |
| 125 |
assert ((firstn depth number ++ skipn depth number) = number) by (apply firstn_skipn). |
| 126 |
destruct (skipn depth number) as [|new junk]. |
| 127 |
- assert (length (firstn depth number ++ []) = length number) by (rewrite H1; reflexivity). |
| 128 |
rewrite app_length in H2. simpl in H2. assert ((length (firstn depth number)) <= depth)%nat by apply firstn_le_length. Lia.lia. |
| 129 |
- cbv [hd]. replace (Core.Positional.eval (UniformWeight.uweight modulus) (depth + 1) number) with (Core.Positional.eval (UniformWeight.uweight modulus) (depth + 1) (firstn depth number ++ [new])). |
| 130 |
2: { |
| 131 |
symmetry. rewrite eval_firstn. rewrite <- H1. |
| 132 |
replace (firstn (depth + 1) (firstn depth number ++ new :: junk)) with (firstn depth (firstn depth number ++ new :: junk) ++ [new]). |
| 133 |
1: { reflexivity. } |
| 134 |
replace (depth + 1)%nat with (S depth) by Lia.lia. |
| 135 |
rewrite <- firstn_nth with (d := 0). |
| 136 |
2: { rewrite app_length. replace (length (firstn depth number)) with depth. |
| 137 |
- assert (length (new::junk) = S (length junk)) by apply ListUtil.cons_length. Lia.lia. |
| 138 |
- rewrite ListUtil.List.firstn_length_le; Lia.lia. |
| 139 |
} |
| 140 |
f_equal. assert (depth = length (firstn depth number)) by (rewrite ListUtil.List.firstn_length_le; Lia.lia). |
| 141 |
remember (firstn depth number) as plz_dont_rewrite_me. |
| 142 |
rewrite H2. rewrite nth_middle. reflexivity. |
| 143 |
} |
| 144 |
rewrite UniformWeight.uweight_eval_app with (n := depth). |
| 145 |
+ rewrite <- eval_firstn. f_equal. rewrite eval_single. f_equal. |
| 146 |
cbv [UniformWeight.uweight ModOps.weight]. rewrite Z.div_1_r. |
| 147 |
rewrite <- Z.pow_mul_r; try Lia.lia. |
| 148 |
f_equal. Lia.lia. |
| 149 |
+ Lia.lia. |
| 150 |
+ rewrite ListUtil.List.firstn_length_le; Lia.lia. |
| 151 |
+ reflexivity. |
| 152 |
Qed. |
| 153 |
|
| 154 |
Theorem array_small: |
| 155 |
forall start arr R m, |
| 156 |
m =* array scalar (word.of_Z 8) start arr * R -> |
| 157 |
Z.of_nat (@length (@word.rep 64 word) arr) * 8 < 2 ^ 64 |
| 158 |
. |
| 159 |
Proof. |
| 160 |
Admitted. |
| 161 |
|
| 162 |
Local Ltac no_call := |
| 163 |
lazymatch goal with |
| 164 |
| |- Semantics.call _ _ _ _ _ _ => fail |
| 165 |
| |- _ => idtac |
| 166 |
end. |
| 167 |
|
| 168 |
Local Ltac original_eexists := eexists. |
| 169 |
Local Tactic Notation "eexists" := no_call; original_eexists. |
| 170 |
|
| 171 |
Theorem redc_alt_ok : |
| 172 |
program_logic_goal_for_function! redc_alt. |
| 173 |
Proof. |
| 174 |
repeat straightline. |
| 175 |
(*after the first loop, our output array is full of zeros*) |
| 176 |
refine ( tailrec (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ HList.polymorphic_list.nil)))))))) |
| 177 |
("Astart":: "Bstart" :: "Sstart" :: "len" :: "i" :: nil) |
| 178 |
(fun l A aval B bval S Ra Rb R t m Astart Bstart Sstart len i => PrimitivePair.pair.mk |
| 179 |
(m =* array scalar (word.of_Z 8) (word.add Sstart (word.mul (word.of_Z 8) i)) S * R /\ |
| 180 |
word.unsigned len - word.unsigned i = Z.of_nat (List.length S) /\ |
| 181 |
|
| 182 |
|
| 183 |
l = List.length S ) |
| 184 |
(fun t' m' Astart' Bstart' Sstart' len' i' => |
| 185 |
( |
| 186 |
t = t' /\ Astart = Astart' /\ Bstart = Bstart' /\ Sstart = Sstart' /\ len = len' /\ |
| 187 |
m' =* array scalar (word.of_Z 8) (word.add Sstart (word.mul (word.of_Z 8) i)) (zeros (word.unsigned len - word.unsigned i)) * R |
| 188 |
) |
| 189 |
) |
| 190 |
) |
| 191 |
lt _ _ _ _ _ _ _ _ _ _ _ _ _); |
| 192 |
cbn [reconstruct map.putmany_of_list HList.tuple.to_list |
| 193 |
HList.hlist.foralls HList.tuple.foralls |
| 194 |
HList.hlist.existss HList.tuple.existss |
| 195 |
HList.hlist.apply HList.tuple.apply |
| 196 |
HList.hlist |
| 197 |
List.repeat Datatypes.length |
| 198 |
HList.polymorphic_list.repeat HList.polymorphic_list.length |
| 199 |
PrimitivePair.pair._1 PrimitivePair.pair._2] in *. |
| 200 |
|
| 201 |
{ repeat straightline. } |
| 202 |
{ exact Wf_nat.lt_wf. } |
| 203 |
{ repeat straightline. |
| 204 |
subst i. |
| 205 |
replace (word.add Sstart (word.mul (word.of_Z 8) (word.of_Z 0))) with (Sstart) by ring. |
| 206 |
repeat split; try eauto. |
| 207 |
- ecancel_assumption. |
| 208 |
- rewrite word.unsigned_of_Z_0. Lia.lia. } |
| 209 |
|
| 210 |
{ repeat straightline. eexists. split. { repeat straightline. } repeat straightline. split. |
| 211 |
|
| 212 |
(*loop exits properly*) |
| 213 |
2: { |
| 214 |
repeat straightline; repeat split. |
| 215 |
rename x3 into S'; rename x9 into Sstart'; rename x10 into len'; rename x11 into i'; rename x6 into R'. |
| 216 |
|
| 217 |
destruct (word.ltu i' len') eqn: Hbreak. |
| 218 |
- rewrite word.unsigned_of_Z_1 in H8; try inversion H8; clear H8. |
| 219 |
- destruct (length S') eqn: HS. |
| 220 |
(*length cannot be nonzero*) |
| 221 |
2: { rewrite word.unsigned_ltu in Hbreak. Lia.lia. } |
| 222 |
(*if length is zero, all arrays are the same*) |
| 223 |
cbv [Z.of_nat] in H7. rewrite H7; clear H7 Hbreak. |
| 224 |
apply ListUtil.length0_nil in HS. |
| 225 |
subst S'. apply H6. |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
(*loop body is good*) |
| 230 |
repeat straightline. |
| 231 |
rename x3 into S'; rename x9 into Sstart'; rename x10 into len'; rename x11 into i'; rename x6 into R'. |
| 232 |
destruct S'. |
| 233 |
- cbv [length Z.of_nat] in H7. |
| 234 |
destruct (word.ltu i' len') eqn: Hbreak; try (rewrite word.unsigned_of_Z_0 in H8; exfalso; apply H8; trivial). |
| 235 |
rewrite word.unsigned_ltu in Hbreak. Lia.lia. |
| 236 |
- cbn [array] in H6. repeat straightline. |
| 237 |
repeat split; try trivial. exists (S'). repeat split; try trivial. exists ( (scalar (word.add Sstart' (word.mul (word.of_Z 8) i')) (word.of_Z 0)) * R')%sep. exists (length S'). |
| 238 |
repeat split; subst v0 a. |
| 239 |
all: try (repeat (destruct H10 as [solver H10]; try assumption; clear solver)); subst i. |
| 240 |
+ replace (word.mul (word.of_Z 8) (word.add i' (word.of_Z 1))) with (word.add (word.mul (word.of_Z 8) i') (word.of_Z 8)) by ring. rewrite word.add_assoc. apply sep_comm. apply sep_assoc. ecancel_assumption. |
| 241 |
+ destruct (word.unsigned i' + 1 <? 2^64) eqn: Hisize. |
| 242 |
2: { assert (2^64 - 1 <= word.unsigned i') by Lia.lia; clear Hisize. |
| 243 |
assert (word.unsigned i' < 2^64) by apply word.unsigned_range. |
| 244 |
assert (Hi': word.unsigned i' = 2^64 - 1) by Lia.lia; clear H10 H11. |
| 245 |
assert (Hlen': word.unsigned len' < 2^64) by apply word.unsigned_range. |
| 246 |
assert (Hneg: word.unsigned len' - word.unsigned i' < 1) by Lia.lia; clear Hi' Hlen'. |
| 247 |
simpl in H7; simpl in Hneg. rewrite Zpos_P_of_succ_nat in H7. |
| 248 |
assert (0 <= (Z.of_nat(length S'))) by apply Zle_0_nat. |
| 249 |
Lia.lia. |
| 250 |
} |
| 251 |
|
| 252 |
rewrite word.unsigned_add. rewrite word.unsigned_of_Z_1. cbv [word.wrap]. |
| 253 |
assert (Hsmall: (word.unsigned i' + 1) mod 2^64 = word.unsigned i' + 1). |
| 254 |
{ apply Z.mod_small. assert (0 <= word.unsigned i') by apply word.unsigned_range. Lia.lia. } |
| 255 |
rewrite Hsmall; clear Hisize Hsmall. |
| 256 |
|
| 257 |
simpl in H7; simpl. rewrite Zpos_P_of_succ_nat in H7. |
| 258 |
assert (0 <= (Z.of_nat(length S'))) by apply Zle_0_nat. |
| 259 |
Lia.lia. |
| 260 |
+ subst v. auto. |
| 261 |
+ assert (Hhead: zeros (word.unsigned len' - word.unsigned i') = word.of_Z 0 :: zeros (word.unsigned len' - word.unsigned (word.add i' (word.of_Z 1))) ). |
| 262 |
{ |
| 263 |
destruct (word.unsigned i' + 1 <? 2^64) eqn: Hisize. |
| 264 |
2: { assert (2^64 - 1 <= word.unsigned i') by Lia.lia; clear Hisize. |
| 265 |
assert (word.unsigned i' < 2^64) by apply word.unsigned_range. |
| 266 |
assert (Hi': word.unsigned i' = 2^64 - 1) by Lia.lia; clear H10 H11. |
| 267 |
assert (Hlen': word.unsigned len' < 2^64) by apply word.unsigned_range. |
| 268 |
assert (Hneg: word.unsigned len' - word.unsigned i' < 1) by Lia.lia; clear Hi' Hlen'. |
| 269 |
simpl in H7; simpl in Hneg. rewrite Zpos_P_of_succ_nat in H7. |
| 270 |
assert (0 <= (Z.of_nat(length S'))) by apply Zle_0_nat. |
| 271 |
Lia.lia. |
| 272 |
} |
| 273 |
|
| 274 |
rewrite word.unsigned_add. rewrite word.unsigned_of_Z_1. cbv [word.wrap]. |
| 275 |
assert (Hsmall: (word.unsigned i' + 1) mod 2^64 = word.unsigned i' + 1). |
| 276 |
{ apply Z.mod_small. assert (0 <= word.unsigned i') by apply word.unsigned_range. Lia.lia. } |
| 277 |
rewrite Hsmall; clear Hisize Hsmall. |
| 278 |
assert (Hpos: 0 <= word.unsigned len' - (word.unsigned i' + 1)). { |
| 279 |
simpl in H7; simpl. rewrite Zpos_P_of_succ_nat in H7. |
| 280 |
assert (0 <= (Z.of_nat(length S'))) by apply Zle_0_nat. |
| 281 |
Lia.lia. |
| 282 |
} |
| 283 |
replace (word.unsigned len' - word.unsigned i') with (Z.succ (word.unsigned len' - (word.unsigned i' + 1)) ) by Lia.lia. |
| 284 |
cbv [zeros]. rewrite Z2Nat.inj_succ; try assumption. cbn [repeat]. trivial. |
| 285 |
} |
| 286 |
rewrite Hhead; clear Hhead. |
| 287 |
cbn [array]. |
| 288 |
replace (word.add (word.add Sstart' (word.mul (word.of_Z 8) i')) (word.of_Z 8)) with (word.add Sstart' (word.mul (word.of_Z 8) (word.add i' (word.of_Z 1)))) by ring. ecancel_assumption. |
| 289 |
} |
| 290 |
|
| 291 |
{ |
| 292 |
repeat straightline. |
| 293 |
(*then, the second loop does the multiplication properly*) |
| 294 |
refine ( tailrec (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ (HList.polymorphic_list.cons _ HList.polymorphic_list.nil)))))))) |
| 295 |
("Astart":: "Bstart" :: "Sstart" :: "len" :: "i" :: nil) |
| 296 |
(fun l A aval B bval S Ra Rb R t m Astart Bstart Sstart len i => PrimitivePair.pair.mk |
| 297 |
( |
| 298 |
m =* array scalar (word.of_Z 8) Astart A * |
| 299 |
array scalar (word.of_Z 8) Bstart B * |
| 300 |
array scalar (word.of_Z 8) Sstart S * R /\ |
| 301 |
word.unsigned len = Z.of_nat (List.length A) /\ |
| 302 |
word.unsigned len = Z.of_nat (List.length B) /\ |
| 303 |
word.unsigned len = Z.of_nat (List.length S) /\ |
| 304 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned A) = aval /\ |
| 305 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned B) = bval /\ |
| 306 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned S) mod prime = |
| 307 |
@eval r (Z.to_nat (word.unsigned i)) (List.map word.unsigned A) |
| 308 |
* bval * ri^(word.unsigned i) mod prime /\ |
| 309 |
0 <= word.unsigned i <= word.unsigned len /\ |
| 310 |
l = Z.to_nat (word.unsigned len - word.unsigned i) |
| 311 |
) |
| 312 |
(fun t' m' Astart' Bstart' Sstart' len' i' => |
| 313 |
( |
| 314 |
t = t' /\ Astart = Astart' /\ Bstart = Bstart' /\ Sstart = Sstart' /\ len = len' /\ |
| 315 |
exists S', |
| 316 |
m' =* array scalar (word.of_Z 8) Astart A * |
| 317 |
array scalar (word.of_Z 8) Bstart B * |
| 318 |
array scalar (word.of_Z 8) Sstart S' * R /\ |
| 319 |
@eval r (Z.to_nat (word.unsigned len)) (List.map word.unsigned S') mod prime = |
| 320 |
aval * bval * ri^(word.unsigned len) mod prime |
| 321 |
) |
| 322 |
) |
| 323 |
) |
| 324 |
lt _ _ _ _ _ _ _ _ _ _ _ _ _); |
| 325 |
cbn [reconstruct map.putmany_of_list HList.tuple.to_list |
| 326 |
HList.hlist.foralls HList.tuple.foralls |
| 327 |
HList.hlist.existss HList.tuple.existss |
| 328 |
HList.hlist.apply HList.tuple.apply |
| 329 |
HList.hlist |
| 330 |
List.repeat Datatypes.length |
| 331 |
HList.polymorphic_list.repeat HList.polymorphic_list.length |
| 332 |
PrimitivePair.pair._1 PrimitivePair.pair._2] in *. |
| 333 |
|
| 334 |
{ repeat straightline. } |
| 335 |
|
| 336 |
{ exact Wf_nat.lt_wf. } |
| 337 |
|
| 338 |
{ repeat straightline; subst_words. |
| 339 |
replace (word.add x1 (word.mul (word.of_Z 8) (word.of_Z 0))) with (x1) in H11 by ring. |
| 340 |
rewrite word.unsigned_of_Z_0 in H11. replace (word.unsigned x2 - 0) with (word.unsigned x2) in H11 by ring. |
| 341 |
repeat split. |
| 342 |
- ecancel_assumption. |
| 343 |
- assumption. |
| 344 |
- assumption. |
| 345 |
- cbv [zeros]. rewrite repeat_length. rewrite H3. rewrite Nat2Z.id. trivial. |
| 346 |
- rewrite word.unsigned_of_Z_0. |
| 347 |
|
| 348 |
assert (eval_zero: forall beep boop, @eval r beep (map word.unsigned (zeros boop)) = 0 ). |
| 349 |
{ clear; intros. |
| 350 |
cbv [zeros eval Core.Positional.eval Core.Associational.eval Core.Positional.to_associational]. |
| 351 |
generalize dependent (map (UniformWeight.uweight r) (seq 0 beep)). |
| 352 |
induction (Z.to_nat boop). |
| 353 |
- simpl. destruct l; trivial. |
| 354 |
- intros l'. cbn [repeat map]. rewrite word.unsigned_of_Z_0. destruct l'; trivial. |
| 355 |
cbn [combine map fst snd fold_right]. rewrite IHn. ring. |
| 356 |
} |
| 357 |
|
| 358 |
rewrite eval_zero. |
| 359 |
|
| 360 |
assert (eval_in_name_only: forall xs, @eval r 0 xs = 0). |
| 361 |
{ clear; intros. |
| 362 |
cbv [eval Core.Positional.eval Core.Associational.eval Core.Positional.to_associational]. |
| 363 |
reflexivity. |
| 364 |
} |
| 365 |
rewrite eval_in_name_only. eexists. |
| 366 |
|
| 367 |
- rewrite word.unsigned_of_Z_0. reflexivity. |
| 368 |
|
| 369 |
|
| 370 |
- rewrite word.unsigned_of_Z_0. rewrite H3. apply Nat2Z.is_nonneg. |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
{ repeat straightline. eexists. split. { repeat straightline. } repeat straightline. split. |
| 375 |
|
| 376 |
(*loop exits properly*) |
| 377 |
2: { |
| 378 |
repeat straightline; repeat split; eauto. eexists. split. |
| 379 |
1: { ecancel_assumption. } |
| 380 |
destruct (word.ltu x16 x15) eqn: Hbreak. |
| 381 |
- rewrite word.unsigned_of_Z_1 in H15; inversion H15. |
| 382 |
- subst x5. rewrite word.unsigned_ltu in Hbreak. |
| 383 |
assert (word.unsigned x15 = word.unsigned x16) by Lia.lia. |
| 384 |
rewrite H10 in *. assumption. |
| 385 |
|
| 386 |
} |
| 387 |
|
| 388 |
(*loop body good*) |
| 389 |
repeat (repeat eexists; straightline). |
| 390 |
|
| 391 |
- eapply load_word_of_sep. seprewrite_in @array_address_inbounds H6. |
| 392 |
|
| 393 |
4: { ecancel_assumption. } |
| 394 |
|
| 395 |
{ destruct (word.ltu x16 x15) eqn: Hbreak; try (rewrite word.unsigned_of_Z_0 in H15; contradiction); |
| 396 |
rewrite word.unsigned_ltu in Hbreak; assert (Hiupper : word.unsigned x16 < Z.of_nat (length x4)) by Lia.lia; |
| 397 |
clear H14 H15 Hbreak. |
| 398 |
assert (Hilower: 0 <= word.unsigned x16) by apply word.unsigned_range. |
| 399 |
ZnWords. } |
| 400 |
|
| 401 |
{ clear. ZnWords. } |
| 402 |
|
| 403 |
|
| 404 |
{ |
| 405 |
replace (word.sub (word.add x12 (word.mul (word.of_Z 8) x16)) x12) |
| 406 |
with (word.mul (word.of_Z 8) x16) by ring. |
| 407 |
eexists. } |
| 408 |
|
| 409 |
- straightline_call. |
| 410 |
{ repeat split. |
| 411 |
- ecancel_assumption. |
| 412 |
- assumption. |
| 413 |
- assumption. |
| 414 |
} |
| 415 |
repeat straightline. |
| 416 |
|
| 417 |
rename x4 into A'; rename x6 into B'; rename x8 into S'. |
| 418 |
rename x12 into Astart'; rename x13 into Bstart'; rename x14 into Sstart'. |
| 419 |
rename x5 into aval'; rename x7 into bval'; rename x15 into len; rename x16 into i'. |
| 420 |
rename x11 into R'. rename x17 into Snew. |
| 421 |
|
| 422 |
exists A'. exists aval'. exists B'. exists bval'. exists Snew. |
| 423 |
exists "duopus". |
| 424 |
exists "voltaire". |
| 425 |
eexists. |
| 426 |
exists (Z.to_nat (word.unsigned len - word.unsigned i)). |
| 427 |
split. |
| 428 |
{ destruct (word.ltu i' len) eqn: Hbreak; try (rewrite word.unsigned_of_Z_0 in H15; contradiction); |
| 429 |
rewrite word.unsigned_ltu in Hbreak; assert (Hiupper : word.unsigned i' < word.unsigned len) by Lia.lia; |
| 430 |
subst i. |
| 431 |
split. { ecancel_assumption. } repeat split; eauto. |
| 432 |
2: { clear - Hiupper. ZnWords. } |
| 433 |
{ |
| 434 |
rewrite <- H21. |
| 435 |
|
| 436 |
assert ( no_overflow: (Z.of_nat (length Snew))*8 < 2^64). |
| 437 |
2: { |
| 438 |
|
| 439 |
rename H7 into HAlen; rename H8 into HBlen; rename H9 into HSoldlen; |
| 440 |
rename H19 into HSnewlen; rename H13 into Holdeval. |
| 441 |
|
| 442 |
assert (Hivalid: word.unsigned (word.add i' (word.of_Z 1)) = word.unsigned i' + 1) by |
| 443 |
(clear - Hiupper; ZnWords). |
| 444 |
|
| 445 |
rewrite Hivalid. assert (8*(word.unsigned i') < 2^64) by Lia.lia. |
| 446 |
|
| 447 |
replace (@word.unsigned 64 word (@word.mul 64 word (@word.of_Z 64 word 8) i') / |
| 448 |
@word.unsigned 64 word (@word.of_Z 64 word 8)) with (@word.unsigned 64 word i'). |
| 449 |
|
| 450 |
2:{ |
| 451 |
rewrite word.unsigned_mul. |
| 452 |
rewrite word.unsigned_of_Z. cbv [word.wrap]. |
| 453 |
replace (8 mod 2^64) with 8 by reflexivity. |
| 454 |
replace ((8 * word.unsigned i') mod 2 ^ 64) with (8 * word.unsigned i') by |
| 455 |
(symmetry; apply Z.mod_small; Lia.lia). |
| 456 |
rewrite Z.mul_comm. |
| 457 |
symmetry; apply Z_div_mult. reflexivity. |
| 458 |
} |
| 459 |
|
| 460 |
rewrite H12. |
| 461 |
|
| 462 |
instantiate (1 := (word.of_Z 0)). |
| 463 |
|
| 464 |
rewrite Z2Nat.inj_add; try Lia.lia. |
| 465 |
|
| 466 |
rewrite eval_one_further. |
| 467 |
|
| 468 |
2: { rewrite map_length. rewrite Z2Nat.id; Lia.lia. } |
| 469 |
2: { Lia.lia. } |
| 470 |
|
| 471 |
rewrite Z.mul_mod_l. rewrite Z.add_mod_r. rewrite Holdeval. |
| 472 |
rewrite <- Z.add_mod_r. rewrite <- Z.mul_mod_l. |
| 473 |
rewrite Z2Nat.id; try Lia.lia. |
| 474 |
rewrite Z.mul_add_distr_r. |
| 475 |
repeat rewrite <- Z.mul_assoc. |
| 476 |
replace (ri ^ word.unsigned i' * ri) with (ri * ri ^ word.unsigned i') by apply Z.mul_comm. |
| 477 |
rewrite Pow.Z.pow_mul_base; try Lia.lia. |
| 478 |
repeat rewrite Z.mul_assoc. |
| 479 |
rewrite ListUtil.skipn_map. rewrite <- hd_map. rewrite word.unsigned_of_Z_0. |
| 480 |
|
| 481 |
pose (Abig := hd 0 (map word.unsigned (skipn (Z.to_nat (word.unsigned i')) A')) ). |
| 482 |
replace (hd 0 (map word.unsigned (skipn (Z.to_nat (word.unsigned i')) A'))) with Abig; try trivial. |
| 483 |
pose (Arest := @eval r (Z.to_nat (@word.unsigned 64 word i')) (@map (@word.rep 64 word) Z (@word.unsigned 64 word) A')). |
| 484 |
replace (@eval r (Z.to_nat (@word.unsigned 64 word i')) (@map (@word.rep 64 word) Z (@word.unsigned 64 word) A')) with Arest; try trivial. |
| 485 |
|
| 486 |
rewrite Z.mul_add_distr_r. rewrite Z.mul_add_distr_r. rewrite Z.add_comm. |
| 487 |
|
| 488 |
repeat rewrite <- Z.mul_assoc. |
| 489 |
replace ((2^r) ^ word.unsigned i' * (Abig * (bval' * ri ^ (word.unsigned i' + 1)))) with ((Abig * (bval' * ri ^ (word.unsigned i' + 1)))*(2^r)^word.unsigned i') by apply Z.mul_comm. |
| 490 |
repeat rewrite <- Z.mul_assoc. |
| 491 |
|
| 492 |
replace ((ri ^ (word.unsigned i' + 1) * (2^r) ^ word.unsigned i')) with ( ri * (ri*(2^r)) ^ (word.unsigned i')). |
| 493 |
2: { |
| 494 |
rewrite <- Pow.Z.pow_mul_base; try rewrite Z.pow_mul_l; Lia.lia. |
| 495 |
} |
| 496 |
|
| 497 |
rewrite Z.add_mod_r. symmetry. rewrite Z.add_mod_r. |
| 498 |
|
| 499 |
replace ((Abig * (bval' * (ri * (ri * (2^r)) ^ word.unsigned i'))) mod prime) with ((Abig * (bval' * ri)) mod prime). |
| 500 |
1: { trivial. } |
| 501 |
|
| 502 |
rewrite Z.mul_mod_r. symmetry. rewrite Z.mul_mod_r. |
| 503 |
|
| 504 |
replace (((bval' * (ri * (ri * (2^r)) ^ word.unsigned i')) mod prime)) with ((bval' * ri) mod prime). |
| 505 |
1: { trivial. } |
| 506 |
|
| 507 |
rewrite Z.mul_mod_r. symmetry. rewrite Z.mul_mod_r. |
| 508 |
|
| 509 |
replace ((ri * (ri * (2^r)) ^ word.unsigned i') mod prime) with (ri mod prime). |
| 510 |
1: {trivial. } |
| 511 |
|
| 512 |
rewrite Z.mul_mod_r. replace ((ri * (2^r)) ^ word.unsigned i' mod prime) with 1. |
| 513 |
1: { rewrite Z.mul_1_r. reflexivity. } |
| 514 |
|
| 515 |
rewrite Z.mod_pow_full. rewrite ri_correct. rewrite Z.pow_1_l; try Lia.lia. rewrite Zmod_1_l; try Lia.lia. |
| 516 |
|
| 517 |
} |
| 518 |
assert (exists Rsnew, a0 =* array scalar (word.of_Z 8) Sstart' Snew * Rsnew). |
| 519 |
2: { |
| 520 |
destruct H17 as [Rsnew H17]. apply array_small with (start := Sstart') (m := a0) (R := Rsnew). apply H17. |
| 521 |
} |
| 522 |
eexists. ecancel_assumption. |
| 523 |
} |
| 524 |
|
| 525 |
{ |
| 526 |
clear -Hbreak. |
| 527 |
assert (word.unsigned i' < word.unsigned len) by Lia.lia. |
| 528 |
ZnWords. |
| 529 |
} |
| 530 |
|
| 531 |
} |
| 532 |
|
| 533 |
{ |
| 534 |
destruct (word.ltu i' len) eqn: Hbreak; try (rewrite word.unsigned_of_Z_0 in H15; contradiction); |
| 535 |
rewrite word.unsigned_ltu in Hbreak; assert (Hiupper : word.unsigned i' < word.unsigned len) by Lia.lia; |
| 536 |
subst i. |
| 537 |
repeat split. |
| 538 |
- clear -Hiupper H14. ZnWords. |
| 539 |
- repeat (destruct H17 as [maybewin H17]; try trivial; clear maybewin). |
| 540 |
- repeat (destruct H17 as [maybewin H17]; try trivial; clear maybewin). |
| 541 |
- repeat (destruct H17 as [maybewin H17]; try trivial; clear maybewin). |
| 542 |
- repeat (destruct H17 as [maybewin H17]; try trivial; clear maybewin). |
| 543 |
- repeat (destruct H17 as [maybewin H17]; try trivial; clear maybewin). |
| 544 |
- destruct H17 as [_ H17]. destruct H17 as [_ H17]. destruct H17 as [_ H17]. |
| 545 |
destruct H17 as [_ H17]. destruct H17 as [_ H17]. destruct H17 as [Ssss H17]. |
| 546 |
destruct H17 as [Hmem Heval]. exists Ssss. split. |
| 547 |
+ ecancel_assumption. |
| 548 |
+ trivial. |
| 549 |
|
| 550 |
} |
| 551 |
} |
| 552 |
repeat straightline. repeat split; trivial. exists x9. split. |
| 553 |
- ecancel_assumption. |
| 554 |
- symmetry. subst aval. subst bval. trivial. |
| 555 |
} |
| 556 |
Unshelve. |
| 557 |
- eauto. |
| 558 |
- eauto. |
| 559 |
- eauto. |
| 560 |
- eauto. |
| 561 |
- eauto. |
| 562 |
- eauto. |
| 563 |
- exact String.HelloWorld. |
| 564 |
- exact " :) ". |
| 565 |
|
| 566 |
Qed. |
| 567 |
|
| 568 |
End WithParameters. |