Debian Codegraph @

src/Assembly/WithBedrock/SymbolicProofs.v

raw
1 From Coq Require Import List.
2 From Coq Require Import Lia.
3 Require Import Crypto.Util.ZUtil.Tactics.PullPush.
4 From Coq Require Import NArith.
5 From Coq Require Import ZArith.
6 Require Import Crypto.Util.ZUtil.Testbit.
7 Require Import Crypto.AbstractInterpretation.ZRange.
8 Require Import Crypto.Util.ErrorT.
9 Import Coq.Lists.List. (* [map] is [List.map] not [ErrorT.map] *)
10 Require Import Crypto.Util.ListUtil.IndexOf.
11 Require Import Crypto.Util.Tactics.WarnIfGoalsRemain.
12 Require Import Crypto.Util.ZUtil.Definitions.
13 Require Crypto.Util.Option.
14 Require Import Crypto.Assembly.Syntax.
15 Require Import Crypto.Assembly.Symbolic.
16 Require Import Crypto.Assembly.WithBedrock.Semantics.
17 Require Import Crypto.Assembly.Equivalence.
18 Import Sorting.Permutation.
19
20 Require Import bedrock2.Map.Separation.
21 Require Import bedrock2.Map.SeparationLogic.
22 Require Import bedrock2.Memory. Import coqutil.Map.Memory.
23 Require Import coqutil.Map.Interface. (* coercions *)
24 Require Import coqutil.Word.Interface.
25 Require Import coqutil.Word.LittleEndianList.
26 Import Word.Naive.
27
28 Section Memory.
29 (* bedrock2/src/bedrock2/Memory.v Section WithoutTuples *)
30 Import Word.Properties Word.Interface Coq.Init.Byte coqutil.Map.OfListWord Map.Properties coqutil.Tactics.Tactics.
31 Context {width: Z} {word: word width} {mem: map.map word byte}.
32 Context {mem_ok: map.ok mem} {word_ok: word.ok word}.
33 Local Infix "$+" := map.putmany (at level 70).
34 Local Notation "xs $@ a" := (map.of_list_word_at a xs) (at level 10, format "xs $@ a").
35 Local Notation unchecked_store_bytes := (unchecked_store_bytes (mem:=mem) (word:=word)).
36 Lemma unchecked_store_bytes_unchecked_store_bytes m a bs1 bs2 :
37 length bs1 = length bs2 ->
38 unchecked_store_bytes (unchecked_store_bytes m a bs1) a bs2 =
39 unchecked_store_bytes m a bs2.
40 Proof using mem_ok word_ok.
41 cbv [unchecked_store_bytes]; intros.
42 eapply map.map_ext; intros.
43 rewrite !map.get_putmany_dec, !map.get_of_list_word_at;
44 repeat (destruct_one_match; trivial).
45 epose proof proj1 (List.nth_error_Some bs1 (BinInt.Z.to_nat (word.unsigned (word.sub k a)))) ltac:(congruence).
46 rewrite H in H0.
47 eapply List.nth_error_Some in E; intuition idtac.
48 Qed.
49
50 (* not sure where to put this since depends on sep *)
51 Import Map.Interface Word.Interface BinInt.
52 Local Coercion Z.of_nat : nat >-> Z.
53 Local Coercion word.unsigned : word.rep >-> Z.
54
55 Lemma unchecked_store_bytes_of_sep
56 m a bs1 bs2 R (Hsep : sep R (bs1$@a) m)
57 (Hlen : length bs1 = length bs2)
58 : sep R (bs2$@a) (unchecked_store_bytes m a bs2).
59 Proof using mem_ok word_ok.
60 destruct Hsep as (?&?&(?&Hd)&HR&?);
61 cbv [sepclause_of_map] in *; subst.
62 setoid_rewrite unchecked_store_bytes_unchecked_store_bytes; trivial.
63 eexists _, _; split; split; try exact HR; try exact eq_refl.
64 cbv [map.disjoint] in *; intros k v1 v2 Hv1 Hv2.
65 pose proof map.get_of_list_word_at_domain a bs1 k as HA.
66 pose proof map.get_of_list_word_at_domain a bs2 k as HB.
67 rewrite Hlen, <-HB in HA; clear HB.
68 destruct (map.get (bs2$@a) k) in *; try congruence.
69 pose proof proj2 HA ltac:(congruence).
70 destruct (map.get (bs1$@a) k) eqn:? in *; try congruence.
71 eauto.
72 Qed.
73 End Memory.
74
75
76 Require Import Crypto.Util.Prod.
77 From Crypto.Util.Tactics Require Import BreakMatch DestructHead UniquePose.
78 Require Import Crypto.Util.Bool.Reflect.
79 Require Import Crypto.Util.ZUtil.Ones.
80 Import coqutil.Tactics.autoforward coqutil.Decidable coqutil.Tactics.Tactics.
81
82 Local Coercion ExprRef : idx >-> expr.
83
84 Section WithFrame.
85 Context (frame : mem_state -> Prop).
86
87 Section WithCtx1.
88 Context (G : symbol -> option Z).
89 Local Notation eval := (Symbolic.eval G).
90 Local Notation gensym_dag_ok := (Symbolic.gensym_dag_ok G).
91 Section WithDag.
92 Context (d : dag).
93 Local Notation eval := (Symbolic.eval G d).
94
95 Definition R_reg (x : option idx) (v : Z) : Prop :=
96 (forall i, x = Some i -> eval i v) /\ (v = Z.land v (Z.ones 64)).
97 Definition R_regs : Symbolic.reg_state -> Semantics.reg_state -> Prop :=
98 Tuple.fieldwise R_reg.
99
100 Definition R_flag (x : option idx) (ob : option bool) : Prop :=
101 forall i, x = Some i -> exists b, eval i (Z.b2z b) /\ ob = Some b.
102 Definition R_flags : Symbolic.flag_state -> Semantics.flag_state -> Prop :=
103 Tuple.fieldwise R_flag.
104
105 Definition R_cell64 (ia iv : idx) : mem_state -> Prop :=
106 Lift1Prop.ex1 (fun a =>
107 Lift1Prop.ex1 (fun bs => sep (emp (
108 eval ia (word.unsigned a) /\
109 length bs = 8%nat /\ eval iv (le_combine bs)))
110 (eq (OfListWord.map.of_list_word_at a bs)))).
111
112 Fixpoint R_mem (sm : Symbolic.mem_state) : mem_state -> Prop :=
113 match sm with
114 | nil => frame
115 | cons (ia, iv) sm' => sep (R_cell64 ia iv) (R_mem sm')
116 end.
117 End WithDag.
118
119 Definition R (ss : symbolic_state) (ms : machine_state) : Prop :=
120 let (mr, mf, mm) := ms in
121 let (d, sr, sf, sm) := ss in
122 gensym_dag_ok d /\ R_regs d sr mr /\ R_flags d sf mf /\ R_mem d sm mm.
123
124 Lemma gensym_dag_ok_of_R ss ms : R ss ms -> gensym_dag_ok ss.
125 Proof using Type. cbv [R]; break_innermost_match; intuition. Qed.
126
127 Lemma R_flag_None_l d f : R_flag d None f.
128 Proof using Type. inversion 1. Qed.
129 Lemma R_flag_None_r d f : autoforward.autoforward (R_flag d f None) (f = None).
130 Proof using Type.
131 destruct f; cbv; trivial. intros H.
132 case (H _ eq_refl) as (?&?&HX); inversion HX.
133 Qed.
134
135 Local Hint Resolve gensym_dag_ok_of_R : core.
136 Local Hint Resolve R_flag_None_l : core.
137 Local Hint Resolve R_flag_None_r : typeclass_instances.
138
139 Lemma get_flag_R s m f (HR : R s m) :
140 forall i, Symbolic.get_flag s f = Some i ->
141 exists b, eval s i (Z.b2z b) /\
142 Semantics.get_flag m f = Some b.
143 Proof using Type.
144 repeat (
145 DestructHead.destruct_head @prod||
146 DestructHead.destruct_head @Semantics.machine_state||
147 DestructHead.destruct_head @Semantics.flag_state||
148 DestructHead.destruct_head @Symbolic.symbolic_state||
149 DestructHead.destruct_head @Symbolic.flag_state||
150 DestructHead.destruct_head @FLAG
151 );
152 cbn [R_flags Tuple.fieldwise Tuple.fieldwise' fst snd] in *; intuition idtac.
153 Qed.
154
155 Lemma R_set_flag_internal s m f (HR : R s m) (i:idx) b (Hi : eval s i (Z.b2z b)) :
156 R_flags s (Symbolic.set_flag s f i) (Semantics.set_flag m f b).
157 Proof using Type.
158 repeat (
159 DestructHead.destruct_head @prod||
160 DestructHead.destruct_head @Semantics.machine_state||
161 DestructHead.destruct_head @Semantics.flag_state||
162 DestructHead.destruct_head @Symbolic.symbolic_state||
163 DestructHead.destruct_head @Symbolic.flag_state||
164 DestructHead.destruct_head @FLAG
165 );
166 cbv [R_flags Tuple.fieldwise Tuple.fieldwise' fst snd] in *; intuition idtac;
167 cbv [R_flag]; intros ? HH; inversion HH; clear HH; subst; eauto.
168 Qed.
169 End WithCtx1.
170 Local Hint Resolve R_flag_None_l : core.
171 Local Hint Resolve R_flag_None_r : typeclass_instances.
172
173 Local Arguments Tuple.tuple : simpl never.
174 Local Arguments Tuple.fieldwise : simpl never.
175 Local Arguments Tuple.from_list_default : simpl never.
176 Local Arguments Tuple.to_list : simpl never.
177 Local Arguments Tuple.nth_default ! _ _.
178 Local Arguments set_reg ! _ _.
179 Local Arguments Syntax.operation_size ! _.
180 Local Arguments N.modulo !_ !_.
181 Local Arguments N.add !_ !_.
182 Local Arguments N.b2n ! _.
183 Local Arguments N.ones ! _.
184 Local Arguments N.land !_ !_.
185 Local Arguments N.lor !_ !_.
186 Local Arguments N.shiftl !_ !_.
187 Local Arguments N.shiftr !_ !_.
188
189 Local Arguments Z.modulo !_ !_.
190 Local Arguments Z.add !_ !_.
191 Local Arguments Z.b2z ! _.
192 Local Arguments Z.ones : simpl never.
193 Local Arguments Z.land !_ !_.
194 Local Arguments Z.lor !_ !_.
195 Local Arguments Z.shiftl !_ !_.
196 Local Arguments Z.shiftr !_ !_.
197
198 Section WithCtx2.
199 Context (G G' : symbol -> option Z).
200 Local Notation eval := (Symbolic.eval G).
201 Local Notation gensym_dag_ok := (Symbolic.gensym_dag_ok G).
202 Local Notation eval' := (Symbolic.eval G').
203 Local Notation gensym_dag_ok' := (Symbolic.gensym_dag_ok G').
204
205 Local Notation R_reg' := (R_reg G').
206 Local Notation R_reg := (R_reg G).
207 Local Notation R_regs' := (R_regs G').
208 Local Notation R_regs := (R_regs G).
209 Local Notation R_flag' := (R_flag G').
210 Local Notation R_flag := (R_flag G).
211 Local Notation R_flags' := (R_flags G').
212 Local Notation R_flags := (R_flags G).
213 Local Notation R_cell64' := (R_cell64 G').
214 Local Notation R_cell64 := (R_cell64 G).
215 Local Notation R_mem' := (R_mem G').
216 Local Notation R_mem := (R_mem G).
217 Local Notation R' := (R G').
218 Local Notation R := (R G).
219
220 Notation subsumed d1 d2 := (forall i v, eval d1 i v -> eval' d2 i v).
221 Local Infix ":<" := subsumed (at level 70, no associativity).
222
223 Lemma R_flag_subsumed d s m (HR : R_flag d s m) d' (Hlt : d :< d')
224 : R_flag' d' s m.
225 Proof using Type.
226 cbv [R_flag] in *; intros.
227 case (HR i H) as (?&?&?); subst; eauto.
228 Qed.
229
230 Lemma R_flags_subsumed d s m (HR : R_flags d s m) d' (Hlt : d :< d')
231 : R_flags' d' s m.
232 Proof using Type.
233 cbv [R_flags Tuple.fieldwise Tuple.fieldwise'] in *;
234 intuition eauto using R_flag_subsumed.
235 Qed.
236
237 Lemma R_reg_subsumed d s m (HR : R_reg d s m) d' (Hlt : d :< d')
238 : R_reg' d' s m.
239 Proof using Type. cbv [R_reg] in *; intuition eauto. Qed.
240
241 Lemma R_regs_subsumed d s m (HR : R_regs d s m) d' (Hlt : d :< d')
242 : R_regs' d' s m.
243 Proof using Type.
244 cbv [R_regs Tuple.fieldwise Tuple.fieldwise'] in *;
245 intuition eauto using R_reg_subsumed.
246 Qed.
247
248 Local Existing Instance Naive.word64_ok.
249 Local Existing Instance SortedListWord.ok.
250
251 Lemma R_cell64_subsumed d i i0 d' (Hd' : d :< d') m :
252 R_cell64 d i i0 m -> R_cell64' d' i i0 m.
253 Proof using Type.
254 clear frame; intros (?&?&?&(?&?&?&?)).
255 eexists _, _, _, _; split; cbv [emp] in *; intuition eauto.
256 Qed.
257
258 Lemma R_mem_subsumed d s m (HR : R_mem d s m) d' (Hlt : d :< d')
259 : R_mem' d' s m.
260 Proof using Type.
261 revert dependent m; induction s; cbn; break_match; intuition idtac.
262 eapply SeparationLogic.Proper_sep_impl1; try eassumption.
263 intro; eauto using R_cell64_subsumed.
264 Unshelve.
265 { refine (SortedListWord.ok _ _). }
266 Qed.
267
268 Lemma R_subsumed s m (HR : R s m) d' (Hd' : gensym_dag_ok' d') (Hlt : s :< d')
269 (s' := update_dag_with s (fun _ => d')) : R' s' m.
270 Proof using Type.
271 destruct s, m; case HR as (Hd&Hr&Hf&Hm);
272 cbv [update_dag_with] in *; cbn in *;
273 intuition eauto using R_flags_subsumed, R_regs_subsumed, R_mem_subsumed.
274 Qed.
275 End WithCtx2.
276
277 Section WithCtx1'.
278 Context (G : symbol -> option Z).
279 Local Notation eval := (Symbolic.eval G).
280 Local Notation gensym_dag_ok := (Symbolic.gensym_dag_ok G).
281 Local Notation R_reg := (R_reg G).
282 Local Notation R_regs := (R_regs G).
283 Local Notation R_flag := (R_flag G).
284 Local Notation R_flags := (R_flags G).
285 Local Notation R_cell64 := (R_cell64 G).
286 Local Notation R_mem := (R_mem G).
287 Local Notation R := (R G).
288
289 Notation subsumed d1 d2 := (forall i v, eval d1 i v -> eval d2 i v).
290 Local Infix ":<" := subsumed (at level 70, no associativity).
291
292 Local Existing Instance Naive.word64_ok.
293 Local Existing Instance SortedListWord.ok.
294
295 Lemma R_mem_Permutation d s1 m (HR : R_mem d s1 m) s2
296 (HP : Permutation s1 s2) : R_mem d s2 m.
297 Proof using Type.
298 revert dependent m. induction HP; cbn; break_match; intros; eauto.
299 { eapply SeparationLogic.Proper_sep_impl1; cbv [Lift1Prop.impl1]; eauto. }
300 cbv [mem_state] in *.
301 refine (Lift1Prop.subrelation_iff1_impl1 _ _ _ _ _ HR); clear HR.
302 SeparationLogic.cancel.
303 SeparationLogic.cancel_seps_at_indices 0%nat 1%nat; try exact _.
304 { refine (SortedListWord.ok _ _). }
305 2: { epose proof Properties.word.eqb_spec. exact H. }
306 trivial.
307 reflexivity.
308 Unshelve.
309 { refine (SortedListWord.ok _ _). }
310 { refine (SortedListWord.ok _ _). }
311 2: { epose proof Properties.word.eqb_spec. exact H. }
312 Qed.
313
314 Lemma get_reg_R_regs d s m (HR : R_regs d s m) ri :
315 forall i, Symbolic.get_reg s ri = Some i ->
316 exists v, eval d i v /\ Tuple.nth_default 0 (N.to_nat ri) m = v.
317 Proof using Type.
318 cbv [Symbolic.get_reg]; intros.
319 rewrite <-Tuple.nth_default_to_list in H.
320 cbv [nth_default] in H; BreakMatch.break_match_hyps; subst; [|solve[congruence] ].
321 destruct s,m; cbn in *; cbv [R_regs R_reg] in *.
322 eapply Tuple.fieldwise_to_list_iff in HR.
323 eapply Forall.Forall2_forall_iff_nth_error in HR; cbv [Crypto.Util.Option.option_eq] in HR.
324
325 rewrite Heqo in HR.
326 BreakMatch.break_match_hyps; [|solve[contradiction]].
327 specialize (proj1 HR _ eq_refl).
328 eexists; split; [eassumption|].
329
330 rewrite <-Tuple.nth_default_to_list.
331 cbv [nth_default].
332 rewrite Heqo0.
333 trivial.
334 Qed.
335
336 Lemma get_reg_R s m (HR : R s m) ri :
337 forall i, Symbolic.get_reg s ri = Some i ->
338 exists v, eval s i v /\ Tuple.nth_default 0 (N.to_nat ri) (m : reg_state) = v.
339 Proof using Type.
340 destruct s, m; apply get_reg_R_regs, HR.
341 Qed.
342
343 Lemma bind_assoc {A B C} (v:M A) (k1:A->M B) (k2:B->M C) s
344 : (y <- (x <- v; k1 x); k2 y)%x86symex s = (x<-v; y<-k1 x; k2 y)%x86symex s.
345 Proof using Type. cbv; destruct v; trivial. Qed.
346
347 (* workaround: using cbn instead of this lemma makes Qed hang after next rewrite in same hyp *)
348 Lemma unfold_bind {A B} ma amb s :
349 @bind A B ma amb s = ltac:(let t := eval unfold bind, ErrorT.bind in (@bind A B ma amb s) in exact t).
350 Proof using Type. exact eq_refl. Qed.
351
352 Local Hint Resolve gensym_dag_ok_of_R : core.
353
354 Ltac step_symex0 :=
355 match goal with
356 | H : (x <- ?v; ?k)%x86symex ?s = _ |- _ =>
357 rewrite !bind_assoc in H
358 | H : (x <- ?v; ?k)%x86symex ?s = Success _ |- _ =>
359 rewrite unfold_bind in H;
360 match type of H with (* workaround: rewrite sometimes reduces the match *)
361 | match _ with _ => _ end = Success _ =>
362 let x := fresh x in
363 let HH := fresh "HS" x in
364 destruct v as [[x ?]|] eqn:HH in H;
365 [|solve[exfalso;clear-H;inversion H]]
366 | _ => idtac
367 end
368 end.
369 Ltac step_symex := step_symex0.
370
371 Lemma GetFlag_R s m (HR : R s m) f i s' (H : GetFlag f s = Success (i, s')) :
372 exists b, eval s i (Z.b2z b) /\ Semantics.get_flag m f = Some b /\ s = s'.
373 Proof using Type.
374 cbv [GetFlag some_or] in H.
375 destruct (Symbolic.get_flag _ _) eqn:? in H;
376 ErrorT.inversion_ErrorT; Prod.inversion_prod; subst; [].
377 eapply get_flag_R in Heqo; eauto; destruct Heqo as (b&Hb&Hf); eauto.
378 Qed.
379
380 Ltac step_GetFlag :=
381 match goal with
382 | H : GetFlag ?f ?s = Success (?i0, _) |- _ =>
383 let v := fresh "v" f in let Hv := fresh "H" v in let Hvf := fresh "Hv" f in
384 let Hi := fresh "H" i0 in let Heq := fresh H "eq" in
385 case (GetFlag_R s _ ltac:(eassumption) _ _ _ H) as (v&Hi&Hvf&Heq); clear H;
386 (let i := fresh "i" f in try rename i0 into i);
387 (try match type of Heq with _ = ?s' => subst s' end)
388 end.
389 Ltac step_symex1 := first [step_symex0 | step_GetFlag].
390 Ltac step_symex ::= step_symex1.
391
392 Lemma Merge_R {descr:description} s m (HR : R s m) e v (H : eval s e v) :
393 forall i s', Merge e s = Success (i, s') ->
394 R s' m /\ s :< s' /\ eval s' i v.
395 Proof using Type.
396 cbv [Merge].
397 inversion 1; subst; match goal with H: ?x = ?x |- _ => clear H end.
398 destruct s, m.
399 case (eval_merge _ e _ dag_state (proj1 HR) H) as (He'&Hd'&Hes).
400 intuition eauto using R_subsumed.
401 Qed.
402
403 Ltac step_Merge :=
404 match goal with
405 | H : Merge ?e ?s = Success (?i, ?s') |- _ =>
406 let v := open_constr:(_) in let Hi := fresh "H" i in
407 let Hs' := fresh "H" s' in let Hl := fresh "Hl" s' in
408 let t := open_constr:(fun A B => Merge_R s _ A e v B _ _ H) in
409 unshelve (edestruct t as (Hs'&Hl&Hi); clear H); shelve_unifiable;
410 [eassumption|..]
411 end.
412 Ltac step_symex2 := first [step_symex1 | step_Merge].
413 Ltac step_symex ::= step_symex2.
414
415 Lemma App_R {opts : symbolic_options_computed_opt} {descr:description} s m (HR : R s m) e v (H : eval_node G s e v) :
416 forall i s', Symbolic.App e s = Success (i, s') ->
417 R s' m /\ s :< s' /\ eval s' i v.
418 Proof using Type.
419 cbv [Symbolic.App]; intros.
420 eapply Merge_R in H0; intuition eauto using eval_simplify.
421 Qed.
422
423 Ltac step_App :=
424 match goal with
425 | H : Symbolic.App ?n ?s = Success (?i, ?s') |- _ =>
426 (*let v := fresh "v" i in*) let Hi := fresh "H" i in (* fresh does not generate fresh evar names *)
427 let Hl := fresh "Hl" s' in let Hs' := fresh "H" s' in
428 let t := open_constr:(fun A B => App_R s _ A n _(*?[v]*) B _ _ H) in
429 unshelve (edestruct t as (Hs'&Hl&Hi); clear H); shelve_unifiable;
430 [eassumption|..]
431 end.
432 Ltac step_symex3 := first [step_symex2 | step_App].
433 Ltac step_symex ::= step_symex3.
434
435 Ltac eval_same_expr_goal :=
436 match goal with
437 | |- eval ?d ?e ?v =>
438 match goal with
439 H : eval d e ?v' |- _ =>
440 let Heq := fresh in
441 enough (Heq : v = v') by (rewrite Heq; exact H);
442 try clear H e
443 end
444 | |- eval ?d ?e ?v =>
445 let H := fresh in
446 let v' := open_constr:(_) in
447 eassert (eval d e v') by (eauto 99 with nocore);
448 let Heq := fresh in
449 enough (Heq : v = v') by (rewrite Heq; exact H);
450 try clear H e
451 end.
452
453 Import ListNotations.
454
455 Lemma SetFlag_R s m f (HR : R s m) (i:idx) b (Hi : eval s i (Z.b2z b)) :
456 forall _tt s', Symbolic.SetFlag f i s = Success (_tt, s') ->
457 R s' (SetFlag m f b) /\ s :< s'.
458 Proof using Type.
459 destruct s; cbv [Symbolic.SetFlag Symbolic.update_flag_with Symbolic.set_flag] in *;
460 intros; inversion_ErrorT_step; Prod.inversion_prod; subst.
461 cbv [R].
462 intuition idtac.
463 all : try eapply R_set_flag_internal; eauto.
464 all : cbv [R] in *; intuition idtac.
465 Qed.
466
467 Ltac step_SetFlag :=
468 match goal with
469 | H : Symbolic.SetFlag ?f ?i ?s = Success (?_tt, ?s') |- _ =>
470 let i := open_constr:(_) in let b := open_constr:(_) in
471 let Hs' := fresh "H" s' in let Hlt := fresh "He" s' in
472 let t := open_constr:(fun A B => SetFlag_R s _ f A i b B _ _ H) in
473 unshelve (edestruct t as (Hs'&Hlt); clear H); shelve_unifiable;
474 [eassumption|..|clear H]
475 end.
476
477 Lemma GetReg_R {opts : symbolic_options_computed_opt} {descr:description} s m (HR: R s m) r i s'
478 (H : GetReg r s = Success (i, s'))
479 : R s' m /\ s :< s' /\ eval s' i (get_reg m r).
480 Proof using Type.
481 cbv [GetReg GetReg64 bind some_or get_reg index_and_shift_and_bitcount_of_reg] in *.
482 pose proof (get_reg_R s _ ltac:(eassumption) (reg_index r)) as Hr.
483 destruct Symbolic.get_reg in *; [|inversion H]; cbn in H.
484 specialize (Hr _ eq_refl); case Hr as (v&Hi0&Hv).
485 rewrite Hv; clear Hv.
486 step_symex; eauto.
487 repeat (eauto || econstructor); cbn [interp_op].
488 Qed.
489
490 Ltac step_GetReg :=
491 match goal with
492 | H : Symbolic.GetReg ?r ?s = Success (?v, ?s') |- _ =>
493 let Hs' := fresh "H" s' in let Hlt := fresh "He" s' in let Hv := fresh "Hv" s' in
494 let t := open_constr:(fun A => GetReg_R s _ A r v s' H) in
495 unshelve (edestruct t as (Hs'&Hlt&Hv); clear H); shelve_unifiable;
496 [eassumption|..|clear H]
497 end.
498
499 Lemma Address_R {opts : symbolic_options_computed_opt} {descr:description} s m (HR : R s m) (sa:AddressSize) o a s' (H : Symbolic.Address o s = Success (a, s'))
500 : R s' m /\ s :< s' /\ exists v, eval s' a v /\ @DenoteAddress sa m o = v.
501 Proof using Type.
502 destruct o as [? ? ? ?]; cbv [Address DenoteAddress Syntax.mem_base_reg Syntax.mem_offset Syntax.mem_scale_reg err ret] in *; repeat step_symex.
503 all : repeat first [ progress inversion_ErrorT
504 | progress inversion_pair
505 | progress subst
506 | first [ step_symex | step_GetReg ]; try solve [repeat (eauto || econstructor)]; []
507 | break_innermost_match_hyps_step; cbn [fst snd] in * ].
508 all : Tactics.ssplit; eauto 99.
509 all : eexists; split; eauto; [].
510 all : cbv [DenoteConst fold_right].
511 all : rewrite !Z.land_ones by lia.
512 all : push_Zmod; pull_Zmod.
513 all : autorewrite with zsimplify_const.
514 all : f_equal; lia.
515 Qed.
516
517 Ltac step_Address :=
518 match goal with HSa: context[Address] |- _ =>
519 eapply Address_R in HSa; [|eassumption];
520 destruct HSa as (?&?&?&?&?)
521 end.
522
523 Ltac step_symex4 := first [step_symex3 | step_SetFlag | step_Address].
524 Ltac step_symex ::= step_symex4.
525
526 Lemma load_bytes_Rcell64 d
527 (a:idx) va (Ha : eval d a va)
528 i m fr (HR : (R_cell64 d a i ⋆ fr)%sep m)
529 : exists bs, load_bytes m (word.of_Z va) 8 = Some bs /\ eval d i (le_combine bs).
530 Proof using Type.
531 cbn in HR. eapply SeparationLogic.sep_comm in HR.
532 destruct HR as (?&?&(?&?)&?&?). rewrite H.
533 destruct H2 as (?&?&?). eapply SeparationLogic.sep_emp_l in H2;
534 destruct_head'_and.
535 eapply eval_eval in Ha; [|eauto]; []; subst.
536 eexists; split; try eassumption; cbv [get_mem Crypto.Util.Option.bind] in *.
537 rewrite word.of_Z_unsigned.
538 unshelve epose proof load_bytes_of_putmany_bytes_at _ x1 x _ H4 ltac:(clear;lia).
539 destruct load_bytes eqn:?; simpl in *; try congruence; eassumption.
540 Unshelve.
541 { refine (SortedListWord.ok _ _). }
542 2: { epose proof Properties.word.eqb_spec. exact H. }
543 Qed.
544
545 Lemma Load64_R s m (HR : R s m) (a : idx)
546 va (Ha : eval s a va)
547 i s' (H : Load64 a s = Success (i, s'))
548 : s' = s /\ exists v, eval s i v /\ get_mem m va 8 = Some v /\ v = Z.land v (Z.ones 64).
549 Proof using Type.
550 cbv [Load64 some_or Symbolic.load option_map] in *.
551 destruct find as [(?&?)|] eqn:? in *;
552 inversion_ErrorT; Prod.inversion_prod; subst.
553 split;trivial.
554 eapply ListUtil.find_some_iff in Heqo;
555 repeat (cbn in *; destruct_head'_and; destruct_head'_ex).
556 clear H1. autoforward with typeclass_instances in H0; subst.
557 eapply nth_error_split in H;
558 repeat (cbn in *; destruct_head'_and; destruct_head'_ex).
559 destruct s'; cbn in *; destruct_head'_and; subst.
560 progress unfold machine_mem_state in *.
561 eapply R_mem_Permutation in H4;
562 [|symmetry; eapply Permutation.Permutation_middle].
563 cbv [get_mem Crypto.Util.Option.bind].
564 eapply load_bytes_Rcell64 in H4; eauto; [];
565 repeat (destruct_head'_and; destruct_head'_ex).
566 eexists; split; try eassumption; split.
567 { destruct_one_match; simpl in *; try congruence. }
568 { epose proof le_combine_bound x as HH.
569 erewrite length_load_bytes in HH by eassumption.
570 rewrite Z.land_ones, Z.mod_small; lia. }
571 Qed.
572
573 Lemma store_R d s m (HR : R_mem d s m)
574 (a : idx) va (Ha : eval d a va)
575 (i : idx) v (Hi :eval d i v) (Hv : v = Z.land v (Z.ones 64))
576 v' (Hv' : Z.land v' (Z.ones 64) = v)
577 s' (H : Symbolic.store a i s = Some s')
578 : exists m', set_mem m va 8 v' = Some m' /\ R_mem d s' m'.
579 Proof using Type.
580 cbv [Symbolic.store Crypto.Util.Option.bind ] in *.
581 destruct List.indexof eqn:Hfound; Option.inversion_option.
582 eapply List.indexof_Some in Hfound; destruct_head_ex; destruct_head_and.
583 autoforward with typeclass_instances in H1; subst a s'.
584 pose H0 as H0'; eapply nth_error_split in H0';
585 repeat (destruct_head_and; destruct_head_ex); subst s n.
586 destruct x; cbn [fst snd] in *.
587 eapply R_mem_Permutation in HR;
588 [|symmetry; eapply Permutation.Permutation_middle].
589 cbv [set_mem store_bytes].
590 pose proof split_le_combine (le_split 8 v').
591 rewrite length_le_split, le_combine_split, <-Z.land_ones in H by lia.
592 setoid_rewrite Hv' in H; clear Hv'; rewrite <-H; clear H.
593 cbn in HR.
594 rewrite length_le_split.
595 pose proof HR as HR'; eapply load_bytes_Rcell64 in HR'; eauto; [];
596 repeat (destruct_head'_and; destruct_head'_ex).
597 rewrite H.
598 eexists; split; trivial; [].
599 erewrite <-Crypto.Util.ListUtil.splice_nth_equiv_update_nth_update
600 by (rewrite app_length; cbn; clear; lia); instantiate (1:=(0,0)%N).
601 cbv [Crypto.Util.ListUtil.splice_nth].
602 rewrite ListUtil.firstn_app_sharp by trivial.
603 rewrite (ListUtil.app_cons_app_app _ _ _ x1) at 2.
604 rewrite ListUtil.skipn_app_sharp by (rewrite app_length; cbn; clear; lia).
605 cbv [nth_default]; destruct_one_match; setoid_rewrite H0 in E;
606 Option.inversion_option; destruct p; Prod.inversion_prod; subst;
607 cbn [fst snd].
608 eapply R_mem_Permutation; [|eapply Permutation.Permutation_middle];
609 cbn [R_mem]; cbv [R_cell64] in *.
610 eapply SeparationLogic.sep_ex1_l in HR. destruct HR as [? HR].
611 (* COQBUG: semicolon on previous line fails *)
612 eapply SeparationLogic.sep_ex1_l in HR. destruct HR as [? HR].
613 eapply SeparationLogic.sep_assoc in HR;
614 eapply SeparationLogic.sep_emp_l in HR. destruct HR as [(?&?&?) HR].
615 eapply eval_eval in Ha; [|eauto]; []; subst.
616 eapply SeparationLogic.sep_ex1_l; eexists.
617 eapply SeparationLogic.sep_ex1_l; eexists.
618 rewrite word.of_Z_unsigned.
619 eapply SeparationLogic.sep_assoc, SeparationLogic.sep_emp_l; split.
620 2:{ eapply SeparationLogic.sep_comm in HR.
621 eapply SeparationLogic.sep_comm, unchecked_store_bytes_of_sep; eauto. }
622 rewrite le_combine_split, Z.mod_small; eauto.
623 rewrite Z.land_ones in Hv by (clear;lia).
624 clear -Hv; cbn in *; Z.div_mod_to_equations; lia.
625
626 all : fail. Unshelve. all : shelve_unifiable.
627 all : try refine (SortedListWord.ok _ _).
628 all : try (epose proof Properties.word.eqb_spec as HH; exact HH).
629 Qed.
630
631 Lemma Store64_R s m (HR : R s m)
632 (a : idx) va (Ha : eval s a va)
633 (i : idx) v (Hi :eval s i v) (Hv : v = Z.land v (Z.ones 64))
634 v' (Hv' : Z.land v' (Z.ones 64) = v)
635 s' _tt (H : Store64 a i s = Success (_tt, s'))
636 : exists m', SetMem m va 8 v' = Some m' /\ R s' m' /\ s :< s'.
637 Proof using Type.
638 cbv [Store64 bind some_or Crypto.Util.Option.bind ErrorT.bind] in *.
639 destruct Symbolic.store eqn:? in H; inversion_ErrorT; Prod.inversion_prod; subst.
640 destruct s, m; cbv [R] in *; destruct_head'_and.
641 eapply store_R in Heqo; try eassumption; trivial; try lia;
642 destruct_head_ex; destruct_head_and.
643 cbv [SetMem]. setoid_rewrite H3; cbv [update_mem_with]; cbn; eauto 9.
644 Qed.
645
646 Lemma store8 m a
647 old (Hold : get_mem m a 8 = Some old) b
648 m' (Hm': set_mem m a 8 (Z.lor (Z.land b (Z.ones 8)) (Z.ldiff old (Z.ones 8))) = Some m')
649 : set_mem m a 1 b = Some m'.
650 Proof using Type.
651 clear frame.
652 cbv [set_mem store_bytes] in *.
653 destruct_one_match_hyp; Option.inversion_option.
654 epose proof length_load_bytes _ _ _ _ E as H;
655 do 9 (destruct l; try solve [inversion H]); clear H.
656 epose proof nth_error_load_bytes _ _ _ _ E as H.
657 rewrite length_le_split in *.
658 destruct_one_match; cycle 1.
659 { eapply load_bytes_None in E0; destruct E0 as (?&?&?).
660 destruct x; [|lia].
661 specialize (H 0%nat ltac:(lia));
662 rewrite H1 in H; inversion H. }
663 f_equal; rewrite <-Hm'; clear Hm'.
664 cbv [unchecked_store_bytes]; eapply map.map_ext; intros k.
665 setoid_rewrite OfListWord.map.of_list_word_singleton.
666 rewrite 2 Properties.map.get_putmany_dec, Properties.map.get_put_dec, map.get_empty, OfListWord.map.get_of_list_word_at.
667 break_innermost_match_step;
668 autoforward with typeclass_instances in Heqb8; subst.
669 { rewrite word.unsigned_sub, word.unsigned_of_Z, Z.sub_diag.
670 cbv [le_split]; cbn -[Z.ones]; f_equal.
671 eapply Byte.byte.unsigned_inj;
672 rewrite !Byte.byte.unsigned_of_Z; cbv [Byte.byte.wrap];
673 rewrite <-!Z.land_ones by lia.
674 bitblast.Z.bitblast. }
675 destruct_one_match; trivial.
676 epose proof Crypto.Util.ListUtil.nth_error_value_length _ _ _ _ E1 as I.
677 rewrite length_le_split in *. specialize (H _ I); clear I.
678 rewrite Z2Nat.id in H by (eapply Properties.word.unsigned_range).
679 rewrite word.of_Z_unsigned in H.
680 replace (word.add (word.of_Z a) (word.sub k (word.of_Z a)))
681 with k in H by ring.
682 rewrite <-H, <-E1; clear H E1.
683 remember (Z.to_nat (word.unsigned (word.sub k (word.of_Z a)))) as i.
684 destruct i.
685 { destruct Heqb8. eapply Properties.word.unsigned_inj.
686 epose proof Properties.word.unsigned_range (word.sub k (word.of_Z a)).
687 assert (word.unsigned (word.sub k (word.of_Z a)) = 0) by lia.
688 change (word.unsigned (word.sub k (word.of_Z a)) = word.unsigned (word.of_Z 0: Naive.word 64)) in H0.
689 rewrite word.unsigned_sub, Properties.word.unsigned_of_Z_0 in H0.
690 cbv [word.wrap] in H0.
691 pose proof Properties.word.unsigned_range k.
692 pose proof Properties.word.unsigned_range (word.of_Z a).
693 Z.div_mod_to_equations.
694 lia. }
695 cbv [get_mem Crypto.Util.Option.bind] in *;
696 destruct load_bytes in *; repeat Option.inversion_option; subst.
697 change (le_combine (cons ?x ?y))
698 with (Z.lor (Byte.byte.unsigned x) (Z.shiftl (le_combine y) 8)).
699 change (le_split 8 ?x)
700 with (Byte.byte.of_Z x:: le_split 7 (Z.shiftr x 8)).
701 progress cbn [nth_error]; f_equal.
702 rewrite Z.shiftr_lor, Z.shiftr_land, Z.land_0_r, Z.lor_0_l.
703 rewrite Z.shiftr_ldiff, Z.ldiff_0_r.
704 rewrite <-(Byte.byte.wrap_unsigned b0);
705 cbv [Byte.byte.wrap]; rewrite <-!Z.land_ones by lia.
706 rewrite Z.shiftr_lor, Z.shiftr_land, Z.land_0_r, Z.lor_0_l.
707 rewrite Z.shiftr_shiftl_l, Z.shiftl_0_r by lia.
708 setoid_rewrite (split_le_combine [b1; b2; b3; b4; b5; b6; b7]); trivial.
709 Qed.
710
711
712 Lemma GetOperand_R {opts : symbolic_options_computed_opt} {descr:description} s m (HR: R s m) (so:OperationSize) (sa:AddressSize) a i s'
713 (H : GetOperand a s = Success (i, s'))
714 : R s' m /\ s :< s' /\ exists v, eval s' i v /\ DenoteOperand sa so m a = Some v.
715 Proof using Type.
716 cbv [GetOperand DenoteOperand err] in *; break_innermost_match; inversion_ErrorT.
717 { eapply GetReg_R in H; intuition eauto. }
718 { progress cbv [Load ret] in *.
719 repeat (cbv [err] in *; cbn [fst snd] in * || step_symex || Tactics.destruct_one_match_hyp || inversion_ErrorT || Prod.inversion_prod || subst).
720 eapply Load64_R in HSv; try eassumption; [];
721 repeat (subst; destruct_head'_and; destruct_head'_ex).
722 repeat step_symex.
723 { repeat (eauto||econstructor). }
724 split; eauto; [].
725 split; eauto; [].
726 rewrite Z.shiftr_0_r in Hi by lia.
727 case E as [E|E];
728 rewrite E in *; simpl Z.of_N in *.
729 { cbv [get_mem Crypto.Util.Option.bind] in *.
730 destruct_one_match_hyp; Option.inversion_option.
731 epose proof length_load_bytes _ _ _ _ E0.
732 do 1 (destruct l; try solve [inversion H]).
733 eapply @nth_error_load_bytes with (i:=0%nat) in E0; [|clear;lia].
734 symmetry in E0; cbn [nth_error] in E0; simpl Z.of_nat in E0.
735 simpl load_bytes.
736 change (Pos.to_nat 1) with 1%nat.
737 cbv [load_bytes footprint List.map seq List.option_all].
738 setoid_rewrite E0.
739 eexists; split; eauto.
740 cbv [le_combine].
741 rewrite Z.shiftl_0_l, Z.lor_0_r.
742 change (Z.lor (Byte.byte.unsigned b) (Z.shiftl (le_combine l) 8) = x) in H4.
743 rewrite <-H4 in H5 at 2; rewrite H5; clear H4 H5.
744 f_equal.
745 rewrite <-Byte.byte.wrap_unsigned at 1; setoid_rewrite <-Z.land_ones; [|clear;lia].
746 rewrite <-Z.land_assoc.
747 change (Z.land (Z.ones 64) (Z.ones 8)) with (Z.ones 8).
748 rewrite Z.land_lor_distr_l.
749 bitblast.Z.bitblast. subst.
750 rewrite (Z.testbit_neg_r _ (_-8)) by lia; Btauto.btauto. }
751 { setoid_rewrite H4.
752 eexists; split; eauto; f_equal.
753 rewrite H5 at 1; trivial. } }
754 { step_symex; repeat (eauto||econstructor). }
755 Qed.
756
757 Ltac step_GetOperand :=
758 match goal with
759 | H : GetOperand ?a ?s = Success (?i0, ?s') |- _ =>
760 let v := fresh "v" (*a*) in let Hv := fresh "H" v
761 in let Hi := fresh "H" i0 in let Heq := fresh H "eq" in
762 let Hs' := fresh "H" s' in let Hl := fresh "Hl" s' in
763 case (GetOperand_R s _ ltac:(eassumption) _ _ _ _ _ H) as (Hs'&Hl&(v&Hi&Hv)); clear H
764 end.
765
766 (* note: do the two SetOperand both truncate inputs or not?... *)
767 Lemma R_SetOperand {opts : symbolic_options_computed_opt} {descr:description} s m (HR : R s m)
768 (sz:OperationSize) (sa:AddressSize) a i _tt s' (H : Symbolic.SetOperand a i s = Success (_tt, s'))
769 v (Hv : eval s i v)
770 : exists m', SetOperand sa sz m a v = Some m' /\ R s' m' /\ s :< s'.
771 Proof using Type.
772 destruct a in *; cbn in H; cbv [err] in *; inversion_ErrorT; [ | ];
773 cbv [SetOperand Crypto.Util.Option.bind SetReg64 update_reg_with Symbolic.update_reg_with] in *;
774 repeat (BreakMatch.break_innermost_match_hyps; Prod.inversion_prod; ErrorT.inversion_ErrorT; subst).
775
776 { repeat step_symex.
777 { repeat (eauto||econstructor). }
778 inversion_ErrorT; Prod.inversion_prod; subst.
779 rewrite Z.shiftr_0_r in *.
780 eexists; split; [exact eq_refl|].
781 destruct s0; cbv [R] in *; intuition try solve [cbn in *; intuition idtac].
782 cbv [R_regs Symbolic.set_reg set_reg index_and_shift_and_bitcount_of_reg].
783 eapply Tuple.fieldwise_to_list_iff.
784 unshelve erewrite 2Tuple.from_list_default_eq, 2Tuple.to_list_from_list;
785 try solve [rewrite ?Crypto.Util.ListUtil.length_set_nth, ?Crypto.Util.ListUtil.length_update_nth, ?Tuple.length_to_list; trivial].
786 eapply Crypto.Util.ListUtil.Forall2_update_nth.
787 { eapply Tuple.fieldwise_to_list_iff; eassumption. }
788 cbv [R_reg bitmask_of_reg index_and_shift_and_bitcount_of_reg].
789 intros. DestructHead.destruct_head'_and.
790 eapply Ndec.Neqb_complete in Heqb; rewrite Heqb.
791 replace (reg_offset r) with 0%N by (destruct r;cbv;trivial||cbv in Heqb;inversion Heqb).
792 assert (Hx64: Z.ldiff v2 (Z.ones 64) = 0). {
793 rewrite Z.land_ones in * by Lia.lia.
794 rewrite Z.ldiff_ones_r, Z.shiftl_eq_0_iff, Z.shiftr_div_pow2 by (clear; Lia.lia).
795 clear -H6. cbn in *; zify; Z.div_mod_to_equations; lia. }
796 rewrite Z.shiftl_0_r, Z.shiftl_0_r. setoid_rewrite Hx64. setoid_rewrite Z.lor_0_r.
797 intuition idtac; try Option.inversion_option; subst; trivial.
798 { cbn -[Z.ones]; rewrite !Z.land_ones, Zmod_mod by (clear;lia); trivial. } }
799 { eexists; split; [exact eq_refl|].
800 repeat (step_symex; []).
801 cbv [GetReg64 some_or] in *.
802 pose proof (get_reg_R s _ ltac:(eassumption) (reg_index r)) as Hr.
803 destruct (Symbolic.get_reg _ _) in *; cbn [ErrorT.bind] in H;
804 ErrorT.inversion_ErrorT; Prod.inversion_prod; subst;cbn [fst snd] in *.
805 specialize (Hr _ eq_refl); case Hr as (?&?&?).
806 step_App.
807 { repeat (eauto || econstructor). }
808 repeat (Prod.inversion_prod; ErrorT.inversion_ErrorT; subst).
809 destruct s1; cbv [R] in *; cbn in *; intuition idtac.
810 cbv [R_regs Symbolic.set_reg set_reg index_and_shift_and_bitcount_of_reg].
811 eapply Tuple.fieldwise_to_list_iff.
812 unshelve erewrite 2Tuple.from_list_default_eq, 2Tuple.to_list_from_list;
813 try solve [rewrite ?Crypto.Util.ListUtil.length_set_nth, ?Crypto.Util.ListUtil.length_update_nth, ?Tuple.length_to_list; trivial].
814 eapply Crypto.Util.ListUtil.Forall2_update_nth.
815 { eapply Tuple.fieldwise_to_list_iff; eassumption. }
816 cbv [R_reg]; intuition idtac; try Option.inversion_option; subst; try eval_same_expr_goal;
817 cbv [bitmask_of_reg index_and_shift_and_bitcount_of_reg].
818 { rewrite <-Tuple.nth_default_to_list. cbv [nth_default]; rewrite H5. trivial. }
819 assert (Z.of_N (reg_size r) + Z.of_N (reg_offset r) <= 64) by (destruct r; clear; cbv; discriminate).
820 eapply Z.bits_inj_iff'; intros j Hj.
821 rewrite Z.land_spec, Z.testbit_ones_nonneg by (clear -Hj; lia).
822 destr.destr (j <? 64); rewrite ?Bool.andb_true_r, ?Bool.andb_false_r; trivial; [].
823 rewrite Z.lor_spec, Z.ldiff_spec, !Z.shiftl_spec, Z.land_spec, !Z.testbit_ones_nonneg by (assumption||lia).
824 destr.destr (j - Z.of_N (reg_offset r) <? Z.of_N (reg_size r)); try (revert dependent j; clear -H6; lia).
825 rewrite Bool.andb_true_r, Bool.andb_false_r, Bool.orb_false_l.
826 rewrite H8, Z.land_spec, Z.ones_spec_high; revert dependent j; lia. }
827 { progress cbv [Store] in *.
828 destruct_one_match_hyp; cbv [err] in *; inversion_ErrorT.
829 repeat (step_symex; cbn [fst snd] in * ).
830 eapply Load64_R in HSold; eauto;
831 repeat (subst; destruct_head'_ex; destruct_head'_and).
832 repeat (step_symex; cbn [fst snd] in * ).
833 { repeat (eauto || econstructor). }
834 { repeat (eauto || econstructor). }
835 rewrite !Z.shiftl_0_r, ?Z.shiftr_0_r, <-Z.land_assoc, Z.land_diag in *.
836 case E as [E|E]; rewrite E in *; simpl Z.of_N in *.
837 { eapply Store64_R with (v':=Z.lor (Z.land v (Z.ones 8)) (Z.ldiff x (Z.ones 8))) in H;
838 try eassumption; eauto with nocore; try solve [rewrite H5; bitblast.Z.bitblast].
839 destruct_head'_ex; destruct_head'_and.
840 cbv [SetMem Crypto.Util.Option.bind update_mem_with] in *;
841 destruct set_mem eqn:? in *; Option.inversion_option; subst.
842 erewrite store8; eauto 9. }
843 { eapply Store64_R with (v':=v) in H;
844 try eassumption; eauto with nocore; try solve [rewrite H5; bitblast.Z.bitblast].
845 destruct_head'_ex; destruct_head'_and. setoid_rewrite H. eauto 9. } }
846 Qed.
847
848 Ltac step_SetOperand :=
849 match goal with
850 | H : Symbolic.SetOperand ?a ?i ?s = Success (?_tt, ?s') |- _ =>
851 let m := fresh "m" in let Hm := fresh "H" m in
852 let HR := fresh "H" s' in let Hl := fresh "Hl" s' in
853 case (R_SetOperand s _ ltac:(eassumption) _ _ _ _ _ _ H _ ltac:(eauto 99 with nocore))
854 as (m&?Hm&HR&Hl); clear H
855 end.
856
857 Lemma HavocFlags_R s m (HR : R s m) :
858 forall _tt s', Symbolic.HavocFlags s = Success (_tt, s') ->
859 R s' (HavocFlags m) /\ s :< s'.
860 Proof using Type.
861 destruct s; cbv [Symbolic.HavocFlags Symbolic.update_flag_with] in *;
862 intros; inversion_ErrorT_step; Prod.inversion_prod; subst.
863 cbv [R]; intuition idtac; try solve [cbv [R] in *; intuition idtac].
864 all: cbv [R_flags Tuple.fieldwise Tuple.fieldwise' R_flag]; cbn;
865 intuition idtac; Option.inversion_option.
866 Qed.
867 Ltac step_HavocFlags :=
868 match goal with
869 | H : Symbolic.HavocFlags ?s = Success (_, ?s') |- _ =>
870 let Hs' := fresh "H" s' in
871 let Hl := fresh "Hlt" s' in
872 let t := open_constr:(fun A => HavocFlags_R _ _ A _ _ H) in
873 unshelve (edestruct t as (Hs'&Hl); clear H); shelve_unifiable;
874 [eassumption|]
875 end.
876
877 Ltac step_symex5 := first [step_symex4 | step_GetOperand | step_SetOperand | step_HavocFlags ].
878 Ltac step_symex ::= step_symex5.
879
880 Ltac him :=
881 match goal with
882 | |- context G [match ?x with _ => _ end] =>
883 assert_fails (idtac; match x with context[match _ with _ => _ end] => idtac end);
884 let ex := eval hnf in x in
885 first [progress change x with ex; progress cbv match beta | destruct x eqn:?]
886 end.
887
888 Global Existing Instance expr_beq_spec.
889
890 Ltac destr_expr_beq :=
891 match goal with
892 | H : context [expr_beq ?a ?b] |- _ => destr.destr (expr_beq a b)
893 end.
894
895 Lemma standalone_operand_size_cases o n : standalone_operand_size o = Some n ->
896 (n = 8 \/ n = 16 \/ n = 32 \/ n = 64)%N.
897 Proof using Type.
898 destruct o; cbn; try congruence.
899 { destruct r; cbn; inversion 1; subst; eauto. }
900 { destruct (mem_bits_access_size _) as [ [] | ]; inversion 1; subst; eauto. }
901 Qed.
902
903 Lemma opcode_size_cases o n : opcode_size o = Some n ->
904 ((o = clc /\ n = 1) \/ n = 8 \/ n = 16 \/ n = 32 \/ n = 64)%N.
905 Proof using Type.
906 destruct o; cbn; intros; Option.inversion_option; try now subst; eauto.
907 Qed.
908
909 Lemma lift_map_standalone_operand_size_cases_Forall args ls
910 (H : Crypto.Util.OptionList.Option.List.lift
911 (map standalone_operand_size args) =
912 Some ls)
913 (P := fun n => (n = 8 \/ n = 16 \/ n = 32 \/ n = 64)%N)
914 : Forall P ls.
915 Proof using Type.
916 revert ls H; induction args as [|x xs IH]; intros; cbn in *; cbv [Crypto.Util.Option.bind] in *; break_match_hyps; repeat Option.inversion_option; subst; try now constructor.
917 specialize (IH _ ltac:(eassumption)).
918 match goal with H : _ |- _ => apply standalone_operand_size_cases in H end.
919 constructor; subst P; eauto.
920 Qed.
921
922 Lemma lift_map_standalone_operand_size_cases_fold_right args x xs f init
923 (H : Crypto.Util.OptionList.Option.List.lift
924 (map standalone_operand_size args) =
925 Some (x :: xs))
926 (P := fun n => (n = 8 \/ n = 16 \/ n = 32 \/ n = 64)%N)
927 (Hf : forall a b, P a -> (b = init \/ P b) -> P (f a b))
928 (n := fold_right f init (x :: xs))
929 : P n.
930 Proof using Type.
931 apply lift_map_standalone_operand_size_cases_Forall in H.
932 inversion H; clear H; subst.
933 subst n; cbn [fold_right].
934 apply Hf; try assumption.
935 induction xs as [|x' xs IH]; inversion_one_head Forall; eauto.
936 Qed.
937
938 Lemma map_id_map_standalone_operand_size_cases_Forall args ls
939 (H : Crypto.Util.OptionList.Option.List.map id
940 (map standalone_operand_size args) =
941 ls)
942 (P := fun n => (n = 8 \/ n = 16 \/ n = 32 \/ n = 64)%N)
943 : Forall P ls.
944 Proof using Type.
945 subst.
946 induction args as [|x xs IH]; intros; cbn in *; cbv [Crypto.Util.Option.bind] in *; break_match; repeat Option.inversion_option; subst; try (now constructor); eauto.
947 constructor; subst P; eauto.
948 eapply standalone_operand_size_cases; eassumption.
949 Qed.
950
951 Lemma map_id_map_standalone_operand_size_cases_fold_right args x xs f init
952 (H : Crypto.Util.OptionList.Option.List.map id
953 (map standalone_operand_size args) =
954 x :: xs)
955 (P := fun n => (n = 8 \/ n = 16 \/ n = 32 \/ n = 64)%N)
956 (Hf : forall a b, P a -> (b = init \/ P b) -> P (f a b))
957 (n := fold_right f init (x :: xs))
958 : P n.
959 Proof using Type.
960 apply map_id_map_standalone_operand_size_cases_Forall in H.
961 inversion H; clear H; subst.
962 subst n; cbn [fold_right].
963 apply Hf; try assumption.
964 induction xs as [|x' xs IH]; inversion_one_head Forall; eauto.
965 Qed.
966
967 Lemma operation_size_cases i n : Syntax.operation_size i = Some n ->
968 (((exists prefix ls, i = Build_NormalInstruction prefix Syntax.clc ls) /\ n = 1) \/ (n = 8 \/ n = 16 \/ n = 32 \/ n = 64))%N.
969 Proof using Type.
970 clear G.
971 intuition idtac; DestructHead.destruct_head'_ex; subst; cbn in *.
972 cbv [Syntax.operation_size] in H.
973 break_match_hyps; Option.inversion_option; subst.
974 all: repeat first [ progress destruct_head'_or
975 | progress destruct_head'_and
976 | progress subst
977 | solve [ eauto ]
978 | eapply lift_map_standalone_operand_size_cases_fold_right; [ eassumption | intros ]
979 | eapply map_id_map_standalone_operand_size_cases_fold_right; [ eassumption | intros ]
980 | apply N.min_case_strong; intros
981 | match goal with
982 | [ H : Syntax.op ?i = _ |- _ ] => is_var i; destruct i; cbn in H
983 | [ H : opcode_size _ = Some _ |- _ ] => apply opcode_size_cases in H
984 | [ |- (_ /\ _) \/ _ ] => right
985 end ].
986 Qed.
987
988 Ltac pose_operation_size_cases :=
989 match goal with
990 | H : Syntax.operation_size _ = Some _ |- _ =>
991 unique pose proof (operation_size_cases _ _ H)
992 end.
993
994 Ltac invert_eval :=
995 match goal with
996 | H : context[match reveal ?d ?n ?i with _ => _ end] |- _ => destruct (reveal d n i) as [?|[[] []]]eqn:? in * (* kludge *)
997 | H : reveal ?d _ ?i = ?rhs, G : eval ?d (ExprRef ?i) ?v |- _ =>
998 let h := Head.head rhs in is_constructor h;
999 let HH := fresh H in
1000 epose proof (eval_reveal _ d _ i v G _ H) as HH;
1001 clear H; rename HH into H
1002 | H : eval ?d (ExprApp ?n) ?v |- _ =>
1003 let HH := fresh H in
1004 let x := fresh "x" in (* COQBUG: using _ below clears unrelated hypothesis *)
1005 inversion E0 as [|? ? x ? [] HH ]; clear x;
1006 clear H; subst; rename HH into H
1007 | H : eval _ (ExprApp _) _ |- _ =>
1008 inversion H; clear H; subst
1009 | H : Forall2 _ nil _ |- _
1010 => inversion H; clear H; subst
1011 | H : Forall2 _ _ nil |- _
1012 => inversion H; clear H; subst
1013 | H : Forall2 _ (_ :: _) _ |- _
1014 => inversion H; clear H; subst
1015 | H : Forall2 _ _ (_ :: _) |- _
1016 => inversion H; clear H; subst
1017 | H : interp_op _ ?o ?a = Some ?v |- _ => inversion H; clear H; subst
1018 end.
1019
1020 Ltac resolve_match_using_hyp :=
1021 let rewrite_for x
1022 := match goal with
1023 | [ H : x = ?v |- _ ]
1024 => let h := Head.head v in
1025 is_constructor h;
1026 rewrite H
1027 end in
1028 match goal with
1029 | [ |- context[match ?x with _ => _ end] ] => rewrite_for x
1030 | [ |- context[match (if _ then ?x else ?y) with _ => _ end] ]
1031 => progress (try rewrite_for x; try rewrite_for y)
1032 end.
1033
1034 Ltac resolve_SetOperand_using_hyp :=
1035 match goal with
1036 | |- context[match SetOperand ?a ?b ?c ?d ?x with _ => _ end] =>
1037 match goal with H : SetOperand a b c d ?y = Some _ |- _ =>
1038 replace x with y; [rewrite H|]; cycle 1 end
1039 | |- context[SetOperand ?a ?b ?c ?d ?x = Some _] =>
1040 match goal with H : SetOperand a b c d ?y = Some _ |- _ =>
1041 replace x with y; [rewrite H|]; cycle 1 end
1042 end.
1043
1044 Ltac lift_let_goal :=
1045 match goal with
1046 | |- context G [let x := ?v in @?C x] =>
1047 let xH := fresh x in pose v as xH;
1048 let g := context G [C xH] in change g; cbv beta
1049 end.
1050
1051 Ltac step :=
1052 first
1053 [ lift_let_goal
1054 | resolve_match_using_hyp
1055 | progress (cbn beta iota delta [fst snd Syntax.op Syntax.args] in *; cbv beta iota delta [Reveal RevealConst Crypto.Util.Option.bind Symbolic.ret Symbolic.err Symeval mapM PreserveFlag some_or] in *; subst)
1056 | Prod.inversion_prod_step
1057 | inversion_ErrorT_step
1058 | Option.inversion_option_step
1059 | invert_eval
1060 | step_symex
1061 | destr_expr_beq
1062 | rewrite N.eqb_refl (* TODO: should this live elsewhere? *)
1063 ].
1064 Ltac step1 := step; (eassumption||trivial); [].
1065 Ltac step01 := solve [step] || step1.
1066
1067 Lemma SetOperand_same n m a v m'
1068 (Hd : DenoteOperand 64 n m a = Some v) (Hs : SetOperand 64 n m a v = Some m')
1069 : m = m'.
1070 Proof using Type.
1071 destruct a, m; cbn -[DenoteAddress] in *; repeat (subst; Option.inversion_option).
1072 { cbv [update_reg_with set_reg]; cbn in *; f_equal.
1073 eapply Tuple.to_list_ext.
1074 rewrite <-Tuple.nth_default_to_list in Hd; rewrite <-Hd; clear Hd.
1075 unshelve erewrite Tuple.from_list_default_eq, Tuple.to_list_from_list.
1076 { abstract (rewrite Crypto.Util.ListUtil.length_update_nth; eapply Tuple.length_to_list). }
1077 eapply List.nth_error_ext; intro i; cbv [nth_default].
1078 rewrite Crypto.Util.ListUtil.nth_update_nth; destruct_one_match; subst; trivial.
1079 destruct_one_match; cbn; trivial; eapply f_equal.
1080 eapply Z.bits_inj_iff'; intros i Hi.
1081 repeat rewrite ?Z.land_spec, ?Z.ldiff_spec, ?Z.lor_spec, ?bitblast.Z.shiftr_spec', ?bitblast.Z.shiftl_spec', ?Z.testbit_ones, ?Z.testbit_0_l by (clear;lia).
1082 destr (i <? 0); cbn; try lia.
1083 destr (i - Z.of_N (reg_offset r) <? 0); cbn; try lia.
1084 { destr (0 <=? i - Z.of_N (reg_offset r)); cbn; rewrite ?Bool.andb_true_r; trivial.
1085 destr (i - Z.of_N (reg_offset r) <? Z.of_N (reg_size r)); cbn; try Btauto.btauto.
1086 lia. }
1087 destr (0 <=? i - Z.of_N (reg_offset r)); try lia; cbn.
1088 replace (i - Z.of_N (reg_offset r) + Z.of_N (reg_offset r)) with i by lia.
1089 destr (i - Z.of_N (reg_offset r) <? Z.of_N (reg_size r)); Btauto.btauto. }
1090 { destruct m'; cbv [SetMem update_mem_with Crypto.Util.Option.bind get_mem option_map set_mem store_bytes unchecked_store_bytes] in *; repeat (destruct_one_match_hyp || Option.inversion_option).
1091 inversion Hs; clear Hs; subst; f_equal.
1092 clear E0.
1093 change (@map.putmany ?K ?V ?M ?m) with (@map.putmany K V M machine_mem_state).
1094 set (word.of_Z _) as a in *; clearbody a.
1095 rename n into n'; set (N.to_nat (operand_size m0 n' / 8)) as n in *; clearbody n; clear n'.
1096 epose proof (length_load_bytes _ _ _ _ E1) as Hl; rewrite <-Hl, split_le_combine.
1097 eapply (@map.map_ext _ _ mem_state _); intro k.
1098 rewrite Properties.map.get_putmany_dec, OfListWord.map.get_of_list_word_at.
1099 destruct_one_match; trivial.
1100 epose proof ListUtil.nth_error_value_length _ _ _ _ E.
1101 rewrite <-E; clear E.
1102 rewrite (nth_error_load_bytes _ _ _ _ E1 (Z.to_nat (word.unsigned (word.sub k a))) ltac:(lia)).
1103 rewrite Z2Nat.id, word.of_Z_unsigned by (eapply Properties.word.unsigned_range).
1104 f_equal. ring. }
1105 Qed.
1106
1107
1108 Lemma SymexNornalInstruction_R {opts : symbolic_options_computed_opt} {descr:description} s m (HR : R s m) instr :
1109 forall _tt s', Symbolic.SymexNormalInstruction instr s = Success (_tt, s') ->
1110 exists m', Semantics.DenoteNormalInstruction m instr = Some m' /\ R s' m' /\ s :< s'.
1111 Proof using Type.
1112 intros [] s' H.
1113 case instr as [op args]; cbv [SymexNormalInstruction OperationSize] in H.
1114 repeat (repeat destruct_one_match_hyp; repeat step01).
1115
1116 all : repeat
1117 match goal with
1118 | |- eval_node _ _ (?op, ?args) ?e =>
1119 solve [repeat (eauto 99 with nocore || econstructor)]
1120 | |- eval _ (ExprApp (?op, ?args)) ?e =>
1121 solve [repeat (eauto 99 with nocore || econstructor)]
1122 | _ => step
1123 | |- eval _ (ExprRef ?v) ?e => eval_same_expr_goal; try solve [ (* bashing *)
1124 exact eq_refl || rewrite Z.bit0_mod; trivial; rewrite Z.mod_small; trivial; Lia.lia]
1125 end.
1126
1127 all: cbv beta delta [DenoteNormalInstruction];
1128 repeat
1129 match goal with
1130 | x := ?v |- _ => let t := type of x in
1131 assert_fails (idtac; match v with context[match _ with _ => _ end] => idtac end);
1132 subst x
1133 | _ => step
1134 | _ => resolve_SetOperand_using_hyp
1135 | _ => rewrite (Bool.pull_bool_if Some)
1136 | |- context[if (?x =? ?y)%N then _ else _] => destruct (x =? y)%N eqn:?; reflect_hyps; subst; try congruence
1137 | |- exists _, Some _ = Some _ /\ _ => eexists; split; [f_equal|]
1138 | |- exists _, None = Some _ /\ _ => exfalso
1139 | |- _ /\ _ :< _ => split; [|solve[eauto 99 with nocore] ]
1140 end.
1141
1142 all: repeat first [ match goal with
1143 | [ H : DenoteOperand _ _ _ (Syntax.const _) = Some _ |- _ ] => cbv [DenoteOperand DenoteConst operand_size standalone_operand_size CONST_of_Z] in H
1144 end
1145 | progress Option.inversion_option
1146 | progress subst ].
1147
1148 all : cbn [fold_right map]; rewrite ?N2Z.id, ?Z.add_0_r, ?Z.add_assoc, ?Z.mul_1_r, ?Z.land_m1_r, ?Z.lxor_0_r, ?Z.lor_0_r;
1149 (congruence||eauto).
1150 all: rewrite ?Z.land_ones_low_alt by now try split; try apply Zpow_facts.Zpower2_lt_lin; lia.
1151 all: rewrite ?(fun x => Z.land_ones_low_alt (x / 8) x) by now split; try (eapply Z.le_lt_trans; [ | apply Zpow_facts.Zpower2_lt_lin ]); try lia; Z.to_euclidean_division_equations; nia.
1152 all: try exact eq_refl.
1153 all : try solve [rewrite Z.land_ones, Z.bit0_mod by Lia.lia; exact eq_refl].
1154
1155 all: try solve[ (* bash flags weakening *)
1156 match goal with H : R ?s' _ |- R ?s' ?m' =>
1157 try (is_var s'; destruct s');
1158 try (is_var m'; destruct m');
1159 repeat match goal with
1160 | x := ?v |- _ => subst x
1161 | H : R_flag _ ?f None |- _ => eapply R_flag_None_r in H; try (rewrite H in * )
1162 | H : R_flag ?d ?f (Some ?y) |- R_flag ?d ?f ?x =>
1163 let HH := fresh in enough (HH : x = Some y) by (rewrite HH; exact H)
1164 | H : _ = None |- _ => progress (try rewrite H; try rewrite H in * )
1165 | _ => progress (cbv [R_flags Tuple.fieldwise Tuple.fieldwise'] in *; cbn -[Syntax.operation_size] in * ; subst)
1166 | _ => destruct_one_match
1167 | _ => progress intuition idtac
1168 end; rewrite ?Z.add_0_r, ?Z.odd_opp; eauto; try Lia.lia
1169 end
1170 ].
1171
1172 Unshelve. all : match goal with H : context[Syntax.sub] |- _ => idtac | _ => shelve end.
1173 { cbn; repeat (rewrite ?Z.land_ones, ?Z.add_opp_r by Lia.lia).
1174 push_Zmod; pull_Zmod. rewrite Z.add_opp_r; congruence. }
1175
1176 Unshelve. all : match goal with H : context[Syntax.sbb] |- _ => idtac | _ => shelve end.
1177 { cbn; repeat (rewrite ?Z.land_ones, ?Z.add_opp_r by Lia.lia).
1178 push_Zmod; pull_Zmod. rewrite ?Z.sub_add_distr, ?Z.add_opp_r. congruence. }
1179
1180 Unshelve. all : match goal with H : context[Syntax.dec] |- _ => idtac | _ => shelve end.
1181 { cbn; repeat (rewrite ?Z.land_ones, ?Z.add_opp_r by Lia.lia).
1182 push_Zmod; pull_Zmod. replace v1 with v by congruence. exact eq_refl. }
1183 { cbv [Symbolic.PreserveFlag Symbolic.HavocFlags Symbolic.update_flag_with ret] in HSx4; cbn in HSx4; induction_path_ErrorT HSx4; Prod.inversion_prod; subst.
1184 inversion H; clear H; subst s'.
1185 split; [|solve[eauto 99 with nocore] ].
1186 destruct s6, m0;
1187 repeat match goal with
1188 | _ => Option.inversion_option_step
1189 | x := ?v |- _ => subst x
1190 | H : R_flag _ ?f None |- _ => eapply R_flag_None_r in H; try (rewrite H in * )
1191 | H : R_flag ?d ?f (Some ?y) |- R_flag ?d ?f ?x =>
1192 let HH := fresh in enough (HH : x = Some y) by (rewrite HH; exact H)
1193 | H : _ = None |- _ => progress (try rewrite H; try rewrite H in * )
1194 | _ => progress (cbv [R_flags R_flag Tuple.fieldwise Tuple.fieldwise' Symbolic.get_flag get_flag ] in *; cbn -[Syntax.operation_size] in * ; subst)
1195 | _ => destruct_one_match
1196 | _ => progress intuition idtac
1197 end; eauto; try Lia.lia; try congruence.
1198 eexists. split. eauto.
1199 f_equal. f_equal.
1200 rewrite ?Z.add_0_r.
1201 f_equal.
1202 1:congruence.
1203 rewrite <-Z.add_opp_r; f_equal.
1204 pose_operation_size_cases; intuition (subst; trivial). }
1205
1206 Unshelve. all : match goal with H : context[Syntax.inc] |- _ => idtac | _ => shelve end.
1207 { cbn; repeat (rewrite ?Z.land_ones, ?Z.add_opp_r by Lia.lia).
1208 push_Zmod; pull_Zmod. replace v1 with v by congruence. exact eq_refl. }
1209 { cbv [Symbolic.PreserveFlag Symbolic.HavocFlags Symbolic.update_flag_with ret] in HSx4; cbn in HSx4; induction_path_ErrorT HSx4; Prod.inversion_prod; subst.
1210 inversion H; subst s'.
1211 split; [|solve[eauto 99 with nocore] ].
1212 destruct s6, m0;
1213 repeat match goal with
1214 | _ => Option.inversion_option_step
1215 | x := ?v |- _ => subst x
1216 | H : R_flag _ ?f None |- _ => eapply R_flag_None_r in H; try (rewrite H in * )
1217 | H : R_flag ?d ?f (Some ?y) |- R_flag ?d ?f ?x =>
1218 let HH := fresh in enough (HH : x = Some y) by (rewrite HH; exact H)
1219 | H : _ = None |- _ => progress (try rewrite H; try rewrite H in * )
1220 | _ => progress (cbv [R_flags R_flag Tuple.fieldwise Tuple.fieldwise' Symbolic.get_flag get_flag ] in *; cbn -[Syntax.operation_size] in * ; subst)
1221 | _ => destruct_one_match
1222 | _ => progress intuition idtac
1223 end; eauto; try Lia.lia; try congruence.
1224 eexists. split. eauto.
1225 f_equal. f_equal.
1226 rewrite ?Z.add_0_r.
1227 f_equal.
1228 1:congruence.
1229 f_equal.
1230 pose_operation_size_cases; intuition (destruct_head'_ex; subst; try discriminate; trivial). }
1231
1232 Unshelve. all : match goal with H : context[Syntax.add] |- _ => idtac | _ => shelve end.
1233 { destruct s';
1234 repeat match goal with
1235 | x := ?v |- _ => subst x
1236 | H : R_flag _ ?f None |- _ => eapply R_flag_None_r in H; try (rewrite H in * )
1237 | H : R_flag ?d ?f (Some ?y) |- R_flag ?d ?f ?x =>
1238 let HH := fresh in enough (HH : x = Some y) by (rewrite HH; exact H)
1239 | H : _ = None |- _ => progress (try rewrite H; try rewrite H in * )
1240 | _ => progress (cbv [R_flags Tuple.fieldwise Tuple.fieldwise'] in *; cbn -[Syntax.operation_size] in * ; subst)
1241 | _ => destruct_one_match
1242 | _ => progress intuition idtac
1243 end; rewrite ?Z.add_0_r, ?Z.odd_opp; eauto; try Lia.lia; try congruence.
1244 replace (Z.signed n 0) with 0; cycle 1.
1245 { pose_operation_size_cases. clear -H0; intuition (subst; cbv; trivial). }
1246 rewrite Z.add_0_r; cbv [Z.signed]; congruence. }
1247
1248 Unshelve. all : match goal with H : context[Syntax.adc] |- _ => idtac | _ => shelve end.
1249 { destruct s';
1250 repeat match goal with
1251 | x := ?v |- _ => subst x
1252 | H : R_flag _ ?f None |- _ => eapply R_flag_None_r in H; try (rewrite H in * )
1253 | H : R_flag ?d ?f (Some ?y) |- R_flag ?d ?f ?x =>
1254 let HH := fresh in enough (HH : x = Some y) by (rewrite HH; exact H)
1255 | H : _ = None |- _ => progress (try rewrite H; try rewrite H in * )
1256 | _ => progress (cbv [R_flags Tuple.fieldwise Tuple.fieldwise'] in *; cbn -[Syntax.operation_size] in * ; subst)
1257 | _ => destruct_one_match
1258 | _ => progress intuition idtac
1259 end; rewrite ?Z.add_assoc, ?Z.add_0_r, ?Z.odd_opp; eauto; try Lia.lia; try congruence. }
1260
1261 Unshelve. all : match goal with H : context[Syntax.adcx] |- _ => idtac | _ => shelve end.
1262 { cbn [fold_right] in *; rewrite ?Z.bit0_odd, ?Z.add_0_r, ?Z.add_assoc in *; assumption. }
1263
1264 Unshelve. all : match goal with H : context[Syntax.adox] |- _ => idtac | _ => shelve end.
1265 { cbn [fold_right] in *; rewrite ?Z.bit0_odd, ?Z.add_0_r, ?Z.add_assoc in *; assumption. }
1266
1267 Unshelve. all : match goal with H : context[Syntax.cmovc] |- _ => idtac | H : context[Syntax.cmovb] |- _ => idtac | H : context[Syntax.cmovo] |- _=> idtac | _ => shelve end.
1268 (* cmovc / cmovb / cmovo *)
1269 all: (destruct vCF||destruct vOF); cbn [negb Z.b2z Z.eqb] in *; eauto 9; [].
1270 all: enough (m = m0) by (subst; eauto 9).
1271 all: clear -Hm0 Hv frame G ; eauto using SetOperand_same.
1272 all: fail.
1273
1274 Unshelve. all : match goal with H : context[Syntax.cmovnz] |- _ => idtac | _ => shelve end.
1275 { (* cmovnz *)
1276 destruct vZF; cbn [negb Z.b2z Z.eqb] in *; eauto 9; [].
1277 enough (m = m0) by (subst; eauto 9).
1278 clear -Hm0 Hv0 frame G ; eauto using SetOperand_same. }
1279
1280 Unshelve. all : match goal with H : context[Syntax.test] |- _ => idtac | _ => shelve end.
1281 { destruct (Equality.ARG_beq_spec a a0); try discriminate; subst a0.
1282 replace v0 with v in * by congruence; clear v0.
1283 rewrite Z.land_diag.
1284 case s', m in *; cbn;
1285 repeat match goal with
1286 | H : R_flag _ ?f None |- _ => eapply R_flag_None_r in H; try (rewrite H in * )
1287 | _ => progress (cbv [R_flags Tuple.fieldwise Tuple.fieldwise'] in *; cbn in * ; subst)
1288 | _ => progress intuition eauto 1
1289 end. }
1290
1291 Unshelve. all : match goal with H : context[Syntax.sar] |- _ => idtac | _ => shelve end; shelve_unifiable.
1292 { rewrite Z.land_m1_r in *.
1293 subst st st0.
1294 rewrite <-H1; cbn.
1295 replace (1 <? Z.of_N n) with true; cycle 1. {
1296 pose_operation_size_cases; intuition (subst; destruct_head'_ex; subst; try discriminate; cbn; clear; lia). }
1297 revert Hs'.
1298 repeat match goal with x:= _ |- _ => subst x end;
1299 destruct s';
1300 repeat match goal with
1301 | H : R_flag _ ?f None |- _ => eapply R_flag_None_r in H; try (rewrite H in * )
1302 | H : R_flag ?d ?f (Some ?y) |- R_flag ?d ?f ?x =>
1303 let HH := fresh in enough (HH : x = Some y) by (rewrite HH; exact H)
1304 | _ => progress (cbv [R_flags Tuple.fieldwise Tuple.fieldwise'] in *; cbn -[Syntax.operation_size] in * ; subst)
1305 | _ => destruct_one_match
1306 | _ => progress intuition idtac
1307 end; eauto. }
1308
1309 Unshelve. all : match goal with H : context[Syntax.rcr] |- _ => idtac | _ => shelve end; shelve_unifiable.
1310 all : change Symbolic.rcrcnt with rcrcnt in *.
1311 { repeat destruct_one_match; try lia.
1312 all:
1313 destruct s';
1314 repeat match goal with
1315 | H : R_flag _ ?f None |- _ => eapply R_flag_None_r in H; try (rewrite H in * )
1316 | H : R_flag ?d ?f (Some ?y) |- R_flag ?d ?f ?x =>
1317 let HH := fresh in enough (HH : x = Some y) by (rewrite HH; exact H)
1318 | H : _ = None |- _ => progress (try rewrite H; try rewrite H in * )
1319 | _ => progress (cbv [R_flags Tuple.fieldwise Tuple.fieldwise' set_flag set_flag_internal] in *; cbn -[Syntax.operation_size] in * ; subst)
1320 | _ => destruct_one_match
1321 | _ => progress intuition idtac
1322 end; try eauto; try Lia.lia.
1323 destr (machine_flag_state m0); cbn [Tuple.tuple'] in *; DestructHead.destruct_head'_prod; subst; Prod.inversion_prod; subst.
1324 f_equal.
1325 let E0 := match goal with H : rcrcnt _ _ = _ |- _ => H end in
1326 rewrite E0; cbn.
1327 rewrite <-2Z.bit0_odd, Z.lor_spec, Z.shiftl_spec_low, Bool.orb_false_r; trivial.
1328 pose_operation_size_cases; intuition (subst; cbn; clear; lia). }
1329
1330 Unshelve. all : match goal with H : context[Syntax.lea] |- _ => idtac | _ => shelve end; shelve_unifiable.
1331 { (* lea *)
1332 cbv [SetOperand update_reg_with] in *; Option.inversion_option; subst; trivial. }
1333
1334 Unshelve. all : match goal with H : context[Syntax.shrd] |- _ => idtac | _ => shelve end; shelve_unifiable.
1335 { eapply Z.bits_inj_iff'; intros i Hi.
1336 repeat rewrite ?Z.land_spec, ?Z.lor_spec, ?Z.shiftr_spec, ?Z.shiftl_spec, ?Z.testbit_ones_nonneg, ?Z.testbit_0_l; try lia.
1337 destr (i <? Z.of_N n); rewrite ?Bool.orb_false_r, ?Bool.andb_false_r, ?Bool.andb_true_r; trivial.
1338 replace v0 with v3 by congruence; f_equal; f_equal.
1339 rewrite Z.land_ones, Z.mod_small.
1340 1: lia.
1341 3: enough (0 <= Z.land v3 (Z.of_N n - 1)) by lia; eapply Z.land_nonneg; right.
1342 1,2,3:pose_operation_size_cases; intuition (subst; cbn; clear; lia). }
1343
1344 Unshelve. all : match goal with H : context[Syntax.shld] |- _ => idtac | _ => shelve end; shelve_unifiable.
1345 { repeat match goal with H : ?x = Some _, H' : ?x = Some _ |- _ => rewrite H' in *; Option.inversion_option end.
1346 progress subst.
1347 replace (Z.land (Z.of_N n) (Z.ones (Z.of_N n))) with (Z.of_N n)
1348 by (rewrite Z.land_ones, Z.mod_small; try split; try lia; apply Zpow_facts.Zpower2_lt_lin; lia).
1349 assert (0 <= Z.of_N n - 1) by (pose_operation_size_cases; intuition (subst; cbn; clear; lia)).
1350 rewrite <- !Z.shiftl_opp_r.
1351 rewrite !Z.shiftl_lor.
1352 rewrite <- !Z.land_lor_distr_l, <- Z.land_assoc, Z.land_diag.
1353 rewrite !Z.shiftl_shiftl by (try apply Z.land_nonneg; lia).
1354 f_equal; f_equal; f_equal; try lia. }
1355
1356 Unshelve. all : match goal with H : context[Syntax.shlx] |- _ => idtac | _ => shelve end; shelve_unifiable.
1357 { rewrite <- Z.land_assoc.
1358 f_equal; f_equal; [].
1359 pose_operation_size_cases; intuition subst; reflexivity. }
1360
1361 Unshelve. all : match goal with H : context[push] |- _ => idtac | H : context[pop] |- _ => idtac | _ => shelve end; shelve_unifiable.
1362 all: rewrite !Z.land_ones by lia; push_Zmod; pull_Zmod; f_equal; lia.
1363
1364 Unshelve. all: shelve_unifiable.
1365 all: fail_if_goals_remain ().
1366 Qed.
1367
1368 Lemma SymexLines_R {opts : symbolic_options_computed_opt} s m (HR : R s m) asm :
1369 forall _tt s', Symbolic.SymexLines asm s = Success (_tt, s') ->
1370 exists m', Semantics.DenoteLines m asm = Some m' /\ R s' m' /\ s :< s'.
1371 Proof using Type.
1372 revert dependent m; revert dependent s; induction asm; cbn [SymexLines DenoteLines]; intros.
1373 { inversion H; subst; eauto. }
1374 destruct_head' Line.
1375 rewrite unfold_bind in *; destruct_one_match_hyp; inversion_ErrorT.
1376 cbv [SymexLine SymexRawLine DenoteLine DenoteRawLine ret err Crypto.Util.Option.bind] in *; cbn in *.
1377 destruct_one_match_hyp; inversion_ErrorT; subst; eauto; destruct_head'_prod.
1378 eapply SymexNornalInstruction_R in E; eauto. destruct E as (m1&Hm1&Rm1&?). rewrite Hm1.
1379 eapply IHasm in H; eauto. destruct H as (?&?&?&?). break_innermost_match; eauto 9.
1380 Qed.
1381 End WithCtx1'.
1382 End WithFrame.