| 1 |
From Coq.Program Require Import Tactics. |
| 2 |
Require Import Rupicola.Lib.Api. |
| 3 |
Require Import Rupicola.Lib.Loops. |
| 4 |
Require Import bedrock2.Semantics. |
| 5 |
Require Import coqutil.Word.Interface coqutil.Byte. |
| 6 |
Local Open Scope Z_scope. |
| 7 |
Require Import Rupicola.Lib.Arrays. |
| 8 |
|
| 9 |
|
| 10 |
Require Import Rupicola.Lib.Api. |
| 11 |
Require Import Rupicola.Lib.Alloc. |
| 12 |
Require Import Rupicola.Lib.SepLocals. |
| 13 |
Require Import Rupicola.Lib.ControlFlow.DownTo. |
| 14 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 15 |
Require Import Crypto.Bedrock.Group.ScalarMult.LadderStep. |
| 16 |
Require Import Crypto.Bedrock.Specs.Field. |
| 17 |
Require Import Crypto.Util.NumTheoryUtil. |
| 18 |
Require Import Crypto.Bedrock.Field.Interface.Compilation2. |
| 19 |
Require Import bedrock2.ZnWords. |
| 20 |
|
| 21 |
|
| 22 |
(*TODO: modified from rupicola example; unify?*) |
| 23 |
Section __. |
| 24 |
|
| 25 |
Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}. |
| 26 |
Context {locals: map.map String.string word}. |
| 27 |
Context {ext_spec: bedrock2.Semantics.ExtSpec}. |
| 28 |
Context {word_ok : word.ok word} {mem_ok : map.ok mem}. |
| 29 |
Context {locals_ok : map.ok locals}. |
| 30 |
Context {ext_spec_ok : Semantics.ext_spec.ok ext_spec}. |
| 31 |
Context {field_parameters : FieldParameters}. |
| 32 |
Context {field_representaton : FieldRepresentation}. |
| 33 |
Context {field_representation_ok : FieldRepresentation_ok}. |
| 34 |
(*TODO: move this requirement to the right place, |
| 35 |
or find a way to discharge it |
| 36 |
Should this go in FieldReprsentation_ok? |
| 37 |
*) |
| 38 |
Context (felem_size_in_words_small |
| 39 |
: Z.of_nat felem_size_in_words < 2^width). |
| 40 |
Hint Resolve relax_bounds : compiler. |
| 41 |
|
| 42 |
|
| 43 |
Notation all_1s := (word.of_Z (-1) : word). |
| 44 |
|
| 45 |
Section Gallina. |
| 46 |
|
| 47 |
|
| 48 |
Definition is_mask mask : Prop := |
| 49 |
mask = all_1s \/ mask = word.of_Z 0. |
| 50 |
|
| 51 |
Definition mask_of_bool (b : bool) := |
| 52 |
if b then all_1s else word.of_Z 0. |
| 53 |
|
| 54 |
|
| 55 |
Instance HasDefault_word : HasDefault word := |
| 56 |
word.of_Z 0. |
| 57 |
|
| 58 |
Instance: HasDefault (word * word) := (default, default). |
| 59 |
Existing Instance Convertible_Z_nat. |
| 60 |
|
| 61 |
Definition cswap_low mask |
| 62 |
(a1: ListArray.t word.rep) |
| 63 |
(a2: ListArray.t word.rep) := |
| 64 |
let from := 0%Z in |
| 65 |
let to := (Z.of_nat felem_size_in_words) in |
| 66 |
let/n nmask := word.sub (word.of_Z 0) mask in |
| 67 |
let/n mask := word.not nmask in |
| 68 |
let/n (a1, a2) := |
| 69 |
nd_ranged_for_all |
| 70 |
from to |
| 71 |
(fun '\<a1,a2\> idx => |
| 72 |
let/n v1 := ListArray.get a1 idx in |
| 73 |
let/n v2 := ListArray.get a2 idx in |
| 74 |
let/n r1 := word.or (word.and mask v1) |
| 75 |
(word.and nmask v2) in |
| 76 |
let/n r2 := word.or (word.and mask v2) |
| 77 |
(word.and nmask v1) in |
| 78 |
let/n a1 := ListArray.put a1 idx r1 in |
| 79 |
let/n a2 := ListArray.put a2 idx r2 in |
| 80 |
\<a1, a2\>) \<a1, a2\> in |
| 81 |
\<a1, a2\>. |
| 82 |
|
| 83 |
|
| 84 |
Definition cswap_word1 (mask nmask a b : word) := |
| 85 |
word.or (word.and mask a) |
| 86 |
(word.and nmask b). |
| 87 |
Definition cswap_word2 (mask nmask a b : word) := |
| 88 |
word.or (word.and mask b) |
| 89 |
(word.and nmask a). |
| 90 |
|
| 91 |
Definition cswap_combine (mask : word) |
| 92 |
(a1: ListArray.t word.rep) |
| 93 |
(a2: ListArray.t word.rep) := |
| 94 |
let/n from := 0%Z in |
| 95 |
let/n to := (Z.of_nat felem_size_in_words) in |
| 96 |
let/n nmask := word.sub (word.of_Z 0) mask in |
| 97 |
let/n mask := word.not nmask in |
| 98 |
let/n (a1, a2) := |
| 99 |
List.split |
| 100 |
(nd_ranged_for_all from to |
| 101 |
(fun ab (idx : Z) => |
| 102 |
let/d xab := ListArray.get ab idx in |
| 103 |
let/d yab := |
| 104 |
(cswap_word1 mask nmask (fst xab) (snd xab), |
| 105 |
cswap_word2 mask nmask (fst xab) (snd xab)) in |
| 106 |
let/d ab1 := ListArray.put ab idx yab in |
| 107 |
ab1) (combine a1 a2)) in |
| 108 |
\<a1, a2\>. |
| 109 |
|
| 110 |
Definition cswap {T} (swap: bool) (a b: T) := |
| 111 |
if swap then \<b, a\> else \<a, b\>. |
| 112 |
|
| 113 |
End Gallina. |
| 114 |
|
| 115 |
|
| 116 |
Lemma foldl_dep'_funext {A B} l (a : A) (f1 f2 : A -> forall (x : B), In x l -> A) l' subl (exit : A-> bool) |
| 117 |
: (forall a (x : B) (pf : In x l), f1 a x pf = f2 a x pf) -> |
| 118 |
foldl_dep' l f1 exit l' subl a |
| 119 |
= foldl_dep' l f2 exit l' subl a. |
| 120 |
Proof using Type. |
| 121 |
revert subl a. |
| 122 |
induction l'; |
| 123 |
simpl; intros; auto. |
| 124 |
destruct (exit a0); auto. |
| 125 |
rewrite IHl'; auto. |
| 126 |
rewrite H. |
| 127 |
reflexivity. |
| 128 |
Qed. |
| 129 |
|
| 130 |
Lemma ranged_for_fequal {A} from1 to1 f1 (init1 : A) from2 to2 f2 init2 |
| 131 |
: from1 = from2 -> |
| 132 |
to1 = to2 -> |
| 133 |
(forall a t idx p1 p2, f1 a t idx p1 = f2 a t idx p2) -> |
| 134 |
init1 = init2 -> |
| 135 |
ranged_for from1 to1 f1 init1 |
| 136 |
= ranged_for from2 to2 f2 init2. |
| 137 |
Proof using Type. |
| 138 |
intros; subst. |
| 139 |
unfold ranged_for,ranged_for', ranged_for_break. |
| 140 |
f_equal. |
| 141 |
apply foldl_dep'_funext. |
| 142 |
eauto. |
| 143 |
Qed. |
| 144 |
|
| 145 |
|
| 146 |
Lemma wrap_felem_size_in_words_small |
| 147 |
: word.wrap (Z.of_nat felem_size_in_words) = (Z.of_nat felem_size_in_words). |
| 148 |
Proof using felem_size_in_words_small. |
| 149 |
unfold word.wrap. |
| 150 |
pose proof felem_size_in_words_small. |
| 151 |
rewrite Z.mod_small; lia. |
| 152 |
Qed. |
| 153 |
|
| 154 |
|
| 155 |
Section __. |
| 156 |
Context {A B: Type}. |
| 157 |
|
| 158 |
Open Scope Z_scope. |
| 159 |
|
| 160 |
Context from to |
| 161 |
(body: forall (acc: A) (tok: ExitToken.t) (idx: Z), |
| 162 |
from - 1 < idx < to -> |
| 163 |
(ExitToken.t * (ListArray.t A * ListArray.t B))) |
| 164 |
(a0: (ListArray.t A * ListArray.t B)). |
| 165 |
|
| 166 |
Context `{HasDefault A} `{HasDefault B}. |
| 167 |
Instance: HasDefault (A * B) := (default, default). |
| 168 |
|
| 169 |
Instance Convertible_Z_nat : Convertible Z nat := Z.to_nat. |
| 170 |
|
| 171 |
|
| 172 |
Lemma replace_nth_combine n (la: list A) (lb: list B) a b |
| 173 |
: (replace_nth n (combine la lb) (a,b)) |
| 174 |
= combine (replace_nth n la a) (replace_nth n lb b). |
| 175 |
Proof using Type. |
| 176 |
revert lb n; induction la; destruct lb; simpl; auto; |
| 177 |
destruct n; simpl; auto. |
| 178 |
f_equal. |
| 179 |
eauto. |
| 180 |
Qed. |
| 181 |
|
| 182 |
Lemma fst_nth_combine n (la: list A) (lb: list B) |
| 183 |
: length la = length lb -> |
| 184 |
(fst (nth n (combine la lb) default)) |
| 185 |
= (nth n la default). |
| 186 |
Proof using a0 body field_representaton. |
| 187 |
revert lb n; induction la; destruct lb; simpl; auto; |
| 188 |
destruct n; simpl; try tauto; try lia. |
| 189 |
intro H'; inversion H'; clear H'; subst. |
| 190 |
eauto. |
| 191 |
Qed. |
| 192 |
|
| 193 |
Lemma snd_nth_combine n (la: list A) (lb: list B) |
| 194 |
: length la = length lb -> |
| 195 |
(snd (nth n (combine la lb) default)) |
| 196 |
= (nth n lb default). |
| 197 |
Proof using a0 body field_representaton. |
| 198 |
revert lb n; induction la; destruct lb; simpl; auto; |
| 199 |
destruct n; simpl; try tauto; try lia. |
| 200 |
intro H'; inversion H'; clear H'; subst. |
| 201 |
eauto. |
| 202 |
Qed. |
| 203 |
|
| 204 |
Lemma nd_ranged_for_combine: forall n (lA: list A) (lB: list B) fA fB, |
| 205 |
length lA = length lB -> |
| 206 |
let '\<x,y\> := nd_ranged_for_all |
| 207 |
0%Z (Z.of_nat n) |
| 208 |
(fun ab idx => |
| 209 |
let/d xa := ListArray.get (P2.car ab) idx in |
| 210 |
let/d xb := ListArray.get (P2.cdr ab) idx in |
| 211 |
let/d ya := fA xa xb in |
| 212 |
let/d yb := fB xa xb in |
| 213 |
let/d a := ListArray.put (P2.car ab) idx ya in |
| 214 |
let/d b := ListArray.put (P2.cdr ab) idx yb in |
| 215 |
\<a, b\>) |
| 216 |
\<lA, lB\> |
| 217 |
in (x,y) = |
| 218 |
List.split |
| 219 |
(nd_ranged_for_all |
| 220 |
0 (Z.of_nat n) |
| 221 |
(fun ab idx => |
| 222 |
let/d xab := ListArray.get ab idx in |
| 223 |
let/d yab := (fA (fst xab) (snd xab), fB (fst xab) (snd xab)) in |
| 224 |
let/d ab := ListArray.put ab idx yab in |
| 225 |
ab) |
| 226 |
(combine lA lB)). |
| 227 |
Proof using a0 body field_representaton. |
| 228 |
intros n la lb fa fb leq. |
| 229 |
rewrite <- !fold_left_as_nd_ranged_for_all. |
| 230 |
generalize (z_range 0 (Z.of_nat n)). |
| 231 |
intro l. |
| 232 |
revert la lb leq. |
| 233 |
induction l. |
| 234 |
{ |
| 235 |
intros. |
| 236 |
simpl. |
| 237 |
rewrite combine_split by auto. |
| 238 |
reflexivity. |
| 239 |
} |
| 240 |
{ |
| 241 |
intros. |
| 242 |
unfold ListArray.get, ListArray.put, dlet in *. |
| 243 |
subst x0 y. |
| 244 |
simpl in *. |
| 245 |
rewrite replace_nth_combine. |
| 246 |
rewrite <- IHl. |
| 247 |
rewrite fst_nth_combine, snd_nth_combine by auto. |
| 248 |
reflexivity. |
| 249 |
rewrite !replace_nth_length. |
| 250 |
auto. |
| 251 |
} |
| 252 |
Qed. |
| 253 |
|
| 254 |
End __. |
| 255 |
|
| 256 |
Lemma cswap_low_combine_eq mask a1 a2 |
| 257 |
: length a1 = length a2 -> |
| 258 |
cswap_low mask a1 a2 = cswap_combine mask a1 a2. |
| 259 |
Proof using Type. |
| 260 |
intros. |
| 261 |
unfold cswap_low. |
| 262 |
unfold cswap_combine. |
| 263 |
unfold nlet. |
| 264 |
|
| 265 |
set (List.split _). |
| 266 |
set (nd_ranged_for_all _ _ _ _). |
| 267 |
enough (p = (P2.car y, P2.cdr y)). |
| 268 |
rewrite H0; reflexivity. |
| 269 |
etransitivity. |
| 270 |
symmetry. |
| 271 |
(* use [rapply] instead of [eapply] to work around [eapply] inferring over-tight universe constraints :-( *) |
| 272 |
rapply (@nd_ranged_for_combine); eauto. |
| 273 |
reflexivity. |
| 274 |
Unshelve. |
| 275 |
all: eauto. |
| 276 |
Qed. |
| 277 |
|
| 278 |
Lemma split_map_combine {A B C D} (f : A * B -> C * D) a1 a2 |
| 279 |
: List.split (List.map f (combine a1 a2)) |
| 280 |
= (List.map (fun p => fst (f p)) (combine a1 a2), List.map (fun p => snd (f p)) (combine a1 a2)). |
| 281 |
Proof using Type. |
| 282 |
revert a2; induction a1; destruct a2; intros; simpl in *; auto. |
| 283 |
rewrite (surjective_pairing (f (a,b))). |
| 284 |
rewrite (surjective_pairing (List.split _)). |
| 285 |
simpl. |
| 286 |
specialize (IHa1 a2). |
| 287 |
rewrite (surjective_pairing (List.split _)) in IHa1. |
| 288 |
congruence. |
| 289 |
Qed. |
| 290 |
|
| 291 |
|
| 292 |
Lemma z_lt_width : (0 <= width)%Z. |
| 293 |
Proof using field_representaton. |
| 294 |
destruct width_cases; lia. |
| 295 |
Qed. |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
Lemma all_1s_and : forall x, word.and all_1s x = x. |
| 300 |
Proof using field_representaton word_ok. |
| 301 |
intros. |
| 302 |
rewrite <- (word.of_Z_unsigned (word.of_Z (-1))). |
| 303 |
rewrite -> word.unsigned_of_Z_minus1. |
| 304 |
rewrite <- (word.of_Z_unsigned x) at 1. |
| 305 |
rewrite <- word.morph_and. |
| 306 |
rewrite Z.land_comm. |
| 307 |
rewrite Z.land_ones. |
| 308 |
rewrite word.wrap_unsigned. |
| 309 |
rewrite word.of_Z_unsigned. |
| 310 |
reflexivity. |
| 311 |
apply z_lt_width. |
| 312 |
Qed. |
| 313 |
|
| 314 |
Lemma word_not_all1s : word.not all_1s = word.of_Z 0. |
| 315 |
Proof using field_representaton word_ok. |
| 316 |
rewrite <- (word.of_Z_signed (word.not all_1s)). |
| 317 |
rewrite word.signed_not. |
| 318 |
rewrite word.signed_of_Z. |
| 319 |
rewrite !word.swrap_inrange. |
| 320 |
change (-1)%Z with (-(1))%Z. |
| 321 |
rewrite Z.lnot_m1; reflexivity. |
| 322 |
destruct width_cases as [H|H]; rewrite !H; lia. |
| 323 |
{ |
| 324 |
rewrite word.swrap_inrange. |
| 325 |
let x := eval compute in ( Z.lnot (-1)) in |
| 326 |
change ( Z.lnot (-1)) with x. |
| 327 |
destruct width_cases as [H|H]; rewrite !H; lia. |
| 328 |
destruct width_cases as [H|H]; rewrite !H; lia. |
| 329 |
} |
| 330 |
Qed. |
| 331 |
|
| 332 |
|
| 333 |
Lemma word_not_impl (x : word) |
| 334 |
: word.not x = word.sub (word.of_Z (-1)) x. |
| 335 |
Proof using field_representaton word_ok. |
| 336 |
rewrite <- (word.of_Z_signed (word.not x)). |
| 337 |
rewrite word.signed_not_nowrap. |
| 338 |
rewrite <- (word.of_Z_signed x). |
| 339 |
rewrite <- word.ring_morph_sub. |
| 340 |
f_equal. |
| 341 |
rewrite (word.of_Z_signed x). |
| 342 |
unfold Z.lnot. |
| 343 |
lia. |
| 344 |
Qed. |
| 345 |
|
| 346 |
|
| 347 |
Lemma word_not_zero : word.not (word.of_Z 0) = all_1s. |
| 348 |
Proof using field_representaton word_ok. |
| 349 |
rewrite word_not_impl. |
| 350 |
rewrite <- (word.of_Z_unsigned (word.sub _ _)). |
| 351 |
rewrite <- word.ring_morph_sub. |
| 352 |
rewrite word.of_Z_unsigned. |
| 353 |
reflexivity. |
| 354 |
Qed. |
| 355 |
|
| 356 |
Lemma zero_and (x : word) |
| 357 |
: word.and (word.of_Z 0) x = word.of_Z 0. |
| 358 |
Proof using word_ok. |
| 359 |
rewrite <- (word.of_Z_unsigned x) at 1. |
| 360 |
rewrite <- word.morph_and. |
| 361 |
rewrite Z.land_0_l. |
| 362 |
reflexivity. |
| 363 |
Qed. |
| 364 |
|
| 365 |
|
| 366 |
Lemma zero_or (x : word) |
| 367 |
: word.or (word.of_Z 0) x = x. |
| 368 |
Proof using word_ok. |
| 369 |
rewrite <- (word.of_Z_unsigned x) at 1. |
| 370 |
rewrite <- word.morph_or. |
| 371 |
rewrite Z.lor_0_l. |
| 372 |
rewrite word.of_Z_unsigned. |
| 373 |
reflexivity. |
| 374 |
Qed. |
| 375 |
|
| 376 |
Lemma or_comm (a b : word) : word.or a b = word.or b a. |
| 377 |
Proof using word_ok. |
| 378 |
rewrite <- (word.of_Z_signed a). |
| 379 |
rewrite <- (word.of_Z_signed b). |
| 380 |
rewrite <-word.morph_or. |
| 381 |
rewrite Z.lor_comm. |
| 382 |
rewrite word.morph_or. |
| 383 |
reflexivity. |
| 384 |
Qed. |
| 385 |
|
| 386 |
Lemma sub_zero (a : word) : word.sub a (word.of_Z 0) = a. |
| 387 |
Proof using field_representaton word_ok. |
| 388 |
rewrite <- (word.of_Z_signed a). |
| 389 |
rewrite <-word.ring_morph_sub. |
| 390 |
replace (word.signed a - 0) with (word.signed a) by lia. |
| 391 |
reflexivity. |
| 392 |
Qed. |
| 393 |
|
| 394 |
|
| 395 |
Lemma zero_minus_one |
| 396 |
: (word.sub (word.of_Z 0) (word.of_Z 1)) = all_1s. |
| 397 |
Proof using word_ok. |
| 398 |
rewrite <- word.ring_morph_sub. |
| 399 |
reflexivity. |
| 400 |
Qed. |
| 401 |
|
| 402 |
Lemma cswap_combine_eq mask a1 a2 |
| 403 |
: (mask = word.of_Z 0) \/ (mask = word.of_Z 1) -> |
| 404 |
felem_size_in_words = length a1 -> |
| 405 |
felem_size_in_words = length a2 -> |
| 406 |
cswap_combine mask a1 a2 = cswap (word.eqb mask (word.of_Z 1)) a1 a2. |
| 407 |
Proof using word_ok. |
| 408 |
unfold cswap,cswap_combine, nlet; intros. |
| 409 |
assert (felem_size_in_words = (length (combine a1 a2))). |
| 410 |
{ |
| 411 |
rewrite H0. |
| 412 |
rewrite combine_length. |
| 413 |
lia. |
| 414 |
} |
| 415 |
rewrite H2. |
| 416 |
rewrite <- map_as_nd_ranged_for_all |
| 417 |
with (f := fun p=> if word.eqb mask (word.of_Z 1) then ((snd p), (fst p)) else ((fst p), (snd p))). |
| 418 |
{ |
| 419 |
destruct H; subst; |
| 420 |
[ rewrite word.eqb_ne | rewrite word.eqb_eq by reflexivity]. |
| 421 |
2:{ |
| 422 |
intro. |
| 423 |
pose proof word.unsigned_of_Z_0. |
| 424 |
rewrite H in H3. |
| 425 |
rewrite word.unsigned_of_Z_1 in H3. |
| 426 |
lia. |
| 427 |
} |
| 428 |
all: rewrite split_map_combine; |
| 429 |
simpl; |
| 430 |
repeat change (fun a => ?f a) with f; |
| 431 |
rewrite ListUtil.map_fst_combine, ListUtil.map_snd_combine; |
| 432 |
rewrite <- H0, H1; |
| 433 |
rewrite firstn_all; |
| 434 |
rewrite <- H1, H0; |
| 435 |
rewrite firstn_all; |
| 436 |
reflexivity. |
| 437 |
} |
| 438 |
{ |
| 439 |
unfold acts_as_replace_nth. |
| 440 |
intros. |
| 441 |
unfold dlet. |
| 442 |
unfold ListArray.put. |
| 443 |
unfold cast, Core.Convertible_Z_nat. |
| 444 |
rewrite Nat2Z.id. |
| 445 |
rewrite replace_nth_app2; eauto. |
| 446 |
f_equal. |
| 447 |
rewrite Nat.sub_diag. |
| 448 |
simpl. |
| 449 |
|
| 450 |
destruct a. |
| 451 |
unfold ListArray.get. |
| 452 |
unfold cast, Core.Convertible_Z_nat; |
| 453 |
rewrite !Nat2Z.id. |
| 454 |
rewrite !app_nth2; eauto. |
| 455 |
rewrite !Nat.sub_diag. |
| 456 |
simpl. |
| 457 |
f_equal. |
| 458 |
|
| 459 |
unfold cswap_word1, cswap_word2. |
| 460 |
|
| 461 |
destruct H; subst; |
| 462 |
[rewrite word.eqb_ne | rewrite word.eqb_eq by reflexivity ]; |
| 463 |
f_equal. |
| 464 |
|
| 465 |
all: rewrite ?sub_zero. |
| 466 |
all: rewrite ?zero_minus_one. |
| 467 |
all: rewrite ?word_not_all1s. |
| 468 |
all: rewrite ?word_not_zero. |
| 469 |
all: rewrite ?all_1s_and. |
| 470 |
all: rewrite ?zero_and. |
| 471 |
all: rewrite ?zero_or. |
| 472 |
all: try rewrite or_comm, ?zero_or. |
| 473 |
all: try reflexivity. |
| 474 |
|
| 475 |
{ |
| 476 |
intro. |
| 477 |
pose proof word.unsigned_of_Z_0. |
| 478 |
rewrite H in H3. |
| 479 |
rewrite word.unsigned_of_Z_1 in H3. |
| 480 |
lia. |
| 481 |
} |
| 482 |
} |
| 483 |
Qed. |
| 484 |
|
| 485 |
|
| 486 |
(*TODO: move to NoExprReflection.v*) |
| 487 |
Lemma compile_word_not |
| 488 |
{tr m l functions} x : |
| 489 |
let v := word.not x in |
| 490 |
forall P (pred: P v -> predicate) |
| 491 |
(k: nlet_eq_k P v) k_impl |
| 492 |
x_var var, |
| 493 |
map.get l x_var = Some x -> |
| 494 |
(let v := v in |
| 495 |
<{ Trace := tr; |
| 496 |
Memory := m; |
| 497 |
Locals := map.put l var v; |
| 498 |
Functions := functions }> |
| 499 |
k_impl |
| 500 |
<{ pred (k v eq_refl) }>) -> |
| 501 |
<{ Trace := tr; |
| 502 |
Memory := m; |
| 503 |
Locals := l; |
| 504 |
Functions := functions }> |
| 505 |
cmd.seq (cmd.set var (expr.op1 op1.not (expr.var x_var))) |
| 506 |
k_impl |
| 507 |
<{ pred (nlet_eq [var] v k) }>. |
| 508 |
Proof using field_representaton word_ok. |
| 509 |
repeat (eexists; split; eauto). |
| 510 |
Qed. |
| 511 |
Hint Extern 10 => simple eapply compile_word_not; shelve : compiler. |
| 512 |
|
| 513 |
Import SizedListArrayCompiler. |
| 514 |
Import LoopCompiler. |
| 515 |
Hint Extern 10 (_ < _) => lia: compiler_side_conditions. |
| 516 |
|
| 517 |
Instance spec_of_cswap : spec_of "felem_cswap" := |
| 518 |
fnspec! "felem_cswap" mask ptr1 ptr2 / c1 c2 R, |
| 519 |
(*TODO: if b then bw should be all 1s*) |
| 520 |
{ requires tr mem := |
| 521 |
(mask = word.of_Z 0 \/ mask = word.of_Z 1) /\ |
| 522 |
(sizedlistarray_value AccessWord felem_size_in_words ptr1 c1 |
| 523 |
* sizedlistarray_value AccessWord felem_size_in_words ptr2 c2 * R)%sep mem; |
| 524 |
ensures tr' mem' := |
| 525 |
tr' = tr /\ |
| 526 |
let/d p := cswap_low mask c1 c2 in |
| 527 |
let (c1,c2) := p in |
| 528 |
(sizedlistarray_value AccessWord felem_size_in_words ptr1 c1 |
| 529 |
* sizedlistarray_value AccessWord felem_size_in_words ptr2 c2 * R)%sep mem' }. |
| 530 |
|
| 531 |
Import LoopCompiler. |
| 532 |
Derive felem_cswap SuchThat |
| 533 |
(defn! "felem_cswap" ("mask", "a1", "a2") { felem_cswap }, |
| 534 |
implements cswap_low) |
| 535 |
As cswap_body_correct. |
| 536 |
Proof. |
| 537 |
pose proof felem_size_in_words_small. |
| 538 |
compile. |
| 539 |
lia. |
| 540 |
Qed. |
| 541 |
|
| 542 |
Lemma Bignum_as_array |
| 543 |
: @Bignum.Bignum width word _ = sizedlistarray_value AccessWord. |
| 544 |
Proof using field_representaton. |
| 545 |
unfold Bignum.Bignum, sizedlistarray_value. |
| 546 |
unfold listarray_value. |
| 547 |
simpl. |
| 548 |
rewrite Z2Nat.id. |
| 549 |
reflexivity. |
| 550 |
unfold bytes_per_word. |
| 551 |
pose proof z_lt_width. |
| 552 |
apply Z.div_pos; try lia. |
| 553 |
Qed. |
| 554 |
|
| 555 |
Lemma compile_felem_cswap {tr m l functions} swap (lhs rhs : F M_pos) : |
| 556 |
let v := cswap swap lhs rhs in |
| 557 |
forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl |
| 558 |
R mask_var bounds lhs_ptr lhs_var rhs_ptr rhs_var, |
| 559 |
|
| 560 |
spec_of_cswap functions -> |
| 561 |
|
| 562 |
map.get l mask_var = Some (word.of_Z (Z.b2z swap)) -> |
| 563 |
|
| 564 |
map.get l lhs_var = Some lhs_ptr -> |
| 565 |
map.get l rhs_var = Some rhs_ptr -> |
| 566 |
|
| 567 |
(FElem bounds lhs_ptr lhs * FElem bounds rhs_ptr rhs * R)%sep m -> |
| 568 |
|
| 569 |
(let v := v in |
| 570 |
forall m', |
| 571 |
(FElem bounds lhs_ptr (P2.car v) * FElem bounds rhs_ptr (P2.cdr v) * R)%sep m' -> |
| 572 |
(<{ Trace := tr; |
| 573 |
Memory := m'; |
| 574 |
Locals := l; |
| 575 |
Functions := functions }> |
| 576 |
k_impl |
| 577 |
<{ pred (k v eq_refl) }>)) -> |
| 578 |
<{ Trace := tr; |
| 579 |
Memory := m; |
| 580 |
Locals := l; |
| 581 |
Functions := functions }> |
| 582 |
cmd.seq |
| 583 |
(cmd.call [] "felem_cswap" [expr.var mask_var; expr.var lhs_var; expr.var rhs_var]) |
| 584 |
k_impl |
| 585 |
<{ pred (nlet_eq [lhs_var; rhs_var] v k) }>. |
| 586 |
Proof using ext_spec_ok locals_ok mem_ok word_ok. |
| 587 |
unfold FElem, Field.FElem. |
| 588 |
rewrite !Bignum_as_array. |
| 589 |
repeat straightline' locals. |
| 590 |
sepsimpl. |
| 591 |
straightline_call. |
| 592 |
ssplit. |
| 593 |
{ destruct swap; simpl; intuition fail. } |
| 594 |
ecancel_assumption. |
| 595 |
repeat straightline' l. |
| 596 |
apply H4. |
| 597 |
sepsimpl. |
| 598 |
eexists. |
| 599 |
sepsimpl. |
| 600 |
{ |
| 601 |
instantiate (1 := P2.car (cswap swap x x0)). |
| 602 |
destruct swap; assumption. |
| 603 |
} |
| 604 |
destruct swap; assumption. |
| 605 |
eexists. |
| 606 |
sepsimpl. |
| 607 |
{ |
| 608 |
instantiate (1 := P2.cdr (cswap swap x x0)). |
| 609 |
destruct swap; assumption. |
| 610 |
} |
| 611 |
destruct swap; assumption. |
| 612 |
rewrite cswap_low_combine_eq in H11. |
| 613 |
assert (felem_size_in_words = length x |
| 614 |
/\ felem_size_in_words = length x0). |
| 615 |
{ |
| 616 |
clear H11 H4 a0. |
| 617 |
unfold sizedlistarray_value in H7; sepsimpl; auto. |
| 618 |
seprewrite_in (sep_emp_2 R) H7. |
| 619 |
sepsimpl. |
| 620 |
auto. |
| 621 |
} |
| 622 |
destruct H9. |
| 623 |
rewrite cswap_combine_eq in H11; |
| 624 |
try intuition congruence; |
| 625 |
try solve [destruct swap; intuition congruence];[]. |
| 626 |
replace ((word.eqb (word.of_Z (Z.b2z swap)) (word.of_Z 1))) with swap in H11. |
| 627 |
unfold dlet in H11. |
| 628 |
use_sep_assumption. |
| 629 |
cancel. |
| 630 |
apply sep_comm. |
| 631 |
{ |
| 632 |
destruct swap; simpl; |
| 633 |
[ rewrite word.eqb_eq | rewrite word.eqb_ne ]; |
| 634 |
try reflexivity. |
| 635 |
intro. |
| 636 |
pose proof word.unsigned_of_Z_1. |
| 637 |
rewrite <- H12 in H13. |
| 638 |
rewrite word.unsigned_of_Z_0 in H13. |
| 639 |
lia. |
| 640 |
} |
| 641 |
{ |
| 642 |
assert (Datatypes.length x0 = felem_size_in_words) as Heqsz;[|rewrite Heqsz; clear Heqsz]. |
| 643 |
{ |
| 644 |
revert H7. |
| 645 |
unfold sizedlistarray_value. |
| 646 |
intros; sepsimpl. |
| 647 |
intuition. |
| 648 |
} |
| 649 |
assert (Datatypes.length x = felem_size_in_words) as Heqsz;[|apply Heqsz]. |
| 650 |
{ |
| 651 |
revert H7. |
| 652 |
unfold sizedlistarray_value. |
| 653 |
intros. |
| 654 |
sepsimpl. |
| 655 |
intuition. |
| 656 |
repeat destruct H9 as [? H9]. |
| 657 |
sepsimpl. |
| 658 |
assumption. |
| 659 |
} |
| 660 |
} |
| 661 |
Qed. |
| 662 |
Hint Resolve compile_felem_cswap : compiler. |
| 663 |
|
| 664 |
End __. |
| 665 |
|
| 666 |
#[global] |
| 667 |
Hint Resolve compile_felem_cswap : compiler. |
| 668 |
|
| 669 |
(* TODO: why doesn't `Existing Instance` work? *) |
| 670 |
#[global] |
| 671 |
Hint Extern 1 (spec_of felem_cswap) => |
| 672 |
(simple refine (spec_of_cswap)) : typeclass_instances. |