| 1 |
(* Rewritten versions of poly1305 and chacha20 that you can compile with Rupicola *) |
| 2 |
From Coq Require Import Utf8. |
| 3 |
Require Import Rupicola.Lib.Api. |
| 4 |
Require Import Rupicola.Lib.Loops. |
| 5 |
(*TODO: move this file to Rupicola.Lib*) |
| 6 |
Require Import Crypto.Bedrock.End2End.RupicolaCrypto.Broadcast. |
| 7 |
Require Crypto.Bedrock.End2End.RupicolaCrypto.Spec. |
| 8 |
Import coqutil.Word.LittleEndianList (le_combine, le_split). |
| 9 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 10 |
Require Import Crypto.Bedrock.Specs.Field. |
| 11 |
Require Import Crypto.Bedrock.Field.Interface.Compilation2. |
| 12 |
Require coqutil.Word.Naive. |
| 13 |
Require Import bedrock2.FE310CSemantics. |
| 14 |
Require Import coqutil.Map.SortedListWord. |
| 15 |
Import Syntax.Coercions ProgramLogic.Coercions. |
| 16 |
Import Datatypes. |
| 17 |
Import Lists.List. |
| 18 |
|
| 19 |
Import Loops. |
| 20 |
Import LoopCompiler. |
| 21 |
|
| 22 |
Notation word := (Naive.word 32). |
| 23 |
Notation locals := (FE310CSemantics.locals (word:=word)). |
| 24 |
Notation mem :=(@SortedListWord.map 32 (Naive.word 32) Naive.word32_ok Init.Byte.byte). |
| 25 |
Notation predicate := (predicate (word:=word) (locals:=locals) (mem:=mem)). |
| 26 |
|
| 27 |
Local Instance locals_ok : map.ok locals := (FE310CSemantics.locals_ok (word:=word)). |
| 28 |
|
| 29 |
(*TODO: connect to Broadcast?*) |
| 30 |
(* Tooling for representing a fixed-length array in local variables *) |
| 31 |
|
| 32 |
(*TODO: generalize beyond word?*) |
| 33 |
(*Note: stricter than dexpr because it should be writeable as well*) |
| 34 |
Definition array_in_locals (l : locals) : list string -> list word -> Prop := |
| 35 |
Forall2 (fun x w => map.get l x = Some w). |
| 36 |
|
| 37 |
Lemma compile_set_locals_array_app {t} {m : mem} {l : locals} {e} (lst1 lst2 : list word) : |
| 38 |
let v := lst1 ++ lst2 in |
| 39 |
forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl vars, |
| 40 |
(*length vars = length lst1 + length lst2 -> *) |
| 41 |
(let v := v in |
| 42 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 43 |
k_impl |
| 44 |
<{ pred (nlet_eq (firstn (length lst1) vars) lst1 |
| 45 |
(fun x1 Heqx1 => |
| 46 |
(nlet_eq (skipn (length lst1) vars) lst2 |
| 47 |
(fun x2 Heqx2 => k (x1 ++ x2) ltac:(subst;reflexivity))))) }>) -> |
| 48 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 49 |
k_impl |
| 50 |
<{ pred (nlet_eq vars v k) }>. |
| 51 |
Proof. |
| 52 |
eauto. |
| 53 |
Qed. |
| 54 |
|
| 55 |
|
| 56 |
Lemma compile_set_locals_array_nil {t m l e}: |
| 57 |
let v := @nil word in |
| 58 |
forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl, |
| 59 |
(*length vars = length lst1 + length lst2 -> *) |
| 60 |
(let v := v in |
| 61 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 62 |
k_impl |
| 63 |
<{ pred (k v eq_refl) }>) -> |
| 64 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 65 |
k_impl |
| 66 |
<{ pred (nlet_eq [] v k) }>. |
| 67 |
Proof. |
| 68 |
eauto. |
| 69 |
Qed. |
| 70 |
|
| 71 |
Lemma compile_set_locals_array_cons_const {t m l e} w (lst : list word): |
| 72 |
let v := w::lst in |
| 73 |
forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl var1 vars, |
| 74 |
(*length vars = length lst1 + length lst2 -> *) |
| 75 |
(let v := v in |
| 76 |
<{ Trace := t; Memory := m; Locals := map.put l var1 w; Functions := e }> |
| 77 |
k_impl |
| 78 |
<{ pred (k v eq_refl) }>) -> |
| 79 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 80 |
cmd.seq |
| 81 |
(cmd.set var1 w) |
| 82 |
k_impl |
| 83 |
<{ pred (nlet_eq (var1::vars) v k) }>. |
| 84 |
Proof. |
| 85 |
intros. |
| 86 |
repeat straightline. |
| 87 |
subst v0. |
| 88 |
subst l0. |
| 89 |
rewrite word.of_Z_unsigned. |
| 90 |
eapply H. |
| 91 |
Qed. |
| 92 |
|
| 93 |
(* Used so that the locals evaluate without having to evaluate lst *) |
| 94 |
Fixpoint array_locs (vars : list string) offset (lst : list word) := |
| 95 |
match vars with |
| 96 |
| [] => [] |
| 97 |
| v::vars => |
| 98 |
(v, (nth offset lst (word.of_Z 0)))::(array_locs vars (S offset) lst) |
| 99 |
end. |
| 100 |
|
| 101 |
|
| 102 |
Lemma sub_nz_implies_lt (m n o : nat) |
| 103 |
: m > 0 -> |
| 104 |
(m = n - o)%nat -> |
| 105 |
n > o. |
| 106 |
Proof. |
| 107 |
intros; subst. |
| 108 |
revert n H. |
| 109 |
induction o; |
| 110 |
simpl; intros; lia. |
| 111 |
Qed. |
| 112 |
|
| 113 |
Lemma array_locs_is_combine' vars offset lst |
| 114 |
: (length vars = length lst - offset)%nat -> |
| 115 |
array_locs vars offset lst = combine vars (skipn offset lst). |
| 116 |
Proof. |
| 117 |
revert offset. |
| 118 |
induction vars; intros; try now (simpl; auto). |
| 119 |
specialize (IHvars (S offset)). |
| 120 |
simpl in H. |
| 121 |
|
| 122 |
assert (length lst > offset). |
| 123 |
{ |
| 124 |
eapply sub_nz_implies_lt; eauto. |
| 125 |
lia. |
| 126 |
} |
| 127 |
assert (1 + length vars = (length lst) - offset). |
| 128 |
{ |
| 129 |
replace (1 + Z.of_nat (length vars)) with (Z.of_nat (S (length vars))) by lia. |
| 130 |
rewrite H. |
| 131 |
rewrite Nat2Z.inj_sub by lia. |
| 132 |
reflexivity. |
| 133 |
} |
| 134 |
erewrite split_hd_tl with (l := skipn _ _). |
| 135 |
2:{ |
| 136 |
rewrite skipn_length. |
| 137 |
lia. |
| 138 |
} |
| 139 |
simpl. |
| 140 |
f_equal. |
| 141 |
{ |
| 142 |
rewrite <- hd_skipn_nth_default. |
| 143 |
rewrite nth_default_eq. |
| 144 |
reflexivity. |
| 145 |
} |
| 146 |
{ |
| 147 |
rewrite tl_skipn. |
| 148 |
apply IHvars. |
| 149 |
lia. |
| 150 |
} |
| 151 |
Qed. |
| 152 |
|
| 153 |
|
| 154 |
Lemma array_locs_is_combine vars lst |
| 155 |
: length vars = length lst -> |
| 156 |
array_locs vars 0 lst = combine vars lst. |
| 157 |
Proof. |
| 158 |
rewrite <- ListUtil.skipn_0 with (xs := lst) at 3. |
| 159 |
intros; apply array_locs_is_combine'; lia. |
| 160 |
Qed. |
| 161 |
|
| 162 |
Definition load_to_vars vars (lst : list expr) k_impl : cmd := |
| 163 |
List.fold_right (fun '(x,e) k_impl => cmd.seq (cmd.set x e) k_impl) k_impl (combine vars lst). |
| 164 |
|
| 165 |
|
| 166 |
Lemma array_dexpr_locals_put |
| 167 |
: ∀ (m : mem) (l : locals) (x : string) (v : word) exp w, |
| 168 |
map.get l x = None → Forall2 (DEXPR m l) exp w → Forall2 (DEXPR m #{ … l; x => v }#) exp w. |
| 169 |
Proof. |
| 170 |
induction 2; constructor; |
| 171 |
solve [eauto|epose dexpr_locals_put; eauto]. |
| 172 |
Qed. |
| 173 |
|
| 174 |
(* we leave prior valiues abstract to support compound operations *) |
| 175 |
Inductive locals_array_expr (P : mem -> Prop) : locals -> list string -> list expr -> list _ -> Prop := |
| 176 |
| locals_array_expr_nil l : locals_array_expr l [] [] [] |
| 177 |
| locals_array_expr_cons l v vars e lst_expr i lst |
| 178 |
: (forall m, P m -> DEXPR m l e i) -> |
| 179 |
(forall i, locals_array_expr (map.put l v i) vars lst_expr lst) -> |
| 180 |
locals_array_expr l (v::vars) (e::lst_expr) (i::lst). |
| 181 |
Local Hint Constructors locals_array_expr : core. |
| 182 |
|
| 183 |
Lemma locals_array_expr_length m l vars lst_expr lst |
| 184 |
: locals_array_expr m l vars lst_expr lst -> |
| 185 |
length vars = length lst_expr |
| 186 |
/\ length lst_expr = length lst. |
| 187 |
Proof. |
| 188 |
induction 1; |
| 189 |
simpl; |
| 190 |
intuition eauto. |
| 191 |
Qed. |
| 192 |
|
| 193 |
Lemma compile_set_locals_array {t m l e} (lst : list word): |
| 194 |
let v := lst in |
| 195 |
forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl vars lst_expr, |
| 196 |
(*TODO: want to put arbitrary value for everything but current var |
| 197 |
(simplification of actual invariant, which is everything before current var) |
| 198 |
TODO: need to know current var for this |
| 199 |
|
| 200 |
Forall2 (fun e i => forall v', DEXPR m l e i) lst_expr lst -> |
| 201 |
*) |
| 202 |
locals_array_expr (eq m) l vars lst_expr lst -> |
| 203 |
(*length vars = length lst -> implied by above *) |
| 204 |
NoDup vars -> |
| 205 |
(* |
| 206 |
(forall v, In v vars -> map.get l v = None) -> |
| 207 |
*) |
| 208 |
(let v := v in |
| 209 |
let l := map.putmany_of_list (array_locs vars 0 v) l in |
| 210 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 211 |
k_impl |
| 212 |
<{ pred (k v eq_refl) }>) -> |
| 213 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 214 |
load_to_vars vars lst_expr k_impl |
| 215 |
<{ pred (nlet_eq vars v k) }>. |
| 216 |
Proof. |
| 217 |
unfold load_to_vars. |
| 218 |
intros until lst_expr. |
| 219 |
intros Hdexprs. |
| 220 |
pose proof (locals_array_expr_length _ _ _ _ _ Hdexprs) as H'. |
| 221 |
destruct H' as [Hlen H']. |
| 222 |
rewrite array_locs_is_combine by congruence. |
| 223 |
|
| 224 |
unfold nlet_eq. |
| 225 |
generalize (pred (k lst eq_refl)). |
| 226 |
clear pred k. |
| 227 |
|
| 228 |
revert k_impl. |
| 229 |
revert dependent lst_expr. |
| 230 |
revert dependent lst. |
| 231 |
revert l. |
| 232 |
induction vars; |
| 233 |
destruct lst; |
| 234 |
destruct lst_expr; |
| 235 |
intros; |
| 236 |
repeat lazymatch goal with |
| 237 |
| H : length _ = length [] |- _ => |
| 238 |
simpl in H; inversion H; subst; clear H |
| 239 |
| H : length [] = length _ |- _ => |
| 240 |
simpl in H; inversion H; subst; clear H |
| 241 |
end; |
| 242 |
cbn [combine fold_left map.putmany_of_list length] in *; |
| 243 |
repeat straightline; |
| 244 |
eauto. |
| 245 |
inversion H'. |
| 246 |
inversion Hdexprs; subst; clear Hdexprs. |
| 247 |
eexists; split; eauto. |
| 248 |
repeat straightline. |
| 249 |
apply IHvars with (l:=l0) (lst:=lst); eauto. |
| 250 |
(*(pose proof (H0 a (ltac:(simpl; intuition)))).*) |
| 251 |
(*eapply array_dexpr_locals_put; eauto.*) |
| 252 |
{ inversion H; subst; auto. } |
| 253 |
Qed. |
| 254 |
|
| 255 |
Section Bedrock2. |
| 256 |
|
| 257 |
Declare Scope word_scope. |
| 258 |
Delimit Scope word_scope with word. |
| 259 |
Local Infix "+" := word.add : word_scope. |
| 260 |
Local Infix "*" := word.mul : word_scope. |
| 261 |
|
| 262 |
Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing) (* experiment*). |
| 263 |
Local Notation "xs $@ a" := (Array.array ptsto (word.of_Z 1) a xs) (at level 10, format "xs $@ a"). |
| 264 |
|
| 265 |
Definition le_combine l : word := word.of_Z (le_combine l). |
| 266 |
Definition le_split n (l : word) := le_split n (word.unsigned l). |
| 267 |
|
| 268 |
(* copied from Low.v, anticipating its removal *) |
| 269 |
Section Low. |
| 270 |
|
| 271 |
Local Notation "a + b" := (word.add (word := word) a b). |
| 272 |
Local Notation "a ^ b" := (word.xor (word := word) a b). |
| 273 |
Local Notation "a <<< b" := (word.slu a b + word.sru a (word.sub (word.of_Z 32) b)) (at level 30). |
| 274 |
|
| 275 |
Definition quarter_gallina a b c d : \<< word, word, word, word \>> := |
| 276 |
let/n a := a + b in let/n d := d ^ a in let/n d := d <<< word.of_Z 16 in |
| 277 |
let/n c := c + d in let/n b := b ^ c in let/n b := b <<< word.of_Z 12 in |
| 278 |
let/n a := a + b in let/n d := d ^ a in let/n d := d <<< word.of_Z 8 in |
| 279 |
let/n c := c + d in let/n b := b ^ c in let/n b := b <<< word.of_Z 7 in |
| 280 |
\< a, b, c, d \>. |
| 281 |
|
| 282 |
Hint Rewrite word.Z_land_ones_rotate using (split; reflexivity) : quarter. |
| 283 |
Hint Rewrite <- word.unsigned_xor_nowrap : quarter. |
| 284 |
Hint Rewrite word.Z_land_ones_word_add : quarter. |
| 285 |
|
| 286 |
Lemma quarter_ok0 a b c d: |
| 287 |
Spec.quarter (word.unsigned a, word.unsigned b, word.unsigned c, word.unsigned d) = |
| 288 |
let '\<a', b', c', d'\> := quarter_gallina a b c d in |
| 289 |
(word.unsigned a', word.unsigned b', word.unsigned c', word.unsigned d'). |
| 290 |
Proof. |
| 291 |
unfold Spec.quarter. |
| 292 |
repeat rewrite ?word.Z_land_ones_word_add, <- ?word.unsigned_xor_nowrap, |
| 293 |
?word.Z_land_ones_rotate by (split; reflexivity). |
| 294 |
reflexivity. |
| 295 |
Qed. |
| 296 |
|
| 297 |
Lemma quarter_ok a b c d: |
| 298 |
in_bounds 32 a -> in_bounds 32 b -> in_bounds 32 c -> in_bounds 32 d -> |
| 299 |
quarter_gallina (word.of_Z (word:=word) a) (word.of_Z b) (word.of_Z c) (word.of_Z d) = |
| 300 |
let '(a', b', c', d') := Spec.quarter (a, b, c, d) in |
| 301 |
\< word.of_Z a', word.of_Z b', word.of_Z c', word.of_Z d' \>. |
| 302 |
Proof. |
| 303 |
unfold in_bounds; intros. |
| 304 |
set (wa := word.of_Z a); set (wb := word.of_Z b); set (wc := word.of_Z c); set (wd := word.of_Z d). |
| 305 |
rewrite <- (word.unsigned_of_Z_nowrap (word:=word) a), |
| 306 |
<- (word.unsigned_of_Z_nowrap (word:=word) b) by assumption. |
| 307 |
rewrite <- (word.unsigned_of_Z_nowrap (word:=word) c), |
| 308 |
<- (word.unsigned_of_Z_nowrap (word:=word) d) by assumption. |
| 309 |
rewrite quarter_ok0; subst wa wb wc wd; destruct (quarter_gallina _ _ _ _) as (?&?&?&?); cbn -[word.of_Z word.unsigned]. |
| 310 |
rewrite !word.of_Z_unsigned; reflexivity. |
| 311 |
Qed. |
| 312 |
|
| 313 |
|
| 314 |
Lemma quarter_in_bounds a b c d: |
| 315 |
in_bounds 32 a -> in_bounds 32 b -> in_bounds 32 c -> in_bounds 32 d -> |
| 316 |
let '(a', b', c', d') := Spec.quarter (a, b, c, d) in |
| 317 |
in_bounds 32 a' /\ in_bounds 32 b' /\ in_bounds 32 c' /\ in_bounds 32 d'. |
| 318 |
Proof. |
| 319 |
unfold in_bounds; intros. |
| 320 |
rewrite <- (word.unsigned_of_Z_nowrap (word:=word) a), |
| 321 |
<- (word.unsigned_of_Z_nowrap (word:=word) b) by assumption. |
| 322 |
rewrite <- (word.unsigned_of_Z_nowrap (word:=word) c), |
| 323 |
<- (word.unsigned_of_Z_nowrap (word:=word) d) by assumption. |
| 324 |
rewrite <- (word.unsigned_of_Z_nowrap (word:=word) a), |
| 325 |
<- (word.unsigned_of_Z_nowrap (word:=word) b) by assumption. |
| 326 |
rewrite <- (word.unsigned_of_Z_nowrap (word:=word) c), |
| 327 |
<- (word.unsigned_of_Z_nowrap (word:=word) d) by assumption. |
| 328 |
rewrite quarter_ok0; destruct (quarter_gallina _ _ _ _) as (?&?&?&?); cbn -[word.of_Z word.unsigned Z.pow]. |
| 329 |
repeat (split; try apply word.unsigned_range). |
| 330 |
Qed. |
| 331 |
|
| 332 |
End Low. |
| 333 |
|
| 334 |
Definition quarterround x y z t (st : list word) := |
| 335 |
let '\<a,b,c,d\> := quarter_gallina (nth x st (word.of_Z 0)) |
| 336 |
(nth y st (word.of_Z 0)) |
| 337 |
(nth z st (word.of_Z 0)) |
| 338 |
(nth t st (word.of_Z 0)) in |
| 339 |
upd (upd (upd (upd st x a) y b) z c) t d. |
| 340 |
|
| 341 |
(*Want: local_alloc; put a fixed-length array in local variables, |
| 342 |
accessible via broadcast |
| 343 |
*) |
| 344 |
|
| 345 |
|
| 346 |
(* TODO: find a better way than hardcoding tuple length *) |
| 347 |
Notation "'let/n' ( x0 , y0 , z0 , t0 , x1 , y1 , z1 , t1 , x2 , y2 , z2 , t2 , x3 , y3 , z3 , t3 ) := val 'in' body" := |
| 348 |
(nlet [IdentParsing.TC.ident_to_string x0; |
| 349 |
IdentParsing.TC.ident_to_string y0; |
| 350 |
IdentParsing.TC.ident_to_string z0; |
| 351 |
IdentParsing.TC.ident_to_string t0; |
| 352 |
IdentParsing.TC.ident_to_string x1; |
| 353 |
IdentParsing.TC.ident_to_string y1; |
| 354 |
IdentParsing.TC.ident_to_string z1; |
| 355 |
IdentParsing.TC.ident_to_string t1; |
| 356 |
IdentParsing.TC.ident_to_string x2; |
| 357 |
IdentParsing.TC.ident_to_string y2; |
| 358 |
IdentParsing.TC.ident_to_string z2; |
| 359 |
IdentParsing.TC.ident_to_string t2; |
| 360 |
IdentParsing.TC.ident_to_string x3; |
| 361 |
IdentParsing.TC.ident_to_string y3; |
| 362 |
IdentParsing.TC.ident_to_string z3; |
| 363 |
IdentParsing.TC.ident_to_string t3] |
| 364 |
val (fun xyz => let '\< x0, y0, z0, t0, |
| 365 |
x1, y1, z1, t1, |
| 366 |
x2, y2, z2, t2, |
| 367 |
x3, y3, z3, t3 \> := xyz in body)) |
| 368 |
(at level 200, |
| 369 |
x0 name, y0 name, z0 name, t0 name, |
| 370 |
x1 name, y1 name, z1 name, t1 name, |
| 371 |
x2 name, y2 name, z2 name, t2 name, |
| 372 |
x3 name, y3 name, z3 name, t3 name, |
| 373 |
body at level 200, |
| 374 |
only parsing). |
| 375 |
|
| 376 |
Definition chacha20_block' (*256bit*)key (*32bit+96bit*)nonce := |
| 377 |
nlet ["qv0"; "qv1"; "qv2"; "qv3"; "qv4"; "qv5"; "qv6"; "qv7"; |
| 378 |
"qv8"; "qv9"; "qv10"; "qv11"; "qv12"; "qv13"; "qv14"; "qv15"] (*512bit*) |
| 379 |
(map le_combine (chunk 4 (list_byte_of_string"expand 32-byte k")) |
| 380 |
++ map le_combine (chunk 4 key) |
| 381 |
++ (word.of_Z 0)::(map le_combine (chunk 4 nonce))) (fun st => |
| 382 |
let '\<qv0, qv1, qv2, qv3, |
| 383 |
qv4, qv5, qv6, qv7, |
| 384 |
qv8, qv9, qv10,qv11, |
| 385 |
qv12,qv13,qv14,qv15\> := |
| 386 |
\<(nth 0 st (word.of_Z 0)), |
| 387 |
(nth 1 st (word.of_Z 0)), |
| 388 |
(nth 2 st (word.of_Z 0)), |
| 389 |
(nth 3 st (word.of_Z 0)), |
| 390 |
(nth 4 st (word.of_Z 0)), |
| 391 |
(nth 5 st (word.of_Z 0)), |
| 392 |
(nth 6 st (word.of_Z 0)), |
| 393 |
(nth 7 st (word.of_Z 0)), |
| 394 |
(nth 8 st (word.of_Z 0)), |
| 395 |
(nth 9 st (word.of_Z 0)), |
| 396 |
(nth 10 st (word.of_Z 0)), |
| 397 |
(nth 11 st (word.of_Z 0)), |
| 398 |
(nth 12 st (word.of_Z 0)), |
| 399 |
(nth 13 st (word.of_Z 0)), |
| 400 |
(nth 14 st (word.of_Z 0)), |
| 401 |
(nth 15 st (word.of_Z 0))\> |
| 402 |
in |
| 403 |
let/n (qv0,qv1,qv2,qv3, |
| 404 |
qv4,qv5,qv6,qv7, |
| 405 |
qv8,qv9,qv10,qv11, |
| 406 |
qv12,qv13,qv14,qv15) := |
| 407 |
Nat.iter 10 (fun '\<qv0, qv1, qv2, qv3, |
| 408 |
qv4, qv5, qv6, qv7, |
| 409 |
qv8, qv9, qv10,qv11, |
| 410 |
qv12,qv13,qv14,qv15\> => |
| 411 |
let/n (qv0, qv4, qv8,qv12) := quarter_gallina qv0 qv4 qv8 qv12 in |
| 412 |
let/n (qv1, qv5, qv9,qv13) := quarter_gallina qv1 qv5 qv9 qv13 in |
| 413 |
let/n (qv2, qv6, qv10,qv14) := quarter_gallina qv2 qv6 qv10 qv14 in |
| 414 |
let/n (qv3, qv7, qv11,qv15) := quarter_gallina qv3 qv7 qv11 qv15 in |
| 415 |
let/n (qv0, qv5, qv10,qv15) := quarter_gallina qv0 qv5 qv10 qv15 in |
| 416 |
let/n (qv1, qv6, qv11,qv12) := quarter_gallina qv1 qv6 qv11 qv12 in |
| 417 |
let/n (qv2, qv7, qv8,qv13) := quarter_gallina qv2 qv7 qv8 qv13 in |
| 418 |
let/n (qv3, qv4, qv9,qv14) := quarter_gallina qv3 qv4 qv9 qv14 in |
| 419 |
\<qv0,qv1,qv2,qv3, |
| 420 |
qv4,qv5,qv6,qv7, |
| 421 |
qv8,qv9,qv10,qv11, |
| 422 |
qv12,qv13,qv14,qv15\>) |
| 423 |
\<qv0,qv1,qv2,qv3, |
| 424 |
qv4,qv5,qv6,qv7, |
| 425 |
qv8,qv9,qv10,qv11, |
| 426 |
qv12,qv13,qv14,qv15\> in |
| 427 |
let ss := [qv0; qv1; qv2; qv3; |
| 428 |
qv4; qv5; qv6; qv7; |
| 429 |
qv8; qv9; qv10; qv11; |
| 430 |
qv12; qv13; qv14; qv15] in |
| 431 |
nlet ["qv0"; "qv1"; "qv2"; "qv3"; "qv4"; "qv5"; "qv6"; "qv7"; |
| 432 |
"qv8"; "qv9"; "qv10"; "qv11"; "qv12"; "qv13"; "qv14"; "qv15"] (*512bit*) |
| 433 |
(map (fun '(s, t) => s + t)%word (combine ss (map le_combine (chunk 4 (list_byte_of_string"expand 32-byte k")) |
| 434 |
++ map le_combine (chunk 4 key) |
| 435 |
++ (word.of_Z 0)::(map le_combine (chunk 4 nonce))))) (fun ss => |
| 436 |
let/n st := flat_map (le_split 4) ss in st)). |
| 437 |
|
| 438 |
(*TODO: don't hardcode 0*) |
| 439 |
#[export] Instance spec_of_chacha20 : spec_of "chacha20_block" := |
| 440 |
fnspec! "chacha20_block" out key nonce / (pt k n : list Byte.byte) (R Rk Rn : mem -> Prop), |
| 441 |
{ requires t m := |
| 442 |
m =* pt$@out * R /\ length pt = 64%nat /\ |
| 443 |
m =* k$@key * Rk /\ length k = 32%nat /\ |
| 444 |
(*TODO: account for difference in nonce length*) |
| 445 |
m =* n$@nonce * Rn /\ length n = 12%nat; |
| 446 |
ensures T m := T = t /\ exists ct, m =* ct$@out * R /\ length ct = 64%nat /\ |
| 447 |
ct = Spec.chacha20_block k (le_split 4 (word.of_Z 0) ++ n) }. |
| 448 |
|
| 449 |
|
| 450 |
Lemma word_add_pair_eqn st: |
| 451 |
(let '(s, t) := st in Z.land (s + t) (Z.ones 32)) = |
| 452 |
word.unsigned (word.of_Z (word:=word) (fst st) + word.of_Z (snd st))%word. |
| 453 |
Proof. |
| 454 |
destruct st. |
| 455 |
rewrite Z.land_ones, <- word.ring_morph_add, word.unsigned_of_Z by lia. |
| 456 |
reflexivity. |
| 457 |
Qed. |
| 458 |
|
| 459 |
(*Copied from Derive anticipating its removal*) |
| 460 |
Section Derive. |
| 461 |
|
| 462 |
Instance spec_of_quarter : spec_of "quarter" := |
| 463 |
fnspec! "quarter" (a b c d : word) ~> (a' b' c' d' : word), |
| 464 |
{ requires tr m := True; |
| 465 |
ensures tr' m' := |
| 466 |
tr = tr' /\ |
| 467 |
(m = m' :> mem) /\ |
| 468 |
let '\<w, x, y, z\> := quarter_gallina a b c d in |
| 469 |
(a' = w /\ b' = x /\ c' = y /\ d' = z)}. |
| 470 |
|
| 471 |
Derive quarter SuchThat |
| 472 |
(defn! "quarter" ("a", "b", "c", "d") ~> "a", "b", "c", "d" { quarter }, |
| 473 |
implements (quarter_gallina) using []) |
| 474 |
As quarter_body_correct. |
| 475 |
Proof. |
| 476 |
compile. |
| 477 |
Qed. |
| 478 |
|
| 479 |
Lemma compile_quarter : forall {tr mem locals functions} a b c d, |
| 480 |
let v := quarter_gallina a b c d in |
| 481 |
|
| 482 |
forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl |
| 483 |
a_var b_var c_var d_var, |
| 484 |
|
| 485 |
spec_of_quarter functions -> |
| 486 |
|
| 487 |
map.get locals a_var = Some a -> |
| 488 |
map.get locals b_var = Some b -> |
| 489 |
map.get locals c_var = Some c -> |
| 490 |
map.get locals d_var = Some d -> |
| 491 |
|
| 492 |
(let v := v in |
| 493 |
let '\<w, x, y, z\> := v in |
| 494 |
(<{ Trace := tr; |
| 495 |
Memory := mem; |
| 496 |
Locals := map.put (map.put (map.put (map.put locals a_var w) b_var x) c_var y) d_var z; |
| 497 |
Functions := functions }> |
| 498 |
k_impl |
| 499 |
<{ pred (k v eq_refl) }>)) -> |
| 500 |
<{ Trace := tr; |
| 501 |
Memory := mem; |
| 502 |
Locals := locals; |
| 503 |
Functions := functions }> |
| 504 |
|
| 505 |
cmd.seq (cmd.call [a_var; b_var; c_var; d_var] "quarter" [expr.var a_var; expr.var b_var; expr.var c_var; expr.var d_var]) |
| 506 |
k_impl |
| 507 |
|
| 508 |
<{ pred (nlet_eq [a_var; b_var; c_var; d_var] v k) }>. |
| 509 |
Proof. |
| 510 |
repeat straightline. |
| 511 |
repeat (eexists; split; eauto). |
| 512 |
handle_call; eauto. |
| 513 |
Qed. |
| 514 |
|
| 515 |
End Derive. |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
Existing Instance spec_of_quarter. |
| 520 |
|
| 521 |
Existing Instance word_ac_ok. |
| 522 |
|
| 523 |
|
| 524 |
Lemma nat_iter_rel {A B} (R : A -> B -> Prop) acca accb fa fb n |
| 525 |
: R acca accb -> |
| 526 |
(forall acca accb, R acca accb -> R (fa acca) (fb accb)) -> |
| 527 |
R (Nat.iter n fa acca) (Nat.iter n fb accb). |
| 528 |
Proof. |
| 529 |
intros. |
| 530 |
induction n; simpl; eauto. |
| 531 |
Qed. |
| 532 |
|
| 533 |
|
| 534 |
Lemma nat_iter_pred {A} (R : A -> Prop) f acc n |
| 535 |
: R acc -> |
| 536 |
(forall acc, R acc -> R (f acc)) -> |
| 537 |
R (Nat.iter n f acc). |
| 538 |
Proof. |
| 539 |
intros. |
| 540 |
induction n; simpl; eauto. |
| 541 |
Qed. |
| 542 |
|
| 543 |
Lemma length_quarterround a b c d l |
| 544 |
: length (Spec.quarterround a b c d l) = length l. |
| 545 |
Proof. |
| 546 |
unfold Spec.quarterround. |
| 547 |
generalize (Spec.quarter (nth a l 0, nth b l 0, nth c l 0, nth d l 0)) as p. |
| 548 |
intro p; destruct p as [[[? ?] ?] ?]. |
| 549 |
rewrite !upd_length. |
| 550 |
reflexivity. |
| 551 |
Qed. |
| 552 |
|
| 553 |
Lemma quarterround_ok x y z t st : |
| 554 |
Forall (in_bounds 32) st -> |
| 555 |
List.map word.of_Z (Spec.quarterround x y z t st) = |
| 556 |
quarterround x y z t (List.map word.of_Z st). |
| 557 |
Proof. |
| 558 |
unfold Spec.quarterround, quarterround, nlet; intros H. |
| 559 |
rewrite forall_in_bounds in H by lia. |
| 560 |
rewrite !map_nth, !quarter_ok by auto. |
| 561 |
destruct (Spec.quarter _) as (((?&?)&?)&?). |
| 562 |
rewrite !map_upd; reflexivity. |
| 563 |
Qed. |
| 564 |
|
| 565 |
|
| 566 |
Lemma nth_upd {A} (l: list A) (a d: A) (i k: nat): |
| 567 |
((i >= length l)%nat /\ nth i (upd l k a) d = d) \/ |
| 568 |
(i = k /\ nth i (upd l k a) d = a) \/ |
| 569 |
(i <> k /\ nth i (upd l k a) d = nth i l d). |
| 570 |
Proof. |
| 571 |
destruct (Nat.lt_ge_cases i (length l)); cycle 1; [ left | right ]. |
| 572 |
- unfold upd; rewrite nth_overflow; rewrite ?upds_length; eauto. |
| 573 |
- destruct (Nat.eq_dec i k) as [-> | Heq]; [ left | right ]. |
| 574 |
+ rewrite nth_upd_same; auto. |
| 575 |
+ rewrite nth_upd_diff; auto. |
| 576 |
Qed. |
| 577 |
|
| 578 |
Lemma Forall_upd {A} (P: A -> Prop) (l: list A) (a: A) k: |
| 579 |
P a -> |
| 580 |
Forall P l -> |
| 581 |
Forall P (upd l k a). |
| 582 |
Proof. |
| 583 |
intros Ha Hl. |
| 584 |
rewrite @Forall_nth_default' with (d := a) in Hl |- *; eauto. |
| 585 |
intros; destruct (nth_upd l a a i k) as [(? & ->) | [ (? & ->) | (? & ->) ] ]; |
| 586 |
subst; eauto; eauto. |
| 587 |
Qed. |
| 588 |
|
| 589 |
Lemma quarterround_in_bounds x y z t a: |
| 590 |
Forall (in_bounds 32) a -> |
| 591 |
Forall (in_bounds 32) (Spec.quarterround x y z t a). |
| 592 |
Proof. |
| 593 |
unfold Spec.quarterround, nlet; intros Ha. |
| 594 |
pose proof Ha as Ha'; rewrite forall_in_bounds in Ha by lia. |
| 595 |
pose proof quarter_in_bounds (nth x a 0) (nth y a 0) (nth z a 0) (nth t a 0) |
| 596 |
ltac:(eauto) ltac:(eauto) ltac:(eauto) ltac:(eauto) as Hb. |
| 597 |
destruct (Spec.quarter _) as (((?&?)&?)&?). |
| 598 |
destruct Hb as (?&?&?&?). |
| 599 |
repeat (apply Forall_upd; auto). |
| 600 |
Qed. |
| 601 |
|
| 602 |
|
| 603 |
Definition list_to_tuple_16 (l : list word) := |
| 604 |
match l with |
| 605 |
| [car; car0; car1; car2; car3; car4; car5; car6; car7; car8; car9; car10; car11; car12; car13; x0] => |
| 606 |
\< car, car0, car1, car2, car3, car4, car5, car6, car7, car8, car9, car10, car11, car12, car13, x0 \> |
| 607 |
| _ => \< word.of_Z 0,word.of_Z 0,word.of_Z 0,word.of_Z 0, |
| 608 |
word.of_Z 0,word.of_Z 0,word.of_Z 0,word.of_Z 0, |
| 609 |
word.of_Z 0,word.of_Z 0,word.of_Z 0,word.of_Z 0, |
| 610 |
word.of_Z 0,word.of_Z 0,word.of_Z 0,word.of_Z 0 \> |
| 611 |
end. |
| 612 |
|
| 613 |
Lemma list_to_tuple_16_injective l1 l2 |
| 614 |
: length l1 = 16%nat -> |
| 615 |
length l2 = 16%nat -> |
| 616 |
list_to_tuple_16 l1 = list_to_tuple_16 l2 -> l1 = l2. |
| 617 |
Proof. |
| 618 |
repeat (destruct l1 as [|? l1]; cbn [length]; try lia). |
| 619 |
|
| 620 |
repeat (destruct l2 as [|? l2]; cbn [length]; try lia). |
| 621 |
cbv. |
| 622 |
intros _ _ H; inversion H; reflexivity. |
| 623 |
Qed. |
| 624 |
|
| 625 |
Strategy 10 [list_to_tuple_16 nlet]. |
| 626 |
Strategy 20 [quarterround Spec.quarterround]. |
| 627 |
Strategy opaque [quarter_gallina Spec.quarter Nat.iter]. |
| 628 |
|
| 629 |
|
| 630 |
Lemma destruct_16 {A} P |
| 631 |
: (forall (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x0 : A), |
| 632 |
P \<x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x0\>) -> |
| 633 |
forall a, P a. |
| 634 |
Proof. |
| 635 |
do 15 destruct a as [? a]. |
| 636 |
eauto. |
| 637 |
Qed. |
| 638 |
|
| 639 |
Lemma chacha20_block_ok key nonce |
| 640 |
: length key = 32%nat -> |
| 641 |
length nonce = 12%nat -> |
| 642 |
Spec.chacha20_block key ((le_split 4 (word.of_Z 0))++nonce) = chacha20_block' key nonce. |
| 643 |
Proof. |
| 644 |
intros Hlenk Hlenn. |
| 645 |
unfold Spec.chacha20_block, chacha20_block'. |
| 646 |
unfold le_split. |
| 647 |
repeat lazymatch goal with |
| 648 |
|- context c [nlet _ ?e ?f] => |
| 649 |
let ex := fresh "x" in |
| 650 |
remember e as ex; |
| 651 |
let x := eval cbn beta in (f ex) in |
| 652 |
unfold nlet at 1 |
| 653 |
end. |
| 654 |
subst x2. |
| 655 |
rewrite <- ListUtil.flat_map_map with (f:=word.unsigned(word:=word)). |
| 656 |
f_equal. |
| 657 |
subst x1. |
| 658 |
|
| 659 |
revert dependent x0; intro x0; simple apply destruct_16 with (a:=x0); intros. |
| 660 |
|
| 661 |
erewrite (map_ext _ _ word_add_pair_eqn). |
| 662 |
rewrite <- map_map with (g:= word.unsigned (word:=word)). |
| 663 |
f_equal. |
| 664 |
change (λ x1 : Z * Z, (word.of_Z (fst x1) + word.of_Z (snd x1))%word) |
| 665 |
with (fun x2 => (fun x => (fst x) + (snd x))%word ((fun x1 => (word.of_Z (word:=word)(fst x1), word.of_Z (snd x1))) x2)). |
| 666 |
|
| 667 |
|
| 668 |
rewrite <- map_map. |
| 669 |
erewrite map_ext with (g:=(λ '(s, t), (s + t)%word)). |
| 670 |
2:{ |
| 671 |
intro a; destruct a; reflexivity. |
| 672 |
} |
| 673 |
f_equal. |
| 674 |
rewrite map_combine_separated. |
| 675 |
subst x. |
| 676 |
f_equal. |
| 677 |
2:{ |
| 678 |
unfold le_combine. |
| 679 |
rewrite !map_app, !map_map. |
| 680 |
repeat (f_equal;[]). |
| 681 |
reflexivity. |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
lazymatch goal with |- map word.of_Z ?lhs = _ => set lhs end. |
| 686 |
assert (length l = 16%nat) as Hlen. |
| 687 |
{ |
| 688 |
subst l. |
| 689 |
apply nat_iter_pred. |
| 690 |
{ |
| 691 |
rewrite !app_length, !map_length, !length_chunk, Hlenk. |
| 692 |
rewrite app_length, LittleEndianList.length_le_split, Hlenn. |
| 693 |
reflexivity. |
| 694 |
all: lia. |
| 695 |
} |
| 696 |
{ |
| 697 |
intros. |
| 698 |
rewrite !length_quarterround. |
| 699 |
auto. |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
apply list_to_tuple_16_injective. |
| 704 |
{ |
| 705 |
rewrite map_length; auto. |
| 706 |
} |
| 707 |
{ |
| 708 |
reflexivity. |
| 709 |
} |
| 710 |
cbn [list_to_tuple_16]. |
| 711 |
|
| 712 |
subst l; rewrite Heqx0; clear Heqx0. |
| 713 |
eapply Nat_iter_rew_inv |
| 714 |
with (g:=fun l => list_to_tuple_16 (map word.of_Z l)) |
| 715 |
(P := fun l => Forall (in_bounds 32) l /\ length l = 16%nat); intros. |
| 716 |
{ |
| 717 |
destruct H. |
| 718 |
split. |
| 719 |
1:repeat apply quarterround_in_bounds; auto. |
| 720 |
repeat rewrite length_quarterround; auto. |
| 721 |
} |
| 722 |
|
| 723 |
{ |
| 724 |
destruct H. |
| 725 |
repeat rewrite quarterround_ok. |
| 726 |
2-9:repeat apply quarterround_in_bounds; auto. |
| 727 |
repeat (destruct a as [|? a]; cbn [length] in H0; try (exfalso;lia)). |
| 728 |
cbn [list_to_tuple_16 map]. |
| 729 |
|
| 730 |
|
| 731 |
do 4 (lazymatch goal with |
| 732 |
|- context ctx [(quarterround ?a ?b ?c ?d (?e1::?lst))] => |
| 733 |
set (quarterround a b c d (e1::lst)) as l1; |
| 734 |
let g := context ctx [l1] in |
| 735 |
change g |
| 736 |
end; |
| 737 |
remember l1 eqn:Heql; |
| 738 |
subst l1; |
| 739 |
unfold quarterround in Heql; |
| 740 |
cbn [nth] in Heql; |
| 741 |
revert Heql; |
| 742 |
set (quarter_gallina _ _ _ _) as l2; |
| 743 |
destruct l2 as [? [? [? ?]]]; |
| 744 |
intro Heql; |
| 745 |
cbn-[word.of_Z] in Heql; |
| 746 |
subst l). |
| 747 |
|
| 748 |
unfold nlet. |
| 749 |
do 4 (lazymatch goal with |
| 750 |
|- context ctx [(quarterround ?a ?b ?c ?d (?e1::?lst))] => |
| 751 |
set (quarterround a b c d (e1::lst)) as l1; |
| 752 |
let g := context ctx [l1] in |
| 753 |
change g |
| 754 |
end; |
| 755 |
remember l1 eqn:Heql; |
| 756 |
subst l1; |
| 757 |
unfold quarterround in Heql; |
| 758 |
cbn [nth] in Heql; |
| 759 |
revert Heql; |
| 760 |
set (quarter_gallina _ _ _ _) as l2; |
| 761 |
destruct l2 as [? [? [? ?]]]; |
| 762 |
intro Heql; |
| 763 |
cbn-[word.of_Z] in Heql; |
| 764 |
subst l). |
| 765 |
reflexivity. |
| 766 |
} |
| 767 |
{ |
| 768 |
split. |
| 769 |
{ |
| 770 |
rewrite !Forall_app. |
| 771 |
repeat split. |
| 772 |
all: change 32 with (8 * Z.of_nat 4). |
| 773 |
all: apply Forall_le_combine_in_bounds. |
| 774 |
all: lia. |
| 775 |
} |
| 776 |
rewrite !app_length, !map_length, !length_chunk, Hlenk by lia. |
| 777 |
rewrite app_length, Hlenn. |
| 778 |
reflexivity. |
| 779 |
} |
| 780 |
{ |
| 781 |
rewrite !map_app, !map_map. |
| 782 |
change (λ x : list Init.Byte.byte, word.of_Z (LittleEndianList.le_combine x)) with le_combine. |
| 783 |
rewrite chunk_app_chunk; try lia. |
| 784 |
2:{ rewrite LittleEndianList.length_le_split; auto. } |
| 785 |
cbn [map]. |
| 786 |
change (le_combine (LittleEndianList.le_split 4 (word.unsigned (word.of_Z 0)))) with (word.of_Z (word:=word) 0). |
| 787 |
remember ((map le_combine (chunk 4 (list_byte_of_string "expand 32-byte k")) ++ |
| 788 |
map le_combine (chunk 4 key) ++ word.of_Z 0 :: map le_combine (chunk 4 nonce))). |
| 789 |
assert (length l = 16%nat). |
| 790 |
{ |
| 791 |
subst l. |
| 792 |
rewrite !app_length, !map_length, !length_chunk, Hlenk by lia. |
| 793 |
cbn [length]. |
| 794 |
rewrite map_length, length_chunk, Hlenn by lia. |
| 795 |
reflexivity. |
| 796 |
} |
| 797 |
clear Heql Hlen. |
| 798 |
repeat (destruct l as [|? l]; cbn [length] in H; try lia). |
| 799 |
reflexivity. |
| 800 |
} |
| 801 |
Qed. |
| 802 |
|
| 803 |
Definition load_offset e o := |
| 804 |
expr.load access_size.word |
| 805 |
(expr.op bopname.add (expr.literal (4*o)) e). |
| 806 |
Definition count_to (n : nat) : list Z := (z_range 0 n). |
| 807 |
|
| 808 |
|
| 809 |
Fixpoint unroll {A} (default : A) (l : list A) n := |
| 810 |
match n with |
| 811 |
| O => [] |
| 812 |
| S n' => |
| 813 |
unroll default l n' ++ [nth n' l default] |
| 814 |
end. |
| 815 |
Lemma unroll_len_helper {A} a (l : list A) n |
| 816 |
: (n <= length l)%nat -> firstn n l = unroll a l n. |
| 817 |
Proof. |
| 818 |
induction n; cbn [length unroll]; auto. |
| 819 |
intros. |
| 820 |
rewrite <- IHn by lia. |
| 821 |
erewrite ListUtil.firstn_succ by lia. |
| 822 |
rewrite nth_default_eq. |
| 823 |
reflexivity. |
| 824 |
Qed. |
| 825 |
|
| 826 |
Lemma unroll_len {A} a (l : list A) |
| 827 |
: l = unroll a l (length l). |
| 828 |
Proof. |
| 829 |
rewrite <- unroll_len_helper by lia. |
| 830 |
rewrite firstn_all. |
| 831 |
reflexivity. |
| 832 |
Qed. |
| 833 |
|
| 834 |
|
| 835 |
Lemma truncate_word_word (w : word) |
| 836 |
: truncate_word access_size.word w = w. |
| 837 |
Proof. |
| 838 |
unfold truncate_word. |
| 839 |
unfold truncate_Z. |
| 840 |
rewrite Z.land_ones_low. |
| 841 |
{ apply word.of_Z_unsigned. } |
| 842 |
{ pose proof (word.unsigned_range w); lia. } |
| 843 |
{ pose proof (word.unsigned_range w). |
| 844 |
change (Z.of_nat (Memory.bytes_per access_size.word) * 8) with 32. |
| 845 |
destruct (Z.eqb_spec (word.unsigned w) 0). |
| 846 |
{ |
| 847 |
rewrite e; cbn; lia. |
| 848 |
} |
| 849 |
apply Hints.log2_lt_pow2_proj_l2r; lia. |
| 850 |
} |
| 851 |
Qed. |
| 852 |
|
| 853 |
|
| 854 |
Notation map_remove_many m ks := |
| 855 |
(List.fold_left map.remove ks m). |
| 856 |
|
| 857 |
Lemma dexpr_locals_put_removed v |
| 858 |
: ∀ (m : mem) l exp (w : word), |
| 859 |
DEXPR m (map.remove l v) exp w → DEXPR m l exp w. |
| 860 |
Proof. |
| 861 |
intros. |
| 862 |
remember (map.get l v) as mg eqn:Hget; |
| 863 |
destruct mg; |
| 864 |
symmetry in Hget. |
| 865 |
{ |
| 866 |
erewrite <- map.put_noop with (m:=l) (k:=v); eauto. |
| 867 |
rewrite <- map.put_remove_same. |
| 868 |
eapply dexpr_locals_put; eauto. |
| 869 |
apply map.get_remove_same. |
| 870 |
} |
| 871 |
{ |
| 872 |
rewrite map.remove_not_in in H; eauto. |
| 873 |
} |
| 874 |
Qed. |
| 875 |
|
| 876 |
|
| 877 |
(*TODO: generalize*) |
| 878 |
Lemma map_remove_comm (l : locals) a b |
| 879 |
: (map.remove (map.remove l a) b) = map.remove (map.remove l b) a. |
| 880 |
Proof. |
| 881 |
eapply map.ext_eq. |
| 882 |
unfold map.map_eq. |
| 883 |
intro k. |
| 884 |
destruct (String.eqb_spec k b); |
| 885 |
destruct (String.eqb_spec k a); |
| 886 |
subst; |
| 887 |
repeat rewrite ?map.get_remove_same, ?map.get_remove_diff by tauto; |
| 888 |
reflexivity. |
| 889 |
Qed. |
| 890 |
|
| 891 |
(*TODO: generalize*) |
| 892 |
Lemma map_remove_many_comm (l : locals) vars a |
| 893 |
: (map_remove_many (map.remove l a) vars) = map.remove (map_remove_many l vars) a. |
| 894 |
Proof. |
| 895 |
unfold map_remove_many. |
| 896 |
revert l. |
| 897 |
induction vars; cbn [map_remove_many List.fold_left]; eauto. |
| 898 |
intros. |
| 899 |
rewrite <- IHvars. |
| 900 |
f_equal. |
| 901 |
destruct (String.eqb_spec a a0). |
| 902 |
{ |
| 903 |
subst. |
| 904 |
rewrite map.remove_not_in. |
| 905 |
2:{ apply map.get_remove_same. } |
| 906 |
reflexivity. |
| 907 |
} |
| 908 |
{ |
| 909 |
apply map_remove_comm. |
| 910 |
} |
| 911 |
Qed. |
| 912 |
|
| 913 |
Lemma dexpr_locals_put_removed_many vars |
| 914 |
: ∀ (m : mem) l exp (w : word), |
| 915 |
DEXPR m (map_remove_many l vars) exp w → DEXPR m l exp w. |
| 916 |
Proof. |
| 917 |
intro m. |
| 918 |
induction vars; cbn [map_remove_many List.fold_left]; eauto. |
| 919 |
intros. |
| 920 |
apply IHvars in H. |
| 921 |
eapply dexpr_locals_put_removed; eauto. |
| 922 |
Qed. |
| 923 |
|
| 924 |
Lemma map_remove_remove_same s (l : locals) |
| 925 |
: map.remove (map.remove l s) s = map.remove l s. |
| 926 |
Proof. |
| 927 |
apply map.map_ext. |
| 928 |
intros. |
| 929 |
destruct (String.eqb_spec s k); subst; |
| 930 |
rewrite ?map.get_remove_diff, ?map.get_remove_same by auto; |
| 931 |
auto. |
| 932 |
Qed. |
| 933 |
|
| 934 |
|
| 935 |
Lemma map_remove_remove_comm s1 s2 (l : locals) |
| 936 |
: map.remove (map.remove l s1) s2 = map.remove (map.remove l s2) s1. |
| 937 |
Proof. |
| 938 |
apply map.map_ext. |
| 939 |
intros. |
| 940 |
destruct (String.eqb_spec s1 k); subst; |
| 941 |
destruct (String.eqb_spec s2 k); subst; |
| 942 |
rewrite ?map.get_remove_diff, ?map.get_remove_same by auto; |
| 943 |
auto. |
| 944 |
rewrite ?map.get_remove_diff, ?map.get_remove_same by auto. |
| 945 |
auto. |
| 946 |
Qed. |
| 947 |
|
| 948 |
Lemma map_remove_remove_many_in s vars (l : locals) |
| 949 |
: In s vars -> |
| 950 |
(map_remove_many (map.remove l s) vars) = (map_remove_many l vars). |
| 951 |
Proof. |
| 952 |
revert l. |
| 953 |
induction vars; |
| 954 |
cbn [List.fold_left In]; |
| 955 |
intuition (subst; eauto). |
| 956 |
1: f_equal; apply map_remove_remove_same. |
| 957 |
rewrite map_remove_remove_comm. |
| 958 |
apply IHvars; auto. |
| 959 |
Qed. |
| 960 |
|
| 961 |
Lemma map_put_remove_many_in s i vars (l : locals) |
| 962 |
: In s vars -> |
| 963 |
(map_remove_many #{ … l; s => i }# vars) |
| 964 |
= (map_remove_many l vars). |
| 965 |
Proof. |
| 966 |
revert l. |
| 967 |
induction vars; |
| 968 |
cbn [List.fold_left In]; |
| 969 |
intuition (subst; eauto). |
| 970 |
1: f_equal; apply map.remove_put_same. |
| 971 |
destruct (String.eqb_spec s a); subst. |
| 972 |
{ |
| 973 |
rewrite map.remove_put_same; auto. |
| 974 |
} |
| 975 |
{ |
| 976 |
rewrite map.remove_put_diff; auto. |
| 977 |
} |
| 978 |
Qed. |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
Lemma map_put_remove_many_notin s i vars (l : locals) |
| 983 |
: ~ In s vars -> |
| 984 |
(map_remove_many #{ … l; s => i }# vars) |
| 985 |
= map.put (map_remove_many l vars) s i. |
| 986 |
Proof. |
| 987 |
revert l. |
| 988 |
induction vars; |
| 989 |
cbn [List.fold_left In]; |
| 990 |
intuition (subst; eauto). |
| 991 |
rewrite map.remove_put_diff; auto. |
| 992 |
Qed. |
| 993 |
|
| 994 |
|
| 995 |
Lemma map_remove_remove_many_notin s vars (l : locals) |
| 996 |
: ~ In s vars -> |
| 997 |
(map_remove_many (map.remove l s) vars) |
| 998 |
= map.remove (map_remove_many l vars) s. |
| 999 |
Proof. |
| 1000 |
revert l. |
| 1001 |
induction vars; |
| 1002 |
cbn [List.fold_left In]; |
| 1003 |
intuition (subst; eauto). |
| 1004 |
rewrite map_remove_comm; auto. |
| 1005 |
Qed. |
| 1006 |
|
| 1007 |
|
| 1008 |
Lemma expr_load_word_of_array_helper m l len lst e ptr R (n : nat) |
| 1009 |
: length lst = len -> |
| 1010 |
n <= len -> |
| 1011 |
(array scalar (word.of_Z (word:=word) 4) ptr lst * R)%sep m -> |
| 1012 |
forall vars, |
| 1013 |
DEXPR m (map_remove_many l vars) e ptr -> |
| 1014 |
length vars = (len - n)%nat -> |
| 1015 |
locals_array_expr (eq m) l vars (map (load_offset e) (z_range n len)) (skipn n lst). |
| 1016 |
Proof. |
| 1017 |
remember (len - n)%nat as diff. |
| 1018 |
|
| 1019 |
revert l n Heqdiff R lst. |
| 1020 |
induction diff. |
| 1021 |
{ |
| 1022 |
intros; simpl in *; subst. |
| 1023 |
rewrite z_range_nil by lia. |
| 1024 |
rewrite List.skipn_all by lia. |
| 1025 |
simpl. |
| 1026 |
destruct vars; simpl in *; try lia. |
| 1027 |
constructor. |
| 1028 |
} |
| 1029 |
{ |
| 1030 |
intros. |
| 1031 |
rewrite z_range_cons by lia. |
| 1032 |
erewrite split_hd_tl with (l := skipn _ _). |
| 1033 |
2:{ |
| 1034 |
rewrite skipn_length. |
| 1035 |
cbn [length]. |
| 1036 |
lia. |
| 1037 |
} |
| 1038 |
rewrite <- hd_skipn_nth_default. |
| 1039 |
rewrite nth_default_eq. |
| 1040 |
cbn [map]. |
| 1041 |
destruct vars; cbn [length] in *; try lia. |
| 1042 |
constructor. |
| 1043 |
{ |
| 1044 |
unfold load_offset. |
| 1045 |
unfold DEXPR. |
| 1046 |
cbn. |
| 1047 |
unfold literal. |
| 1048 |
unfold dlet. |
| 1049 |
intros ? ?; subst. |
| 1050 |
eapply WeakestPrecondition_dexpr_expr; eauto. |
| 1051 |
unfold load. |
| 1052 |
eexists; split; eauto. |
| 1053 |
instantiate (1:= word.of_Z 0). |
| 1054 |
change (@nth (@Naive.rep 32) n lst (@word.of_Z 32 word 0)) |
| 1055 |
with (@nth (@word.rep _ word) n lst (@word.of_Z 32 word 0)). |
| 1056 |
replace (nth n lst (word.of_Z 0)) |
| 1057 |
with (truncate_word access_size.word (nth n lst (word.of_Z 0))). |
| 1058 |
eapply array_load_of_sep; eauto. |
| 1059 |
{ |
| 1060 |
|
| 1061 |
change (Naive.unsigned ?a) with (word.unsigned (word:=word) a). |
| 1062 |
rewrite word.unsigned_of_Z. |
| 1063 |
(*lia. |
| 1064 |
rewrite Radd_comm by apply word.ring_theory. |
| 1065 |
reflexivity.*) |
| 1066 |
change (Naive.wrap ?a) with (word.of_Z (word:=word) a). |
| 1067 |
rewrite word.ring_morph_add. |
| 1068 |
rewrite word.of_Z_unsigned. |
| 1069 |
rewrite Radd_comm by apply word.ring_theory. |
| 1070 |
f_equal. |
| 1071 |
rewrite word.unsigned_of_Z. |
| 1072 |
symmetry. |
| 1073 |
rewrite word.of_Z_wrap. |
| 1074 |
change (word.wrap 4) with 4. |
| 1075 |
reflexivity. |
| 1076 |
} |
| 1077 |
lia. |
| 1078 |
{ |
| 1079 |
apply truncate_word_word. |
| 1080 |
} |
| 1081 |
eapply dexpr_locals_put_removed_many; eauto. |
| 1082 |
} |
| 1083 |
rewrite tl_skipn. |
| 1084 |
replace (Z.of_nat n + 1) with (Z.of_nat (S n)) by lia. |
| 1085 |
intro. |
| 1086 |
eapply IHdiff; eauto. |
| 1087 |
lia. |
| 1088 |
lia. |
| 1089 |
cbn [List.fold_left map_remove_many] in H2. |
| 1090 |
destruct (ListDec.In_dec string_dec s vars). |
| 1091 |
{ |
| 1092 |
rewrite map_put_remove_many_in by assumption. |
| 1093 |
rewrite map_remove_remove_many_in in H2 by assumption. |
| 1094 |
auto. |
| 1095 |
} |
| 1096 |
{ |
| 1097 |
rewrite map_put_remove_many_notin; auto. |
| 1098 |
rewrite map_remove_remove_many_notin in H2 by auto. |
| 1099 |
eapply dexpr_locals_put with (x:=s) (v:=i) in H2. |
| 1100 |
2: apply map.get_remove_same. |
| 1101 |
rewrite map.put_remove_same in H2. |
| 1102 |
auto. |
| 1103 |
} |
| 1104 |
} |
| 1105 |
Qed. |
| 1106 |
|
| 1107 |
Lemma expr_load_word_of_array m l len lst e ptr R vars |
| 1108 |
: length lst = len -> |
| 1109 |
length vars = len -> |
| 1110 |
(array scalar (word.of_Z (word:=word) 4) ptr lst * R)%sep m -> |
| 1111 |
DEXPR m (map_remove_many l vars) e ptr -> |
| 1112 |
locals_array_expr m l vars (map (load_offset e) (count_to len)) lst. |
| 1113 |
Proof. |
| 1114 |
unfold count_to. |
| 1115 |
change 0 with (Z.of_nat 0). |
| 1116 |
change lst with (skipn 0 lst). |
| 1117 |
intros. |
| 1118 |
eapply expr_load_word_of_array_helper; eauto. |
| 1119 |
lia. |
| 1120 |
lia. |
| 1121 |
Qed. |
| 1122 |
|
| 1123 |
Lemma expr_load_word_of_byte_array m l len lst e ptr R vars |
| 1124 |
: (*(length lst mod Memory.bytes_per (width :=32) access_size.word)%nat = 0%nat ->*) |
| 1125 |
length lst = (4*len)%nat -> |
| 1126 |
length vars = len -> |
| 1127 |
(lst$@ptr * R)%sep m -> |
| 1128 |
DEXPR m (map_remove_many l vars) e ptr -> |
| 1129 |
locals_array_expr (eq m) l vars (map (load_offset e) (count_to len)) (map le_combine (chunk 4 lst)). |
| 1130 |
Proof. |
| 1131 |
intros. |
| 1132 |
seprewrite_in words_of_bytes H1; auto. |
| 1133 |
{ |
| 1134 |
rewrite H. |
| 1135 |
rewrite Nat.mul_comm. |
| 1136 |
rewrite Nat.mod_mul; cbv; congruence. |
| 1137 |
} |
| 1138 |
eapply expr_load_word_of_array; eauto. |
| 1139 |
{ |
| 1140 |
rewrite map_length, length_chunk by lia. |
| 1141 |
rewrite H. |
| 1142 |
rewrite Nat.mul_comm. |
| 1143 |
rewrite Nat.div_up_exact; lia. |
| 1144 |
} |
| 1145 |
|
| 1146 |
unfold le_combine. |
| 1147 |
unfold bs2ws, zs2ws, bs2zs in *. |
| 1148 |
rewrite <- map_map with (g:= word.of_Z (word:=word)). |
| 1149 |
change (Z.of_nat (Memory.bytes_per access_size.word)) with 4 in *. |
| 1150 |
change (Memory.bytes_per access_size.word) with 4%nat in *. |
| 1151 |
ecancel_assumption. |
| 1152 |
Qed. |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
Definition store_offset e o := |
| 1158 |
cmd.store access_size.word |
| 1159 |
(expr.op bopname.add (expr.literal (4*o)) e). |
| 1160 |
|
| 1161 |
Definition store_to_array ptr (lst : list expr) (k_impl : cmd) : cmd := |
| 1162 |
List.fold_right (fun '(x,e) k_impl => cmd.seq (store_offset (expr.var ptr) x e) k_impl) |
| 1163 |
k_impl |
| 1164 |
(combine (count_to (length lst)) lst). |
| 1165 |
|
| 1166 |
|
| 1167 |
Lemma Forall2_distr_forall {A B C} (P : A -> B -> C -> Prop) l1 l2 |
| 1168 |
: Forall2 (fun b c => forall a, P a b c) l1 l2 -> |
| 1169 |
forall (a:A), Forall2 (P a) l1 l2. |
| 1170 |
Proof. |
| 1171 |
revert l2; induction l1; inversion 1; subst; intros; constructor; eauto. |
| 1172 |
Qed. |
| 1173 |
|
| 1174 |
|
| 1175 |
Lemma forall_distr_Forall2 {A B C} (P : A -> B -> C -> Prop) l1 l2 |
| 1176 |
: A -> |
| 1177 |
(forall (a:A), Forall2 (P a) l1 l2) -> |
| 1178 |
Forall2 (fun b c => forall a, P a b c) l1 l2. |
| 1179 |
Proof. |
| 1180 |
intro a. |
| 1181 |
revert l2; induction l1; intros. |
| 1182 |
{ |
| 1183 |
specialize (H a); inversion H; subst; eauto. |
| 1184 |
} |
| 1185 |
{ |
| 1186 |
pose proof (H a) as H'; inversion H'; subst. |
| 1187 |
constructor. |
| 1188 |
{ |
| 1189 |
intros. |
| 1190 |
specialize (H a1); inversion H; subst; eauto. |
| 1191 |
} |
| 1192 |
apply IHl1. |
| 1193 |
intros. |
| 1194 |
specialize (H a1); inversion H; subst; eauto. |
| 1195 |
} |
| 1196 |
Qed. |
| 1197 |
|
| 1198 |
|
| 1199 |
Lemma upd_chunk n lst a |
| 1200 |
: (length lst mod 4 = 0)%nat -> |
| 1201 |
upd (chunk 4 lst) n (LittleEndianList.le_split 4 a) |
| 1202 |
= chunk 4 (upds lst (n * 4) (LittleEndianList.le_split 4 a)). |
| 1203 |
Proof. |
| 1204 |
intro. |
| 1205 |
unfold upd. |
| 1206 |
unfold upds. |
| 1207 |
cbn [length]. |
| 1208 |
rewrite !chunk_app; try lia. |
| 1209 |
f_equal. |
| 1210 |
1: rewrite !firstn_chunk by lia; reflexivity. |
| 1211 |
f_equal. |
| 1212 |
2:{ |
| 1213 |
rewrite skipn_chunk by lia. |
| 1214 |
Import coqutil.Word.LittleEndianList (length_le_split). |
| 1215 |
rewrite length_le_split. |
| 1216 |
replace ((1 + n) * 4) with (4 + n * 4) by lia. |
| 1217 |
reflexivity. |
| 1218 |
} |
| 1219 |
{ |
| 1220 |
rewrite length_chunk by lia. |
| 1221 |
rewrite <- Nat.div_up_exact_mod with (a:= length lst) (b:=4%nat) at 2 by lia. |
| 1222 |
generalize (Nat.div_up (length lst) 4). |
| 1223 |
clear lst H. |
| 1224 |
intros. |
| 1225 |
replace (n0 * 4 - n * 4)%nat with ((n0 - n) * 4)%nat by lia. |
| 1226 |
generalize (n0 - n)%nat; intros. |
| 1227 |
destruct (n1); cbn [firstn chunk]. |
| 1228 |
{ reflexivity. } |
| 1229 |
{ |
| 1230 |
rewrite firstn_nil. |
| 1231 |
rewrite <- firstn_chunk by lia. |
| 1232 |
rewrite chunk_small. |
| 1233 |
simpl. |
| 1234 |
rewrite firstn_nil. |
| 1235 |
reflexivity. |
| 1236 |
rewrite length_le_split. |
| 1237 |
lia. |
| 1238 |
} |
| 1239 |
} |
| 1240 |
{ |
| 1241 |
rewrite firstn_length. |
| 1242 |
rewrite length_le_split. |
| 1243 |
apply Nat.min_case. |
| 1244 |
{ |
| 1245 |
unfold Nat.modulo in *. |
| 1246 |
pose proof (Nat.divmod_spec (length lst) 3 0 3 ltac:(lia)). |
| 1247 |
remember (Nat.divmod (length lst) 3 0 3 ). |
| 1248 |
destruct p. |
| 1249 |
destruct H0. |
| 1250 |
pose proof (Nat.divmod_spec (length lst - n * 4) 3 0 3 ltac:(lia)). |
| 1251 |
remember (Nat.divmod (length lst - n * 4) 3 0 3 ). |
| 1252 |
destruct p. |
| 1253 |
destruct H2. |
| 1254 |
cbn [snd] in *. |
| 1255 |
lia. |
| 1256 |
} |
| 1257 |
apply Nat.mod_same; lia. |
| 1258 |
} |
| 1259 |
{ |
| 1260 |
rewrite firstn_length. |
| 1261 |
apply Nat.min_case. |
| 1262 |
apply Nat.mod_mul; lia. |
| 1263 |
auto. |
| 1264 |
} |
| 1265 |
Qed. |
| 1266 |
|
| 1267 |
Lemma compile_store_locals_array' {t} {m : mem} {l e} (lst : list word) : |
| 1268 |
forall (n : nat) P out ptr k_impl var lst_expr R, |
| 1269 |
(out$@ptr * R)%sep m -> |
| 1270 |
(*TODO: is this general enough?*) |
| 1271 |
(Forall2 (fun e v => forall m' out', (out'$@ptr * R)%sep m' -> DEXPR m' l e v) lst_expr lst) -> |
| 1272 |
(*TODO: move byte/word conversion to a better place*) |
| 1273 |
length out = (4* (length lst + n))%nat -> |
| 1274 |
DEXPR m l (expr.var var) ptr -> |
| 1275 |
(forall m, |
| 1276 |
((firstn (4*n)%nat out)$@ptr * (array scalar (word.of_Z 4) (ptr + word.of_Z(4*n))%word lst) * R)%sep m -> |
| 1277 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 1278 |
k_impl |
| 1279 |
<{ P }>) -> |
| 1280 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 1281 |
List.fold_right (fun '(x,e) k_impl => cmd.seq (store_offset (expr.var var) x e) k_impl) |
| 1282 |
k_impl |
| 1283 |
(combine (z_range n (length lst_expr + n)) lst_expr) |
| 1284 |
<{ P }>. |
| 1285 |
Proof. |
| 1286 |
revert m. |
| 1287 |
induction lst. |
| 1288 |
{ |
| 1289 |
|
| 1290 |
intros until R; |
| 1291 |
intros Hm Hlst. |
| 1292 |
eapply Forall2_distr_forall in Hlst. |
| 1293 |
eapply Forall2_distr_forall in Hlst. |
| 1294 |
eapply Forall2_distr_forall in Hlst; eauto. |
| 1295 |
inversion Hlst; subst. |
| 1296 |
|
| 1297 |
cbn [length Nat.add] in *. |
| 1298 |
rewrite z_range_nil by lia. |
| 1299 |
intros HA HB HC. |
| 1300 |
eapply HC. |
| 1301 |
rewrite <- HA. |
| 1302 |
rewrite firstn_all. |
| 1303 |
cbn [array]. |
| 1304 |
ecancel_assumption. |
| 1305 |
} |
| 1306 |
{ |
| 1307 |
cbn [length]. |
| 1308 |
intros. |
| 1309 |
inversion H0; subst; clear H0. |
| 1310 |
|
| 1311 |
cbn [length]. |
| 1312 |
replace (S (length l0) + n) with ((length l0) + (n+1)%nat) by lia. |
| 1313 |
rewrite z_range_cons by lia. |
| 1314 |
cbn [combine fold_right]. |
| 1315 |
repeat straightline. |
| 1316 |
eexists; split; eauto. |
| 1317 |
{ |
| 1318 |
eapply expr_compile_word_add. |
| 1319 |
eapply expr_compile_Z_literal. |
| 1320 |
eauto. |
| 1321 |
} |
| 1322 |
eexists; split; eauto. |
| 1323 |
seprewrite_in words_of_bytes H. |
| 1324 |
{ rewrite H1. |
| 1325 |
rewrite Nat.mul_comm. |
| 1326 |
apply Nat.mod_mul. |
| 1327 |
cbv; congruence. |
| 1328 |
} |
| 1329 |
unfold store. |
| 1330 |
eapply array_store_of_sep; cycle 1. |
| 1331 |
{ |
| 1332 |
unfold scalar in H. |
| 1333 |
ecancel_assumption. |
| 1334 |
} |
| 1335 |
3:{ |
| 1336 |
rewrite Radd_comm by apply word.ring_theory. |
| 1337 |
reflexivity. |
| 1338 |
} |
| 1339 |
{ |
| 1340 |
rewrite bs2ws_length. |
| 1341 |
rewrite H1. |
| 1342 |
rewrite Nat.mul_comm. |
| 1343 |
rewrite Nat.div_mul. |
| 1344 |
lia. |
| 1345 |
cbv;congruence. |
| 1346 |
cbv;congruence. |
| 1347 |
rewrite H1. |
| 1348 |
rewrite Nat.mul_comm. |
| 1349 |
rewrite Nat.mod_mul. |
| 1350 |
lia. |
| 1351 |
cbv;congruence. |
| 1352 |
} |
| 1353 |
replace (Z.of_nat n + 1) with (Z.of_nat (n+1)) by lia. |
| 1354 |
intros. |
| 1355 |
eapply IHlst with (n := (n + 1)%nat); eauto. |
| 1356 |
seprewrite words_of_bytes; cycle 1. |
| 1357 |
{ |
| 1358 |
unfold scalar. |
| 1359 |
unfold bs2ws, zs2ws, bs2zs in *. |
| 1360 |
rewrite <- word.of_Z_unsigned with (x:=a) in H0. |
| 1361 |
replace (word.unsigned a) |
| 1362 |
with (word.unsigned a mod 2 ^ (Z.of_nat 4 * 8)) in H0. |
| 1363 |
Import coqutil.Word.LittleEndianList (le_combine_split). |
| 1364 |
rewrite <- le_combine_split in H0. |
| 1365 |
rewrite <- !map_upd in H0. |
| 1366 |
rewrite upd_chunk in H0. |
| 1367 |
change (Memory.bytes_per access_size.word) with 4%nat in *. |
| 1368 |
ecancel_assumption. |
| 1369 |
rewrite H1. |
| 1370 |
rewrite Nat.mul_comm. |
| 1371 |
rewrite Nat.mod_mul. |
| 1372 |
lia. |
| 1373 |
lia. |
| 1374 |
{ |
| 1375 |
rewrite Z.mod_small; auto. |
| 1376 |
apply word.unsigned_range. |
| 1377 |
} |
| 1378 |
} |
| 1379 |
{ |
| 1380 |
rewrite upds_length. |
| 1381 |
rewrite H1. |
| 1382 |
rewrite Nat.mul_comm. |
| 1383 |
rewrite Nat.mod_mul. |
| 1384 |
lia. |
| 1385 |
cbv; congruence. |
| 1386 |
} |
| 1387 |
{ |
| 1388 |
rewrite upds_length. |
| 1389 |
rewrite H1. |
| 1390 |
rewrite Nat.mul_comm. |
| 1391 |
lia. |
| 1392 |
} |
| 1393 |
intros m' Hm'. |
| 1394 |
eapply H3. |
| 1395 |
cbn [array]. |
| 1396 |
unfold upds in Hm'. |
| 1397 |
rewrite !firstn_app, !firstn_firstn in Hm'. |
| 1398 |
rewrite !firstn_length, !length_le_split in Hm'. |
| 1399 |
replace (Init.Nat.min (4 * (n + 1)) (n * 4)) with (4 * n)%nat in Hm' by lia. |
| 1400 |
assert (length out > n * 4) by lia. |
| 1401 |
replace (4 * (n + 1) - Init.Nat.min (n * 4) (length out))%nat |
| 1402 |
with 4%nat in Hm' by lia. |
| 1403 |
replace (length out - n * 4)%nat |
| 1404 |
with (4 * S (length lst))%nat in Hm' by lia. |
| 1405 |
replace (Init.Nat.min 4 (4 * S (length lst)))%nat with 4%nat in Hm' by lia. |
| 1406 |
replace (Init.Nat.min (4 * S (length lst)) 4)%nat with 4%nat in Hm' by lia. |
| 1407 |
rewrite Nat.sub_diag in Hm'. |
| 1408 |
rewrite firstn_all2 with (n:=4%nat) in Hm'. |
| 1409 |
cbn [firstn] in Hm'. |
| 1410 |
rewrite app_nil_r in Hm'. |
| 1411 |
replace (4 * Z.of_nat (n + 1)) with (4 * Z.of_nat n + 4) in Hm' by lia. |
| 1412 |
rewrite word.ring_morph_add in Hm'. |
| 1413 |
seprewrite_in (bytearray_append (firstn (4*n) out) (LittleEndianList.le_split 4 (word.unsigned a)) ptr) Hm'. |
| 1414 |
seprewrite_in (scalar_of_bytes (ptr + word.of_Z (Z.of_nat (length (firstn (4 * n) out))))%word |
| 1415 |
(LittleEndianList.le_split 4 (word.unsigned a))) Hm'. |
| 1416 |
{ |
| 1417 |
rewrite length_le_split. |
| 1418 |
reflexivity. |
| 1419 |
} |
| 1420 |
rewrite !firstn_length in Hm'. |
| 1421 |
replace (Z.of_nat (Init.Nat.min (4 * n) (length out))) |
| 1422 |
with (4 * Z.of_nat n) in Hm' by lia. |
| 1423 |
rewrite le_combine_split in Hm'. |
| 1424 |
{ |
| 1425 |
rewrite Z.mod_small in Hm'; auto. |
| 1426 |
2:apply word.unsigned_range. |
| 1427 |
rewrite word.of_Z_unsigned in Hm'. |
| 1428 |
replace (ptr + (word.of_Z (4 * Z.of_nat n) + word.of_Z 4))%word |
| 1429 |
with (ptr + word.of_Z (4 * Z.of_nat n) + word.of_Z 4)%word in Hm'. |
| 1430 |
ecancel_assumption. |
| 1431 |
{ |
| 1432 |
rewrite Radd_assoc. |
| 1433 |
reflexivity. |
| 1434 |
apply word.ring_theory. |
| 1435 |
} |
| 1436 |
} |
| 1437 |
{ |
| 1438 |
rewrite length_le_split; lia. |
| 1439 |
} |
| 1440 |
} |
| 1441 |
Qed. |
| 1442 |
|
| 1443 |
|
| 1444 |
Lemma compile_store_locals_array {t m l e} (lst : list word): |
| 1445 |
let v := lst in |
| 1446 |
forall P (pred: P v -> predicate) (k: nlet_eq_k P v) |
| 1447 |
out ptr k_impl var lst_expr R, |
| 1448 |
(*TODO: is this general enough?*) |
| 1449 |
(Forall2 (fun e v => forall m' out', (out'$@ptr * R)%sep m' -> DEXPR m' l e v) lst_expr lst) -> |
| 1450 |
(*TODO: move byte/word conversion to a better place*) |
| 1451 |
length out = (4* length v)%nat -> |
| 1452 |
DEXPR m l (expr.var var) ptr -> |
| 1453 |
(out$@ptr * R)%sep m -> |
| 1454 |
(let v := v in |
| 1455 |
forall m, |
| 1456 |
(array scalar (word.of_Z 4) ptr v * R)%sep m -> |
| 1457 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 1458 |
k_impl |
| 1459 |
<{ pred (k v eq_refl) }>) -> |
| 1460 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 1461 |
store_to_array var lst_expr k_impl |
| 1462 |
<{ pred (nlet_eq [var] v k) }>. |
| 1463 |
Proof. |
| 1464 |
intros. |
| 1465 |
unfold store_to_array. |
| 1466 |
unfold count_to. |
| 1467 |
replace (Z.of_nat (length lst_expr)) with (Z.of_nat (length lst_expr) + 0%nat) by lia. |
| 1468 |
change 0 with (Z.of_nat 0). |
| 1469 |
eapply compile_store_locals_array'; eauto. |
| 1470 |
{ |
| 1471 |
subst v. |
| 1472 |
lia. |
| 1473 |
} |
| 1474 |
intros; apply H3. |
| 1475 |
change (4*0)%nat with 0%nat in H4. |
| 1476 |
replace (ptr + word.of_Z (4 * Z.of_nat 0))%word with ptr in H4. |
| 1477 |
{ |
| 1478 |
cbn [firstn array] in H4. |
| 1479 |
ecancel_assumption. |
| 1480 |
} |
| 1481 |
{ |
| 1482 |
rewrite <- word.of_Z_unsigned with (x := ptr) at 2. |
| 1483 |
rewrite <- word.ring_morph_add. |
| 1484 |
replace ((word.unsigned ptr + 4 * Z.of_nat 0)) with (word.unsigned ptr) by lia. |
| 1485 |
rewrite word.of_Z_unsigned. |
| 1486 |
reflexivity. |
| 1487 |
} |
| 1488 |
Qed. |
| 1489 |
|
| 1490 |
|
| 1491 |
(*TODO: generalize to all ops*) |
| 1492 |
|
| 1493 |
Lemma array_expr_compile_word_add P l vars |
| 1494 |
: ∀ (w1 : list word) (e1 : list expr), |
| 1495 |
locals_array_expr P l vars e1 w1 -> |
| 1496 |
forall e2 w2, |
| 1497 |
locals_array_expr P l vars e2 w2 -> |
| 1498 |
locals_array_expr P l vars |
| 1499 |
(map (λ '(s, t0), (expr.op bopname.add s t0)%word) |
| 1500 |
(combine e1 e2)) |
| 1501 |
(map (λ '(s, t0), (s + t0)%word) |
| 1502 |
(combine w1 w2)). |
| 1503 |
Proof. |
| 1504 |
induction 1; |
| 1505 |
inversion 1; |
| 1506 |
subst; |
| 1507 |
cbn [combine map]; |
| 1508 |
constructor; |
| 1509 |
eauto. |
| 1510 |
{ |
| 1511 |
intros. |
| 1512 |
eapply expr_compile_word_add; eauto. |
| 1513 |
} |
| 1514 |
Qed. |
| 1515 |
|
| 1516 |
|
| 1517 |
(*used because concrete computation on maps seems to be slow here*) |
| 1518 |
Ltac eval_map_get := |
| 1519 |
repeat rewrite map.get_put_diff by (cbv; congruence); |
| 1520 |
rewrite map.get_put_same; reflexivity. |
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
Ltac dedup s := |
| 1525 |
repeat rewrite map.put_put_diff with (k1:=s) by congruence; |
| 1526 |
rewrite ?map.put_put_same with (k:=s). |
| 1527 |
|
| 1528 |
Lemma forall_distr_Forall2' {A B C} (P : A -> B -> C -> Prop) l1 l2 |
| 1529 |
: length l1 = length l2 -> |
| 1530 |
(forall (a:A), Forall2 (P a) l1 l2) -> |
| 1531 |
Forall2 (fun b c => forall a, P a b c) l1 l2. |
| 1532 |
Proof. |
| 1533 |
revert l2; induction l1; destruct l2; cbn [length]; |
| 1534 |
intros; try lia; constructor; intros. |
| 1535 |
{ |
| 1536 |
specialize (H0 a0); inversion H0; subst; eauto. |
| 1537 |
} |
| 1538 |
{ |
| 1539 |
apply IHl1; try lia. |
| 1540 |
intros. |
| 1541 |
specialize (H0 a0); inversion H0; subst; eauto. |
| 1542 |
} |
| 1543 |
Qed. |
| 1544 |
|
| 1545 |
|
| 1546 |
Lemma locals_array_expr_app_helper m l vars1 vars2 le1 le2 l1 l2 |
| 1547 |
: locals_array_expr m l vars1 le1 l1 -> |
| 1548 |
(forall l1', length l1' = length vars1 -> |
| 1549 |
locals_array_expr m (map.putmany_of_list (combine vars1 l1') l) vars2 le2 l2) -> |
| 1550 |
locals_array_expr m l (vars1 ++ vars2) (le1 ++ le2) (l1 ++ l2). |
| 1551 |
Proof. |
| 1552 |
induction 1; |
| 1553 |
intros; |
| 1554 |
cbn [app] in *; |
| 1555 |
try tauto. |
| 1556 |
{ eapply H; eauto. exact (eq_refl (x:= length [])). } |
| 1557 |
{ |
| 1558 |
constructor; |
| 1559 |
intuition (subst; eauto). |
| 1560 |
specialize (H0 i0). |
| 1561 |
specialize (H1 i0). |
| 1562 |
eapply H1; eauto. |
| 1563 |
intros. |
| 1564 |
apply (H2 (i0::l1')). |
| 1565 |
cbn [length]; congruence. |
| 1566 |
} |
| 1567 |
Qed. |
| 1568 |
|
| 1569 |
Lemma locals_array_expr_app m l vars le1 le2 l1 l2 |
| 1570 |
: locals_array_expr m l (firstn (length l1) vars) le1 l1 -> |
| 1571 |
(forall l1', length l1' = length l1 -> |
| 1572 |
locals_array_expr m (map.putmany_of_list (array_locs (firstn (length l1) vars) 0 l1') l) |
| 1573 |
(skipn (length l1) vars) le2 l2) -> |
| 1574 |
locals_array_expr m l vars (le1 ++ le2) (l1 ++ l2). |
| 1575 |
Proof. |
| 1576 |
intros. |
| 1577 |
rewrite <- firstn_skipn with (n:= length l1) (l:= vars). |
| 1578 |
intros; eapply locals_array_expr_app_helper; eauto. |
| 1579 |
intros l1' Hlen. |
| 1580 |
pose proof (locals_array_expr_length _ _ _ _ _ H); intuition. |
| 1581 |
rewrite firstn_length in Hlen. |
| 1582 |
assert (length vars >= length l1)%nat. |
| 1583 |
{ |
| 1584 |
rewrite <- H3. |
| 1585 |
rewrite <- H2. |
| 1586 |
rewrite firstn_length. |
| 1587 |
lia. |
| 1588 |
} |
| 1589 |
assert (length l1' = length l1). |
| 1590 |
{ |
| 1591 |
rewrite Hlen. |
| 1592 |
lia. |
| 1593 |
} |
| 1594 |
specialize (H0 l1'). |
| 1595 |
rewrite array_locs_is_combine in H0; eauto. |
| 1596 |
intuition congruence. |
| 1597 |
Qed. |
| 1598 |
|
| 1599 |
Derive chacha20_block SuchThat |
| 1600 |
(defn! "chacha20_block" ("st", "key", "nonce") { chacha20_block }, |
| 1601 |
implements (Spec.chacha20_block) using [ "quarter" ]) |
| 1602 |
As chacha20_block_body_correct. |
| 1603 |
Proof. |
| 1604 |
compile_setup. |
| 1605 |
replace (pred _) with (pred (Spec.chacha20_block k (le_split 4 (word.of_Z 0) ++ n))) by reflexivity. |
| 1606 |
rewrite chacha20_block_ok. |
| 1607 |
unfold chacha20_block'. |
| 1608 |
compile_step. |
| 1609 |
simple eapply compile_nlet_as_nlet_eq. |
| 1610 |
simple eapply compile_set_locals_array. |
| 1611 |
2:{ |
| 1612 |
repeat constructor; simpl; |
| 1613 |
intuition congruence. |
| 1614 |
} |
| 1615 |
{ |
| 1616 |
repeat (apply locals_array_expr_app; intros). |
| 1617 |
all: rewrite !map_length, !length_chunk by lia. |
| 1618 |
all: rewrite ?H5, ?H8. |
| 1619 |
all: cbn -[combine le_combine]. |
| 1620 |
all: repeat constructor; intros; subst. |
| 1621 |
all: try compile_step. |
| 1622 |
{ |
| 1623 |
eapply expr_load_word_of_byte_array; try eassumption. |
| 1624 |
{ |
| 1625 |
rewrite H5. |
| 1626 |
instantiate (1:= 8%nat). |
| 1627 |
reflexivity. |
| 1628 |
} |
| 1629 |
compile_step. |
| 1630 |
cbn [List.fold_left]; |
| 1631 |
repeat rewrite ?map.remove_put_same, ?map.remove_put_diff, ?map.remove_empty by congruence. |
| 1632 |
compile_step. |
| 1633 |
} |
| 1634 |
{ |
| 1635 |
eapply expr_load_word_of_byte_array; try eassumption. |
| 1636 |
{ |
| 1637 |
rewrite H8. |
| 1638 |
instantiate (1:= 3%nat). |
| 1639 |
reflexivity. |
| 1640 |
} |
| 1641 |
compile_step. |
| 1642 |
cbn [List.fold_left]; |
| 1643 |
repeat rewrite ?map.remove_put_same, ?map.remove_put_diff, ?map.remove_empty by congruence. |
| 1644 |
compile_step. |
| 1645 |
} |
| 1646 |
} |
| 1647 |
compile_step. |
| 1648 |
let l' := eval cbn in l in |
| 1649 |
change l with l'. |
| 1650 |
|
| 1651 |
|
| 1652 |
rewrite Nat_iter_as_nd_ranged_for_all with (i:=0). |
| 1653 |
change (0 + Z.of_nat 10) with 10%Z. |
| 1654 |
|
| 1655 |
compile_step. |
| 1656 |
compile_step. |
| 1657 |
assert (m = m) by reflexivity. |
| 1658 |
compile_step. |
| 1659 |
{ compile_step. } |
| 1660 |
{ compile_step. } |
| 1661 |
{ intuition subst; |
| 1662 |
repeat rewrite map.get_put_diff by (cbv;congruence); |
| 1663 |
apply map.get_put_same. |
| 1664 |
} |
| 1665 |
{ intuition subst. |
| 1666 |
reflexivity. |
| 1667 |
} |
| 1668 |
{ intuition subst. } |
| 1669 |
{ lia. } |
| 1670 |
compile_step. |
| 1671 |
{ |
| 1672 |
compile_step. |
| 1673 |
compile_step. |
| 1674 |
compile_step. |
| 1675 |
|
| 1676 |
Optimize Proof. |
| 1677 |
Optimize Heap. |
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
(* TODO: why doesn't simple eapply work? *) |
| 1682 |
do 7 (change (lp from' (ExitToken.new, ?y)) with ((fun v => lp from' (ExitToken.new, v)) y); |
| 1683 |
simple eapply compile_nlet_as_nlet_eq; |
| 1684 |
change (lp from' (ExitToken.new, ?y)) with ((fun v => lp from' (ExitToken.new, v)) y); |
| 1685 |
simple eapply compile_quarter; [ now (eval_map_get || (repeat compile_step)) ..| repeat compile_step]). |
| 1686 |
|
| 1687 |
change (lp from' (ExitToken.new, ?y)) with ((fun v => lp from' (ExitToken.new, v)) y); |
| 1688 |
simple eapply compile_nlet_as_nlet_eq; |
| 1689 |
change (lp from' (ExitToken.new, ?y)) with ((fun v => lp from' (ExitToken.new, v)) y). |
| 1690 |
simple eapply compile_quarter; [ now (eval_map_get || (repeat compile_step)) ..|]. |
| 1691 |
|
| 1692 |
Optimize Proof. |
| 1693 |
Optimize Heap. |
| 1694 |
compile_step. |
| 1695 |
|
| 1696 |
|
| 1697 |
(*TODO: compile_step takes too long (related to computations on maps?)*) |
| 1698 |
unshelve refine (compile_unsets _ _ _); [ shelve | intros | ]; cycle 1. |
| 1699 |
simple apply compile_skip. |
| 1700 |
2: exact []. |
| 1701 |
cbv beta delta [lp]. |
| 1702 |
split; [tauto|]. |
| 1703 |
split. |
| 1704 |
|
| 1705 |
{ |
| 1706 |
|
| 1707 |
(*TODO: why are quarter calls getting subst'ed?*) |
| 1708 |
cbv [P2.car P2.cdr fst snd]. |
| 1709 |
cbv [map.remove_many fold_left]. |
| 1710 |
cbv [gs]. |
| 1711 |
|
| 1712 |
Ltac concrete_maps_equal := |
| 1713 |
eapply map.map_ext; |
| 1714 |
intro; |
| 1715 |
repeat (rewrite ?map.get_put_diff by assumption; |
| 1716 |
lazymatch goal with |
| 1717 |
|- _ = map.get (map.put _ ?k2 _) ?k1 => |
| 1718 |
destruct (String.eqb_spec k1 k2); |
| 1719 |
[subst; rewrite ?map.get_put_diff, ?map.get_put_same by congruence; now auto |
| 1720 |
| rewrite map.get_put_diff with (k:=k1) (k':=k2) by assumption] |
| 1721 |
end); |
| 1722 |
reflexivity. |
| 1723 |
concrete_maps_equal. |
| 1724 |
} |
| 1725 |
ecancel_assumption. |
| 1726 |
} |
| 1727 |
|
| 1728 |
Optimize Proof. |
| 1729 |
Optimize Heap. |
| 1730 |
|
| 1731 |
compile_step. |
| 1732 |
compile_step. |
| 1733 |
compile_step. |
| 1734 |
compile_step. |
| 1735 |
compile_step. |
| 1736 |
|
| 1737 |
simple eapply compile_nlet_as_nlet_eq. |
| 1738 |
(*TODO: need to strengthen this a la broadcast |
| 1739 |
so that it allows other vars to change, doesn't need fresh names |
| 1740 |
*) |
| 1741 |
simple eapply compile_set_locals_array. |
| 1742 |
{ |
| 1743 |
|
| 1744 |
(*TODO: generalize to all ops*) |
| 1745 |
eapply array_expr_compile_word_add. |
| 1746 |
{ |
| 1747 |
repeat constructor; repeat compile_step. |
| 1748 |
all:apply expr_compile_var; |
| 1749 |
let v := lazymatch goal with |- _ = Some ?v => v end in |
| 1750 |
let k := lazymatch goal with |- map.get _ ?k = Some _ => k end in |
| 1751 |
lazymatch goal with |
| 1752 |
|- context [ map.put _ ?k' v] => |
| 1753 |
unify k k' |
| 1754 |
end; |
| 1755 |
eval_map_get. |
| 1756 |
} |
| 1757 |
{ |
| 1758 |
|
| 1759 |
repeat (apply locals_array_expr_app; intros). |
| 1760 |
{ |
| 1761 |
let x := eval cbn -[word.of_Z] in (map le_combine (chunk 4 (list_byte_of_string "expand 32-byte k"))) in |
| 1762 |
change (map le_combine (chunk 4 (list_byte_of_string "expand 32-byte k"))) with x. |
| 1763 |
unfold le_combine. |
| 1764 |
cbn [firstn length]. |
| 1765 |
repeat (constructor; intros); |
| 1766 |
apply expr_compile_Z_literal. |
| 1767 |
} |
| 1768 |
{ |
| 1769 |
eapply expr_load_word_of_byte_array; try eassumption. |
| 1770 |
{ |
| 1771 |
rewrite H5. |
| 1772 |
instantiate (1:= 8%nat). |
| 1773 |
reflexivity. |
| 1774 |
} |
| 1775 |
{ |
| 1776 |
rewrite !map_length, !length_chunk, H5. |
| 1777 |
reflexivity. |
| 1778 |
all: lia. |
| 1779 |
} |
| 1780 |
{ |
| 1781 |
rewrite !map_length, !length_chunk, H5. |
| 1782 |
all: try lia. |
| 1783 |
repeat (set (Nat.div_up _ _) as x; |
| 1784 |
let x' := eval compute in x in |
| 1785 |
change x with x'; clear x). |
| 1786 |
cbn [firstn skipn length map.putmany_of_list List.fold_left array_locs]. |
| 1787 |
repeat rewrite ?map.remove_put_same, ? map.remove_put_diff, ?map.remove_empty by (cbv;congruence). |
| 1788 |
compile_step. |
| 1789 |
} |
| 1790 |
} |
| 1791 |
{ |
| 1792 |
rewrite !map_length, !length_chunk, H5. |
| 1793 |
all: try lia. |
| 1794 |
repeat (set (Nat.div_up _ _) as x; |
| 1795 |
let x' := eval compute in x in |
| 1796 |
change x with x'; clear x). |
| 1797 |
cbn [firstn skipn length map.putmany_of_list List.fold_left array_locs]. |
| 1798 |
constructor. |
| 1799 |
{ repeat compile_step. } |
| 1800 |
intros. |
| 1801 |
eapply expr_load_word_of_byte_array; try eassumption. |
| 1802 |
{ |
| 1803 |
rewrite H8. |
| 1804 |
instantiate (1:= 3%nat). |
| 1805 |
reflexivity. |
| 1806 |
} |
| 1807 |
compile_step. |
| 1808 |
cbn [firstn skipn length map.putmany_of_list List.fold_left array_locs]. |
| 1809 |
repeat rewrite ?map.remove_put_same, ? map.remove_put_diff, ?map.remove_empty by (cbv;congruence). |
| 1810 |
repeat compile_step. |
| 1811 |
} |
| 1812 |
} |
| 1813 |
} |
| 1814 |
{ |
| 1815 |
repeat constructor; cbv; intuition congruence. |
| 1816 |
} |
| 1817 |
compile_step. |
| 1818 |
cbv [le_split]. |
| 1819 |
subst l0. |
| 1820 |
change (array_locs ?a ?b ?c) with (array_locs a b v1). |
| 1821 |
set (array_locs _ _ _) as l0. |
| 1822 |
let l' := eval cbn -[v1 combine] in l0 in change l0 with l'. |
| 1823 |
cbv [map.putmany_of_list gs]. |
| 1824 |
dedup "_gs_from0". |
| 1825 |
dedup "_gs_to0". |
| 1826 |
dedup "qv0". |
| 1827 |
dedup "qv1". |
| 1828 |
dedup "qv2". |
| 1829 |
dedup "qv3". |
| 1830 |
dedup "qv4". |
| 1831 |
dedup "qv5". |
| 1832 |
dedup "qv6". |
| 1833 |
dedup "qv7". |
| 1834 |
dedup "qv8". |
| 1835 |
dedup "qv9". |
| 1836 |
dedup "qv10". |
| 1837 |
dedup "qv11". |
| 1838 |
dedup "qv12". |
| 1839 |
dedup "qv13". |
| 1840 |
dedup "qv14". |
| 1841 |
dedup "qv15". |
| 1842 |
|
| 1843 |
|
| 1844 |
Optimize Proof. |
| 1845 |
Optimize Heap. |
| 1846 |
|
| 1847 |
(*TODO: earlier in derivation: why is key counted to 32? definitely wrong*) |
| 1848 |
(*TODO: do I need to eval the combine?*) |
| 1849 |
rewrite <- ListUtil.flat_map_map. |
| 1850 |
(*change (flat_map _ (map _ v1)) with (bytes_of_w32s v1).*) |
| 1851 |
|
| 1852 |
simple eapply compile_nlet_as_nlet_eq. |
| 1853 |
change (pred (let/n x as "st" eq:_ := _ in x)) |
| 1854 |
with (pred (let/n v1 as "st" eq:_ := v1 in |
| 1855 |
let/n x as "st" eq:_ := flat_map (LittleEndianList.le_split 4) (map word.unsigned v1) in x)). |
| 1856 |
simple eapply compile_store_locals_array. |
| 1857 |
{ |
| 1858 |
rewrite unroll_len with (l:=v1) (a:= word.of_Z 0) at 1. |
| 1859 |
replace (length v1) with 16%nat. |
| 1860 |
cbn [unroll app]. |
| 1861 |
{ |
| 1862 |
repeat constructor; repeat compile_step. |
| 1863 |
all: apply expr_compile_var; cbn [word word.rep Naive.gen_word word.of_Z]; |
| 1864 |
let v := lazymatch goal with |- _ = Some ?v => v end in |
| 1865 |
let k := lazymatch goal with |- map.get _ ?k = Some _ => k end in |
| 1866 |
lazymatch goal with |
| 1867 |
|- context [ map.put _ ?k' v] => |
| 1868 |
unify k k' |
| 1869 |
end; |
| 1870 |
eval_map_get. |
| 1871 |
} |
| 1872 |
{ |
| 1873 |
unfold v1. |
| 1874 |
rewrite !map_length, !combine_length. |
| 1875 |
cbn [length]. |
| 1876 |
rewrite !app_length, !map_length, !length_chunk. |
| 1877 |
cbn [length]. |
| 1878 |
rewrite ?app_length, ?map_length, ?length_chunk. |
| 1879 |
rewrite H5, H8. |
| 1880 |
reflexivity. |
| 1881 |
all: lia. |
| 1882 |
} |
| 1883 |
} |
| 1884 |
2:{ |
| 1885 |
eapply expr_compile_var. |
| 1886 |
eval_map_get. |
| 1887 |
} |
| 1888 |
2:{ |
| 1889 |
ecancel_assumption. |
| 1890 |
} |
| 1891 |
{ |
| 1892 |
rewrite H4. |
| 1893 |
unfold v1. |
| 1894 |
rewrite !map_length, !combine_length. |
| 1895 |
cbn [length]. |
| 1896 |
rewrite !app_length, !map_length, !length_chunk. |
| 1897 |
cbn [length]. |
| 1898 |
rewrite ?app_length, ?map_length, ?length_chunk. |
| 1899 |
rewrite H5, H8. |
| 1900 |
reflexivity. |
| 1901 |
all: lia. |
| 1902 |
} |
| 1903 |
compile_step. |
| 1904 |
unfold nlet_eq. |
| 1905 |
|
| 1906 |
(*TODO: compile_step takes too long (related to computations on maps?)*) |
| 1907 |
unshelve refine (compile_unsets _ _ _); [ shelve | intros | ]; cycle 1. |
| 1908 |
simple apply compile_skip. |
| 1909 |
2: exact []. |
| 1910 |
cbv beta delta [wp_bind_retvars pred]. |
| 1911 |
eexists; intuition eauto. |
| 1912 |
eexists; split. |
| 1913 |
pose proof (bytes_of_words (width:=32) v2 out) as H'. |
| 1914 |
cbn [id] in H'. |
| 1915 |
change (Z.of_nat (Memory.bytes_per (width:=32) access_size.word)) with 4 in H'. |
| 1916 |
seprewrite_in H' H7. |
| 1917 |
clear H'. |
| 1918 |
ecancel_assumption. |
| 1919 |
compile_step. |
| 1920 |
{ |
| 1921 |
rewrite ws2bs_length. |
| 1922 |
unfold v2, v1. |
| 1923 |
rewrite !map_length, !combine_length, !app_length. |
| 1924 |
cbn [length]. |
| 1925 |
rewrite !map_length, ! length_chunk. |
| 1926 |
rewrite H5,H8. |
| 1927 |
reflexivity. |
| 1928 |
all:lia. |
| 1929 |
} |
| 1930 |
auto. |
| 1931 |
intuition. |
| 1932 |
intuition. |
| 1933 |
Qed. |
| 1934 |
|
| 1935 |
|
| 1936 |
(* |
| 1937 |
|
| 1938 |
Local Open Scope string_scope. |
| 1939 |
Import Syntax Syntax.Coercions NotationsCustomEntry. |
| 1940 |
Import ListNotations. |
| 1941 |
Import Coq.Init.Byte. |
| 1942 |
Set Printing Depth 150. |
| 1943 |
Compute chacha20_block. |
| 1944 |
Print Assumptions chacha20_block_body_correct. |
| 1945 |
*) |
| 1946 |
|
| 1947 |
End Bedrock2. |