Debian Codegraph @

src/Bedrock/End2End/X25519/GarageDoor.v

raw
1 From Coq Require Import String.
2 From Coq Require Import List.
3 From Coq Require Import ZArith.
4 Require Import Crypto.Spec.Curve25519.
5 Require Import bedrock2.Map.Separation.
6 Require Import bedrock2.Syntax.
7 Require Import compiler.Pipeline.
8 Require Import compiler.Symbols.
9 Require Import compiler.MMIO.
10 Require Import coqutil.Word.Bitwidth32.
11 Require Import Crypto.Arithmetic.PrimeFieldTheorems.
12 Require Crypto.Bedrock.End2End.RupicolaCrypto.ChaCha20.
13 Require Crypto.Bedrock.End2End.RupicolaCrypto.Spec.
14 Require Import Crypto.Bedrock.Field.Synthesis.New.UnsaturatedSolinas.
15 Require Import Crypto.Bedrock.Group.AdditionChains.
16 Require Import Crypto.Bedrock.Group.ScalarMult.LadderStep.
17 Require Import Crypto.Bedrock.Group.ScalarMult.CSwap.
18 Require Import Crypto.Bedrock.Group.ScalarMult.MontgomeryLadder.
19 Require Import Crypto.Bedrock.End2End.X25519.Field25519.
20 Require Import Crypto.Bedrock.End2End.X25519.MontgomeryLadder.
21 Require Import bedrock2Examples.LAN9250.
22 Require Import bedrock2Examples.lightbulb.
23 Require Import bedrock2Examples.memequal.
24 Require Import bedrock2Examples.memswap.
25 Require Import bedrock2Examples.memconst.
26 Require Import Rupicola.Examples.Net.IPChecksum.IPChecksum.
27
28 Local Open Scope string_scope.
29 Import Syntax Syntax.Coercions NotationsCustomEntry.
30 Import ListNotations.
31 Import Coq.Init.Byte.
32 Import Tuple LittleEndianList.
33 Local Coercion F.to_Z : F >-> Z.
34
35 Definition garageowner : list byte :=
36 [x7b; x06; x18; x0c; x54; x0c; xca; x9f; xa3; x16; x0b; x2f; x2b; x69; x89; x63; x77; x4c; xc1; xef; xdc; x04; x91; x46; x76; x8b; xb2; xbf; x43; x0e; x34; x34].
37
38 Definition garageowner_P : Curve25519.M.point.
39 refine (
40 let x := F.of_Z _ (le_combine garageowner) in
41 let y2 := (x*x*x + Curve25519.M.a*x*x +x)%F in
42 let sqrtm1 := (F.pow (F.of_Z _ 2) ((N.pos p-1)/4)) in
43 let y := F.sqrt_5mod8 sqrtm1 y2 in
44 exist _ (inl (x, y)) _).
45 Decidable.vm_decide.
46 Defined.
47
48 Lemma garageowner_P_correct : le_split 32 (Curve25519.M.X0 garageowner_P) = garageowner.
49 Proof. vm_compute. reflexivity. Qed.
50
51 Local Notation ST := 0x80000000.
52 Local Notation PK := 0x80000040.
53 Local Notation BUF:= 0x80000060.
54
55 Definition initfn := func! {
56 memconst_pk($PK);
57 output! MMIOWRITE($0x10012038, $(Z.lor (Z.shiftl (0xf) 2) (Z.shiftl 1 9)));
58 output! MMIOWRITE($0x10012008, $(Z.lor (Z.shiftl 1 11) (Z.shiftl 1 12)));
59 output! MMIOWRITE($0x10024010, $2);
60 unpack! err = lan9250_init()
61 }.
62
63 Definition loopfn := func! {
64 st=$ST; pk=$PK; buf=$BUF;
65
66 unpack! pktlen, err = recvEthernet(buf);
67 require !err;
68 require ($63 < pktlen);
69
70 ethertype = load1(buf + $12) << $8 | load1(buf + $13);
71 require ($1535 < ethertype);
72 protocol = load1(buf+$23);
73 require (protocol == $0x11);
74
75 if $(14+20+8 +2+32 +4) == pktlen { (* getpk *)
76 memswap(buf, buf+$6, $6); (* ethernet address *)
77 memswap(buf+$(14+12), buf+$(14+16), $4); (* IP address *)
78 memswap(buf+$(14+20+0), buf+$(14+20+2), $2); (* UDP port *)
79 store1(buf+$(14+2), $0); (* ip length *)
80 store1(buf+$(14+3), $(20+ 8+ 32+2)); (* ip length *)
81 store1(buf+$(14+10), $0); (* preliminary ip checksum *)
82 store1(buf+$(14+11), $0); (* preliminary ip checksum *)
83
84 unpack! chk = ip_checksum(buf+$14, $20);
85 store1(buf+$(14+11), chk>>$8);
86 store1(buf+$(14+10), chk);
87
88 store1(buf+$(14+20+4), $0); (* udp length *)
89 store1(buf+$(14+20+5), $(8+ 32+2)); (* udp length *)
90 store1(buf+$(14+20+6), $0); (* udp checksum *)
91 store1(buf+$(14+20+7), $0); (* udp checksum *)
92
93 x25519_base(buf+$(14+20+8 +2), st+$32);
94 unpack! err = lan9250_tx(buf, $(14+20+8 +2+32))
95 } else if $(14+20+8 +2+16 +4) == pktlen { (* operate *)
96 stackalloc 32 as tmp;
97 x25519(tmp, st+$32, pk);
98 unpack! set0 = memequal(tmp, buf+$(14+20+8 +2), $16);
99 unpack! set1 = memequal(tmp+$16, buf+$(14+20+8 +2), $16);
100
101 io! mmio_val = MMIOREAD($0x1001200c);
102 mmio_val = mmio_val & coq:(Z.clearbit (Z.clearbit (2^32-1) 11) 12);
103 output! MMIOWRITE($0x1001200c, mmio_val | (set1<<$1 | set0) << $11);
104
105 if (set0|set1) { (* rekey *)
106 chacha20_block(st, st, (*nonce*)pk) (* NOTE: another impl? *)
107 }
108 }
109 }.
110
111 Import TracePredicate TracePredicateNotations SPI lightbulb_spec.
112 Notation OP := (lightbulb_spec.OP _).
113 Definition iocfg : list OP -> Prop :=
114 one ("st", word.of_Z (0x10012038), word.of_Z (Z.lor (Z.shiftl (0xf) 2) (Z.shiftl 1 9))) +++
115 one ("st", word.of_Z (0x10012008), word.of_Z (Z.lor (Z.shiftl 1 11) (Z.shiftl 1 12))) +++
116 one ("st", word.of_Z (0x10024010), word.of_Z 2).
117 Definition BootSeq : list OP -> Prop :=
118 iocfg +++ (lan9250_init_trace _
119 ||| lan9250_boot_timeout _
120 ||| (any+++spi_timeout _)).
121
122 Import LeakageSemantics LeakageWeakestPrecondition LeakageProgramLogic SeparationLogic.
123 Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing) (* experiment*).
124 Local Notation "xs $@ a" := (Array.array ptsto (word.of_Z 1) a xs) (at level 10, format "xs $@ a").
125
126 Ltac fwd :=
127 repeat match goal with
128 | |- _ /\ _ => split
129 | |- exists _, _ => eexists
130 | _ => straightline
131 end; trivial.
132 Ltac slv := try solve [ trivial | ecancel_assumption | SepAutoArray.listZnWords | intuition idtac | reflexivity | eassumption].
133
134 Section WithPickSp.
135 Context {pick_sp: PickSp}.
136
137 Global Instance spec_of_initfn : spec_of "initfn" :=
138 fnspec! "initfn" / bs R,
139 { requires k t m := m =* bs $@(word.of_Z PK) * R /\ length bs = 32%nat;
140 ensures k' t' m' := m' =* garageowner$@(word.of_Z PK) * R /\
141 exists iol, t' = iol ++ t /\
142 exists ioh, mmio_trace_abstraction_relation ioh iol /\
143 BootSeq ioh }.
144
145 Local Instance spec_of_memconst_pk : spec_of "memconst_pk" := spec_of_memconst "memconst_pk" garageowner.
146 Local Instance WHY_spec_of_lan9250_init : spec_of "lan9250_init" := spec_of_lan9250_init.
147 Lemma initfn_ok : program_logic_goal_for_function! initfn.
148 Proof.
149 repeat straightline.
150 straightline_call.
151 { ssplit. eassumption. rewrite H2. all : vm_compute; trivial. }
152 repeat straightline.
153 eapply LeakageWeakestPreconditionProperties.interact_nomem; repeat straightline.
154 eexists; fwd; slv.
155 { vm_compute. intuition congruence. }
156 eapply LeakageWeakestPreconditionProperties.interact_nomem; repeat straightline.
157 eexists; fwd; slv.
158 { vm_compute. intuition congruence. }
159 eapply LeakageWeakestPreconditionProperties.interact_nomem; repeat straightline.
160 eexists; fwd; slv.
161 { vm_compute. intuition congruence. }
162 straightline_call; repeat straightline; fwd.
163 { repeat (eapply align_trace_cons || exact (eq_sym (List.app_nil_l _)) || eapply align_trace_app). }
164 { repeat (eapply List.Forall2_cons || eapply List.Forall2_refl || eapply List.Forall2_app; eauto). all: cbv[mmio_event_abstraction_relation]; eauto. }
165 cbv [BootSeq ].
166 eapply TracePredicate.concat_app.
167 2: solve[cbv [choice]; intuition eauto].
168 cbv [iocfg].
169 eapply (TracePredicate.concat_app _ _ [_;_] [_]); [|cbv [one]; trivial].
170 eapply (TracePredicate.concat_app _ _ [_] [_]); cbv [one]; trivial.
171 Qed.
172
173 Local Open Scope list_scope.
174 Local Definition be2 z := rev (le_split 2 z).
175 Local Coercion to_list : tuple >-> list.
176 Local Coercion Z.b2z : bool >-> Z.
177
178 Record state := { prng_state : list byte ; x25519_ephemeral_secret : list byte }.
179
180 Local Notation MMIO := (string * word.rep * word.rep)%type.
181 Goal True.
182 pose (lightbulb_spec.lan9250_writepacket Naive.word32 :
183 list Init.Byte.byte -> list MMIO -> Prop).
184 Abort.
185
186 Definition protocol_step : state -> list MMIO -> state -> Prop :=
187 fun '(Build_state seed x25519_ephemeral_secret) ioh '(Build_state SEED SK) =>
188 (lightbulb_spec.lan9250_recv_no_packet _ ioh \/
189 lightbulb_spec.lan9250_recv_packet_too_long _ ioh \/
190 TracePredicate.concat TracePredicate.any (lightbulb_spec.spi_timeout _) ioh) /\ SEED=seed /\ SK=x25519_ephemeral_secret \/
191 (exists incoming, lightbulb_spec.lan9250_recv _ incoming ioh /\
192 let ethertype := le_combine (rev (firstn 2 (skipn 12 incoming))) in ethertype < 1536 \/
193 let ipproto := nth 23 incoming x00 in (ipproto <> x11 \/
194 length incoming <> 14+20+8 +2+16 +4 /\ length incoming <> 14+20+8 +2+32 +4)%nat) /\ SEED=seed /\ SK=x25519_ephemeral_secret \/
195 exists (mac_local mac_remote : tuple byte 6),
196 exists (ethertype : Z) (ih_const : tuple byte 2) (ip_length : Z) (ip_idff : tuple byte 5),
197 exists (ipproto := x11) (ip_checksum : Z) (ip_local ip_remote : tuple byte 4),
198 exists (udp_local udp_remote : tuple byte 2) (udp_length : Z) (udp_checksum : Z),
199 exists (garagedoor_header : tuple byte 2) (garagedoor_payload : list byte),
200 let incoming : list byte :=
201 (mac_local ++ mac_remote ++ be2 ethertype ++
202 ih_const ++ be2 ip_length ++
203 ip_idff ++ [ipproto] ++ le_split 2 ip_checksum ++
204 ip_remote ++ ip_local ++
205 udp_remote ++ udp_local ++
206 be2 udp_length ++ be2 udp_checksum ++
207 garagedoor_header ++ garagedoor_payload) in
208 (exists doorstate action : Naive.word32,
209 TracePredicate.concat
210 (lightbulb_spec.lan9250_recv _ incoming) (TracePredicate.concat
211 (TracePredicate.one ("ld", lightbulb_spec.GPIO_DATA_ADDR _, doorstate))
212 (TracePredicate.one ("st", lightbulb_spec.GPIO_DATA_ADDR _, action))) ioh
213 /\ (
214 let m := firstn 16 garagedoor_payload in
215 let v := x25519_spec x25519_ephemeral_secret garageowner_P in
216 exists set0 set1 : Naive.word32,
217 (word.unsigned set0 = 1 <-> firstn 16 v = m) /\
218 (word.unsigned set1 = 1 <-> skipn 16 v = m) /\
219 action = word.or (word.and doorstate (word.of_Z (Z.clearbit (Z.clearbit (2^32-1) 11) 12))) (word.slu (word.or (word.slu set1 (word.of_Z 1)) set0) (word.of_Z 11)) /\
220 (word.unsigned (word.or set0 set1) = 0 -> SEED=seed /\ SK=x25519_ephemeral_secret) /\
221 (word.unsigned (word.or set0 set1) <> 0 -> SEED++SK = RupicolaCrypto.Spec.chacha20_block seed (ChaCha20.le_split 4 (word.of_Z 0) ++ firstn 12 garageowner))
222 )) \/
223 TracePredicate.concat (lightbulb_spec.lan9250_recv _ incoming)
224 (lightbulb_spec.lan9250_send _
225 (let ip_length := 62 in
226 let udp_length := 42 in
227
228 mac_remote ++ mac_local ++ be2 ethertype ++
229 let ip_hdr checksum := ih_const ++ be2 ip_length ++
230 ip_idff ++ [ipproto] ++ le_split 2 checksum ++
231 ip_local ++ ip_remote in
232 ip_hdr (IPChecksum.Spec.ip_checksum (ip_hdr 0)) ++
233 udp_local ++ udp_remote ++ be2 udp_length ++ be2 0 ++
234 garagedoor_header ++
235 x25519_spec x25519_ephemeral_secret Curve25519.M.B
236
237 )) ioh /\ SEED=seed /\ SK=x25519_ephemeral_secret.
238
239 Local Instance spec_of_recvEthernet : spec_of "recvEthernet" := spec_of_recvEthernet.
240 Local Instance spec_of_lan9250_tx : spec_of "lan9250_tx" := spec_of_lan9250_tx.
241 Local Instance spec_of_memswap : spec_of "memswap" := spec_of_memswap.
242 Local Instance spec_of_memequal : spec_of "memequal" := spec_of_memequal.
243
244
245 Definition memrep bs R : state -> map.rep(map:=SortedListWord.map _ _) -> Prop := fun '(Build_state seed sk) m =>
246 m =*
247 seed$@(word.of_Z ST) *
248 sk$@(word.add (word.of_Z ST) (word.of_Z 32)) *
249 garageowner$@(word.of_Z PK) *
250 bs $@(word.of_Z BUF) * R /\
251 length seed = 32%nat /\
252 length sk = 32%nat /\
253 length bs = 1520%nat.
254
255 Global Instance spec_of_loopfn : spec_of "loopfn" :=
256 fnspec! "loopfn" / seed sk bs R,
257 { requires k t m := memrep bs R (Build_state seed sk) m;
258 ensures K T M := exists SEED SK BS, memrep BS R (Build_state SEED SK) M /\
259 exists iol, T = iol ++ t /\
260 exists ioh, SPI.mmio_trace_abstraction_relation ioh iol /\
261 protocol_step (Build_state seed sk) ioh (Build_state SEED SK) }.
262
263 Import ZnWords.
264 Import coqutil.Tactics.autoforward.
265
266 Import Crypto.Util.FixCoqMistakes.
267
268 Local Existing Instance ChaCha20.spec_of_chacha20.
269
270 Lemma loopfn_ok : program_logic_goal_for_function! loopfn.
271 Proof.
272 straightline.
273 cbv [memrep protocol_step] in *.
274 repeat straightline.
275 rename H11 into Lseed. rename H12 into Lsk. rename H13 into H11.
276 straightline_call; try ecancel_assumption; trivial; repeat straightline.
277 intuition idtac; repeat straightline;
278 eexists; eexists; split; repeat straightline; split; intros; try contradiction; [|]; repeat straightline.
279 2: fwd; slv.
280
281 pose proof H12 as Hbuf.
282 seprewrite_in @bytearray_index_merge Hbuf. { ZnWords. }
283
284 eexists; eexists; split; repeat straightline.
285 rewrite word.unsigned_ltu, ?word.unsigned_of_Z_nowrap by ZnWords.ZnWords;
286 destr Z.ltb; rewrite ?word.unsigned_of_Z_0, ?word.unsigned_of_Z_1; intuition try discriminate;
287 autoforward with typeclass_instances in E.
288 2: { fwd; slv. right. left. fwd; slv; intuition try ZnWords. }
289
290 repeat straightline.
291 eapply LeakageWeakestPreconditionProperties.dexpr_expr.
292 eexists; split; repeat straightline.
293
294 case (SepAutoArray.list_expose_nth x3 12 ltac:(ZnWords)) as (Hpp&Lpp).
295 forget (List.firstn 12 x3) as mac.
296 forget (nth 12 x3 Inhabited.default) as ethertype_hi.
297 forget (List.skipn 13 x3) as pp.
298 subst x3.
299 repeat seprewrite_in @Array.bytearray_append H12; cbn [Array.array] in H12.
300 rewrite ?app_length in *; cbn [length] in *; rewrite ?Lpp in *.
301 change (Z.of_nat 12) with 12 in *.
302
303 repeat straightline.
304
305 destruct pp as [|ethertype_lo pp]; cbn [length app Array.array] in *.
306 { exfalso; ZnWords. }
307 Import SetEvars coqutil.Tactics.eplace Word.Naive LittleEndianList.
308 eplace (word.add (word.add buf _) _) with (word.add buf _) in H12 by (ring_simplify; trivial).
309 repeat straightline.
310
311 let v := match goal with l := map.put _ "ethertype" ?v |- _ => v end in
312 remember v as ethertype in *;
313 assert (word.unsigned ethertype = le_combine [ethertype_lo; ethertype_hi]);
314 [ rewrite Heqethertype | ]; clear Heqethertype.
315 { pose proof byte.unsigned_range ethertype_hi.
316 pose proof byte.unsigned_range ethertype_lo.
317 subst_words.
318 cbn [le_combine]. rewrite ?Z.shiftl_0_l, ?Z.lor_0_r.
319 rewrite_strat (bottomup (terms word.unsigned_or_nowrap word.unsigned_and_nowrap word.unsigned_of_Z word.unsigned_slu)).
320 2: ZnWords.
321 cbv [word.wrap]; (rewrite_strat (bottomup (terms (Zmod_small)))); try ZnWords.ZnWords.
322 { rewrite Z.lor_comm; reflexivity. } }
323
324 eexists; eexists; split; repeat straightline.
325 rewrite word.unsigned_ltu, ?word.unsigned_of_Z_nowrap by ZnWords.ZnWords;
326 destr Z.ltb; rewrite ?word.unsigned_of_Z_0, ?word.unsigned_of_Z_1; intuition try discriminate;
327 autoforward with typeclass_instances in E0.
328 2: {
329 fwd; slv; [].
330 right. left. ssplit; trivial; []. eexists; split; try eassumption; trivial. left.
331 rewrite skipn_app, skipn_all2, ?Lpp, ?app_nil_l by ZnWords.
332 cbn [List.skipn minus firstn List.firstn List.app rev]. ZnWords. }
333
334 case (SepAutoArray.list_expose_nth pp 9 ltac:(ZnWords)) as (Hppp&Lppp).
335 forget (List.firstn 9 pp) as ih_l.
336 forget (nth 9 pp Inhabited.default) as ipproto.
337 forget (List.skipn 10 pp) as ppp.
338 subst pp.
339 repeat seprewrite_in @Array.bytearray_append H12; cbn [Array.array] in H12.
340 rewrite ?app_length in *; cbn [length] in *; rewrite ?Lppp in *.
341 change (Z.of_nat 9) with 9 in *.
342 change (Z.of_nat 1) with 1 in *.
343
344 repeat eplace (word.add (word.add buf _) _) with (word.add buf _) in H12 by (ring_simplify; trivial).
345
346 repeat straightline.
347 eexists; eexists; split; repeat straightline.
348 subst protocol.
349 pose proof byte.unsigned_range ipproto.
350 rewrite word.unsigned_eqb, ?word.unsigned_and_nowrap, ?word.unsigned_of_Z_nowrap by ZnWords.ZnWords.
351 destr Z.eqb; rewrite ?word.unsigned_of_Z_0, ?word.unsigned_of_Z_1; intuition try discriminate; autoforward with typeclass_instances in E1.
352 2: {
353 fwd; slv; [].
354 right. left. ssplit; trivial; []. eexists. ssplit; try eassumption. right. left.
355 rewrite app_nth2 by ZnWords.
356 rewrite app_comm_cons.
357 rewrite app_nth2 by SepAutoArray.listZnWords.
358 rewrite app_nth2 by SepAutoArray.listZnWords.
359 rewrite app_nth1 by SepAutoArray.listZnWords.
360 match goal with |- context[nth ?x] => replace x with O by SepAutoArray.listZnWords end.
361 cbn. intro. subst. apply E1. reflexivity. }
362
363 repeat straightline.
364 eexists; eexists; split; repeat straightline.
365 rewrite word.unsigned_eqb, ?word.unsigned_of_Z_nowrap by ZnWords.ZnWords.
366 destr Z.eqb; rewrite ?word.unsigned_of_Z_0, ?word.unsigned_of_Z_1; intuition try discriminate; autoforward with typeclass_instances in E2.
367
368 2: {
369 repeat straightline.
370 eexists; eexists; split; repeat straightline.
371 rewrite word.unsigned_eqb, ?word.unsigned_of_Z_nowrap by ZnWords.ZnWords.
372 destr Z.eqb; rewrite ?word.unsigned_of_Z_0, ?word.unsigned_of_Z_1; intuition try discriminate; autoforward with typeclass_instances in E3.
373 2: {
374 fwd; slv.
375 right. left. ssplit; trivial; []. eexists. ssplit; try eassumption. right. right. SepAutoArray.listZnWords. }
376 repeat straightline.
377 straightline_call; ssplit; try ecancel_assumption; try trivial; try ZnWords.
378 { cbv. inversion 1. }
379 { instantiate (1:=garageowner_P). Decidable.vm_decide. }
380
381 rename Lppp into Lihl; assert (List.length ppp = 40)%nat as Lppp by ZnWords.
382
383 repeat straightline.
384 pose proof (List.firstn_skipn (14+20+8 +2 - 12-2-9-1) ppp) as HH.
385 pose proof (@firstn_length_le _ ppp (14+20+8 +2 - 12-2-9-1) ltac:(ZnWords)).
386 pose proof skipn_length (14+20+8 +2 - 12-2-9-1) ppp; rewrite Lppp in *.
387 forget (List.firstn (14+20+8 +2 - 12-2-9-1) ppp) as pPP.
388 forget (List.skipn (14+20+8 +2 - 12-2-9-1) ppp) as pPPP.
389 simpl minus in H31, H32.
390 subst ppp.
391 repeat rewrite ?(app_assoc _ _ pPPP), ?app_comm_cons in H33.
392 do 3 (seprewrite_in @Array.bytearray_append H33; cbn [Array.array] in H33).
393
394 change (unsigned x) with (@word.unsigned _ word32 x) in H32.
395 repeat straightline.
396 pose proof (List.firstn_skipn 16 pPPP) as HH.
397 pose proof (@firstn_length_le _ pPPP 16 ltac:(ZnWords)).
398 pose proof skipn_length 16 pPPP. rewrite H32 in *.
399 forget (List.firstn 16 pPPP) as cmp1.
400 forget (List.skipn 16 pPPP) as trailer.
401 subst pPPP.
402 seprewrite_in_by (Array.bytearray_append cmp1) H33 SepAutoArray.listZnWords.
403
404 remember (x25519_spec sk garageowner_P) as vv.
405 repeat straightline.
406 pose proof (List.firstn_skipn 16 vv) as Hvv.
407 pose proof (@firstn_length_le _ vv 16 ltac:(subst vv; rewrite length_x25519_spec; ZnWords)).
408 pose proof skipn_length 16 vv.
409 forget (List.firstn 16 vv) as vv0.
410 forget (List.skipn 16 vv) as vv1.
411 subst vv.
412 rewrite <-Hvv in H33.
413 rewrite length_x25519_spec in *.
414 seprewrite_in_by (Array.bytearray_append vv0) H33 SepAutoArray.listZnWords.
415
416 repeat straightline.
417 repeat rewrite <- app_assoc in * || cbn [List.app] in *.
418 straightline_call; ssplit.
419 { ecancel_assumption. }
420 { use_sep_assumption. cancel. cancel_seps_at_indices 2%nat 0%nat.
421 { Morphisms.f_equiv. SepAutoArray.listZnWords. }
422 ecancel_done. }
423 { ZnWords. }
424 { ZnWords. }
425 repeat straightline.
426
427 repeat straightline.
428 straightline_call; ssplit.
429 { use_sep_assumption. cancel. cancel_seps_at_indices 1%nat 0%nat.
430 { Morphisms.f_equiv. SepAutoArray.listZnWords. }
431 ecancel_done. }
432 { use_sep_assumption. cancel. cancel_seps_at_indices 2%nat 0%nat.
433 { Morphisms.f_equiv. SepAutoArray.listZnWords. }
434 ecancel_done. }
435 { ZnWords. }
436 { ZnWords. }
437
438 repeat straightline.
439 eexists. eexists. split. repeat straightline.
440 eexists _, _; split; [eapply map.split_empty_r; reflexivity|].
441 eexists; ssplit; trivial.
442 { cbv. clear. intuition congruence. }
443
444 repeat straightline.
445 eexists. eexists. split. repeat straightline.
446 eexists _, _; split; [eapply map.split_empty_r; reflexivity|].
447 eexists; ssplit; trivial.
448 eexists; ssplit; trivial.
449 { cbv. clear. intuition congruence. }
450 repeat straightline.
451
452 eapply map.split_empty_r in H38; destruct H38.
453 eapply map.split_empty_r in H41; destruct H41.
454 seprewrite_in_by @bytearray_index_merge H33 SepAutoArray.listZnWords.
455 seprewrite_in_by @bytearray_index_merge H33 SepAutoArray.listZnWords.
456 seprewrite_in_by @bytearray_index_merge H33 SepAutoArray.listZnWords.
457 seprewrite_in_by @bytearray_index_merge H33 SepAutoArray.listZnWords.
458 seprewrite_in_by @bytearray_index_merge H33 SepAutoArray.listZnWords.
459 assert (length (vv0 ++ vv1) = 32%nat) by SepAutoArray.listZnWords.
460
461
462 change (word.of_Z 134217728) with st in H33.
463 repeat straightline.
464 eexists; eexists; ssplit; repeat straightline.
465 { (* chacha20 *)
466 (* NOTE: viewing the same memory in two different ways, ++ combined and split *)
467 straightline_call; ssplit.
468 { seprewrite_in_by @bytearray_index_merge H33 SepAutoArray.listZnWords.
469 ecancel_assumption. }
470 { SepAutoArray.listZnWords. }
471 { ecancel_assumption. }
472 { SepAutoArray.listZnWords. }
473 { rewrite <-(List.firstn_skipn 12 garageowner) in H33.
474 seprewrite_in_by (Array.bytearray_append (List.firstn 12 garageowner)) H33 SepAutoArray.listZnWords.
475 ecancel_assumption. }
476 { SepAutoArray.listZnWords. }
477 repeat straightline.
478
479 rewrite <-(List.firstn_skipn 32 x6) in H48.
480 seprewrite_in_by (Array.bytearray_append (List.firstn 32 x6)) H48 SepAutoArray.listZnWords.
481 replace (word.of_Z (BinInt.Z.of_nat (Datatypes.length (List.firstn 32 x6)))) with (word.of_Z 32 : word32) in * by SepAutoArray.listZnWords.
482
483 ssplit; trivial.
484 eexists _, _, _; ssplit; try ecancel_assumption; try SepAutoArray.listZnWords.
485
486 eexists; ssplit.
487 { subst a7 a0. change (?x::?y::?t) with ([x;y]++t). rewrite app_assoc. trivial. }
488 eexists; ssplit.
489 { eapply Forall2_app; try eassumption.
490 eapply Forall2_cons. 2:eapply Forall2_cons. 3:eapply Forall2_nil.
491 all:[>left|right]; eexists _, _; ssplit; trivial. }
492 right.
493 right.
494 eexists _, _, _, _, _, _, _, _, _, _, _, _, _, _, _. left; ssplit; trivial.
495 eexists _, _.
496 split.
497 {
498 eapply TracePredicate.concat_app.
499 1: match goal with |- _ ?x _ => eplace x with _ end; [|eassumption].
500
501 symmetry. (* NOTE: systematic list cancellation *)
502 rewrite <-(firstn_skipn 6 mac) at 1; rewrite <-?app_assoc.
503 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
504 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
505 change (?x::?y::?z) with ([x;y]++z).
506 eapply (f_equal2 app).
507 { cbv [be2]. progress change 2%nat with (List.length [ethertype_lo; ethertype_hi]).
508 setoid_rewrite split_le_combine; trivial. }
509 rewrite <-(firstn_skipn 2 ih_l) at 1; rewrite <-?app_assoc.
510 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
511 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
512 eapply (f_equal2 app).
513 { cbv [be2]. eplace 2%nat with _ at 3; cycle 1.
514 rewrite split_le_combine, rev_involutive; trivial.
515 rewrite rev_length. SepAutoArray.listZnWords. }
516 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
517 eapply (f_equal2 cons). { f_equal. eapply byte.unsigned_inj. rewrite E1. trivial. }
518 rewrite <-(firstn_skipn 2 pPP) at 1; rewrite <-?app_assoc.
519 eapply (f_equal2 app).
520 { eplace 2%nat with _ at 2; cycle 1.
521 rewrite split_le_combine; trivial.
522 SepAutoArray.listZnWords. }
523 rewrite <-(firstn_skipn 4 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
524 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
525 rewrite <-(firstn_skipn 4 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
526 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
527 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
528 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
529 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
530 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
531 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
532 eapply (f_equal2 app).
533 { cbv [be2]. eplace 2%nat with _ at 7; cycle 1.
534 rewrite split_le_combine, rev_involutive; trivial.
535 rewrite rev_length. SepAutoArray.listZnWords. }
536 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
537 eapply (f_equal2 app).
538 { cbv [be2]. eplace 2%nat with _ at 8; cycle 1.
539 rewrite split_le_combine, rev_involutive; trivial.
540 rewrite rev_length. SepAutoArray.listZnWords. }
541 eapply (f_equal2 app). { rewrite to_list_from_list; trivial. }
542 reflexivity.
543
544 change [?x;?y] with ([x]++[y]).
545 eapply TracePredicate.concat_app; cbv [TracePredicate.one]; f_equal. }
546
547 rewrite <-Hvv.
548 rewrite !ListUtil.firstn_app_sharp by ZnWords.
549 rewrite !ListUtil.skipn_app_sharp by ZnWords.
550 eexists _, _; ssplit; try eassumption; subst mmio_val; eauto.
551
552 all : rewrite ?firstn_skipn; intuition auto.
553 (* end chacha20*) }
554
555 Optimize Proof. Optimize Heap.
556
557 ssplit; trivial.
558 eexists _, _, _; ssplit; try ecancel_assumption; try SepAutoArray.listZnWords.
559 eexists; ssplit.
560 { subst a0. change (?x::?y::?t) with ([x;y]++t). rewrite app_assoc. trivial. }
561 eexists; ssplit.
562 { eapply Forall2_app; try eassumption.
563 eapply Forall2_cons. 2:eapply Forall2_cons. 3:eapply Forall2_nil.
564 all:[>left|right]; eexists _, _; ssplit; trivial. }
565 right.
566 right.
567 eexists _, _, _, _, _, _, _, _, _, _, _, _, _, _, _. left; ssplit; trivial.
568 eexists _, _.
569 split.
570 {
571 eapply TracePredicate.concat_app.
572 1: match goal with |- _ ?x _ => eplace x with _ end; [|eassumption].
573
574 symmetry. (* NOTE: systematic list cancellation *)
575 rewrite <-(firstn_skipn 6 mac) at 1; rewrite <-?app_assoc.
576 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
577 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
578 change (?x::?y::?z) with ([x;y]++z).
579 eapply (f_equal2 app).
580 { cbv [be2]. progress change 2%nat with (List.length [ethertype_lo; ethertype_hi]).
581 setoid_rewrite split_le_combine; trivial. }
582 rewrite <-(firstn_skipn 2 ih_l) at 1; rewrite <-?app_assoc.
583 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
584 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
585 eapply (f_equal2 app).
586 { cbv [be2]. eplace 2%nat with _ at 3; cycle 1.
587 rewrite split_le_combine, rev_involutive; trivial.
588 rewrite rev_length. SepAutoArray.listZnWords. }
589 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
590 eapply (f_equal2 cons). { f_equal. eapply byte.unsigned_inj. rewrite E1. trivial. }
591 rewrite <-(firstn_skipn 2 pPP) at 1; rewrite <-?app_assoc.
592 eapply (f_equal2 app).
593 { eplace 2%nat with _ at 2; cycle 1.
594 rewrite split_le_combine; trivial.
595 SepAutoArray.listZnWords. }
596 rewrite <-(firstn_skipn 4 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
597 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
598 rewrite <-(firstn_skipn 4 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
599 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
600 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
601 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
602 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
603 eapply (f_equal2 app); [rewrite to_list_from_list; trivial|].
604 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
605 eapply (f_equal2 app).
606 { cbv [be2]. eplace 2%nat with _ at 7; cycle 1.
607 rewrite split_le_combine, rev_involutive; trivial.
608 rewrite rev_length. SepAutoArray.listZnWords. }
609 rewrite <-(firstn_skipn 2 (List.skipn _ _)) at 1; rewrite <-?app_assoc, ?List.skipn_skipn.
610 eapply (f_equal2 app).
611 { cbv [be2]. eplace 2%nat with _ at 8; cycle 1.
612 rewrite split_le_combine, rev_involutive; trivial.
613 rewrite rev_length. SepAutoArray.listZnWords. }
614 eapply (f_equal2 app). { rewrite to_list_from_list; trivial. }
615 reflexivity.
616
617 change [?x;?y] with ([x]++[y]).
618 eapply TracePredicate.concat_app; cbv [TracePredicate.one]; f_equal. }
619
620 rewrite <-Hvv.
621 rewrite !ListUtil.firstn_app_sharp by ZnWords.
622 rewrite !ListUtil.skipn_app_sharp by ZnWords.
623 eexists _, _; ssplit; try eassumption; subst mmio_val; eauto; congruence.
624 }
625
626 {
627 repeat straightline.
628 pose proof (List.firstn_skipn 6 mac) as HH.
629 pose proof (@firstn_length_le _ mac 6 ltac:(ZnWords)).
630 pose proof skipn_length 6 mac; rewrite ?Lpp in *.
631 forget (List.firstn 6 mac) as mac_local.
632 forget (List.skipn 6 mac) as mac_remote.
633 subst mac.
634 repeat seprewrite_in @Array.bytearray_append H12; cbn [Array.array] in H12.
635 rewrite H26 in *.
636 change (Z.of_nat 6) with 6 in *.
637
638 straightline_call; ssplit; try ecancel_assumption; try ZnWords.
639
640 repeat straightline.
641
642 pose proof (List.firstn_skipn 2 ppp) as HH.
643 pose proof (@firstn_length_le _ ppp 2 ltac:(ZnWords)).
644 pose proof skipn_length 2 ppp as Lip_checksum.
645 forget (List.firstn 2 ppp) as ip_checksum.
646 forget (List.skipn 2 ppp) as pppp.
647 subst ppp; rewrite ?app_length in *.
648 repeat seprewrite_in @Array.bytearray_append H29; cbn [Array.array] in H29.
649 rewrite ?H28 in *.
650 change (Z.of_nat 24) with 24 in *.
651 change (Z.of_nat 2) with 2 in *.
652 eplace (word.add (word.add buf _) _) with (word.add buf _) in H29 by (ring_simplify; trivial).
653
654 pose proof (List.firstn_skipn 4 pppp) as HH.
655 pose proof (@firstn_length_le _ pppp 4 ltac:(ZnWords)).
656 forget (List.firstn 4 pppp) as ip_remote.
657 forget (List.skipn 4 pppp) as ppppp.
658 subst pppp; rewrite ?app_length in *.
659 repeat seprewrite_in @Array.bytearray_append H29; cbn [Array.array] in H29.
660 rewrite ?H30 in *.
661 change (Z.of_nat 28) with 28 in *.
662 change (Z.of_nat 4) with 4 in *.
663 eplace (word.add (word.add buf _) _) with (word.add buf _) in H29 by (ring_simplify; trivial).
664
665 pose proof (List.firstn_skipn 4 ppppp) as HH.
666 pose proof (@firstn_length_le _ ppppp 4 ltac:(ZnWords)).
667 forget (List.firstn 4 ppppp) as ip_local.
668 forget(List.skipn 4 ppppp) as pppppp.
669 subst ppppp; rewrite ?app_length in *.
670 repeat seprewrite_in @Array.bytearray_append H29; cbn [Array.array] in H29.
671 rewrite ?H31 in *.
672 change (Z.of_nat 30) with 30 in *.
673 change (Z.of_nat 4) with 4 in *.
674 eplace (word.add (word.add buf _) _) with (word.add buf _) in H29 by (ring_simplify; trivial).
675
676 straightline_call; ssplit; try ecancel_assumption; try ZnWords.
677
678 repeat straightline.
679
680 pose proof (List.firstn_skipn 2 pppppp) as HH.
681 pose proof (@firstn_length_le _ pppppp 2 ltac:(ZnWords)).
682 forget (List.firstn 2 pppppp) as udp_remote.
683 forget(List.skipn 2 pppppp) as pP.
684 subst pppppp; rewrite ?app_length in *.
685 repeat seprewrite_in @Array.bytearray_append H33; cbn [Array.array] in H33.
686 rewrite ?H32 in *.
687 change (Z.of_nat 34) with 34 in *.
688 change (Z.of_nat 4) with 4 in *.
689 eplace (word.add (word.add buf _) _) with (word.add buf _) in H33 by (ring_simplify; trivial).
690
691 pose proof (List.firstn_skipn 2 pP) as HH.
692 pose proof (@firstn_length_le _ pP 2 ltac:(ZnWords)).
693 forget (List.firstn 2 pP) as udp_local.
694 forget(List.skipn 2 pP) as pPP.
695 subst pP; rewrite ?app_length in *.
696 repeat seprewrite_in @Array.bytearray_append H33; cbn [Array.array] in H33.
697 rewrite ?H34 in *.
698 change (Z.of_nat 36) with 36 in *.
699 change (Z.of_nat 4) with 4 in *.
700 eplace (word.add (word.add buf _) _) with (word.add buf _) in H33 by (ring_simplify; trivial).
701
702 straightline_call; ssplit; try ecancel_assumption; try ZnWords.
703
704 repeat straightline.
705
706 case (SepAutoArray.list_expose_nth ih_l 2 ltac:(ZnWords)) as (HH&LL).
707 forget (List.firstn 2 ih_l) as ih_const.
708 forget (nth 2 ih_l Inhabited.default) as ip_length_hi.
709 forget (List.skipn 3 ih_l) as ip_length_lo.
710 subst ih_l.
711 repeat seprewrite_in @Array.bytearray_append H36; cbn [Array.array] in H36.
712 rewrite ?app_length in *; cbn [length] in *; rewrite ?LL in *.
713 change (Z.of_nat 1) with 1 in *.
714 change (Z.of_nat 2) with 2 in *.
715 eplace (word.add (word.add buf _) _) with (word.add buf _) in H36 by (ring_simplify; trivial).
716 eplace (word.add (word.add buf _) _) with (word.add buf _) in H36 by (ring_simplify; trivial).
717
718 repeat straightline.
719
720 destruct ip_length_lo as [|ip_length_lo ip_idff ].
721 { cbn [length] in *. exfalso; Lia.lia. }
722 cbn [Array.array] in *.
723 eplace (word.add (word.add buf _) _) with (word.add buf _) in H35 by (ring_simplify; trivial).
724
725 repeat straightline.
726 destruct ip_checksum as [|ip_checksum_0 [|ip_checksum_1 [|] ] ];
727 try (cbn [length] in *; discriminate); cbn [Array.array] in *.
728 eplace (word.add (word.add buf _) _) with (word.add buf _) in H37 by (ring_simplify; trivial).
729
730 straightline.
731 straightline.
732 straightline.
733 straightline.
734 straightline.
735 straightline.
736 straightline.
737 repeat straightline.
738 assert (ptsto_to_array :
739 forall {width : Z} {word : Interface.word width},
740 word.ok word ->
741 forall mem : map.map (word.rep(word:=word)) Byte.byte,
742 map.ok mem -> forall a b,
743 Lift1Prop.iff1 (ptsto a b) (Array.array(mem:=mem) ptsto (word.of_Z 1) a [b])).
744 { cbn [Array.array]. intros. cancel. }
745
746 assert (bytearray_address_merge :
747 forall {width : Z} {word : Interface.word width},
748 word.ok word ->
749 forall mem : map.map (word.rep(word:=word)) Byte.byte,
750 map.ok mem ->
751 forall (xs ys : list byte) (start b : word.rep),
752 word.unsigned (word.sub b start) = Z.of_nat (Datatypes.length xs) ->
753 Lift1Prop.iff1 (xs$@start ⋆ ys$@b) ((xs ++ ys)$@start : mem -> Prop)).
754 { intros.
755 replace b with (word.add start (word.sub b start)).
756 { eapply Array.bytearray_index_merge; trivial. }
757 eapply word.unsigned_inj. rewrite ?word.unsigned_add, ?word.unsigned_sub.
758 cbv [word.wrap].
759 rewrite Zplus_mod_idemp_r.
760 transitivity (word.unsigned b mod 2^width).
761 { f_equal. ring. }
762 rewrite Z.mod_small; trivial; eapply word.unsigned_range. }
763
764 Optimize Proof. Optimize Heap.
765
766 repeat seprewrite_in @ptsto_to_array H39.
767 rewrite ?word.unsigned_of_Z_nowrap in H39 by ZnWords.
768 seprewrite_in_by (@bytearray_address_merge _ _ _ _ _ ih_const) H39 ZnWords.
769 seprewrite_in_by (fun x=>@bytearray_address_merge _ _ _ _ _ (ih_const++x)%list) H39 SepAutoArray.listZnWords.
770 rewrite <-app_assoc in H39.
771 seprewrite_in_by (fun x=>@bytearray_address_merge _ _ _ _ _ (ih_const++x)%list) H39 SepAutoArray.listZnWords.
772 rewrite <-!app_assoc in H39.
773 cbn [length] in *.
774 seprewrite_in_by (fun x=>@bytearray_address_merge _ _ _ _ _ (ih_const++x)%list) H39 SepAutoArray.listZnWords.
775 rewrite <-!app_assoc in H39.
776 seprewrite_in_by (fun x=>@bytearray_address_merge _ _ _ _ _ (ih_const++x)%list) H39 SepAutoArray.listZnWords.
777 rewrite <-!app_assoc in H39.
778 seprewrite_in_by (fun x=>@bytearray_address_merge _ _ _ _ _ (ih_const++x)%list) H39 SepAutoArray.listZnWords.
779 rewrite <-!app_assoc in H39.
780 seprewrite_in_by (fun x=>@bytearray_address_merge _ _ _ _ _ (ih_const++x)%list) H39 SepAutoArray.listZnWords.
781 rewrite <-!app_assoc in H39.
782 seprewrite_in_by (fun x=>@bytearray_address_merge _ _ _ _ _ (ih_const++x)%list) H39 SepAutoArray.listZnWords.
783
784 straightline_call; [(ssplit; cycle -1)|];
785 cbv [Arrays.listarray_value Arrays.ai_repr Arrays._access_info Arrays.ai_width Arrays.ai_size Arrays.ai_type Memory.bytes_per] in *;
786 change (Z.of_nat 1) with 1%Z in *;
787 try ecancel_assumption;
788 try SepAutoArray.listZnWords.
789
790 Optimize Proof. Optimize Heap.
791
792 repeat straightline.
793
794 repeat match goal with x := word.of_Z 0 |- _ => subst x end.
795 repeat match goal with x := word.of_Z 62 |- _ => subst x end.
796 rewrite !word.unsigned_of_Z_nowrap in H42 by Lia.lia.
797 progress replace ((ih_const ++ [byte.of_Z 0] ++ [byte.of_Z 62] ++ ip_idff ++ [ipproto] ++ [byte.of_Z 0] ++ [byte.of_Z 0] ++ ip_local) ++ ip_remote)%list
798 with ((ih_const ++ [byte.of_Z 0] ++ [byte.of_Z 62] ++ ip_idff ++ [ipproto]) ++ [byte.of_Z 0] ++ [byte.of_Z 0] ++ ip_local ++ ip_remote)%list
799 in * by (rewrite ?app_assoc; trivial).
800 seprewrite_in @Array.bytearray_append H42.
801 seprewrite_in (@Array.bytearray_append _ _ _ _ _ [byte.of_Z 0]) H42.
802 seprewrite_in (@Array.bytearray_append _ _ _ _ _ [byte.of_Z 0]) H42.
803 rewrite ?app_length in H42; cbn [length] in H42; rewrite ?LL in H42.
804 replace (Datatypes.length ip_idff) with 5%nat in H42 by ZnWords.
805 cbn [plus] in H42.
806 repeat eplace (word.add (word.add buf _) _) with (word.add buf _) in H42 by (ring_simplify; trivial).
807
808 Import symmetry.
809 seprewrite_in (symmetry! (fun a=>@ptsto_to_array _ _ _ _ _ a (byte.of_Z 0))) H42.
810 seprewrite_in (symmetry! (fun a=>@ptsto_to_array _ _ _ _ _ a (byte.of_Z 0))) H42.
811
812 repeat straightline.
813 match goal with H : ?P ?m |- LeakageWeakestPrecondition.store _ ?m _ _ _ => revert H end.
814 repeat match goal with H : sep _ _ _ |- _ => clear H end.
815 repeat straightline.
816
817 do 6 (destruct pPP as [|? pPP]; (cbn [Datatypes.length] in *; try Lia.lia)).
818 set ((b :: b0 :: b1 :: b2 :: b3 :: b4 :: pPP)$@(word.add buf (word.of_Z 38))) as X in H40.
819 cbn [Array.array] in X. subst X.
820 repeat eplace (word.add (word.add buf _) _) with (word.add buf _) in H40 by (ring_simplify; trivial).
821
822 repeat straightline.
823 pose proof (List.firstn_skipn 32 pPP) as HH.
824 pose proof (@firstn_length_le _ pPP 32 ltac:(ZnWords)).
825 forget (List.firstn 32 pPP) as _pkpad.
826 forget(List.skipn 32 pPP) as pPPP.
827 subst pPP; rewrite ?app_length in *.
828 repeat seprewrite_in (@Array.bytearray_append _ _ _ _ _ _pkpad) H29.
829 rewrite ?H33 in *.
830 change (Z.of_nat 32) with 32 in *.
831 repeat eplace (word.add (word.add buf _) _) with (word.add buf _) in H29 by (ring_simplify; trivial).
832 straightline_call; ssplit; try ecancel_assumption; trivial.
833
834 Optimize Proof. Optimize Heap.
835
836 repeat straightline.
837
838 revert H37.
839 repeat match goal with H : sep _ _ _ |- _ => clear H end.
840 intros.
841 repeat seprewrite_in @ptsto_to_array H37.
842 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 ZnWords.
843 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
844 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
845 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
846 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
847 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
848 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
849 Optimize Proof. Optimize Heap.
850 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
851 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
852 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
853 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
854 Optimize Proof. Optimize Heap.
855 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
856 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
857 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
858 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
859 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
860 Optimize Proof. Optimize Heap.
861
862 rename x3 into ipchk.
863 match goal with x := word.sru ipchk _ |- _ => subst x end.
864 progress rewrite ?word.unsigned_sru_nowrap, ?word.unsigned_of_Z_nowrap in H37 by ZnWords.
865
866 straightline_call; [ssplit; cycle -1|]; try ecancel_assumption.
867 { rewrite ?app_length, ?length_x25519_spec. SepAutoArray.listZnWords. }
868 { ZnWords. }
869
870 pose proof length_x25519_spec sk Curve25519.M.B as Hpkl.
871 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
872 seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
873
874 repeat straightline.
875 destruct H35; repeat straightline; [intuition idtac | ].
876 { eexists; ssplit. eexists _, _; ssplit; slv.
877 eexists; ssplit. subst a18. subst a0. rewrite app_assoc. reflexivity.
878 eexists; ssplit. eapply Forall2_app; eassumption.
879 intuition eauto using TracePredicate.any_app_more. }
880
881 ssplit; trivial.
882 eexists _, _, _; ssplit; slv.
883 eexists; ssplit. subst a18. subst a0. rewrite app_assoc. reflexivity.
884 eexists; ssplit. eapply Forall2_app; eassumption.
885 right. right.
886
887 exists (from_list 6 mac_local ltac:(assumption)); rewrite to_list_from_list.
888 exists (from_list 6 mac_remote ltac:(assumption)); rewrite to_list_from_list.
889 cbv beta delta [be2].
890 exists (le_combine [ethertype_lo; ethertype_hi]); rewrite split_le_combine' by trivial.
891 exists (from_list 2 ih_const ltac:(assumption)); rewrite to_list_from_list.
892 exists (le_combine [ip_length_lo; ip_length_hi]); rewrite split_le_combine' by trivial.
893 exists (from_list 5 ip_idff ltac:(ZnWords)); rewrite to_list_from_list.
894 exists (le_combine [ip_checksum_0; ip_checksum_1]); rewrite split_le_combine' by trivial.
895 exists (from_list 4 ip_local ltac:(assumption)); rewrite to_list_from_list.
896 exists (from_list 4 ip_remote ltac:(assumption)); rewrite to_list_from_list.
897 exists (from_list 2 udp_local ltac:(assumption)); rewrite to_list_from_list.
898 exists (from_list 2 udp_remote ltac:(assumption)); rewrite to_list_from_list.
899 exists (le_combine [b0; b]); rewrite split_le_combine' by trivial.
900 exists (le_combine [b2; b1]); rewrite split_le_combine' by trivial.
901 exists (from_list 2 [b3;b4] eq_refl); rewrite to_list_from_list.
902 exists (_pkpad ++ pPPP).
903
904 assert (x11 = ipproto) as Hp. { eapply byte.unsigned_inj. rewrite E1. trivial. }
905 destruct Hp.
906
907 right.
908 ssplit; trivial.
909 eapply TracePredicate.concat_app.
910 all : match goal with |- _ ?x _ => eplace x with _; try eassumption; [] end.
911 assert (app_singleton_l : forall {A} (x:A) xs, [x] ++ xs = x :: xs) by (intros; reflexivity).
912 2 : simpl le_split at 1.
913 all : cbn [rev].
914 all : repeat (rewrite <-?app_assoc, ?app_nil_l, ?app_singleton_l, <-?app_comm_cons).
915 all : trivial; repeat (f_equal; []).
916
917 match goal with c := Impl.ip_checksum_impl ?y |- context[Spec.ip_checksum ?x] =>
918 progress rewrite <-(Impl.ip_checksum_impl_ok' y : _ = word.unsigned c) in *;
919 progress replace x with y in * end.
920 2: {
921 repeat (rewrite <-?app_assoc, ?app_nil_l, ?app_singleton_l, <-?app_comm_cons).
922 trivial. }
923 remember (Spec.ip_checksum _) as chk.
924 unfold le_split at 1.
925 repeat (rewrite <-?app_assoc, ?app_nil_l, ?app_singleton_l, <-?app_comm_cons).
926 trivial. }
927
928 Unshelve.
929 all : try SepAutoArray.listZnWords.
930 Qed.
931 End WithPickSp.