| 1 |
From Coq Require Import SetoidList. |
| 2 |
From Coq Require Import Permutation. |
| 3 |
From Coq Require Import List. |
| 4 |
From Coq Require Import Lia. |
| 5 |
From Coq Require Import NArith. |
| 6 |
From Coq Require Import ZArith. |
| 7 |
From Coq Require Import Morphisms. |
| 8 |
Require Import Crypto.Language.PreExtra. |
| 9 |
Require Import Crypto.Language.API. |
| 10 |
Require Import Crypto.Language.APINotations. |
| 11 |
Require Import Crypto.AbstractInterpretation.ZRange. |
| 12 |
Require Import Crypto.Util.ErrorT. |
| 13 |
Require Import Crypto.Assembly.Syntax. |
| 14 |
Require Import Crypto.Assembly.Symbolic. |
| 15 |
Require Import Crypto.Assembly.Equivalence. |
| 16 |
Require Import Crypto.CastLemmas. |
| 17 |
Require Import Crypto.Util.Option. |
| 18 |
Require Import Crypto.Util.Prod. |
| 19 |
Require Import Crypto.Util.Sigma. |
| 20 |
Require Import Crypto.Util.Notations. |
| 21 |
Require Import Crypto.Util.Sum. |
| 22 |
Require Import Crypto.Util.Bool.Reflect. |
| 23 |
Require Import Crypto.Util.Bool. |
| 24 |
Require Import Crypto.Util.NatUtil. |
| 25 |
Require Import Crypto.Util.ListUtil. |
| 26 |
Require Import Crypto.Util.ListUtil.Partition. |
| 27 |
Require Import Crypto.Util.ListUtil.Permutation. |
| 28 |
Require Import Crypto.Util.ListUtil.FoldMap. |
| 29 |
Require Import Crypto.Util.ListUtil.Forall. |
| 30 |
Require Import Crypto.Util.ListUtil.StdlibCompat. |
| 31 |
Require Import Crypto.Util.ListUtil.IndexOf. |
| 32 |
Require Import Crypto.Util.ListUtil.Split. |
| 33 |
Require Import Crypto.Util.OptionList. |
| 34 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 35 |
Require Import Crypto.Util.ZUtil.AddGetCarry. |
| 36 |
Require Import Crypto.Util.ZUtil.MulSplit. |
| 37 |
Require Import Crypto.Util.ZUtil.TruncatingShiftl. |
| 38 |
Require Import Crypto.Util.ZUtil.Land. |
| 39 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 40 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 41 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 42 |
Require Import Crypto.Util.ZUtil.Ones. |
| 43 |
Require Import Crypto.Util.ZUtil.LandLorShiftBounds. |
| 44 |
Require Import Crypto.Util.ZUtil.LandLorBounds. |
| 45 |
Require Import Crypto.Util.ZUtil.Tactics.PeelLe. |
| 46 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 47 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 48 |
Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy. |
| 49 |
Require Import Crypto.Util.Tactics.HasBody. |
| 50 |
Require Import Crypto.Util.Tactics.Head. |
| 51 |
Require Import Crypto.Util.Tactics.PrintContext. |
| 52 |
Require Import Crypto.Util.Tactics.PrintGoal. |
| 53 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 54 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 55 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 56 |
Require Import Crypto.Util.Tactics.SetEvars. |
| 57 |
Require Import Crypto.Util.Tactics.SubstEvars. |
| 58 |
Require Import Crypto.Util.Tactics.ClearHead. |
| 59 |
Import API.Compilers APINotations.Compilers AbstractInterpretation.ZRange.Compilers. |
| 60 |
Import ListNotations. |
| 61 |
Local Open Scope list_scope. |
| 62 |
|
| 63 |
(* TODO: move to global settings *) |
| 64 |
Local Set Keyed Unification. |
| 65 |
|
| 66 |
Definition eval_idx_Z (G : symbol -> option Z) (dag : dag) (idx : idx) (v : Z) : Prop |
| 67 |
:= eval G dag (ExprRef idx) v. |
| 68 |
Definition eval_idx_or_list_idx (G : symbol -> option Z) (d : dag) (v1 : idx + list idx) (v2 : Z + list Z) |
| 69 |
: Prop |
| 70 |
:= match v1, v2 with |
| 71 |
| inl idx, inl v => eval_idx_Z G d idx v |
| 72 |
| inr idxs, inr vs => Forall2 (eval_idx_Z G d) idxs vs |
| 73 |
| inl _, inr _ | inr _, inl _ => False |
| 74 |
end. |
| 75 |
|
| 76 |
Lemma lift_eval_idx_Z_impl G1 G2 d1 d2 |
| 77 |
(H : forall v n, eval G1 d1 v n -> eval G2 d2 v n) |
| 78 |
: forall v n, eval_idx_Z G1 d1 v n -> eval_idx_Z G2 d2 v n. |
| 79 |
Proof using Type. |
| 80 |
cbv [eval_idx_Z]; intros; destruct_head'_ex; destruct_head'_and; eauto. |
| 81 |
Qed. |
| 82 |
|
| 83 |
Lemma lift_eval_idx_or_list_idx_impl G1 G2 d1 d2 |
| 84 |
(H : forall v n, eval G1 d1 v n -> eval G2 d2 v n) |
| 85 |
: forall v n, eval_idx_or_list_idx G1 d1 v n -> eval_idx_or_list_idx G2 d2 v n. |
| 86 |
Proof using Type. |
| 87 |
cbv [eval_idx_or_list_idx]; intros; break_innermost_match; eauto using lift_eval_idx_Z_impl, Forall2_weaken. |
| 88 |
Qed. |
| 89 |
|
| 90 |
Lemma eval_eval_gen G1 G2 d1 d2 |
| 91 |
(H : forall v n, eval G1 d1 v n -> eval G2 d2 v n) |
| 92 |
: forall e v1 v2, eval G1 d1 e v1 -> eval G2 d2 e v2 -> v1 = v2. |
| 93 |
Proof. intros; eapply eval_eval; (idtac + apply H); eassumption. Qed. |
| 94 |
|
| 95 |
Lemma eval_eval_Forall2_gen G1 G2 d1 d2 |
| 96 |
(H : forall v n, eval G1 d1 v n -> eval G2 d2 v n) |
| 97 |
: forall e v1 v2, Forall2 (eval G1 d1) e v1 -> Forall2 (eval G2 d2) e v2 -> v1 = v2. |
| 98 |
Proof. intros; eapply eval_eval_Forall2; (idtac + (eapply Forall2_weaken; [ apply H | ])); eassumption. Qed. |
| 99 |
|
| 100 |
Lemma eval_eval_idx_Z G d |
| 101 |
: forall e v1 v2, eval_idx_Z G d e v1 -> eval_idx_Z G d e v2 -> v1 = v2. |
| 102 |
Proof. cbv [eval_idx_Z]. intros; eapply eval_eval; eassumption. Qed. |
| 103 |
|
| 104 |
Lemma eval_eval_idx_Z_gen G1 G2 d1 d2 |
| 105 |
(H : forall v n, eval G1 d1 v n -> eval G2 d2 v n) |
| 106 |
: forall e v1 v2, eval_idx_Z G1 d1 e v1 -> eval_idx_Z G2 d2 e v2 -> v1 = v2. |
| 107 |
Proof. intros; eapply eval_eval_idx_Z; (idtac + (eapply lift_eval_idx_Z_impl; [ apply H | ])); eassumption. Qed. |
| 108 |
|
| 109 |
Lemma eval_eval_idx_Z_Forall2 G d |
| 110 |
: forall e v1 v2, Forall2 (eval_idx_Z G d) e v1 -> Forall2 (eval_idx_Z G d) e v2 -> v1 = v2. |
| 111 |
Proof. |
| 112 |
intros *; rewrite <- Forall2_eq; rewrite !@Forall2_forall_iff_nth_error. |
| 113 |
intros H1 H2 i; specialize (H1 i); specialize (H2 i); revert H1 H2. |
| 114 |
cbv [option_eq]; break_innermost_match; try intuition congruence. |
| 115 |
apply eval_eval_idx_Z. |
| 116 |
Qed. |
| 117 |
|
| 118 |
Lemma eval_eval_idx_Z_Forall2_gen G1 G2 d1 d2 |
| 119 |
(H : forall v n, eval G1 d1 v n -> eval G2 d2 v n) |
| 120 |
: forall e v1 v2, Forall2 (eval_idx_Z G1 d1) e v1 -> Forall2 (eval_idx_Z G2 d2) e v2 -> v1 = v2. |
| 121 |
Proof. intros; eapply eval_eval_idx_Z_Forall2; (idtac + (eapply Forall2_weaken; [ apply lift_eval_idx_Z_impl, H | ])); eassumption. Qed. |
| 122 |
|
| 123 |
Lemma eval_eval_idx_or_list_idx G d |
| 124 |
: forall e v1 v2, eval_idx_or_list_idx G d e v1 -> eval_idx_or_list_idx G d e v2 -> v1 = v2. |
| 125 |
Proof. |
| 126 |
cbv [eval_idx_or_list_idx]; intros *; break_innermost_match; try intuition congruence; intros; apply f_equal. |
| 127 |
{ eapply eval_eval_idx_Z; eassumption. } |
| 128 |
{ eapply eval_eval_idx_Z_Forall2; eassumption. } |
| 129 |
Qed. |
| 130 |
|
| 131 |
Lemma eval_eval_idx_or_list_idx_gen G1 G2 d1 d2 |
| 132 |
(H : forall v n, eval G1 d1 v n -> eval G2 d2 v n) |
| 133 |
: forall e v1 v2, eval_idx_or_list_idx G1 d1 e v1 -> eval_idx_or_list_idx G2 d2 e v2 -> v1 = v2. |
| 134 |
Proof. intros; eapply eval_eval_idx_or_list_idx; (idtac + (eapply lift_eval_idx_or_list_idx_impl; [ apply H | ])); eassumption. Qed. |
| 135 |
|
| 136 |
Lemma eval_eval_idx_or_list_idx_Forall2 G d |
| 137 |
: forall e v1 v2, Forall2 (eval_idx_or_list_idx G d) e v1 -> Forall2 (eval_idx_or_list_idx G d) e v2 -> v1 = v2. |
| 138 |
Proof. |
| 139 |
intros *; rewrite <- Forall2_eq; rewrite !@Forall2_forall_iff_nth_error. |
| 140 |
intros H1 H2 i; specialize (H1 i); specialize (H2 i); revert H1 H2. |
| 141 |
cbv [option_eq]; break_innermost_match; try intuition congruence. |
| 142 |
apply eval_eval_idx_or_list_idx. |
| 143 |
Qed. |
| 144 |
|
| 145 |
Lemma eval_eval_idx_or_list_idx_Forall2_gen G1 G2 d1 d2 |
| 146 |
(H : forall v n, eval G1 d1 v n -> eval G2 d2 v n) |
| 147 |
: forall e v1 v2, Forall2 (eval_idx_or_list_idx G1 d1) e v1 -> Forall2 (eval_idx_or_list_idx G2 d2) e v2 -> v1 = v2. |
| 148 |
Proof. intros; eapply eval_eval_idx_or_list_idx_Forall2; (idtac + (eapply Forall2_weaken; [ apply lift_eval_idx_or_list_idx_impl, H | ])); eassumption. Qed. |
| 149 |
|
| 150 |
Section WithFixedCtx. |
| 151 |
Context (G : symbol -> option Z). |
| 152 |
|
| 153 |
Fixpoint eval_base_var (dag : dag) {t : base.type} : base_var t -> API.interp_type (type.base t) -> Prop := |
| 154 |
match t with |
| 155 |
| base.type.unit => fun _ _ => True |
| 156 |
| base.type.type_base base.type.Z => fun idx v => eval G dag (ExprRef idx) v |
| 157 |
| base.type.prod _ _ => fun a b => eval_base_var dag (fst a) (fst b) /\ eval_base_var dag (snd a) (snd b) |
| 158 |
| base.type.list _ => List.Forall2 (eval_base_var dag) |
| 159 |
| base.type.type_base base.type.nat => @eq _ |
| 160 |
| base.type.type_base base.type.zrange => @eq _ |
| 161 |
| _ => fun (bad : Empty_set) _ => match bad with end |
| 162 |
end%bool. |
| 163 |
|
| 164 |
Definition eval_var (dag : dag) {t : API.type} : var t -> API.interp_type t -> Prop |
| 165 |
:= match t with |
| 166 |
| type.base _ => eval_base_var dag |
| 167 |
| type.arrow _ _ => fun (bad : Empty_set) _ => match bad with end |
| 168 |
end. |
| 169 |
|
| 170 |
Local Lemma ex_Z_of_N_iff P v |
| 171 |
: (exists n, Z.of_N n = v /\ P n) <-> (0 <= v /\ P (Z.to_N v))%Z. |
| 172 |
Proof using Type. |
| 173 |
split; [ intros [n [H1 H2] ] | intros [H1 H2]; exists (Z.to_N v) ]; subst; rewrite ?N2Z.id, ?Z2N.id by lia. |
| 174 |
all: split; eauto; lia. |
| 175 |
Qed. |
| 176 |
|
| 177 |
Local Lemma ex_Z_of_N_iff' P v (H : (0 <= v)%Z) |
| 178 |
: (exists n, Z.of_N n = v /\ P n) <-> P (Z.to_N v). |
| 179 |
Proof using Type. rewrite ex_Z_of_N_iff; intuition lia. Qed. |
| 180 |
|
| 181 |
Lemma symex_T_app_curried_symex_T_error {t} err v d |
| 182 |
: @symex_T_app_curried t (symex_T_error err) v d = Error err. |
| 183 |
Proof using Type. induction t; cbn in *; break_innermost_match; try reflexivity; eauto. Qed. |
| 184 |
|
| 185 |
Lemma symex_T_app_curried_symex_T_bind_base_split T t v1 v2 v3 d rets d'' |
| 186 |
(H : symex_T_app_curried (@symex_T_bind_base T t v1 v2) v3 d = Success (rets, d'')) |
| 187 |
: exists rets' d', |
| 188 |
v1 d = Success (rets', d') |
| 189 |
/\ symex_T_app_curried (v2 rets') v3 d' = Success (rets, d''). |
| 190 |
Proof using Type. |
| 191 |
induction t; cbn [symex_T symex_T_bind_base symex_T_app_curried] in *. |
| 192 |
all: cbv [symex_bind ErrorT.bind] in *. |
| 193 |
all: break_innermost_match_hyps; try discriminate; eauto. |
| 194 |
Qed. |
| 195 |
|
| 196 |
Lemma symex_T_app_curried_symex_T_bind_base_split_err T t v1 v2 v3 d err |
| 197 |
(H : symex_T_app_curried (@symex_T_bind_base T t v1 v2) v3 d = Error err) |
| 198 |
: v1 d = Error err |
| 199 |
\/ exists rets' d', |
| 200 |
v1 d = Success (rets', d') |
| 201 |
/\ symex_T_app_curried (v2 rets') v3 d' = Error err. |
| 202 |
Proof using Type. |
| 203 |
induction t; cbn [symex_T symex_T_bind_base symex_T_app_curried] in *. |
| 204 |
all: cbv [symex_bind ErrorT.bind] in *. |
| 205 |
all: break_innermost_match_hyps; try discriminate; eauto. |
| 206 |
left; congruence. |
| 207 |
Qed. |
| 208 |
|
| 209 |
Lemma lift_eval_base_var_impl d1 d2 |
| 210 |
(H : forall v n, eval G d1 v n -> eval G d2 v n) |
| 211 |
: forall t v n, @eval_base_var d1 t v n -> @eval_base_var d2 t v n. |
| 212 |
Proof using Type. |
| 213 |
induction t; cbn [base_var eval_base_var]; break_innermost_match; intros; destruct_head'_ex; destruct_head'_and; subst; eauto using Forall2_weaken. |
| 214 |
Qed. |
| 215 |
Lemma lift_eval_var_impl d1 d2 |
| 216 |
(H : forall v n, eval G d1 v n -> eval G d2 v n) |
| 217 |
: forall t v n, @eval_var d1 t v n -> @eval_var d2 t v n. |
| 218 |
Proof using Type. |
| 219 |
induction t; cbn [var eval_var]; eauto using lift_eval_base_var_impl. |
| 220 |
Qed. |
| 221 |
|
| 222 |
Lemma RevealConstant_correct idx d d' v |
| 223 |
(H : RevealConstant idx d = Success (v, d')) |
| 224 |
(d_ok : gensym_dag_ok G d) |
| 225 |
: eval G d (ExprRef idx) (Z.of_N v) /\ d = d'. |
| 226 |
Proof using Type. |
| 227 |
cbv [RevealConstant] in *; break_innermost_match_hyps; |
| 228 |
inversion_ErrorT; inversion_prod; subst; split; trivial; []. |
| 229 |
pose proof Heqe as Hx. |
| 230 |
cbv [reveal reveal_step] in Hx. |
| 231 |
break_innermost_match_hyps; inversion Hx; subst. |
| 232 |
eapply map_eq_nil in H1; subst. |
| 233 |
repeat (eauto || econstructor). |
| 234 |
rewrite Z2N.id; eauto using Zle_bool_imp_le. |
| 235 |
Qed. |
| 236 |
|
| 237 |
Lemma RevealWidth_correct idx d d' v |
| 238 |
(H : RevealWidth idx d = Success (v, d')) |
| 239 |
(d_ok : gensym_dag_ok G d) |
| 240 |
: eval G d (ExprRef idx) (2 ^ Z.of_N v) /\ d = d'. |
| 241 |
Proof using Type. |
| 242 |
cbv [RevealWidth symex_bind ErrorT.bind symex_return symex_error] in *. |
| 243 |
break_innermost_match_hyps; inversion_ErrorT; inversion_prod; subst. |
| 244 |
eapply RevealConstant_correct in Heqe; intuition idtac. |
| 245 |
eapply Neqb_ok in Heqb; subst. |
| 246 |
epose proof N2Z.inj_pow 2 (N.log2 n) as HH. |
| 247 |
rewrite <-Heqb in HH. rewrite HH in H. exact H. |
| 248 |
Qed. |
| 249 |
|
| 250 |
(** Makes use of [eval_merge] and [eval_merge_node] lemmas, leaving |
| 251 |
over evars for unknown evaluation values, reading off which [merge] |
| 252 |
and [merge_node] constructs to pose proofs about from the goal and |
| 253 |
hypotheses *) |
| 254 |
Local Ltac saturate_eval_merge_step := |
| 255 |
first [ eassumption |
| 256 |
| let saturate_lem eval_lem mergev do_change do_clear := |
| 257 |
(** ensure that we do innermost merge first *) |
| 258 |
(let e := lazymatch mergev with |
| 259 |
| merge_node ?e ?d => e |
| 260 |
| merge ?e ?d => e |
| 261 |
end in |
| 262 |
lazymatch e with |
| 263 |
| context[merge_node] => fail |
| 264 |
| context[merge] => fail |
| 265 |
| _ => idtac |
| 266 |
end); |
| 267 |
let n := open_constr:(_) in |
| 268 |
let G := open_constr:(_) in |
| 269 |
let T := open_constr:(_) in |
| 270 |
cut T; |
| 271 |
[ let H := fresh in |
| 272 |
intro H; |
| 273 |
let T' := open_constr:(_) in |
| 274 |
cut T'; |
| 275 |
[ let H' := fresh in |
| 276 |
intro H'; |
| 277 |
let H'' := fresh in |
| 278 |
pose proof (eval_lem G n H' H); cbv beta zeta in *; |
| 279 |
do_change (); |
| 280 |
let k := fresh in |
| 281 |
set (k := mergev) in *; clearbody k |
| 282 |
| do_clear () ] |
| 283 |
| do_clear () ] |
| 284 |
in |
| 285 |
let saturate_of T do_clear := |
| 286 |
match T with |
| 287 |
| context[merge_node (@pair ?A ?B ?op ?args) ?d] |
| 288 |
=> saturate_lem (fun G n H' H => eval_merge_node G d H' op args n H) (merge_node (@pair A B op args) d) |
| 289 |
ltac:(fun _ => progress change (op, args) with (@pair A B op args) in * ) do_clear |
| 290 |
| context[merge ?e ?d] |
| 291 |
=> saturate_lem (fun G n H' H => eval_merge G e n d H' H) (merge e d) |
| 292 |
ltac:(fun _ => idtac) do_clear |
| 293 |
end in |
| 294 |
match goal with |
| 295 |
| [ |- ?T ] => saturate_of T ltac:(fun _ => idtac) |
| 296 |
| [ H : ?T |- _ ] => saturate_of T ltac:(fun _ => clear H) |
| 297 |
end |
| 298 |
| progress destruct_head'_and |
| 299 |
| solve [ eauto 100 ] ]. |
| 300 |
Local Ltac saturate_eval_merge := repeat saturate_eval_merge_step. |
| 301 |
|
| 302 |
Lemma App_correct {opts : symbolic_options_computed_opt} {descr:description} n d (Hdag : gensym_dag_ok G d) i d' (H : App n d = Success (i, d')) |
| 303 |
v (Heval : eval_node G d n v) |
| 304 |
: eval G d' (ExprRef i) v /\ gensym_dag_ok G d' /\ (forall e n, eval G d e n -> eval G d' e n). |
| 305 |
Proof using Type. |
| 306 |
cbv [App] in *. inversion_ErrorT. |
| 307 |
match goal with |
| 308 |
| H : context [simplify ?d ?n] |- _ => |
| 309 |
unique pose proof (fun G => eval_simplify G d n) |
| 310 |
end. |
| 311 |
specialize_under_binders_by eassumption. |
| 312 |
match goal with |
| 313 |
| H : context [merge ?e ?d] |- _ => |
| 314 |
edestruct (fun G n => eval_merge G e n d) as (?&?&?); try eassumption |
| 315 |
end. |
| 316 |
inversion_prod; subst; eauto 9. |
| 317 |
Qed. |
| 318 |
|
| 319 |
(* workaround: using cbn instead of this lemma makes Qed hang after next destruct *) |
| 320 |
Lemma unfold_symex_bind {A B} ma amb : |
| 321 |
@symex_bind A B ma amb = ltac:(let t := eval cbv [symex_bind ErrorT.bind] in (@symex_bind A B ma amb) in exact t). |
| 322 |
Proof using Type. exact eq_refl. Qed. |
| 323 |
|
| 324 |
Theorem symex_ident_correct |
| 325 |
{opts : symbolic_options_computed_opt} |
| 326 |
{descr:description} |
| 327 |
{t} (idc : ident t) |
| 328 |
(d : dag) |
| 329 |
(input_var_data : type.for_each_lhs_of_arrow var t) |
| 330 |
(input_runtime_var : type.for_each_lhs_of_arrow API.interp_type t) |
| 331 |
(* N.B. We need [Hinput] for the [gensym_dag_ok] conclusion, because |
| 332 |
we need to know that we can evaluate the indices in the |
| 333 |
arguments w.r.t. the dag for, e.g., [eval_merge_node] *) |
| 334 |
(Hinput : type.and_for_each_lhs_of_arrow (@eval_var d) input_var_data input_runtime_var) |
| 335 |
(rets : base_var (type.final_codomain t)) |
| 336 |
(d' : dag) |
| 337 |
(H : symex_T_app_curried (symex_ident idc) input_var_data d = Success (rets, d')) |
| 338 |
(d_ok : gensym_dag_ok G d) |
| 339 |
: eval_base_var d' rets (type.app_curried (Compilers.ident_interp idc) input_runtime_var) |
| 340 |
/\ gensym_dag_ok G d' |
| 341 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 342 |
Proof using Type. |
| 343 |
cbv [symex_ident] in H; break_innermost_match_hyps. |
| 344 |
all: cbn [type.app_curried type.and_for_each_lhs_of_arrow Compilers.ident_interp symex_T_app_curried type.for_each_lhs_of_arrow] in *. |
| 345 |
all: cbn [API.interp_type base_var type.final_codomain var] in *. |
| 346 |
all: destruct_head'_prod; destruct_head'_unit. |
| 347 |
all: cbv [symex_T_error symex_error symex_return ident.eagerly ident.literal] in *. |
| 348 |
all : repeat match goal with |
| 349 |
| H : context[symex_bind _ _] |- _ => rewrite !unfold_symex_bind in H |
| 350 |
| _ => destruct_one_head'_and |
| 351 |
| _ => inversion_ErrorT_step |
| 352 |
| H : App ?e ?d = Success (?i, ?d') |- _ => |
| 353 |
eapply (App_correct e d ltac:(eassumption)) in H; [|clear H..] |
| 354 |
| [ H : RevealWidth _ _ = Success _ |- _ ] |
| 355 |
=> eapply RevealWidth_correct in H; [ | clear H; try eassumption .. ] |
| 356 |
| [ H : RevealConstant _ _ = Success _ |- _ ] |
| 357 |
=> eapply RevealConstant_correct in H; [ | clear H; try eassumption .. ] |
| 358 |
| H : eval ?a ?b ?c ?x, G : eval ?a ?b ?c ?y |- _ => epose proof eval_eval _ _ _ _ H _ G; clear G |
| 359 |
| _ => inversion_prod_step |
| 360 |
| _ => break_innermost_match_hyps_step |
| 361 |
| _ => progress cbn [eval_base_var eval_var fst snd List.map] in * |
| 362 |
| _ => progress subst |
| 363 |
| |- eval _ _ (ExprRef _) _ => solve[eauto 99 with nocore] |
| 364 |
| |- eval_node _ _ (?op, ?args) _ => let h := Head.head op in is_constructor h; econstructor |
| 365 |
| |- interp_op _ ?op ?args = Some _ => let h := Head.head op in is_constructor h; exact eq_refl |
| 366 |
| |- Forall2 _ _ _ => econstructor |
| 367 |
end. |
| 368 |
all : Tactics.ssplit; eauto 9. |
| 369 |
all : match goal with |
| 370 |
| |- context[eval _ _ _ _] => idtac (* solve below *) |
| 371 |
| _ => solve [ repeat first [ solve [ eauto using length_Forall2, Forall.Forall2_firstn, Forall.Forall2_skipn, Forall.Forall2_combine, Forall2_app, Forall2_rev ] |
| 372 |
| eapply Forall.Forall2_combine; [ | eassumption .. ]; eauto |
| 373 |
| rewrite Forall2_eq |
| 374 |
| rewrite Forall.Forall2_repeat_iff |
| 375 |
| now apply Forall.Forall2_forall_iff'' ] ] end. |
| 376 |
all: match goal with (* eval_same_expr_goal *) |
| 377 |
| |- eval G ?d ?e ?v => |
| 378 |
match goal with |
| 379 |
H : eval G d e ?v' |- _ => |
| 380 |
let Heq := fresh in |
| 381 |
enough (Heq : v = v') by (rewrite Heq; exact H); |
| 382 |
try clear H e |
| 383 |
end |
| 384 |
| |- eval G ?d ?e ?v => |
| 385 |
let H := fresh in |
| 386 |
let v' := open_constr:(_) in |
| 387 |
eassert (eval G d e v') by (eauto 99 with nocore); |
| 388 |
let Heq := fresh in |
| 389 |
enough (Heq : v = v') by (rewrite Heq; exact H); |
| 390 |
try clear H e |
| 391 |
end. |
| 392 |
all : cbv [fold_right Z.add_with_carry Z.truncating_shiftl] in *. |
| 393 |
all : repeat (rewrite ?Z.mul_split_mod, ?Z.mul_split_div, ?Z.mul_high_div, |
| 394 |
?Z.add_get_carry_full_div, ?Z.add_get_carry_full_mod, ?Z.sub_add_distr, |
| 395 |
?Z.add_with_get_carry_full_div, ?Z.add_with_get_carry_full_mod, |
| 396 |
?Z.sub_get_borrow_full_div, ?Z.sub_get_borrow_full_mod, ?Z.sub_0_r, |
| 397 |
?Z.sub_with_get_borrow_full_div, ?Z.sub_with_get_borrow_full_mod, |
| 398 |
?Z.add_0_r, ?Z.mul_1_r, ?Z.lor_0_r, ?Z.land_m1_r, ?Z.add_opp_r, |
| 399 |
?Z.add_assoc, ?Z.shiftr_0_r, ?Z.shiftr_div_pow2, ?Z.land_ones; |
| 400 |
push_Zmod; pull_Zmod); |
| 401 |
try Lia.lia. |
| 402 |
all: repeat first [ progress cbn [fst snd List.map] in * |
| 403 |
| progress subst |
| 404 |
| progress split_andb |
| 405 |
| progress reflect_hyps |
| 406 |
| progress cbv [ident.cast2] |
| 407 |
| match goal with |
| 408 |
| [ H : _ = fst ?x |- _ ] => is_var x; destruct x |
| 409 |
| [ H : _ = snd ?x |- _ ] => is_var x; destruct x |
| 410 |
end ]. |
| 411 |
all : match goal with |
| 412 |
| [ |- context[ident.cast ?r ?v] ] |
| 413 |
=> rewrite !ident.platform_specific_cast_0_is_mod |
| 414 |
by match goal with |
| 415 |
| [ H : Z.succ ?v = (2^Z.log2 (Z.succ ?v))%Z |- (0 <= ?v)%Z ] |
| 416 |
=> clear -H; destruct v; try lia; rewrite Z.log2_nonpos, Z.pow_0_r in H by lia; lia |
| 417 |
end |
| 418 |
end. |
| 419 |
all : rewrite Z2N.id by eapply Z.log2_nonneg. |
| 420 |
all : match goal with H : Z.succ _=_|-_=> rewrite <-H, Z.add_1_r end; trivial. |
| 421 |
all : fail. |
| 422 |
Qed. |
| 423 |
|
| 424 |
Theorem symex_expr_correct |
| 425 |
{opts : symbolic_options_computed_opt} |
| 426 |
{descr:description} |
| 427 |
{t} (expr1 : API.expr (var:=var) t) (expr2 : API.expr (var:=API.interp_type) t) |
| 428 |
(d : dag) |
| 429 |
(input_var_data : type.for_each_lhs_of_arrow var t) (input_runtime_var : type.for_each_lhs_of_arrow API.interp_type t) |
| 430 |
(Hinputs : type.and_for_each_lhs_of_arrow (@eval_var d) input_var_data input_runtime_var) |
| 431 |
(rets : base_var (type.final_codomain t)) |
| 432 |
(d' : dag) |
| 433 |
GG |
| 434 |
(HG : forall t v1 v2, List.In (existT _ t (v1, v2)) GG -> eval_var d v1 v2) |
| 435 |
(Hwf : API.wf GG expr1 expr2) |
| 436 |
(H : symex_T_app_curried (symex_expr expr1) input_var_data d = Success (rets, d')) |
| 437 |
(d_ok : gensym_dag_ok G d) |
| 438 |
: eval_base_var d' rets (type.app_curried (API.interp expr2) input_runtime_var) |
| 439 |
/\ gensym_dag_ok G d' |
| 440 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 441 |
Proof using Type. |
| 442 |
revert dependent descr; revert dependent d; revert dependent d'; revert dependent input_var_data; revert dependent input_runtime_var. |
| 443 |
induction Hwf; intros; cbn [symex_expr] in *. |
| 444 |
all: repeat first [ match goal with |
| 445 |
(* ident case *) |
| 446 |
| [ |- context[expr.Ident] ] => eapply @symex_ident_correct; eassumption |
| 447 |
(* var case *) |
| 448 |
| [ HIn : List.In (existT _ ?t (?v1, ?v2)) _ |- context[expr.Var] ] |
| 449 |
=> is_var t; specialize (HG t _ _ HIn); destruct t |
| 450 |
(* abs case, eventually *) |
| 451 |
| [ H : context[_ /\ gensym_dag_ok _ /\ (forall e n, _ -> _)] |- context[expr.Abs] ] |
| 452 |
=> eapply H; try eassumption; clear H; [] |
| 453 |
end |
| 454 |
| match goal with |
| 455 |
| [ H : symex_T_app_curried (symex_T_bind_base _ _) _ _ = Success _ |- _ ] |
| 456 |
=> apply symex_T_app_curried_symex_T_bind_base_split in H |
| 457 |
| [ H : forall v, (forall t v1 v2, existT _ _ _ = _ \/ _ -> _) -> _ |- _ ] |
| 458 |
=> specialize (fun H3 v H2 |
| 459 |
=> H v (fun t v1 v2 pf |
| 460 |
=> match pf with |
| 461 |
| or_introl pf => H2 t v1 v2 pf |
| 462 |
| or_intror pf => H3 t v1 v2 pf |
| 463 |
end)) |
| 464 |
end |
| 465 |
| exfalso; assumption |
| 466 |
| progress subst |
| 467 |
| progress destruct_head'_and |
| 468 |
| progress destruct_head'_ex |
| 469 |
| progress inversion_ErrorT |
| 470 |
| progress inversion_pair |
| 471 |
| progress cbn [symex_T_app_curried symex_T_return eval_var type.app_curried] in * |
| 472 |
| progress cbn [type.and_for_each_lhs_of_arrow type.for_each_lhs_of_arrow fst snd List.In eq_rect] in * |
| 473 |
| progress cbn [type.final_codomain base_var] in * |
| 474 |
| progress cbv [symex_return] in * |
| 475 |
| rewrite @symex_T_app_curried_symex_T_error in * |
| 476 |
| solve [ auto ] |
| 477 |
| break_innermost_match_hyps_step |
| 478 |
| progress specialize_by_assumption |
| 479 |
| progress |
| 480 |
guarded_specialize_dep_under_binders_by |
| 481 |
ltac:(idtac; |
| 482 |
lazymatch goal with |
| 483 |
| [ |- True ] => exact I |
| 484 |
| [ |- unit ] => exact tt |
| 485 |
| [ |- _ /\ _ ] => split |
| 486 |
| [ |- _ * _ ] => split |
| 487 |
end) |
| 488 |
ltac:(fun H => lazymatch type of H with |
| 489 |
| context[forall x : True, _] => idtac |
| 490 |
| context[forall x : unit, _] => idtac |
| 491 |
| context[forall x : _ * _, _] => idtac |
| 492 |
| context[forall x : _ /\ _, _] => idtac |
| 493 |
end) |
| 494 |
| progress |
| 495 |
guarded_specialize_under_binders_by |
| 496 |
ltac:(idtac; |
| 497 |
match goal with |
| 498 |
| [ H : symex_T_app_curried _ _ _ = Success _ |- symex_T_app_curried _ _ _ = Success _ ] |
| 499 |
=> exact H |
| 500 |
| [ H : symex_expr _ _ = Success _ |- symex_expr _ _ = Success _ ] |
| 501 |
=> exact H |
| 502 |
end) |
| 503 |
ltac:(fun H => lazymatch type of H with |
| 504 |
| context[symex_T_app_curried _ _ _ = Success _ -> _] => idtac |
| 505 |
| context[symex_expr _ _ = Success _ -> _] => idtac |
| 506 |
end) |
| 507 |
| progress |
| 508 |
guarded_specialize_under_binders_by |
| 509 |
ltac:(idtac; |
| 510 |
match goal with |
| 511 |
| [ H : type.and_for_each_lhs_of_arrow _ _ _ |- type.and_for_each_lhs_of_arrow _ _ _ ] |
| 512 |
=> first [ exact H |
| 513 |
| eapply Wf.Compilers.type.and_for_each_lhs_of_arrow_Proper_impl; [ cbv [forall_relation pointwise_relation Basics.impl] .. | exact H ]; try eapply lift_eval_var_impl; eauto ] |
| 514 |
| [ H : eval_base_var _ _ _ |- eval_base_var _ _ _ ] => exact H |
| 515 |
| [ |- forall t v1 v2, existT _ _ _ = existT _ t (v1, v2) -> _ ] |
| 516 |
=> intros; inversion_sigma; subst; cbn [eq_rect] in *; inversion_prod; subst; eassumption |
| 517 |
end) |
| 518 |
ltac:(fun H => lazymatch type of H with |
| 519 |
| context[type.and_for_each_lhs_of_arrow _ _ _ -> _] => idtac |
| 520 |
| context[eval_base_var _ _ _ -> _] => idtac |
| 521 |
| context[(forall t v1 v2, existT _ _ _ = existT _ t (v1, v2) -> _) -> _] => idtac |
| 522 |
end) |
| 523 |
| progress specialize_under_binders_by eauto using lift_eval_var_impl ]. |
| 524 |
Qed. |
| 525 |
|
| 526 |
Lemma symex_expr_App_curried {opts : symbolic_options_computed_opt} {descr:description} {t} (e : API.expr t) input_var_data d |
| 527 |
: symex_expr (invert_expr.App_curried e (type.map_for_each_lhs_of_arrow (fun t v => ($$v)%expr) input_var_data)) d |
| 528 |
= symex_T_app_curried (symex_expr e) input_var_data d. |
| 529 |
Proof using Type. |
| 530 |
induction t; cbn [invert_expr.App_curried symex_T_app_curried type.map_for_each_lhs_of_arrow]; |
| 531 |
destruct_head_hnf' prod; destruct_head_hnf' unit; cbn [fst snd]; try reflexivity. |
| 532 |
match goal with H : _ |- _ => rewrite H; clear H end. |
| 533 |
repeat first [ progress cbn [symex_expr var] in * |
| 534 |
| progress break_innermost_match |
| 535 |
| solve [ destruct_head'_Empty_set ] ]. |
| 536 |
cbv [symex_T_return symex_return] in *. |
| 537 |
let LHS := match goal with |- ?LHS = _ => LHS end in |
| 538 |
destruct LHS as [ [? ?] | ? ] eqn:H; |
| 539 |
[ apply symex_T_app_curried_symex_T_bind_base_split in H | apply symex_T_app_curried_symex_T_bind_base_split_err in H ]. |
| 540 |
all: destruct_head'_or; destruct_head'_ex; destruct_head'_and; congruence. |
| 541 |
Qed. |
| 542 |
|
| 543 |
Lemma symex_expr_smart_App_curried {opts : symbolic_options_computed_opt} {descr:description} {t} (e : API.expr t) input_var_data d |
| 544 |
: symex_expr (invert_expr.smart_App_curried e input_var_data) d |
| 545 |
= symex_T_app_curried (symex_expr e) input_var_data d. |
| 546 |
Proof using Type. |
| 547 |
induction e; cbn [invert_expr.smart_App_curried]; |
| 548 |
rewrite ?symex_expr_App_curried; try reflexivity. |
| 549 |
match goal with H : _ |- _ => rewrite H; clear H end. |
| 550 |
cbn [symex_expr symex_T_app_curried]; break_innermost_match; cbn [fst snd]; reflexivity. |
| 551 |
Qed. |
| 552 |
|
| 553 |
Theorem symex_PHOAS_PHOAS_correct |
| 554 |
{opts : symbolic_options_computed_opt} |
| 555 |
{t} (expr : API.Expr t) |
| 556 |
(d : dag) |
| 557 |
(input_var_data : type.for_each_lhs_of_arrow var t) (input_runtime_var : type.for_each_lhs_of_arrow API.interp_type t) |
| 558 |
(Hinputs : type.and_for_each_lhs_of_arrow (@eval_var d) input_var_data input_runtime_var) |
| 559 |
(rets : base_var (type.final_codomain t)) |
| 560 |
(d' : dag) |
| 561 |
(Hwf : API.Wf expr) |
| 562 |
(H : symex_PHOAS_PHOAS expr input_var_data d = Success (rets, d')) |
| 563 |
(d_ok : gensym_dag_ok G d) |
| 564 |
: eval_base_var d' rets (type.app_curried (API.Interp expr) input_runtime_var) |
| 565 |
/\ gensym_dag_ok G d' |
| 566 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 567 |
Proof using Type. |
| 568 |
cbv [symex_PHOAS_PHOAS] in H. |
| 569 |
rewrite symex_expr_smart_App_curried in H. |
| 570 |
eapply symex_expr_correct with (GG:=[]); try eassumption; cbn [List.In]; try eapply Hwf; try now intros; exfalso. |
| 571 |
Qed. |
| 572 |
|
| 573 |
Fixpoint build_base_runtime (t : base.type) (values : list (Z + list Z)) |
| 574 |
: option (base.interp t * list (Z + list Z)) |
| 575 |
:= match t, values return option (base.interp t * list (Z + list Z)) with |
| 576 |
| base.type.unit, _ |
| 577 |
=> Some (tt, values) |
| 578 |
| base.type.type_base base.type.Z, inl val :: values |
| 579 |
=> Some (val, values) |
| 580 |
| base.type.prod A B, _ |
| 581 |
=> (vA <- build_base_runtime A values; |
| 582 |
let '(vA, values) := vA in |
| 583 |
vB <- build_base_runtime B values; |
| 584 |
let '(vB, values) := vB in |
| 585 |
Some ((vA, vB), values)) |
| 586 |
| base.type.list (base.type.type_base base.type.Z), inr value :: values |
| 587 |
=> Some (value, values) |
| 588 |
| base.type.type_base base.type.Z, inr _ :: _ |
| 589 |
| base.type.list (base.type.type_base base.type.Z), inl _ :: _ |
| 590 |
| base.type.type_base _, [] |
| 591 |
| base.type.list _, [] |
| 592 |
| base.type.type_base _, _ |
| 593 |
| base.type.option _, _ |
| 594 |
| base.type.list _, _ :: _ |
| 595 |
=> None |
| 596 |
end%option. |
| 597 |
Definition build_runtime (t : API.type) (values : list (Z + list Z)) |
| 598 |
: option (API.interp_type t * list (Z + list Z)) |
| 599 |
:= match t with |
| 600 |
| type.base t => build_base_runtime t values |
| 601 |
| type.arrow _ _ => None |
| 602 |
end. |
| 603 |
Fixpoint build_input_runtime (t : API.type) (values : list (Z + list Z)) |
| 604 |
: option (type.for_each_lhs_of_arrow API.interp_type t * list (Z + list Z)) |
| 605 |
:= match t with |
| 606 |
| type.base _ => Some (tt, values) |
| 607 |
| type.arrow A B |
| 608 |
=> (vA <- build_runtime A values; |
| 609 |
let '(vA, values) := vA in |
| 610 |
vB <- build_input_runtime B values; |
| 611 |
let '(vB, values) := vB in |
| 612 |
Some ((vA, vB), values)) |
| 613 |
end%option. |
| 614 |
|
| 615 |
Fixpoint simplify_base_runtime {t : base.type} : base.interp t -> option (list (Z + list Z)) |
| 616 |
:= match t return base.interp t -> option (list (Z + list Z)) with |
| 617 |
| base.type.unit |
| 618 |
=> fun 'tt => Some [] |
| 619 |
| base.type.type_base base.type.Z => fun val => Some [inl val] |
| 620 |
| base.type.prod A B => fun ab => (a <- simplify_base_runtime (fst ab); b <- simplify_base_runtime (snd ab); Some (a ++ b)) |
| 621 |
| base.type.list (base.type.type_base base.type.Z) |
| 622 |
=> fun ls : list Z => Some [inr ls] |
| 623 |
| base.type.list _ |
| 624 |
| base.type.type_base _ |
| 625 |
| base.type.option _ |
| 626 |
=> fun _ => None |
| 627 |
end%option. |
| 628 |
|
| 629 |
Lemma build_base_var_runtime_gen |
| 630 |
d |
| 631 |
(inputs : list (idx + list idx)) (runtime_inputs : list (Z + list Z)) |
| 632 |
(Hinputs : Forall2 (eval_idx_or_list_idx G d) inputs runtime_inputs) |
| 633 |
t |
| 634 |
: match build_base_var t inputs, build_base_runtime t runtime_inputs with |
| 635 |
| Success (v1, ls1), Some (v2, ls2) |
| 636 |
=> eval_base_var d v1 v2 |
| 637 |
/\ Forall2 (eval_idx_or_list_idx G d) ls1 ls2 |
| 638 |
| Success _, None | Error _, Some _ => False |
| 639 |
| Error _, None => True |
| 640 |
end. |
| 641 |
Proof using Type. |
| 642 |
revert inputs runtime_inputs Hinputs; induction t; cbn [build_base_var build_base_runtime]; intros; break_innermost_match. |
| 643 |
all: repeat first [ exact I |
| 644 |
| progress subst |
| 645 |
| progress cbn [eval_idx_or_list_idx eval_base_var] in * |
| 646 |
| progress cbv [Crypto.Util.Option.bind Rewriter.Util.Option.bind ErrorT.bind] in * |
| 647 |
| exfalso; assumption |
| 648 |
| solve [ auto ] |
| 649 |
| match goal with |
| 650 |
| [ H : Forall2 _ [] _ |- _ ] => inversion H; clear H |
| 651 |
| [ H : Forall2 _ _ [] |- _ ] => inversion H; clear H |
| 652 |
| [ H : Forall2 _ (_ :: _) _ |- _ ] => inversion H; clear H |
| 653 |
| [ H : Forall2 _ _ (_ :: _) |- _ ] => inversion H; clear H |
| 654 |
end |
| 655 |
| progress break_innermost_match_hyps |
| 656 |
| progress specialize_by_assumption |
| 657 |
| progress destruct_head'_and |
| 658 |
| match goal with |
| 659 |
| [ H : context[match build_base_var ?t1 _ with _ => _ end] |- context[build_base_var ?t1 ?inputs] ] |
| 660 |
=> specialize (H inputs) |
| 661 |
| [ H : context[match build_base_runtime ?t1 _ with _ => _ end] |- context[build_base_runtime ?t1 ?inputs] ] |
| 662 |
=> specialize (H inputs) |
| 663 |
end ]. |
| 664 |
Qed. |
| 665 |
|
| 666 |
Lemma build_var_runtime_gen |
| 667 |
d |
| 668 |
(inputs : list (idx + list idx)) (runtime_inputs : list (Z + list Z)) |
| 669 |
(Hinputs : Forall2 (eval_idx_or_list_idx G d) inputs runtime_inputs) |
| 670 |
t |
| 671 |
: match build_var t inputs, build_runtime t runtime_inputs with |
| 672 |
| Success (v1, ls1), Some (v2, ls2) |
| 673 |
=> eval_var d v1 v2 |
| 674 |
/\ Forall2 (eval_idx_or_list_idx G d) ls1 ls2 |
| 675 |
| Success _, None | Error _, Some _ => False |
| 676 |
| Error _, None => True |
| 677 |
end. |
| 678 |
Proof using Type. |
| 679 |
destruct t as [t|s d']; [ pose proof (build_base_var_runtime_gen _ _ _ Hinputs t) | ]. |
| 680 |
all: cbv [build_var build_runtime]; break_innermost_match; try assumption; try exact I. |
| 681 |
Qed. |
| 682 |
|
| 683 |
Lemma build_input_var_runtime_gen |
| 684 |
d |
| 685 |
(inputs : list (idx + list idx)) (runtime_inputs : list (Z + list Z)) |
| 686 |
(Hinputs : Forall2 (eval_idx_or_list_idx G d) inputs runtime_inputs) |
| 687 |
t |
| 688 |
: match build_input_var t inputs, build_input_runtime t runtime_inputs with |
| 689 |
| Success (v1, ls1), Some (v2, ls2) |
| 690 |
=> type.and_for_each_lhs_of_arrow (@eval_var d) v1 v2 |
| 691 |
/\ Forall2 (eval_idx_or_list_idx G d) ls1 ls2 |
| 692 |
| Success _, None | Error _, Some _ => False |
| 693 |
| Error _, None => True |
| 694 |
end. |
| 695 |
Proof using Type. |
| 696 |
revert inputs runtime_inputs Hinputs; induction t as [|s _ d' IHd]; |
| 697 |
cbn [build_input_runtime build_input_var build_var type.and_for_each_lhs_of_arrow] in *; intros; auto; []. |
| 698 |
cbv [Crypto.Util.Option.bind Rewriter.Util.Option.bind ErrorT.bind]. |
| 699 |
pose proof (build_var_runtime_gen _ _ _ Hinputs s). |
| 700 |
break_innermost_match_hyps; try exact I; try (now exfalso); []. |
| 701 |
destruct_head'_and. |
| 702 |
let l := match goal with |- context[build_input_var _ ?l] => l end in |
| 703 |
specialize (IHd l _ ltac:(eassumption)). |
| 704 |
break_innermost_match_hyps; try exact I; try (now exfalso); []. |
| 705 |
destruct_head'_and; auto. |
| 706 |
Qed. |
| 707 |
|
| 708 |
Lemma simplify_base_var_runtime_gen {t} d b v |
| 709 |
(H : eval_base_var d b v) |
| 710 |
: match @simplify_base_var t b, @simplify_base_runtime t v with |
| 711 |
| Success rets, Some val => Forall2 (eval_idx_or_list_idx G d) rets val |
| 712 |
| Success _, None | Error _, Some _ => False |
| 713 |
| Error _, None => True |
| 714 |
end. |
| 715 |
Proof using Type. |
| 716 |
induction t; cbn [eval_base_var simplify_base_var simplify_base_runtime] in *; break_innermost_match. |
| 717 |
all: repeat first [ assumption |
| 718 |
| exact I |
| 719 |
| match goal with |
| 720 |
| [ |- Forall2 _ _ _ ] => constructor |
| 721 |
end |
| 722 |
| progress destruct_head'_and |
| 723 |
| progress destruct_head_hnf' prod |
| 724 |
| progress cbn [fst snd] in * |
| 725 |
| progress cbv [Crypto.Util.Option.bind Rewriter.Util.Option.bind ErrorT.bind] in * |
| 726 |
| match goal with |
| 727 |
| [ IH : context[simplify_base_var _] |- context[simplify_base_var ?b] ] |
| 728 |
=> specialize (IH b _ ltac:(eassumption)) |
| 729 |
end |
| 730 |
| progress break_innermost_match_hyps |
| 731 |
| rewrite Forall.Forall2_map_map_iff |
| 732 |
| solve [ eauto using Forall2_app ] ]. |
| 733 |
Qed. |
| 734 |
|
| 735 |
Theorem symex_PHOAS_correct |
| 736 |
{opts : symbolic_options_computed_opt} |
| 737 |
{t} (expr : API.Expr t) |
| 738 |
(d : dag) |
| 739 |
(inputs : list (idx + list idx)) (runtime_inputs : list (Z + list Z)) |
| 740 |
(Hinputs : List.Forall2 (eval_idx_or_list_idx G d) inputs runtime_inputs) |
| 741 |
(rets : list (idx + list idx)) |
| 742 |
(d' : dag) |
| 743 |
(Hwf : API.Wf expr) |
| 744 |
(H : symex_PHOAS expr inputs d = Success (rets, d')) |
| 745 |
(d_ok : gensym_dag_ok G d) |
| 746 |
: (exists (input_runtime_var : type.for_each_lhs_of_arrow API.interp_type t) |
| 747 |
(runtime_rets : list (Z + list Z)), |
| 748 |
build_input_runtime t runtime_inputs = Some (input_runtime_var, []) |
| 749 |
/\ simplify_base_runtime (type.app_curried (API.Interp expr) input_runtime_var) = Some runtime_rets |
| 750 |
/\ List.Forall2 (eval_idx_or_list_idx G d') rets runtime_rets) |
| 751 |
/\ gensym_dag_ok G d' |
| 752 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 753 |
Proof using Type. |
| 754 |
cbv [symex_PHOAS ErrorT.bind] in H; break_innermost_match_hyps; try discriminate. |
| 755 |
destruct (build_input_runtime t runtime_inputs) as [ [input_runtime_var [|] ] | ] eqn:H'; [ | exfalso.. ]. |
| 756 |
all: lazymatch goal with |
| 757 |
| [ H : symex_PHOAS_PHOAS _ _ _ = Success _, |
| 758 |
input_runtime_var : type.for_each_lhs_of_arrow API.interp_type _ |
| 759 |
|- _ ] => apply @symex_PHOAS_PHOAS_correct with (input_runtime_var:=input_runtime_var) in H; try assumption; [ | clear H .. ] |
| 760 |
| [ |- False ] => idtac |
| 761 |
end. |
| 762 |
all: repeat first [ progress break_innermost_match_hyps |
| 763 |
| progress destruct_head'_and |
| 764 |
| progress subst |
| 765 |
| progress inversion_option |
| 766 |
| progress inversion_prod |
| 767 |
| discriminate |
| 768 |
| assumption |
| 769 |
| apply (f_equal (@Some _)) |
| 770 |
| apply (f_equal2 (@pair _ _)) |
| 771 |
| reflexivity |
| 772 |
| progress reflect_hyps |
| 773 |
| match goal with |
| 774 |
| [ H : Success _ = Success _ |- _ ] => inversion H; clear H |
| 775 |
| [ H : symex_PHOAS_PHOAS _ _ _ = _ |- _ ] => apply symex_PHOAS_PHOAS_correct in H; [ | assumption.. ] |
| 776 |
| [ |- exists a b, ?v = Some (a, _) /\ _ = Some b /\ _ ] |
| 777 |
=> let av := fresh in destruct v as [ [av ?] |] eqn:?; [ exists av | exfalso ] |
| 778 |
| [ |- exists a, _ /\ ?v = Some a /\ _ ] |
| 779 |
=> let av := fresh in destruct v as [ av |] eqn:?; [ exists av | exfalso ] |
| 780 |
| [ H : List.length ?l = 0%nat |- _ ] => is_var l; destruct l; cbn in H |
| 781 |
| [ H : Forall2 _ [] _ |- _ ] => inversion H; clear H |
| 782 |
| [ H : Forall2 _ _ [] |- _ ] => inversion H; clear H |
| 783 |
| [ H : Forall2 _ (_ :: _) _ |- _ ] => inversion H; clear H |
| 784 |
| [ H : Forall2 _ _ (_ :: _) |- _ ] => inversion H; clear H |
| 785 |
end |
| 786 |
| apply conj |
| 787 |
| progress intros |
| 788 |
| match goal with |
| 789 |
| [ H : build_input_runtime ?t ?ri = _, H' : build_input_var ?t ?i = _ |- _ ] |
| 790 |
=> let H'' := fresh in |
| 791 |
pose proof (build_input_var_runtime_gen _ i ri ltac:(eassumption) t) as H''; |
| 792 |
rewrite H, H' in H''; clear H H' |
| 793 |
| [ H : simplify_base_var ?b = _, H' : simplify_base_runtime ?v = _ |- _ ] |
| 794 |
=> let H'' := fresh in |
| 795 |
pose proof (simplify_base_var_runtime_gen _ b v ltac:(eassumption)) as H''; |
| 796 |
rewrite H, H' in H''; clear H H' |
| 797 |
end ]. |
| 798 |
Qed. |
| 799 |
End WithFixedCtx. |
| 800 |
|
| 801 |
|
| 802 |
Definition val_or_list_val_matches_spec (arg : Z + list Z) (spec : option nat) |
| 803 |
:= match arg, spec with |
| 804 |
| inl _, None => True |
| 805 |
| inr ls, Some len => List.length ls = len |
| 806 |
| inl _, Some _ | inr _, None => False |
| 807 |
end. |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
Local Ltac build_runtime_ok_t_step := |
| 812 |
first [ progress subst |
| 813 |
| progress destruct_head'_unit |
| 814 |
| progress inversion_option |
| 815 |
| progress inversion_prod |
| 816 |
| progress destruct_head'_prod |
| 817 |
| progress destruct_head'_and |
| 818 |
| progress destruct_head'_ex |
| 819 |
| progress cbv [Crypto.Util.Option.bind Rewriter.Util.Option.bind ErrorT.bind] in * |
| 820 |
| progress cbn [val_or_list_val_matches_spec] in * |
| 821 |
| progress cbn [fst snd ZRange.type.base.option.is_bounded_by type.andb_bool_for_each_lhs_of_arrow] in * |
| 822 |
| progress split_andb |
| 823 |
| progress break_innermost_match_hyps |
| 824 |
| rewrite <- !List.app_assoc in * |
| 825 |
| discriminate |
| 826 |
| progress specialize_by_assumption |
| 827 |
| match goal with |
| 828 |
| [ H : ?x = Some _ |- context[?x] ] => rewrite H |
| 829 |
| [ H : forall extra, ?f (?x ++ extra) = Some _ |- context[?f (?x ++ _)] ] => rewrite !H |
| 830 |
| [ H : Success _ = Success _ |- _ ] => inversion H; clear H |
| 831 |
| [ |- exists args, ?extra = args ++ ?extra /\ _ /\ _ ] |
| 832 |
=> exists nil |
| 833 |
| [ |- exists args, ?x :: ?extra = args ++ ?extra /\ _ /\ _ ] |
| 834 |
=> exists (x :: nil) |
| 835 |
| [ |- exists args, ?x ++ ?extra = args ++ ?extra /\ _ /\ _ ] |
| 836 |
=> exists x |
| 837 |
| [ |- exists args, ?x ++ (?y ++ ?extra) = args ++ ?extra /\ _ /\ _ ] |
| 838 |
=> exists (x ++ y); rewrite !List.app_assoc |
| 839 |
| [ IH : forall a b c d e, simplify_base_type _ _ = Success _ -> build_base_runtime _ _ = Some _ -> _ |- _ ] |
| 840 |
=> specialize (IH _ _ _ _ _ ltac:(eassumption) ltac:(eassumption)) |
| 841 |
| [ IH : forall a b c d e, simplify_type _ _ = Success _ -> build_runtime _ _ = Some _ -> _ |- _ ] |
| 842 |
=> specialize (IH _ _ _ _ _ ltac:(eassumption) ltac:(eassumption)) |
| 843 |
| [ IH : forall a b c d e, simplify_input_type _ _ = Success _ -> build_input_runtime _ _ = Some _ -> _ |- _ ] |
| 844 |
=> specialize (IH _ _ _ _ _ ltac:(eassumption) ltac:(eassumption)) |
| 845 |
| [ |- Forall2 _ (_ ++ _) (_ ++ _) ] => apply Forall2_app |
| 846 |
| [ H : FoldBool.fold_andb_map _ _ _ = true |- List.length _ = List.length _ ] |
| 847 |
=> apply FoldBool.fold_andb_map_length in H |
| 848 |
end |
| 849 |
| progress intros |
| 850 |
| apply conj |
| 851 |
| reflexivity |
| 852 |
| (idtac + symmetry); assumption ]. |
| 853 |
|
| 854 |
Lemma build_base_runtime_ok t arg_bounds args types PHOAS_args extra |
| 855 |
(H : simplify_base_type t arg_bounds = Success types) |
| 856 |
(Hargs : build_base_runtime t args = Some (PHOAS_args, extra)) |
| 857 |
(HPHOAS_args : ZRange.type.base.option.is_bounded_by arg_bounds PHOAS_args = true) |
| 858 |
: exists args', |
| 859 |
args = args' ++ extra |
| 860 |
/\ (forall extra', build_base_runtime t (args' ++ extra') = Some (PHOAS_args, extra')) |
| 861 |
/\ Forall2 val_or_list_val_matches_spec args' types. |
| 862 |
Proof using Type. |
| 863 |
revert args extra types H Hargs HPHOAS_args; induction t; intros; |
| 864 |
cbn [simplify_base_type build_base_runtime type.for_each_lhs_of_arrow Language.Compilers.base.interp] in *; |
| 865 |
break_innermost_match_hyps; |
| 866 |
repeat first [ build_runtime_ok_t_step |
| 867 |
| constructor ]. |
| 868 |
Qed. |
| 869 |
|
| 870 |
Lemma build_runtime_ok t arg_bounds args types PHOAS_args extra |
| 871 |
(H : simplify_type t arg_bounds = Success types) |
| 872 |
(Hargs : build_runtime t args = Some (PHOAS_args, extra)) |
| 873 |
(HPHOAS_args : ZRange.type.option.is_bounded_by arg_bounds PHOAS_args = true) |
| 874 |
: exists args', |
| 875 |
args = args' ++ extra |
| 876 |
/\ (forall extra', build_runtime t (args' ++ extra') = Some (PHOAS_args, extra')) |
| 877 |
/\ Forall2 val_or_list_val_matches_spec args' types. |
| 878 |
Proof using Type. |
| 879 |
revert args extra types H Hargs HPHOAS_args; induction t; intros; |
| 880 |
cbn [simplify_type build_runtime type.for_each_lhs_of_arrow] in *; |
| 881 |
[ eapply build_base_runtime_ok; eassumption | discriminate ]. |
| 882 |
Qed. |
| 883 |
|
| 884 |
Lemma build_input_runtime_ok t arg_bounds args input_types PHOAS_args extra |
| 885 |
(H : simplify_input_type t arg_bounds = Success input_types) |
| 886 |
(Hargs : build_input_runtime t args = Some (PHOAS_args, extra)) |
| 887 |
(HPHOAS_args : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) arg_bounds PHOAS_args = true) |
| 888 |
: exists args', |
| 889 |
args = args' ++ extra |
| 890 |
/\ (forall extra', build_input_runtime t (args' ++ extra') = Some (PHOAS_args, extra')) |
| 891 |
/\ Forall2 val_or_list_val_matches_spec args' input_types. |
| 892 |
Proof using Type. |
| 893 |
revert args extra input_types H Hargs HPHOAS_args; induction t as [|t1 ? t2]; intros; |
| 894 |
[ eexists [] | pose proof (build_runtime_ok t1) ]; |
| 895 |
cbn [simplify_input_type build_input_runtime type.for_each_lhs_of_arrow] in *; |
| 896 |
repeat first [ build_runtime_ok_t_step |
| 897 |
| constructor ]. |
| 898 |
Qed. |
| 899 |
|
| 900 |
Lemma build_input_runtime_ok_nil t arg_bounds args input_types PHOAS_args |
| 901 |
(H : simplify_input_type t arg_bounds = Success input_types) |
| 902 |
(Hargs : build_input_runtime t args = Some (PHOAS_args, [])) |
| 903 |
(HPHOAS_args : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) arg_bounds PHOAS_args = true) |
| 904 |
: Forall2 val_or_list_val_matches_spec args input_types. |
| 905 |
Proof using Type. |
| 906 |
pose proof (build_input_runtime_ok t arg_bounds args input_types PHOAS_args [] H Hargs HPHOAS_args). |
| 907 |
destruct_head'_ex; destruct_head'_and; subst; rewrite app_nil_r; assumption. |
| 908 |
Qed. |
| 909 |
|
| 910 |
Definition ctx_set (s : symbol) (v : Z) (G : symbol -> option Z) : symbol -> option Z |
| 911 |
:= fun idx => if (idx =? s)%N then Some v else G idx. |
| 912 |
|
| 913 |
Module dagG. |
| 914 |
Definition M A := (symbol -> option Z) * dag -> A * ((symbol -> option Z) * dag). |
| 915 |
End dagG. |
| 916 |
|
| 917 |
Definition merge_fresh_symbol_G {descr:description} (v : Z) : dagG.M idx |
| 918 |
:= fun '(G, d) => let '(idx, d) := merge_fresh_symbol d in (idx, (ctx_set idx v G, d)). |
| 919 |
|
| 920 |
Definition build_inputarray_G {descr:description} (vals : list Z) : dagG.M (list idx) |
| 921 |
:= List.foldmap merge_fresh_symbol_G vals. |
| 922 |
|
| 923 |
Fixpoint build_inputs_G {descr:description} (vals : list (Z + list Z)) |
| 924 |
: dagG.M (list (idx + list idx)) |
| 925 |
:= match vals with |
| 926 |
| [] => fun st => ([], st) |
| 927 |
| inr vs :: args |
| 928 |
=> fun st |
| 929 |
=> let '(idxs, st) := build_inputarray_G vs st in |
| 930 |
let '(rest, st) := build_inputs_G args st in |
| 931 |
(inr idxs :: rest, st) |
| 932 |
| inl v :: args |
| 933 |
=> fun st |
| 934 |
=> let '(idx, st) := merge_fresh_symbol_G v st in |
| 935 |
let '(rest, st) := build_inputs_G args st in |
| 936 |
(inl idx :: rest, st) |
| 937 |
end. |
| 938 |
|
| 939 |
Fixpoint dag_gensym_n_G {descr:description} (vals : list Z) : dagG.M (list idx) |
| 940 |
:= match vals with |
| 941 |
| nil => fun st => ([], st) |
| 942 |
| v :: vs |
| 943 |
=> fun st |
| 944 |
=> let '(idx, st) := merge_fresh_symbol_G v st in |
| 945 |
let '(rest, st) := dag_gensym_n_G vs st in |
| 946 |
(idx :: rest, st) |
| 947 |
end. |
| 948 |
|
| 949 |
Module G. |
| 950 |
Definition M A := (symbol -> option Z) * symbolic_state -> option (A * ((symbol -> option Z) * symbolic_state)). |
| 951 |
Definition lift {A} (v : Symbolic.M A) : M A |
| 952 |
:= fun '(G, s) => match v s with |
| 953 |
| Success (v, s) => Some (v, (G, s)) |
| 954 |
| Error _ => None |
| 955 |
end. |
| 956 |
Definition ret {A} (v : A) : M A := fun s => Some (v, s). |
| 957 |
Definition bind {A B} (v : M A) (f : A -> M B) : M B |
| 958 |
:= fun s => (v <- v s; let '(v, s) := v in f v s)%option. |
| 959 |
End G. |
| 960 |
Declare Scope GM_scope. |
| 961 |
Delimit Scope GM_scope with GM. |
| 962 |
Bind Scope GM_scope with G.M. |
| 963 |
Notation "x <- y ; f" := (G.bind y (fun x => f%GM)) : GM_scope. |
| 964 |
|
| 965 |
Definition lift_dag_G {A} (v : dagG.M A) : G.M A |
| 966 |
:= fun '(G, s) => let '(v, (G, d)) := v (G, s.(dag_state)) in |
| 967 |
Some (v, (G, update_dag_with s (fun _ => d))). |
| 968 |
|
| 969 |
Definition SetRegFresh_G {opts : symbolic_options_computed_opt} {descr:description} (r : REG) (v : Z) : G.M idx |
| 970 |
:= (idx <- lift_dag_G (merge_fresh_symbol_G v); |
| 971 |
_ <- G.lift (SetReg r idx); |
| 972 |
G.ret idx)%GM. |
| 973 |
|
| 974 |
Fixpoint build_merge_base_addresses_G |
| 975 |
{opts : symbolic_options_computed_opt} |
| 976 |
{descr:description} |
| 977 |
{dereference_scalar:bool} |
| 978 |
(items : list (idx + list idx)) (reg_available : list REG) (runtime_reg : list Z) {struct items} |
| 979 |
: G.M (list ((REG + idx) + idx)) |
| 980 |
:= match items, reg_available, runtime_reg with |
| 981 |
| [], _, _ | _, [], _ => G.ret [] |
| 982 |
| _, _, [] |
| 983 |
=> fun _ => None |
| 984 |
| inr idxs :: xs, r :: reg_available, rv :: runtime_reg |
| 985 |
=> (base <- SetRegFresh_G r rv; (* note: overwrites initial value *) |
| 986 |
addrs <- G.lift (build_merge_array_addresses base idxs); |
| 987 |
rest <- build_merge_base_addresses_G (dereference_scalar:=dereference_scalar) xs reg_available runtime_reg; |
| 988 |
G.ret (inr base :: rest)) |
| 989 |
| inl idx :: xs, r :: reg_available, rv :: runtime_reg => |
| 990 |
(addr <- (if dereference_scalar |
| 991 |
then |
| 992 |
(addr <- SetRegFresh_G r rv; |
| 993 |
G.lift (fun s => Success (inr addr, update_mem_with s (cons (addr, idx))))) |
| 994 |
else |
| 995 |
(G.lift (_ <- SetReg r idx; (* note: overwrites initial value *) |
| 996 |
ret (inl r)))); |
| 997 |
rest <- build_merge_base_addresses_G (dereference_scalar:=dereference_scalar) xs reg_available runtime_reg; |
| 998 |
G.ret (inl addr :: rest)) |
| 999 |
end%GM%N%x86symex. |
| 1000 |
|
| 1001 |
Definition compute_stack_base_G {opts : symbolic_options_computed_opt} {descr:description} (rsp_val : Z) (stack_size : nat) |
| 1002 |
: G.M idx |
| 1003 |
:= (rsp_idx <- SetRegFresh_G rsp rsp_val; |
| 1004 |
stack_size <- G.lift (Symbolic.App (zconst 64 (-8 * Z.of_nat stack_size), [])); |
| 1005 |
G.lift (Symbolic.App (add 64%N, [rsp_idx; stack_size])))%GM. |
| 1006 |
|
| 1007 |
Definition build_merge_stack_placeholders_G {opts : symbolic_options_computed_opt} {descr:description} (rsp_val : Z) (stack_vals : list Z) |
| 1008 |
: G.M idx |
| 1009 |
:= (let stack_size := List.length stack_vals in |
| 1010 |
stack_placeholders <- lift_dag_G (build_inputarray_G stack_vals); |
| 1011 |
stack_base <- compute_stack_base_G rsp_val stack_size; |
| 1012 |
_ <- G.lift (build_merge_array_addresses stack_base stack_placeholders); |
| 1013 |
G.ret stack_base)%GM. |
| 1014 |
|
| 1015 |
(* TODO: Move to SymbolicProofs? *) |
| 1016 |
Local Lemma mapM_Proper_inv {A B B'} R (Hinv : _ -> Prop) |
| 1017 |
(f : A -> M B) (f' : A -> M B') |
| 1018 |
(Hf : forall a st, |
| 1019 |
Hinv st |
| 1020 |
-> match f a st, f' a st with |
| 1021 |
| Success (v, st), Success (v', st') => R v v' /\ st = st' /\ Hinv st |
| 1022 |
| Error err, Error err' => err = err' |
| 1023 |
| Success _, Error _ | Error _, Success _ => False |
| 1024 |
end) |
| 1025 |
(l1 : list A) (l2 : list A) (Hl : l1 = l2) |
| 1026 |
: forall st, |
| 1027 |
Hinv st |
| 1028 |
-> match mapM f l1 st, mapM f' l2 st with |
| 1029 |
| Success (v, st), Success (v', st') => Forall2 R v v' /\ st = st' /\ Hinv st |
| 1030 |
| Error err, Error err' => err = err' |
| 1031 |
| Success _, Error _ | Error _, Success _ => False |
| 1032 |
end. |
| 1033 |
Proof. |
| 1034 |
subst l2; induction l1 as [|x xs IHxs]; intros st Hst; cbn [mapM]; cbv [ret]; eauto. |
| 1035 |
cbv [Symbolic.bind Crypto.Util.Option.bind ErrorT.bind]. |
| 1036 |
specialize (Hf x st Hst). |
| 1037 |
break_match_hyps; try tauto. |
| 1038 |
cbn [fst snd]. |
| 1039 |
destruct_head'_and; subst. |
| 1040 |
let s := match goal with |- context[mapM f xs ?s] => s end in |
| 1041 |
specialize (IHxs s ltac:(assumption)). |
| 1042 |
break_match_hyps; try tauto. |
| 1043 |
repeat first [ progress cbn [fst snd] |
| 1044 |
| progress destruct_head'_and |
| 1045 |
| progress subst |
| 1046 |
| constructor |
| 1047 |
| assumption ]. |
| 1048 |
Qed. |
| 1049 |
|
| 1050 |
(* TODO: Move to SymbolicProofs? *) |
| 1051 |
Local Lemma mapM_Proper {A B B'} R |
| 1052 |
(f : A -> M B) (f' : A -> M B') |
| 1053 |
(Hf : forall a st, |
| 1054 |
match f a st, f' a st with |
| 1055 |
| Success (v, st), Success (v', st') => R v v' /\ st = st' |
| 1056 |
| Error err, Error err' => err = err' |
| 1057 |
| Success _, Error _ | Error _, Success _ => False |
| 1058 |
end) |
| 1059 |
(l1 : list A) (l2 : list A) (Hl : l1 = l2) |
| 1060 |
: forall st, |
| 1061 |
match mapM f l1 st, mapM f' l2 st with |
| 1062 |
| Success (v, st), Success (v', st') => Forall2 R v v' /\ st = st' |
| 1063 |
| Error err, Error err' => err = err' |
| 1064 |
| Success _, Error _ | Error _, Success _ => False |
| 1065 |
end. |
| 1066 |
Proof. |
| 1067 |
intro st. |
| 1068 |
unshelve epose proof ((fun H => @mapM_Proper_inv A B B' R (fun _ => True) f f' (fun a st _ => H a st (Hf a st)) l1 l2 Hl st I) _) as H'. |
| 1069 |
all: intros; break_match; intuition. |
| 1070 |
Qed. |
| 1071 |
|
| 1072 |
(* TODO: Move to SymbolicProofs? *) |
| 1073 |
Local Lemma mapM__Proper_inv {A B B'} (Hinv : _ -> Prop) |
| 1074 |
(f : A -> M B) (f' : A -> M B') |
| 1075 |
(Hf : forall a st, |
| 1076 |
Hinv st |
| 1077 |
-> match f a st, f' a st with |
| 1078 |
| Success (_, st), Success (_, st') => st = st' /\ Hinv st |
| 1079 |
| Error err, Error err' => err = err' |
| 1080 |
| Success _, Error _ | Error _, Success _ => False |
| 1081 |
end) |
| 1082 |
(l1 : list A) (l2 : list A) (Hl : l1 = l2) |
| 1083 |
: forall st, Hinv st -> mapM_ f l1 st = mapM_ f' l2 st. |
| 1084 |
Proof. |
| 1085 |
intros st Hst. |
| 1086 |
unshelve epose proof ((fun H => @mapM_Proper_inv A B B' (fun _ _ => True) Hinv f f' (fun a st pf => H a st (Hf a st pf)) l1 l2 Hl st Hst) _). |
| 1087 |
{ intros; break_match; intuition. } |
| 1088 |
{ cbv [mapM_ Symbolic.bind ErrorT.bind]. |
| 1089 |
break_match_hyps; destruct_head'_and; subst; cbn [fst snd] in *; intuition. } |
| 1090 |
Qed. |
| 1091 |
|
| 1092 |
(* TODO: Move to SymbolicProofs? *) |
| 1093 |
Local Lemma mapM__Proper {A B B'} |
| 1094 |
(f : A -> M B) (f' : A -> M B') |
| 1095 |
(Hf : forall a st, |
| 1096 |
match f a st, f' a st with |
| 1097 |
| Success (_, st), Success (_, st') => st = st' |
| 1098 |
| Error err, Error err' => err = err' |
| 1099 |
| Success _, Error _ | Error _, Success _ => False |
| 1100 |
end) |
| 1101 |
(l1 : list A) (l2 : list A) (Hl : l1 = l2) |
| 1102 |
: forall st, mapM_ f l1 st = mapM_ f' l2 st. |
| 1103 |
Proof. |
| 1104 |
intro st. |
| 1105 |
unshelve epose proof ((fun H => @mapM__Proper_inv A B B' (fun _ => True) f f' (fun a st _ => H a st (Hf a st)) l1 l2 Hl st I) _) as H'. |
| 1106 |
all: intros; break_match; intuition. |
| 1107 |
Qed. |
| 1108 |
|
| 1109 |
Lemma merge_fresh_symbol_eq_G {descr:description} G d v |
| 1110 |
(res := merge_fresh_symbol_G v (G, d)) |
| 1111 |
: merge_fresh_symbol d = (fst res, snd (snd res)). |
| 1112 |
Proof. |
| 1113 |
subst res; cbv [merge_fresh_symbol_G]; break_innermost_match; reflexivity. |
| 1114 |
Qed. |
| 1115 |
|
| 1116 |
Lemma merge_fresh_symbol_G_ok {descr:description} G d v G' d' idx |
| 1117 |
(Hd : gensym_dag_ok G d) |
| 1118 |
(H : merge_fresh_symbol_G v (G, d) = (idx, (G', d'))) |
| 1119 |
: eval_idx_Z G' d' idx (Z.land v (Z.ones (Z.of_N 64))) |
| 1120 |
/\ gensym_dag_ok G' d' |
| 1121 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1122 |
Proof. |
| 1123 |
cbv [merge_fresh_symbol_G merge_fresh_symbol merge_node gensym_dag_ok dag_ok] in *. |
| 1124 |
destruct_head'_and. |
| 1125 |
assert (forall e n, eval G d e n -> eval G' d' e n). |
| 1126 |
all: break_innermost_match_hyps; inversion_prod; subst. |
| 1127 |
1: inversion_prod; subst; intros e n H; induction H; econstructor. |
| 1128 |
all: repeat first [ progress inversion_prod |
| 1129 |
| progress inversion_option |
| 1130 |
| progress subst |
| 1131 |
| progress cbn [List.map interp_op fst snd] in * |
| 1132 |
| reflexivity |
| 1133 |
| exact _ |
| 1134 |
| progress intros |
| 1135 |
| progress split_and |
| 1136 |
| progress destruct_head'_and |
| 1137 |
| progress cbv [eval_idx_Z gensym_dag_ok gensym_ok ctx_set dag_ok node_ok] in * |
| 1138 |
| rewrite dag.fst_merge_node_gensym by assumption |
| 1139 |
| rewrite @dag.lookup_merge_node in * by assumption |
| 1140 |
| rewrite @dag.fst_merge_node_gensym in * by assumption |
| 1141 |
| rewrite dag.size_merge_node_gensym by assumption |
| 1142 |
| rewrite dag.lookup_size_error in * by first [ assumption | lia ] |
| 1143 |
| rewrite @dag.reverse_lookup_gensym in * by assumption |
| 1144 |
| rewrite Bool.andb_true_iff in * |
| 1145 |
| rewrite Bool.andb_false_iff in * |
| 1146 |
| progress reflect_hyps |
| 1147 |
| match goal with |
| 1148 |
| [ H : ?x = Some _ |- context[?x] ] => rewrite H |
| 1149 |
| [ |- _ /\ _ ] => split |
| 1150 |
| [ H : Forall2 _ ?xs _ |- Forall2 _ ?xs _ ] => eapply Forall2_weaken; [ | eassumption ]; intuition tauto |
| 1151 |
| [ H : interp_op _ ?op ?args = Some ?n |- interp_op _ ?op ?args = Some ?n ] |
| 1152 |
=> revert H; destruct op; cbn [interp_op]; break_innermost_match |
| 1153 |
end |
| 1154 |
| progress destruct_head'_or |
| 1155 |
| progress break_innermost_match |
| 1156 |
| progress break_innermost_match_hyps |
| 1157 |
| solve [ eauto ] |
| 1158 |
| specialize_under_binders_by eassumption; lia |
| 1159 |
| match goal with |
| 1160 |
| [ |- eval _ _ (ExprRef (dag.size _)) _ ] => econstructor |
| 1161 |
| [ |- exists v, eval _ _ (ExprRef (dag.size _)) v ] |
| 1162 |
=> eexists; econstructor |
| 1163 |
| [ H : forall i r, dag.lookup ?d i = Some r -> exists v, eval _ ?d (ExprRef i) v, |
| 1164 |
H' : dag.lookup ?d ?idx = Some _ |
| 1165 |
|- exists v', eval _ (snd (dag.merge_node _ ?d)) (ExprRef ?idx) v' ] |
| 1166 |
=> let v := fresh "v" in specialize (H _ _ H'); destruct H as [v H]; exists v |
| 1167 |
end ]. |
| 1168 |
Qed. |
| 1169 |
|
| 1170 |
Local Lemma land_ones_eq_of_bounded v n |
| 1171 |
(H : (0 <= v < 2 ^ (Z.of_N n))%Z) |
| 1172 |
: Z.land v (Z.ones (Z.of_N n)) = v. |
| 1173 |
Proof. |
| 1174 |
rewrite Z.land_ones by lia. |
| 1175 |
rewrite Z.mod_small by lia. |
| 1176 |
reflexivity. |
| 1177 |
Qed. |
| 1178 |
|
| 1179 |
Lemma merge_fresh_symbol_G_ok_bounded {descr:description} G d v G' d' idx |
| 1180 |
(Hd : gensym_dag_ok G d) |
| 1181 |
(H : merge_fresh_symbol_G v (G, d) = (idx, (G', d'))) |
| 1182 |
(Hv : (0 <= v < 2^64)%Z) |
| 1183 |
: eval_idx_Z G' d' idx v |
| 1184 |
/\ gensym_dag_ok G' d' |
| 1185 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1186 |
Proof. |
| 1187 |
replace v with (Z.land v (Z.ones (Z.of_N 64))); [ now apply merge_fresh_symbol_G_ok | rewrite land_ones_eq_of_bounded by assumption ]. |
| 1188 |
reflexivity. |
| 1189 |
Qed. |
| 1190 |
|
| 1191 |
Lemma build_inputarray_eq_G {descr:description} G d vals (len := List.length vals) |
| 1192 |
: build_inputarray len d = (fst (build_inputarray_G vals (G, d)), snd (snd (build_inputarray_G vals (G, d)))). |
| 1193 |
Proof. |
| 1194 |
cbv [build_inputarray build_inputarray_G]. |
| 1195 |
generalize (seq_length len 0); generalize (seq 0 len); subst len; intro l; revert l. |
| 1196 |
induction vals, l; cbn [List.length List.foldmap fst snd]; try congruence; try reflexivity. |
| 1197 |
intro H; inversion H; clear H; specialize (IHvals _ ltac:(eassumption)). |
| 1198 |
rewrite !IHvals; clear IHvals. |
| 1199 |
clear dependent l. |
| 1200 |
edestruct List.foldmap; destruct_head'_prod; cbn [fst snd]. |
| 1201 |
repeat f_equal. |
| 1202 |
all: erewrite !@merge_fresh_symbol_eq_G; cbn [fst snd]; reflexivity. |
| 1203 |
Qed. |
| 1204 |
|
| 1205 |
|
| 1206 |
Lemma build_inputarray_G_ok {descr:description} G d vals G' d' ia |
| 1207 |
(Hd : gensym_dag_ok G d) |
| 1208 |
(H : build_inputarray_G vals (G, d) = (ia, (G', d'))) |
| 1209 |
(Hbounds : Forall (fun v => (0 <= v < 2^64)%Z) vals) |
| 1210 |
: Forall2 (eval_idx_Z G' d') ia vals |
| 1211 |
/\ gensym_dag_ok G' d' |
| 1212 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1213 |
Proof. |
| 1214 |
cbv [build_inputarray_G] in *. |
| 1215 |
revert ia G d G' d' Hd H. |
| 1216 |
induction vals as [|v vals IHvals], ia as [|i ia]; cbn [List.foldmap]; intros; |
| 1217 |
inversion_prod; subst; eauto; try congruence. |
| 1218 |
{ set (res := List.foldmap _ _ _) in *. |
| 1219 |
destruct res eqn:Hres; subst res; destruct_head'_prod; cbn [fst snd] in *. |
| 1220 |
inversion Hbounds; subst. |
| 1221 |
specialize (IHvals ltac:(assumption) _ _ _ _ _ ltac:(eassumption) ltac:(eassumption)). |
| 1222 |
repeat first [ progress subst |
| 1223 |
| progress destruct_head'_and |
| 1224 |
| match goal with |
| 1225 |
| [ H : ?x :: ?xs = ?y :: ?ys |- _ ] => assert (x = y /\ xs = ys) by (now inversion H; split); clear H |
| 1226 |
end ]. |
| 1227 |
let lem := match goal with |- context[merge_fresh_symbol_G ?v (?G, ?d)] => constr:(merge_fresh_symbol_G_ok_bounded G d v) end in |
| 1228 |
pose proof (lem _ _ _ ltac:(assumption) ltac:(repeat rewrite <- surjective_pairing; reflexivity) ltac:(lia)). |
| 1229 |
repeat first [ progress subst |
| 1230 |
| progress destruct_head'_and |
| 1231 |
| assumption |
| 1232 |
| solve [ eauto ] |
| 1233 |
| match goal with |
| 1234 |
| [ |- _ /\ _ ] => split |
| 1235 |
| [ |- Forall2 _ (_ :: _) (_ :: _) ] => constructor |
| 1236 |
| [ H : Forall2 _ ?l1 ?l2 |- Forall2 _ ?l1 ?l2 ] => eapply Forall2_weaken; [ clear H | exact H ] |
| 1237 |
| [ |- forall a b, eval_idx_Z _ _ a b -> eval_idx_Z _ _ a b ] => apply lift_eval_idx_Z_impl |
| 1238 |
end ]. } |
| 1239 |
Qed. |
| 1240 |
|
| 1241 |
Lemma build_inputarray_ok {descr:description} G d len idxs args d' |
| 1242 |
(d_ok : gensym_dag_ok G d) |
| 1243 |
(H : build_inputarray len d = (idxs, d')) |
| 1244 |
(Hargs : List.length args = len) |
| 1245 |
(Hbounds : Forall (fun v => (0 <= v < 2^64)%Z) args) |
| 1246 |
: exists G', |
| 1247 |
Forall2 (eval_idx_Z G' d') idxs args |
| 1248 |
/\ gensym_dag_ok G' d' |
| 1249 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1250 |
Proof. |
| 1251 |
subst len; erewrite build_inputarray_eq_G in H; inversion_prod. |
| 1252 |
eexists; eapply build_inputarray_G_ok; try eassumption. |
| 1253 |
repeat first [ eassumption | apply path_prod | reflexivity ]. |
| 1254 |
Qed. |
| 1255 |
|
| 1256 |
Lemma dag_gensym_n_eq_G {descr:description} G d vals (len := List.length vals) |
| 1257 |
: dag_gensym_n len d = (fst (dag_gensym_n_G vals (G, d)), snd (snd (dag_gensym_n_G vals (G, d)))). |
| 1258 |
Proof. |
| 1259 |
subst len. |
| 1260 |
revert G d; induction vals as [|v vals IHvals]; cbn [dag_gensym_n dag_gensym_n_G fst snd List.length]; [ reflexivity | ]; intros. |
| 1261 |
cbv [dag.bind dag.ret]; eta_expand; cbn [fst snd]. |
| 1262 |
erewrite !@merge_fresh_symbol_eq_G, IHvals; cbn [fst snd]; eta_expand; reflexivity. |
| 1263 |
Qed. |
| 1264 |
|
| 1265 |
Lemma dag_gensym_n_G_ok {descr:description} G d vals G' d' ia |
| 1266 |
(Hd : gensym_dag_ok G d) |
| 1267 |
(H : dag_gensym_n_G vals (G, d) = (ia, (G', d'))) |
| 1268 |
(Hbounds : Forall (fun v => (0 <= v < 2^64)%Z) vals) |
| 1269 |
: Forall2 (eval_idx_Z G' d') ia vals |
| 1270 |
/\ gensym_dag_ok G' d' |
| 1271 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1272 |
Proof. |
| 1273 |
revert ia G d G' d' Hd H. |
| 1274 |
induction vals as [|v vals IHvals], ia as [|i ia]; cbn [dag_gensym_n_G]; intros; |
| 1275 |
try solve [ inversion_prod; subst; eta_expand; cbn [fst snd] in *; eauto; try congruence ]. |
| 1276 |
{ break_innermost_match_hyps. |
| 1277 |
inversion Hbounds; subst; clear Hbounds. |
| 1278 |
destruct_head'_prod. |
| 1279 |
specialize (fun H => IHvals ltac:(assumption) _ _ _ _ _ H ltac:(eassumption)). |
| 1280 |
match goal with H : pair _ _ = pair _ _ |- _ => inversion H; clear H end; subst. |
| 1281 |
let lem := match goal with H : context[merge_fresh_symbol_G ?v (?G, ?d)] |- _ => constr:(merge_fresh_symbol_G_ok_bounded G d v) end in |
| 1282 |
pose proof (lem _ _ _ ltac:(assumption) ltac:(eassumption) ltac:(lia)). |
| 1283 |
destruct_head'_and; specialize_by_assumption. |
| 1284 |
repeat first [ progress subst |
| 1285 |
| progress destruct_head'_and |
| 1286 |
| assumption |
| 1287 |
| solve [ eauto ] |
| 1288 |
| match goal with |
| 1289 |
| [ |- _ /\ _ ] => split |
| 1290 |
| [ |- Forall2 _ (_ :: _) (_ :: _) ] => constructor |
| 1291 |
| [ H : Forall2 _ ?l1 ?l2 |- Forall2 _ ?l1 ?l2 ] => eapply Forall2_weaken; [ clear H | exact H ] |
| 1292 |
| [ |- forall a b, eval_idx_Z _ _ a b -> eval_idx_Z _ _ a b ] => apply lift_eval_idx_Z_impl |
| 1293 |
| [ |- eval_idx_Z _ _ _ _ ] => eapply lift_eval_idx_Z_impl; now eauto |
| 1294 |
end ]. } |
| 1295 |
Qed. |
| 1296 |
|
| 1297 |
Lemma dag_gensym_n_ok {descr:description} G d len idxs args d' |
| 1298 |
(d_ok : gensym_dag_ok G d) |
| 1299 |
(H : dag_gensym_n len d = (idxs, d')) |
| 1300 |
(Hargs : List.length args = len) |
| 1301 |
(Hbounds : Forall (fun v => (0 <= v < 2^64)%Z) args) |
| 1302 |
: exists G', |
| 1303 |
Forall2 (eval_idx_Z G' d') idxs args |
| 1304 |
/\ gensym_dag_ok G' d' |
| 1305 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1306 |
Proof. |
| 1307 |
subst len; erewrite dag_gensym_n_eq_G in H; inversion_prod. |
| 1308 |
eexists; eapply dag_gensym_n_G_ok; try eassumption. |
| 1309 |
repeat first [ eassumption | apply path_prod | reflexivity ]. |
| 1310 |
Qed. |
| 1311 |
|
| 1312 |
Definition type_spec_of_runtime {A} (ls : list (A + list A)) : list (option nat) |
| 1313 |
:= List.map (fun v => match v with inl _ => None | inr ls => Some (List.length ls) end) ls. |
| 1314 |
|
| 1315 |
Lemma type_spec_of_runtime_val_or_list_val_matches_spec vals types |
| 1316 |
: types = type_spec_of_runtime vals <-> Forall2 val_or_list_val_matches_spec vals types. |
| 1317 |
Proof. |
| 1318 |
split. |
| 1319 |
{ intro; subst; induction vals; cbn; constructor; break_innermost_match; cbn; eauto. } |
| 1320 |
{ intro H; induction H; |
| 1321 |
repeat (subst || cbn in * || destruct_head' option || break_innermost_match || intuition). } |
| 1322 |
Qed. |
| 1323 |
|
| 1324 |
Lemma build_inputs_eq_G {descr:description} G d vals (types := type_spec_of_runtime vals) |
| 1325 |
: build_inputs types d = (fst (build_inputs_G vals (G, d)), snd (snd (build_inputs_G vals (G, d)))). |
| 1326 |
Proof. |
| 1327 |
revert G d; subst types; induction vals as [|v vals IHvals]; [ reflexivity | ]. |
| 1328 |
cbn [build_inputs type_spec_of_runtime List.map build_inputs_G]; fold (type_spec_of_runtime vals); intros. |
| 1329 |
cbv [dag.bind dag.ret]; break_innermost_match; destruct_head'_prod; cbn [fst snd]. |
| 1330 |
all: repeat first [ progress cbn [fst snd] in * |
| 1331 |
| progress subst |
| 1332 |
| reflexivity |
| 1333 |
| match goal with |
| 1334 |
| [ H : pair _ _ = pair _ _ |- _ ] => inversion H; clear H |
| 1335 |
| [ H : merge_fresh_symbol _ = _, H' : merge_fresh_symbol_G _ _ = _ |- _ ] => erewrite merge_fresh_symbol_eq_G, H' in H |
| 1336 |
| [ H : build_inputarray _ _ = _, H' : build_inputarray_G _ _ = _ |- _ ] => erewrite build_inputarray_eq_G, H' in H |
| 1337 |
| [ H : build_inputs _ _ = _, H' : build_inputs_G _ _ = _ |- _ ] => erewrite IHvals, H' in H |
| 1338 |
end ]. |
| 1339 |
Qed. |
| 1340 |
|
| 1341 |
Lemma build_inputs_G_ok {descr:description} G d vals G' d' inputs |
| 1342 |
(Hd : gensym_dag_ok G d) |
| 1343 |
(H : build_inputs_G vals (G, d) = (inputs, (G', d'))) |
| 1344 |
(Hbounds : Forall (fun v => match v with |
| 1345 |
| inl v => (0 <= v < 2^64)%Z |
| 1346 |
| inr vs => Forall (fun v => (0 <= v < 2^64)%Z) vs |
| 1347 |
end) vals) |
| 1348 |
: Forall2 (eval_idx_or_list_idx G' d') inputs vals |
| 1349 |
/\ gensym_dag_ok G' d' |
| 1350 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1351 |
Proof. |
| 1352 |
revert inputs G d G' d' Hd H. |
| 1353 |
induction vals as [|v vals IHvals], inputs as [|i inputs]; cbn [build_inputs_G]; intros; |
| 1354 |
inversion_prod; subst; eauto; try (break_innermost_match; cbn [fst snd] in *; congruence). |
| 1355 |
{ break_innermost_match; destruct_head'_prod; cbn [fst snd] in *. |
| 1356 |
all: repeat first [ progress subst |
| 1357 |
| progress destruct_head'_and |
| 1358 |
| solve [ eauto ] |
| 1359 |
| match goal with |
| 1360 |
| [ H : ?x :: ?xs = ?y :: ?ys |- _ ] => assert (x = y /\ xs = ys) by (now inversion H; split); clear H |
| 1361 |
| [ H : merge_fresh_symbol_G _ _ = _ |- _ ] => apply merge_fresh_symbol_G_ok_bounded in H; [ | (assumption + lia) .. ] |
| 1362 |
| [ H : build_inputarray_G _ _ = _ |- _ ] => apply build_inputarray_G_ok in H; [ | assumption .. ] |
| 1363 |
| [ H : build_inputs_G _ _ = _ |- _ ] => apply IHvals in H; [ | assumption .. ] |
| 1364 |
| [ H : Forall _ (_ :: _) |- _ ] => inversion H; clear H |
| 1365 |
| [ |- _ /\ _ ] => split |
| 1366 |
| [ |- Forall2 _ (_ :: _) (_ :: _) ] => constructor |
| 1367 |
| [ H : eval_idx_Z _ _ ?e ?n |- eval_idx_Z _ _ ?e ?n ] => revert H; apply lift_eval_idx_Z_impl; assumption |
| 1368 |
| [ H : Forall2 (eval_idx_Z _ _) ?e ?n |- Forall2 (eval_idx_Z _ _) ?e ?n ] => revert H; apply Forall2_weaken, lift_eval_idx_Z_impl; assumption |
| 1369 |
end |
| 1370 |
| progress cbv [eval_idx_or_list_idx] ]. } |
| 1371 |
Qed. |
| 1372 |
|
| 1373 |
Lemma build_inputs_ok {descr:description} G d types inputs args d' |
| 1374 |
(d_ok : gensym_dag_ok G d) |
| 1375 |
(H : build_inputs types d = (inputs, d')) |
| 1376 |
(Hbounds : Forall (fun v => match v with |
| 1377 |
| inl v => (0 <= v < 2^64)%Z |
| 1378 |
| inr vs => Forall (fun v => (0 <= v < 2^64)%Z) vs |
| 1379 |
end) args) |
| 1380 |
(Hargs : Forall2 val_or_list_val_matches_spec args types) |
| 1381 |
: exists G', |
| 1382 |
Forall2 (eval_idx_or_list_idx G' d') inputs args |
| 1383 |
/\ gensym_dag_ok G' d' |
| 1384 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1385 |
Proof. |
| 1386 |
apply type_spec_of_runtime_val_or_list_val_matches_spec in Hargs; subst types. |
| 1387 |
erewrite build_inputs_eq_G in H; inversion_prod. |
| 1388 |
eexists; eapply build_inputs_G_ok; try eassumption. |
| 1389 |
repeat first [ eassumption | apply path_prod | reflexivity ]. |
| 1390 |
Qed. |
| 1391 |
|
| 1392 |
Lemma lift_dag_eq_G {A} G v_G v s s' res |
| 1393 |
(Hv : v s.(dag_state) = (fst (v_G (G, s.(dag_state))), snd (snd (v_G (G, s.(dag_state)))))) |
| 1394 |
(H : lift_dag v s = Success (res, s')) |
| 1395 |
: exists G', |
| 1396 |
@lift_dag_G A v_G (G, s) = Some (res, (G', s')). |
| 1397 |
Proof. |
| 1398 |
revert H; cbv [lift_dag lift_dag_G] in *; break_innermost_match. |
| 1399 |
intros; inversion_ErrorT; inversion_option; inversion_prod; subst. |
| 1400 |
set (vs := v s) in *; clearbody vs; clear v; destruct vs; cbn [fst snd] in *; subst. |
| 1401 |
eexists; reflexivity. |
| 1402 |
Qed. |
| 1403 |
|
| 1404 |
Lemma SetRegFresh_eq_G {opts : symbolic_options_computed_opt} {descr:description} G r s v idx s' |
| 1405 |
(H : SetRegFresh r s = Success (idx, s')) |
| 1406 |
: exists G', |
| 1407 |
SetRegFresh_G r v (G, s) = Some (idx, (G', s')). |
| 1408 |
Proof. |
| 1409 |
revert H; cbv [SetRegFresh SetRegFresh_G G.bind Symbolic.bind ErrorT.bind Crypto.Util.Option.bind G.lift Symbolic.ret G.ret]. |
| 1410 |
repeat first [ progress inversion_ErrorT |
| 1411 |
| progress inversion_option |
| 1412 |
| progress subst |
| 1413 |
| progress destruct_head'_ex |
| 1414 |
| progress cbn [fst snd] in * |
| 1415 |
| progress inversion_prod |
| 1416 |
| eexists; reflexivity |
| 1417 |
| discriminate |
| 1418 |
| match goal with |
| 1419 |
| [ H : lift_dag _ _ = Success _ |- _ ] |
| 1420 |
=> eapply lift_dag_eq_G in H; [ | clear H ] |
| 1421 |
| [ H : ?x = _, H' : ?x = _ |- _ ] => rewrite H in H' |
| 1422 |
| [ H : lift_dag_G ?x ?y = _, H' : lift_dag_G ?x' ?y' = _ |- _ ] |
| 1423 |
=> unify x x'; unify y y'; rewrite H in H' |
| 1424 |
end |
| 1425 |
| break_innermost_match_step |
| 1426 |
| progress intros |
| 1427 |
| apply merge_fresh_symbol_eq_G |
| 1428 |
| progress destruct_head'_prod ]. |
| 1429 |
Qed. |
| 1430 |
|
| 1431 |
Lemma build_merge_base_addresses_eq_G {opts : symbolic_options_computed_opt} {descr:description} {dereference_scalar:bool} |
| 1432 |
G items reg_available runtime_reg s res s' |
| 1433 |
(Hreg : Nat.min (List.length items) (List.length reg_available) <= List.length runtime_reg) |
| 1434 |
(H : build_merge_base_addresses (dereference_scalar:=dereference_scalar) items reg_available s = Success (res, s')) |
| 1435 |
: exists G', |
| 1436 |
build_merge_base_addresses_G (dereference_scalar:=dereference_scalar) items reg_available runtime_reg (G, s) = Some (res, (G', s')). |
| 1437 |
Proof. |
| 1438 |
move H at top; move items at top; move dereference_scalar at top; move opts at top; repeat match goal with H : _ |- _ => revert H end. |
| 1439 |
intros until items. |
| 1440 |
induction items as [|? ? IH]; cbn [build_merge_base_addresses build_merge_base_addresses_G]. |
| 1441 |
all: cbv [G.ret Symbolic.ret Symbolic.bind ErrorT.bind G.bind Crypto.Util.Option.bind G.lift]; intros. |
| 1442 |
all: repeat first [ progress destruct_head'_ex |
| 1443 |
| match goal with |
| 1444 |
| [ H : SetRegFresh _ _ = Success _ |- _ ] |
| 1445 |
=> eapply SetRegFresh_eq_G in H |
| 1446 |
| [ H : SetRegFresh_G ?r ?v ?s = _, H' : SetRegFresh_G ?r' ?v' ?s' = _ |- _ ] |
| 1447 |
=> unify r r'; unify v v'; unify s s'; rewrite H in H' |
| 1448 |
end |
| 1449 |
| progress subst |
| 1450 |
| progress inversion_ErrorT |
| 1451 |
| progress inversion_option |
| 1452 |
| progress inversion_prod |
| 1453 |
| eexists; reflexivity |
| 1454 |
| progress cbn [List.length fst snd] in * |
| 1455 |
| discriminate |
| 1456 |
| break_innermost_match_step |
| 1457 |
| break_innermost_match_hyps_step |
| 1458 |
| progress specialize_by_assumption |
| 1459 |
| progress destruct_head'_ex |
| 1460 |
| rewrite <- Nat.succ_min_distr in * |
| 1461 |
| exfalso; (assumption + lia) |
| 1462 |
| match goal with |
| 1463 |
| [ H : Forall2 _ (_ :: _) _ |- _ ] => inversion H; clear H |
| 1464 |
| [ H : Forall2 _ [] _ |- _ ] => inversion H; clear H |
| 1465 |
| [ H : S ?x <= S ?y |- _ ] => assert (x <= y) by lia; clear H |
| 1466 |
| [ H : build_merge_base_addresses _ _ _ = _ |- _ ] |
| 1467 |
=> specialize (IH _ _ _ _ _ H) |
| 1468 |
| [ IH : forall G rr, _ -> exists G', build_merge_base_addresses_G ?it ?ra rr (G, ?s) = _, |
| 1469 |
H' : build_merge_base_addresses_G ?it ?ra ?rrv (?Gv, ?s) = _ |- _ ] |
| 1470 |
=> specialize (IH Gv rrv) |
| 1471 |
| [ H : ?x = _, H' : ?x = _ |- _ ] => rewrite H in H' |
| 1472 |
| [ H : context[match mapM ?f ?l ?s with _ => _ end] |- context[match mapM ?f ?l ?s with _ => _ end] ] |
| 1473 |
=> destruct (mapM f l s) eqn:? |
| 1474 |
end |
| 1475 |
| progress destruct_head'_prod ]. |
| 1476 |
Qed. |
| 1477 |
|
| 1478 |
Lemma compute_stack_base_eq_G {opts : symbolic_options_computed_opt} {descr:description} G rsp_val stack_size s res s' |
| 1479 |
(H : compute_stack_base stack_size s = Success (res, s')) |
| 1480 |
: exists G', |
| 1481 |
compute_stack_base_G rsp_val stack_size (G, s) = Some (res, (G', s')). |
| 1482 |
Proof. |
| 1483 |
revert H; cbv [compute_stack_base_G compute_stack_base G.bind G.ret Crypto.Util.Option.bind Symbolic.bind ErrorT.bind G.lift Symbolic.ret]. |
| 1484 |
repeat first [ progress intros |
| 1485 |
| progress cbn [fst snd] in * |
| 1486 |
| progress subst |
| 1487 |
| progress inversion_ErrorT |
| 1488 |
| progress inversion_option |
| 1489 |
| progress destruct_head'_prod |
| 1490 |
| progress subst_prod |
| 1491 |
| progress destruct_head'_ex |
| 1492 |
| eexists; reflexivity |
| 1493 |
| match goal with |
| 1494 |
| [ H : SetRegFresh _ _ = Success _ |- _ ] |
| 1495 |
=> eapply SetRegFresh_eq_G in H |
| 1496 |
| [ H : SetRegFresh_G ?r ?v ?s = _, H' : SetRegFresh_G ?r' ?v' ?s' = _ |- _ ] |
| 1497 |
=> unify r r'; unify v v'; unify s s'; rewrite H in H' |
| 1498 |
| [ H : ?x = Success _ |- context[?x] ] => rewrite H |
| 1499 |
end |
| 1500 |
| break_innermost_match_hyps_step |
| 1501 |
| break_innermost_match_step ]. |
| 1502 |
Qed. |
| 1503 |
|
| 1504 |
Lemma build_merge_stack_placeholders_eq_G {opts : symbolic_options_computed_opt} {descr:description} G rsp_val stack_vals s res s' |
| 1505 |
(H : build_merge_stack_placeholders (List.length stack_vals) s = Success (res, s')) |
| 1506 |
: exists G', |
| 1507 |
build_merge_stack_placeholders_G rsp_val stack_vals (G, s) = Some (res, (G', s')). |
| 1508 |
Proof. |
| 1509 |
revert H; cbv [build_merge_stack_placeholders_G build_merge_stack_placeholders G.bind G.ret Crypto.Util.Option.bind Symbolic.bind ErrorT.bind G.lift Symbolic.ret]. |
| 1510 |
repeat first [ progress intros |
| 1511 |
| progress cbn [fst snd] in * |
| 1512 |
| progress subst |
| 1513 |
| progress inversion_ErrorT |
| 1514 |
| progress inversion_option |
| 1515 |
| progress destruct_head'_prod |
| 1516 |
| progress subst_prod |
| 1517 |
| progress destruct_head'_ex |
| 1518 |
| eexists; reflexivity |
| 1519 |
| match goal with |
| 1520 |
| [ H : compute_stack_base _ _ = Success _ |- _ ] |
| 1521 |
=> eapply compute_stack_base_eq_G in H |
| 1522 |
| [ H : compute_stack_base_G ?r ?v ?s = _, H' : compute_stack_base_G ?r' ?v' ?s' = _ |- _ ] |
| 1523 |
=> unify r r'; unify v v'; unify s s'; rewrite H in H' |
| 1524 |
| [ H : lift_dag _ _ = Success _ |- _ ] |
| 1525 |
=> eapply lift_dag_eq_G in H; [ | clear H ] |
| 1526 |
| [ H : lift_dag_G ?x ?y = _, H' : lift_dag_G ?x' ?y' = _ |- _ ] |
| 1527 |
=> unify x x'; unify y y'; rewrite H in H' |
| 1528 |
| [ H : ?x = Success _ |- context[?x] ] => rewrite H |
| 1529 |
end |
| 1530 |
| apply build_inputarray_eq_G |
| 1531 |
| break_innermost_match_hyps_step |
| 1532 |
| break_innermost_match_step ]. |
| 1533 |
Qed. |
| 1534 |
|
| 1535 |
Lemma SetReg_ok {opts : symbolic_options_computed_opt} {descr:description} G s s' reg idx rn lo sz v |
| 1536 |
(Hreg : index_and_shift_and_bitcount_of_reg reg = (rn, lo, sz)) |
| 1537 |
(H64 : sz = 64%N) |
| 1538 |
(H : SetReg reg idx s = Success (tt, s')) |
| 1539 |
(d := s.(dag_state)) |
| 1540 |
(d' := s'.(dag_state)) |
| 1541 |
(r := s.(symbolic_reg_state)) |
| 1542 |
(r' := s'.(symbolic_reg_state)) |
| 1543 |
(f := s.(symbolic_flag_state)) |
| 1544 |
(f' := s'.(symbolic_flag_state)) |
| 1545 |
(m := s.(symbolic_mem_state)) |
| 1546 |
(m' := s'.(symbolic_mem_state)) |
| 1547 |
(Hd : gensym_dag_ok G d) |
| 1548 |
(Hidx : eval_idx_Z G d idx v) |
| 1549 |
: ((exists idx', |
| 1550 |
set_reg r rn idx' = r' |
| 1551 |
/\ eval_idx_Z G d' idx' (Z.land v (Z.ones 64))) |
| 1552 |
/\ f = f' |
| 1553 |
/\ m = m') |
| 1554 |
/\ gensym_dag_ok G d' |
| 1555 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 1556 |
Proof. |
| 1557 |
cbv [SetReg Symbolic.bind ErrorT.bind Crypto.Util.Option.bind GetReg SetReg64] in *. |
| 1558 |
break_match_hyps. |
| 1559 |
all: repeat first [ progress inversion_option |
| 1560 |
| progress cbn [fst snd dag_state symbolic_reg_state symbolic_flag_state symbolic_mem_state] in * |
| 1561 |
| progress subst |
| 1562 |
| progress inversion_prod |
| 1563 |
| progress destruct_head'_prod |
| 1564 |
| progress destruct_head'_and |
| 1565 |
| progress inversion_ErrorT |
| 1566 |
| progress reflect_hyps |
| 1567 |
| tauto |
| 1568 |
| progress cbv [update_reg_with] |
| 1569 |
| match goal with |
| 1570 |
| [ H := _ |- _ ] => subst H |
| 1571 |
end ]. |
| 1572 |
(* dealing with App *) |
| 1573 |
cbv [Symbolic.App Merge] in *. |
| 1574 |
(* need to do this early to deal with conversion slowness *) |
| 1575 |
repeat match goal with |
| 1576 |
| [ H : context[simplify ?s ?n] |- _ ] |
| 1577 |
=> unshelve epose proof (@eval_simplify _ _ s n _ _ _); shelve_unifiable; |
| 1578 |
[ solve [ repeat first [ eassumption | exactly_once econstructor ] ] .. | ]; |
| 1579 |
generalize dependent (simplify s n); intros |
| 1580 |
| [ H : context[merge ?e ?d] |- _ ] |
| 1581 |
=> pose proof (@eval_merge _ _ e _ d ltac:(eassumption) ltac:(eassumption)); |
| 1582 |
generalize dependent (merge e d); intros |
| 1583 |
end. |
| 1584 |
repeat first [ progress inversion_ErrorT |
| 1585 |
| progress inversion_prod |
| 1586 |
| progress subst |
| 1587 |
| progress destruct_head'_and |
| 1588 |
| progress cbn [fst snd dag_state symbolic_reg_state symbolic_flag_state symbolic_mem_state] in * |
| 1589 |
| progress cbv [update_dag_with eval_idx_Z] in * |
| 1590 |
| solve [ eauto ] |
| 1591 |
| apply conj |
| 1592 |
| eapply ex_intro ]. |
| 1593 |
Qed. |
| 1594 |
|
| 1595 |
Lemma SetReg_ok_bounded {opts : symbolic_options_computed_opt} {descr:description} G s s' reg idx rn lo sz v |
| 1596 |
(Hreg : index_and_shift_and_bitcount_of_reg reg = (rn, lo, sz)) |
| 1597 |
(H64 : sz = 64%N) |
| 1598 |
(H : SetReg reg idx s = Success (tt, s')) |
| 1599 |
(d := s.(dag_state)) |
| 1600 |
(d' := s'.(dag_state)) |
| 1601 |
(r := s.(symbolic_reg_state)) |
| 1602 |
(r' := s'.(symbolic_reg_state)) |
| 1603 |
(f := s.(symbolic_flag_state)) |
| 1604 |
(f' := s'.(symbolic_flag_state)) |
| 1605 |
(m := s.(symbolic_mem_state)) |
| 1606 |
(m' := s'.(symbolic_mem_state)) |
| 1607 |
(Hd : gensym_dag_ok G d) |
| 1608 |
(Hidx : eval_idx_Z G d idx v) |
| 1609 |
(Hv : (0 <= v < 2^64)%Z) |
| 1610 |
: ((exists idx', |
| 1611 |
set_reg r rn idx' = r' |
| 1612 |
/\ eval_idx_Z G d' idx' v) |
| 1613 |
/\ f = f' |
| 1614 |
/\ m = m') |
| 1615 |
/\ gensym_dag_ok G d' |
| 1616 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 1617 |
Proof. |
| 1618 |
replace v with (Z.land v (Z.ones (Z.of_N 64))); [ eapply SetReg_ok; eassumption | rewrite land_ones_eq_of_bounded by assumption ]. |
| 1619 |
reflexivity. |
| 1620 |
Qed. |
| 1621 |
|
| 1622 |
Lemma SetRegFresh_G_ok {opts : symbolic_options_computed_opt} {descr:description} G G' s s' reg idx rn lo sz v |
| 1623 |
(Hreg : index_and_shift_and_bitcount_of_reg reg = (rn, lo, sz)) |
| 1624 |
(H64 : sz = 64%N) |
| 1625 |
(H : SetRegFresh_G reg v (G, s) = Some (idx, (G', s'))) |
| 1626 |
(d := s.(dag_state)) |
| 1627 |
(d' := s'.(dag_state)) |
| 1628 |
(r := s.(symbolic_reg_state)) |
| 1629 |
(r' := s'.(symbolic_reg_state)) |
| 1630 |
(f := s.(symbolic_flag_state)) |
| 1631 |
(f' := s'.(symbolic_flag_state)) |
| 1632 |
(m := s.(symbolic_mem_state)) |
| 1633 |
(m' := s'.(symbolic_mem_state)) |
| 1634 |
(Hd : gensym_dag_ok G d) |
| 1635 |
: (eval_idx_Z G' d' idx (Z.land v (Z.ones 64)) |
| 1636 |
/\ (exists idx', |
| 1637 |
set_reg r rn idx' = r' |
| 1638 |
/\ eval_idx_Z G' d' idx' (Z.land v (Z.ones 64))) |
| 1639 |
/\ f = f' |
| 1640 |
/\ m = m') |
| 1641 |
/\ gensym_dag_ok G' d' |
| 1642 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1643 |
Proof. |
| 1644 |
cbv [SetRegFresh_G lift_dag_G G.lift G.bind Crypto.Util.Option.bind G.ret] in H. |
| 1645 |
break_innermost_match_hyps; inversion_option; []. |
| 1646 |
repeat first [ progress inversion_option |
| 1647 |
| progress subst |
| 1648 |
| progress destruct_head'_and |
| 1649 |
| progress destruct_head'_ex |
| 1650 |
| progress destruct_head'_unit |
| 1651 |
| progress subst_prod |
| 1652 |
| progress cbv [update_dag_with] in * |
| 1653 |
| progress cbn [dag_state symbolic_reg_state symbolic_flag_state symbolic_mem_state] in * |
| 1654 |
| rewrite Z.land_same_r in * |
| 1655 |
| solve [ cbv [eval_idx_Z] in *; eauto ] |
| 1656 |
| match goal with |
| 1657 |
| [ H := _ |- _ ] => subst H |
| 1658 |
| [ H : merge_fresh_symbol_G _ _ = _ |- _ ] |
| 1659 |
=> apply merge_fresh_symbol_G_ok in H; [ | assumption .. ] |
| 1660 |
| [ H : SetReg _ _ _ = _ |- _ ] |
| 1661 |
=> eapply SetReg_ok in H; |
| 1662 |
[ | first [ eassumption | reflexivity ] .. ] |
| 1663 |
end |
| 1664 |
| apply conj |
| 1665 |
| eapply ex_intro ]. |
| 1666 |
Qed. |
| 1667 |
|
| 1668 |
Lemma SetRegFresh_G_ok_bounded {opts : symbolic_options_computed_opt} {descr:description} G G' s s' reg idx rn lo sz v |
| 1669 |
(Hreg : index_and_shift_and_bitcount_of_reg reg = (rn, lo, sz)) |
| 1670 |
(H64 : sz = 64%N) |
| 1671 |
(H : SetRegFresh_G reg v (G, s) = Some (idx, (G', s'))) |
| 1672 |
(d := s.(dag_state)) |
| 1673 |
(d' := s'.(dag_state)) |
| 1674 |
(r := s.(symbolic_reg_state)) |
| 1675 |
(r' := s'.(symbolic_reg_state)) |
| 1676 |
(f := s.(symbolic_flag_state)) |
| 1677 |
(f' := s'.(symbolic_flag_state)) |
| 1678 |
(m := s.(symbolic_mem_state)) |
| 1679 |
(m' := s'.(symbolic_mem_state)) |
| 1680 |
(Hd : gensym_dag_ok G d) |
| 1681 |
(Hv : (0 <= v < 2^64)%Z) |
| 1682 |
: (eval_idx_Z G' d' idx v |
| 1683 |
/\ (exists idx', |
| 1684 |
set_reg r rn idx' = r' |
| 1685 |
/\ eval_idx_Z G' d' idx' v) |
| 1686 |
/\ f = f' |
| 1687 |
/\ m = m') |
| 1688 |
/\ gensym_dag_ok G' d' |
| 1689 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 1690 |
Proof. |
| 1691 |
replace v with (Z.land v (Z.ones (Z.of_N 64))); [ eapply SetRegFresh_G_ok; eassumption | rewrite land_ones_eq_of_bounded by assumption ]. |
| 1692 |
reflexivity. |
| 1693 |
Qed. |
| 1694 |
|
| 1695 |
Lemma GetReg_ok {opts : symbolic_options_computed_opt} {descr:description} G s s' reg idx rn lo sz v |
| 1696 |
(Hreg : index_and_shift_and_bitcount_of_reg reg = (rn, lo, sz)) |
| 1697 |
(H64 : sz = 64%N) |
| 1698 |
(H : GetReg reg s = Success (idx, s')) |
| 1699 |
(d := s.(dag_state)) |
| 1700 |
(d' := s'.(dag_state)) |
| 1701 |
(r := s.(symbolic_reg_state)) |
| 1702 |
(r' := s'.(symbolic_reg_state)) |
| 1703 |
(f := s.(symbolic_flag_state)) |
| 1704 |
(f' := s'.(symbolic_flag_state)) |
| 1705 |
(m := s.(symbolic_mem_state)) |
| 1706 |
(m' := s'.(symbolic_mem_state)) |
| 1707 |
(Hd : gensym_dag_ok G d) |
| 1708 |
(Hr : forall idx', get_reg r rn = Some idx' -> eval_idx_Z G d idx' v) |
| 1709 |
: ((exists idx', |
| 1710 |
get_reg r rn = Some idx' |
| 1711 |
/\ eval_idx_Z G d idx' v) |
| 1712 |
/\ eval_idx_Z G d' idx (Z.land v (Z.ones 64)) |
| 1713 |
/\ r = r' |
| 1714 |
/\ f = f' |
| 1715 |
/\ m = m') |
| 1716 |
/\ gensym_dag_ok G d' |
| 1717 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 1718 |
Proof. |
| 1719 |
cbv [GetReg GetReg64 some_or Symbolic.bind ErrorT.bind Symbolic.App Merge] in *. |
| 1720 |
subst sz. |
| 1721 |
assert (lo = 0%N) by ( |
| 1722 |
clear -Hreg; inversion_prod; subst; |
| 1723 |
destruct reg; vm_compute in *; try reflexivity; discriminate |
| 1724 |
). |
| 1725 |
subst lo. |
| 1726 |
rewrite Hreg in *. |
| 1727 |
break_innermost_match_hyps; cbn [fst snd] in *; inversion_ErrorT. |
| 1728 |
specialize (Hr _ ltac:(eassumption)). |
| 1729 |
(* need to do this early to deal with conversion slowness *) |
| 1730 |
repeat match goal with |
| 1731 |
| [ H : context[simplify ?s ?n] |- _ ] |
| 1732 |
=> unshelve epose proof (@eval_simplify _ _ s n _ _ _); shelve_unifiable; |
| 1733 |
[ solve [ repeat first [ eassumption | exactly_once econstructor ] ] .. | ]; |
| 1734 |
generalize dependent (simplify s n); intros |
| 1735 |
| [ H : context[merge ?e ?d] |- _ ] |
| 1736 |
=> pose proof (@eval_merge _ _ e _ d ltac:(eassumption) ltac:(eassumption)); |
| 1737 |
generalize dependent (merge e d); intros |
| 1738 |
end. |
| 1739 |
all: repeat first [ progress inversion_ErrorT |
| 1740 |
| progress inversion_prod |
| 1741 |
| progress subst |
| 1742 |
| progress destruct_head'_and |
| 1743 |
| progress destruct_head'_prod |
| 1744 |
| progress cbv [update_dag_with eval_idx_Z] in * |
| 1745 |
| progress cbn [fst snd dag_state symbolic_reg_state symbolic_flag_state symbolic_mem_state] in * |
| 1746 |
| solve [ eauto ] |
| 1747 |
| match goal with |
| 1748 |
| [ H := _ |- _ ] => subst H |
| 1749 |
end |
| 1750 |
| apply conj |
| 1751 |
| eapply ex_intro ]. |
| 1752 |
Qed. |
| 1753 |
|
| 1754 |
Lemma GetReg_ok_bounded {opts : symbolic_options_computed_opt} {descr:description} G s s' reg idx rn lo sz v |
| 1755 |
(Hreg : index_and_shift_and_bitcount_of_reg reg = (rn, lo, sz)) |
| 1756 |
(H64 : sz = 64%N) |
| 1757 |
(H : GetReg reg s = Success (idx, s')) |
| 1758 |
(d := s.(dag_state)) |
| 1759 |
(d' := s'.(dag_state)) |
| 1760 |
(r := s.(symbolic_reg_state)) |
| 1761 |
(r' := s'.(symbolic_reg_state)) |
| 1762 |
(f := s.(symbolic_flag_state)) |
| 1763 |
(f' := s'.(symbolic_flag_state)) |
| 1764 |
(m := s.(symbolic_mem_state)) |
| 1765 |
(m' := s'.(symbolic_mem_state)) |
| 1766 |
(Hd : gensym_dag_ok G d) |
| 1767 |
(Hr : forall idx', get_reg r rn = Some idx' -> eval_idx_Z G d idx' v) |
| 1768 |
(Hv : (0 <= v < 2^64)%Z) |
| 1769 |
: ((exists idx', |
| 1770 |
get_reg r rn = Some idx' |
| 1771 |
/\ eval_idx_Z G d idx' v) |
| 1772 |
/\ eval_idx_Z G d' idx v |
| 1773 |
/\ r = r' |
| 1774 |
/\ f = f' |
| 1775 |
/\ m = m') |
| 1776 |
/\ gensym_dag_ok G d' |
| 1777 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 1778 |
Proof. |
| 1779 |
replace v with (Z.land v (Z.ones (Z.of_N 64))) at 1; [ eapply GetReg_ok; eassumption | rewrite land_ones_eq_of_bounded by assumption ]. |
| 1780 |
reflexivity. |
| 1781 |
Qed. |
| 1782 |
|
| 1783 |
Local Lemma mapM_ok_via_upd |
| 1784 |
G {A B} (f : A -> M B) ls s v s' |
| 1785 |
(H : mapM f ls s = Success (v, s')) |
| 1786 |
upd_mem upd_reg upd_flag (R_static : _ -> Prop) (R_inv : _ -> Prop) (R : _ -> _ -> _ -> Prop) |
| 1787 |
(d := s.(dag_state)) |
| 1788 |
(d' := s'.(dag_state)) |
| 1789 |
|
| 1790 |
(Hd : gensym_dag_ok G d) |
| 1791 |
(Hf : forall s' s'' x v, |
| 1792 |
let d' := s'.(dag_state) in |
| 1793 |
let d'' := s''.(dag_state) in |
| 1794 |
f x s' = Success (v, s'') |
| 1795 |
-> gensym_dag_ok G s' |
| 1796 |
-> R_inv s' |
| 1797 |
-> R_static x |
| 1798 |
-> (forall e n, eval G d e n -> eval G d' e n) |
| 1799 |
-> upd_mem s'.(symbolic_mem_state) (x, v) = s''.(symbolic_mem_state) |
| 1800 |
/\ upd_reg s'.(symbolic_reg_state) (x, v) = s''.(symbolic_reg_state) |
| 1801 |
/\ upd_flag s'.(symbolic_flag_state) (x, v) = s''.(symbolic_flag_state) |
| 1802 |
/\ (forall e n, eval G d' e n -> eval G d'' e n) |
| 1803 |
/\ R s'' x v |
| 1804 |
/\ (forall x v, R s' x v -> R s'' x v) |
| 1805 |
/\ gensym_dag_ok G s'' |
| 1806 |
/\ R_inv s'') |
| 1807 |
(Hinv : R_inv s) |
| 1808 |
(Hstatic : Forall R_static ls) |
| 1809 |
: ((Forall2 (R s') ls v |
| 1810 |
/\ fold_left upd_mem (List.combine ls v) s.(symbolic_mem_state) = s'.(symbolic_mem_state) |
| 1811 |
/\ fold_left upd_reg (List.combine ls v) s.(symbolic_reg_state) = s'.(symbolic_reg_state) |
| 1812 |
/\ fold_left upd_flag (List.combine ls v) s.(symbolic_flag_state) = s'.(symbolic_flag_state) |
| 1813 |
/\ forall x v, R s x v -> R s' x v) |
| 1814 |
/\ R_inv s') |
| 1815 |
/\ gensym_dag_ok G d' |
| 1816 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 1817 |
Proof. |
| 1818 |
cbv beta iota zeta in *. |
| 1819 |
subst d d'. |
| 1820 |
revert v s' H. |
| 1821 |
remember s as s0 eqn:Hs in |- *. |
| 1822 |
assert (Hs0 : forall e n, eval G s e n -> eval G s0 e n) by now subst s0. |
| 1823 |
assert (Hd0 : gensym_dag_ok G s0) by now subst s0. |
| 1824 |
assert (Hinv0 : R_inv s0) by now subst s0. |
| 1825 |
clear Hs; revert s0 Hs0 Hd0 Hinv0. |
| 1826 |
induction ls as [|x xs IHxs]; cbn [mapM]; cbv [ret Symbolic.bind ErrorT.bind]. |
| 1827 |
{ intros; inversion_ErrorT; inversion_prod; subst; cbn [fold_left]; eauto 10. } |
| 1828 |
{ repeat first [ progress intros |
| 1829 |
| progress inversion_ErrorT |
| 1830 |
| progress inversion_prod |
| 1831 |
| progress subst |
| 1832 |
| progress break_innermost_match_hyps |
| 1833 |
| progress destruct_head'_prod |
| 1834 |
| progress cbn [fst snd fold_left] in * |
| 1835 |
| match goal with |
| 1836 |
| [ H : Forall _ (_ :: _) |- _ ] => inversion H; clear H |
| 1837 |
end ]. |
| 1838 |
specialize (Hf _ _ _ _ ltac:(eassumption)); specialize_by_assumption. |
| 1839 |
destruct_head'_and; subst. |
| 1840 |
unshelve epose proof (IHxs _ _ _ _ _ _ ltac:(eassumption)) as IHxs'; clear IHxs; eauto; []. |
| 1841 |
cbn [List.combine fold_left]. |
| 1842 |
destruct_head'_and; repeat first [ solve [ eauto ] | apply conj ]. |
| 1843 |
all: destruct_head symbolic_state; subst; reflexivity. } |
| 1844 |
Qed. |
| 1845 |
|
| 1846 |
(* TODO: this is Symbolic.get_reg; move to SymbolicProofs? *) |
| 1847 |
Lemma get_reg_set_reg_full s rn rn' v |
| 1848 |
: get_reg (set_reg s rn v) rn' |
| 1849 |
= if ((rn <? ((fun n (_ : Tuple.tuple _ n) => N.of_nat n) _ s)) && (rn =? rn'))%N%bool |
| 1850 |
then Some v |
| 1851 |
else get_reg s rn'. |
| 1852 |
Proof. |
| 1853 |
cbv [get_reg set_reg]. |
| 1854 |
break_innermost_match; split_andb; reflect_hyps; subst. |
| 1855 |
all: rewrite <- !Tuple.nth_default_to_list. |
| 1856 |
all: rewrite [email protected]_to_list in *. |
| 1857 |
all: unshelve erewrite Tuple.from_list_default_eq, Tuple.to_list_from_list, set_nth_nth_default_full; |
| 1858 |
rewrite ?length_set_nth, ?Tuple.length_to_list; try easy. |
| 1859 |
all: break_innermost_match; try lia; try reflexivity. |
| 1860 |
now rewrite nth_default_out_of_bounds by now rewrite Tuple.length_to_list; lia. |
| 1861 |
Qed. |
| 1862 |
|
| 1863 |
(* TODO: this is Symbolic.get_reg; move to SymbolicProofs? *) |
| 1864 |
Local Lemma get_reg_set_reg_same s rn v |
| 1865 |
(H : (rn < (fun n (_ : Tuple.tuple _ n) => N.of_nat n) _ s)%N) |
| 1866 |
: get_reg (set_reg s rn v) rn = Some v. |
| 1867 |
Proof. |
| 1868 |
rewrite get_reg_set_reg_full; break_innermost_match; reflect_hyps; cbv beta in *; try reflexivity; lia. |
| 1869 |
Qed. |
| 1870 |
|
| 1871 |
Lemma compute_array_address_ok {opts : symbolic_options_computed_opt} {descr:description} G s s' base i idx base_val |
| 1872 |
(H : compute_array_address base i s = Success (idx, s')) |
| 1873 |
(d := s.(dag_state)) |
| 1874 |
(d' := s'.(dag_state)) |
| 1875 |
(r := s.(symbolic_reg_state)) |
| 1876 |
(r' := s'.(symbolic_reg_state)) |
| 1877 |
(f := s.(symbolic_flag_state)) |
| 1878 |
(f' := s'.(symbolic_flag_state)) |
| 1879 |
(m := s.(symbolic_mem_state)) |
| 1880 |
(m' := s'.(symbolic_mem_state)) |
| 1881 |
(Hd : gensym_dag_ok G d) |
| 1882 |
(Hidx : eval_idx_Z G d base base_val) |
| 1883 |
: (eval_idx_Z G d' idx (Z.land (base_val + 8 * Z.of_nat i) (Z.ones 64)) |
| 1884 |
/\ r = r' |
| 1885 |
/\ f = f' |
| 1886 |
/\ m = m') |
| 1887 |
/\ gensym_dag_ok G d' |
| 1888 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 1889 |
Proof. |
| 1890 |
cbv [compute_array_address Symbolic.App Merge Symbolic.bind] in H. |
| 1891 |
subst d d'. |
| 1892 |
repeat first [ progress inversion_ErrorT |
| 1893 |
| progress destruct_head'_prod |
| 1894 |
| progress inversion_option |
| 1895 |
| progress cbn [fst snd] in * |
| 1896 |
| progress destruct_head'_and |
| 1897 |
| progress cbv [update_dag_with] in * |
| 1898 |
| match goal with |
| 1899 |
| [ H : ErrorT.bind ?x _ = _ |- _ ] |
| 1900 |
=> destruct x eqn:?; unfold ErrorT.bind at 1 in H |
| 1901 |
| [ H : ?v = Success _ |- _ ] |
| 1902 |
=> match v with |
| 1903 |
| context[match ?x with Some _ => _ | _ => _ end] |
| 1904 |
=> destruct x eqn:? |
| 1905 |
end |
| 1906 |
end ]. |
| 1907 |
(* need to do this early to deal with conversion slowness *) |
| 1908 |
repeat first [ match goal with |
| 1909 |
| [ H : context[simplify ?s ?n] |- _ ] |
| 1910 |
=> unshelve epose proof (@eval_simplify _ _ s n _ _ _); shelve_unifiable; |
| 1911 |
[ solve [ repeat first [ eassumption | solve [ eauto ] | exactly_once econstructor ] ] .. | ]; |
| 1912 |
generalize dependent (simplify s n); intros |
| 1913 |
| [ H : context[merge ?e ?d] |- _ ] |
| 1914 |
=> pose proof (@eval_merge _ _ e _ d ltac:(eassumption) ltac:(eassumption)); |
| 1915 |
generalize dependent (merge e d); intros |
| 1916 |
| [ H : (?x, ?y) = (?z, ?w) |- _ ] => is_var x; is_var z; is_var w; inversion H; clear H |
| 1917 |
end |
| 1918 |
| progress destruct_head'_prod |
| 1919 |
| progress destruct_head'_and |
| 1920 |
| progress subst |
| 1921 |
| progress cbn [fst snd symbolic_reg_state symbolic_flag_state symbolic_mem_state dag_state fold_right] in * |
| 1922 |
| break_innermost_match_hyps_step ]. |
| 1923 |
all: repeat first [ solve [ eauto 10 ] |
| 1924 |
| apply conj |
| 1925 |
| progress cbv [eval_idx_Z] |
| 1926 |
| match goal with |
| 1927 |
| [ H : eval ?G ?d ?e ?v |- eval ?G ?d' ?e ?v' ] |
| 1928 |
=> cut (v' = v); |
| 1929 |
[ solve [ intros ->; eauto 10 ] |
| 1930 |
| ] |
| 1931 |
end |
| 1932 |
| progress change (Z.of_N 64) with 64%Z |
| 1933 |
| rewrite !Z.land_ones by lia |
| 1934 |
| progress autorewrite with zsimplify_const |
| 1935 |
| progress (push_Zmod; pull_Zmod) ]. |
| 1936 |
Qed. |
| 1937 |
|
| 1938 |
Lemma compute_array_address_ok_bounded {opts : symbolic_options_computed_opt} {descr:description} G s s' base i idx base_val |
| 1939 |
(H : compute_array_address base i s = Success (idx, s')) |
| 1940 |
(d := s.(dag_state)) |
| 1941 |
(d' := s'.(dag_state)) |
| 1942 |
(r := s.(symbolic_reg_state)) |
| 1943 |
(r' := s'.(symbolic_reg_state)) |
| 1944 |
(f := s.(symbolic_flag_state)) |
| 1945 |
(f' := s'.(symbolic_flag_state)) |
| 1946 |
(m := s.(symbolic_mem_state)) |
| 1947 |
(m' := s'.(symbolic_mem_state)) |
| 1948 |
(Hd : gensym_dag_ok G d) |
| 1949 |
(Hidx : eval_idx_Z G d base base_val) |
| 1950 |
(Hv : (0 <= base_val + 8 * Z.of_nat i < 2^64)%Z) |
| 1951 |
: (eval_idx_Z G d' idx (base_val + 8 * Z.of_nat i) |
| 1952 |
/\ r = r' |
| 1953 |
/\ f = f' |
| 1954 |
/\ m = m') |
| 1955 |
/\ gensym_dag_ok G d' |
| 1956 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 1957 |
Proof. |
| 1958 |
set (v := (base_val + 8 * Z.of_nat i)%Z) in *. |
| 1959 |
replace v with (Z.land v (Z.ones (Z.of_N 64))); [ eapply compute_array_address_ok; eassumption | rewrite land_ones_eq_of_bounded by assumption ]. |
| 1960 |
reflexivity. |
| 1961 |
Qed. |
| 1962 |
|
| 1963 |
Lemma build_merge_array_addresses_ok {opts : symbolic_options_computed_opt} {descr:description} G s s' |
| 1964 |
base base_val items addrs |
| 1965 |
(H : build_merge_array_addresses base items s = Success (addrs, s')) |
| 1966 |
(d := s.(dag_state)) |
| 1967 |
(d' := s'.(dag_state)) |
| 1968 |
(r := s.(symbolic_reg_state)) |
| 1969 |
(r' := s'.(symbolic_reg_state)) |
| 1970 |
(f := s.(symbolic_flag_state)) |
| 1971 |
(f' := s'.(symbolic_flag_state)) |
| 1972 |
(m := s.(symbolic_mem_state)) |
| 1973 |
(m' := s'.(symbolic_mem_state)) |
| 1974 |
(Hd : gensym_dag_ok G d) |
| 1975 |
(addrs_vals := List.map (fun i => Z.land (base_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length items))) |
| 1976 |
(Hbase : eval_idx_Z G d base base_val) |
| 1977 |
: (r = r' |
| 1978 |
/\ f = f' |
| 1979 |
/\ (Forall2 (eval_idx_Z G d') addrs addrs_vals |
| 1980 |
/\ (* TODO: Is this too specific a spec? *) List.rev (List.combine addrs items) ++ m = m')) |
| 1981 |
/\ gensym_dag_ok G d' |
| 1982 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 1983 |
Proof. |
| 1984 |
cbv [build_merge_array_addresses ret Symbolic.bind ErrorT.bind Crypto.Util.Option.bind] in H. |
| 1985 |
apply (@mapM_ok_via_upd G) with |
| 1986 |
(upd_mem := fun st v => cons (let '((i, array_val_idx), array_addr_idx) := ((fst (fst v), snd (fst v)), snd v) in (array_addr_idx, array_val_idx)) st) |
| 1987 |
(R_inv := fun st => eval_idx_Z G st base base_val) |
| 1988 |
(R_static := fun _ => True) |
| 1989 |
(R := fun st i_array_val_idx array_addr_idx |
| 1990 |
=> eval_idx_Z G st.(dag_state) array_addr_idx (Z.land (base_val + 8 * Z.of_nat (fst (i_array_val_idx))) (Z.ones 64))) |
| 1991 |
(upd_flag := fun st _ => st) |
| 1992 |
(upd_reg := fun st _ => st) |
| 1993 |
in H; |
| 1994 |
[ | clear H .. ]. |
| 1995 |
all: repeat first [ progress inversion_option |
| 1996 |
| progress inversion_ErrorT |
| 1997 |
| progress subst |
| 1998 |
| progress destruct_head'_and |
| 1999 |
| progress destruct_head'_prod |
| 2000 |
| progress subst_prod |
| 2001 |
| progress cbn [fst snd dag_state update_dag_with update_mem_with symbolic_reg_state symbolic_flag_state symbolic_mem_state] in * |
| 2002 |
| progress destruct_head'_ex |
| 2003 |
| assumption |
| 2004 |
| progress intros |
| 2005 |
| solve [ eauto ] |
| 2006 |
| rewrite @fold_left_id in * |
| 2007 |
| match goal with |
| 2008 |
| [ H : compute_array_address _ _ _ = _ |- _ ] |
| 2009 |
=> eapply compute_array_address_ok in H; [ | solve [ eassumption | cbv [eval_idx_Z] in *; eauto ] .. ] |
| 2010 |
| [ H := _ |- _ ] => assert_fails constr_eq H offset; subst H |
| 2011 |
| [ H : context[match ?x with Success _ => _ | _ => _ end] |- _ ] => destruct x eqn:? |
| 2012 |
| [ |- Forall (fun _ => True) _ ] => rewrite Forall_forall |
| 2013 |
end ]. |
| 2014 |
{ cbv [eval_idx_Z update_mem_with symbolic_reg_state dag_state symbolic_mem_state symbolic_flag_state update_dag_with reg_index] in *; destruct_head' symbolic_state; subst. |
| 2015 |
repeat (apply conj; eauto; []). |
| 2016 |
setoid_rewrite <- fold_left_map with (f:=fun xs x => cons x xs). |
| 2017 |
rewrite <- (map_swap_combine (enumerate _)), List.map_map; cbn [fst snd]; cbv [enumerate] in *. |
| 2018 |
rewrite <- combine_map_r, map_snd_combine, seq_length, List.firstn_all, fold_left_cons. |
| 2019 |
repeat esplit; eauto using Forall2_weaken; |
| 2020 |
try solve [ eapply Forall2_weaken; [ | eassumption ]; eauto ]; |
| 2021 |
[]. |
| 2022 |
rewrite Forall2_map_r_iff. |
| 2023 |
apply Forall2_flip. |
| 2024 |
replace (seq 0 (length items)) with (List.map fst (enumerate items)) |
| 2025 |
by now cbv [enumerate]; rewrite map_fst_combine, firstn_all2 by now rewrite seq_length. |
| 2026 |
cbv [enumerate] in *. |
| 2027 |
rewrite Forall2_map_l_iff; eapply Forall2_weaken; [ | eassumption ]; intros *; cbv beta. |
| 2028 |
rewrite !Z.land_ones by lia; push_Zmod; pull_Zmod. |
| 2029 |
trivial. } |
| 2030 |
{ (* addressing / loop body *) |
| 2031 |
cbv [eval_idx_Z update_mem_with symbolic_reg_state dag_state symbolic_mem_state symbolic_flag_state update_dag_with reg_index] in *; destruct_head' symbolic_state; subst. |
| 2032 |
eauto 20. } |
| 2033 |
Qed. |
| 2034 |
|
| 2035 |
Lemma compute_stack_base_G_ok {opts : symbolic_options_computed_opt} {descr:description} G G' s s' rv |
| 2036 |
(rsp_val : Z) (stack_size : nat) |
| 2037 |
(H : compute_stack_base_G rsp_val stack_size (G, s) = Some (rv, (G', s'))) |
| 2038 |
(d := s.(dag_state)) |
| 2039 |
(d' := s'.(dag_state)) |
| 2040 |
(r := s.(symbolic_reg_state)) |
| 2041 |
(r' := s'.(symbolic_reg_state)) |
| 2042 |
(f := s.(symbolic_flag_state)) |
| 2043 |
(f' := s'.(symbolic_flag_state)) |
| 2044 |
(m := s.(symbolic_mem_state)) |
| 2045 |
(m' := s'.(symbolic_mem_state)) |
| 2046 |
(Hd : gensym_dag_ok G d) |
| 2047 |
: ((exists rsp_idx, |
| 2048 |
set_reg r (reg_index rsp) rsp_idx = r' |
| 2049 |
/\ eval_idx_Z G' d' rsp_idx (Z.land rsp_val (Z.ones 64))) |
| 2050 |
/\ f = f' |
| 2051 |
/\ m = m') |
| 2052 |
/\ eval_idx_Z G' d' rv (Z.land (rsp_val - 8 * Z.of_nat stack_size) (Z.ones 64)) |
| 2053 |
/\ gensym_dag_ok G' d' |
| 2054 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 2055 |
Proof. |
| 2056 |
cbv [compute_stack_base_G ret Symbolic.bind ErrorT.bind G.bind G.lift Crypto.Util.Option.bind lift_dag_G G.ret] in H. |
| 2057 |
break_match_hyps. |
| 2058 |
all: repeat first [ progress inversion_option |
| 2059 |
| progress inversion_ErrorT |
| 2060 |
| progress subst |
| 2061 |
| progress destruct_head'_and |
| 2062 |
| progress destruct_head'_prod |
| 2063 |
| progress cbn [fst snd dag_state update_dag_with update_mem_with symbolic_reg_state symbolic_flag_state] in * |
| 2064 |
| progress destruct_head'_prod |
| 2065 |
| progress destruct_head'_ex |
| 2066 |
| assumption |
| 2067 |
| progress intros |
| 2068 |
| solve [ eauto ] |
| 2069 |
| progress break_innermost_match_hyps |
| 2070 |
| rewrite @fold_left_id in * |
| 2071 |
| progress cbv [Symbolic.App Merge] in * |
| 2072 |
| progress cbn [fold_right update_dag_with Symbolic.dag_state Symbolic.symbolic_mem_state] in * |
| 2073 |
| progress change (Z.of_N 64) with 64%Z in * |
| 2074 |
| match goal with |
| 2075 |
| [ H : (?x, ?y) = (?x', ?y') |- _ ] |
| 2076 |
=> first [ is_var x | is_var y ]; is_var x'; is_var y'; inversion H; clear H |
| 2077 |
| [ H : (?x, (?y, ?z)) = (?x', (?y', ?z')) |- _ ] |
| 2078 |
=> is_var x; is_var y; is_var x'; is_var y'; is_var z'; inversion H; clear H |
| 2079 |
| [ H : (tt, (?y, ?z)) = (tt, (?y', ?z')) |- _ ] |
| 2080 |
=> is_var y; is_var z; is_var y'; is_var z'; inversion H; clear H |
| 2081 |
| [ H : context[simplify ?s ?n] |- _ ] |
| 2082 |
=> unshelve epose proof (@eval_simplify _ _ s n _ _ _); shelve_unifiable; |
| 2083 |
[ solve [ repeat first [ eassumption | solve [ eauto ] | exactly_once econstructor ] ] .. | ]; |
| 2084 |
generalize dependent (simplify s n); intros |
| 2085 |
| [ H : context[merge ?e ?d] |- _ ] |
| 2086 |
=> pose proof (@eval_merge _ _ e _ d ltac:(eassumption) ltac:(eassumption)); |
| 2087 |
generalize dependent (merge e d); intros |
| 2088 |
| [ H : SetRegFresh_G _ _ _ = _ |- _ ] |
| 2089 |
=> eapply SetRegFresh_G_ok in H; |
| 2090 |
[ |
| 2091 |
| lazymatch goal with |
| 2092 |
| [ |- index_and_shift_and_bitcount_of_reg _ = _ ] => cbv; reflexivity |
| 2093 |
| _ => idtac |
| 2094 |
end .. ]; |
| 2095 |
[ | solve [ reflexivity | assumption | split; assumption ] .. ] |
| 2096 |
| [ H : eval ?G ?d ?i ?v |- eval ?G ?d ?i ?v' ] |
| 2097 |
=> replace v' with v; [ exact H | ] |
| 2098 |
| [ H := _ |- _ ] => subst H |
| 2099 |
end |
| 2100 |
| progress cbn [Symbolic.dag_state Symbolic.symbolic_reg_state Symbolic.symbolic_mem_state Symbolic.symbolic_flag_state] in * |
| 2101 |
| progress destruct_head' symbolic_state |
| 2102 |
| progress cbv [eval_idx_Z] in * |
| 2103 |
| apply conj; eauto 10; [] |
| 2104 |
| rewrite !Z.land_ones in * by lia |
| 2105 |
| progress (push_Zmod; pull_Zmod) |
| 2106 |
| f_equal; lia ]. |
| 2107 |
Qed. |
| 2108 |
|
| 2109 |
Lemma build_merge_stack_placeholders_G_ok {opts : symbolic_options_computed_opt} {descr:description} G G' s s' |
| 2110 |
(rsp_val : Z) (stack_vals : list Z) stack_base |
| 2111 |
(H : build_merge_stack_placeholders_G rsp_val stack_vals (G, s) = Some (stack_base, (G', s'))) |
| 2112 |
(d := s.(dag_state)) |
| 2113 |
(d' := s'.(dag_state)) |
| 2114 |
(r := s.(symbolic_reg_state)) |
| 2115 |
(r' := s'.(symbolic_reg_state)) |
| 2116 |
(f := s.(symbolic_flag_state)) |
| 2117 |
(f' := s'.(symbolic_flag_state)) |
| 2118 |
(m := s.(symbolic_mem_state)) |
| 2119 |
(m' := s'.(symbolic_mem_state)) |
| 2120 |
(Hd : gensym_dag_ok G d) |
| 2121 |
(Hstack_vals_bounded : Forall (fun v : Z => (0 <= v < 2 ^ 64)%Z) stack_vals) |
| 2122 |
(stack_addr_vals := List.map (fun i => Z.land (rsp_val - 8 * Z.of_nat (List.length stack_vals) + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length stack_vals))) |
| 2123 |
: ((exists rsp_idx, |
| 2124 |
set_reg r (reg_index rsp) rsp_idx = r' |
| 2125 |
/\ eval_idx_Z G' d' rsp_idx (Z.land rsp_val (Z.ones 64))) |
| 2126 |
/\ f = f' |
| 2127 |
/\ (exists stack_addr_idxs stack_idxs, |
| 2128 |
Forall2 (eval_idx_Z G' d') stack_addr_idxs stack_addr_vals |
| 2129 |
/\ Forall2 (eval_idx_Z G' d') stack_idxs stack_vals |
| 2130 |
/\ (* TODO: Is this too specific a spec? *) List.rev (List.combine stack_addr_idxs stack_idxs) ++ m = m') |
| 2131 |
/\ eval_idx_Z G' d' stack_base (Z.land (rsp_val - 8 * Z.of_nat (List.length stack_vals)) (Z.ones 64))) |
| 2132 |
/\ gensym_dag_ok G' d' |
| 2133 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 2134 |
Proof. |
| 2135 |
cbv [build_merge_stack_placeholders_G ret Symbolic.bind ErrorT.bind G.bind G.lift Crypto.Util.Option.bind lift_dag_G G.ret] in H. |
| 2136 |
break_match_hyps. |
| 2137 |
all: repeat first [ progress inversion_option |
| 2138 |
| progress inversion_ErrorT |
| 2139 |
| progress subst |
| 2140 |
| progress destruct_head'_and |
| 2141 |
| progress destruct_head'_prod |
| 2142 |
| progress cbn [fst snd dag_state update_dag_with update_mem_with symbolic_reg_state symbolic_flag_state] in * |
| 2143 |
| progress destruct_head'_prod |
| 2144 |
| progress destruct_head'_ex |
| 2145 |
| assumption |
| 2146 |
| progress intros |
| 2147 |
| solve [ eauto ] |
| 2148 |
| progress break_innermost_match_hyps |
| 2149 |
| rewrite @fold_left_id in * |
| 2150 |
| progress cbv [Symbolic.App Merge] in * |
| 2151 |
| progress cbn [fold_right update_dag_with Symbolic.dag_state Symbolic.symbolic_mem_state] in * |
| 2152 |
| progress change (Z.of_N 64) with 64%Z in * |
| 2153 |
| match goal with |
| 2154 |
| [ H : (?x, ?y) = (?x', ?y') |- _ ] |
| 2155 |
=> first [ is_var x | is_var y ]; is_var x'; is_var y'; inversion H; clear H |
| 2156 |
| [ H : (?x, (?y, ?z)) = (?x', (?y', ?z')) |- _ ] |
| 2157 |
=> is_var x; is_var y; is_var x'; is_var y'; is_var z'; inversion H; clear H |
| 2158 |
| [ H : (tt, (?y, ?z)) = (tt, (?y', ?z')) |- _ ] |
| 2159 |
=> is_var y; is_var z; is_var y'; is_var z'; inversion H; clear H |
| 2160 |
| [ H : compute_stack_base_G _ _ _ = _ |- _ ] |
| 2161 |
=> eapply compute_stack_base_G_ok in H; |
| 2162 |
[ | assumption ] |
| 2163 |
| [ H : build_inputarray_G _ _ = _ |- _ ] |
| 2164 |
=> apply build_inputarray_G_ok in H; [ | assumption .. ] |
| 2165 |
| [ H : build_merge_array_addresses _ _ _ = _ |- _ ] |
| 2166 |
=> eapply build_merge_array_addresses_ok in H; |
| 2167 |
[ | clear H; try solve [ assumption | lia ] .. ] |
| 2168 |
| [ H := _ |- _ ] => subst H |
| 2169 |
end ]. |
| 2170 |
{ cbv [eval_idx_Z update_mem_with symbolic_reg_state dag_state symbolic_mem_state symbolic_flag_state update_dag_with reg_index] in *; destruct_head' symbolic_state; subst. |
| 2171 |
repeat (apply conj; eauto 10; []). |
| 2172 |
repeat esplit; eauto using Forall2_weaken; |
| 2173 |
try solve [ eapply Forall2_weaken; [ | eassumption ]; eauto ]; |
| 2174 |
[]. |
| 2175 |
lazymatch goal with |
| 2176 |
| [ H : Forall2 ?R ?l1 (List.map ?f (seq 0 ?n)) |
| 2177 |
|- Forall2 ?R ?l1 (List.map ?f' (seq 0 ?n')) ] |
| 2178 |
=> cut (n = n'); [ intros <-; erewrite map_ext; [ eexact H | cbv beta ] | ] |
| 2179 |
end. |
| 2180 |
{ intros; rewrite !Z.land_ones by lia; push_Zmod; pull_Zmod. |
| 2181 |
(* saturate with length congruences *) |
| 2182 |
repeat match goal with |
| 2183 |
| [ H : Forall2 _ ?y ?x |- _ ] |
| 2184 |
=> unique assert (length y = length x) by eapply eq_length_Forall2, H |
| 2185 |
| [ |- context[length (@List.map ?A ?B ?f ?x)] ] |
| 2186 |
=> unique assert (length (@List.map A B f x) = length x) by apply map_length |
| 2187 |
end. |
| 2188 |
f_equal; lia. } |
| 2189 |
{ eapply eq_length_Forall2; eassumption. } } |
| 2190 |
Qed. |
| 2191 |
|
| 2192 |
(* TODO: move *) |
| 2193 |
Ltac handle_eval_eval := |
| 2194 |
repeat match goal with |
| 2195 |
| [ H : eval_idx_Z _ _ ?i _, H' : eval_idx_Z _ _ ?i _ |- _ ] |
| 2196 |
=> eapply eval_eval_idx_Z in H'; [ | eapply lift_eval_idx_Z_impl; [ | exact H ]; now eauto ] |
| 2197 |
| [ H : eval _ _ ?i ?v, H' : eval _ _ ?i ?v' |- _ ] |
| 2198 |
=> eapply (@eval_eval _ _ _ v) in H'; [ | now eauto ] |
| 2199 |
end. |
| 2200 |
|
| 2201 |
Lemma build_merge_base_addresses_G_ok |
| 2202 |
{opts : symbolic_options_computed_opt} {descr:description} {dereference_scalar:bool} |
| 2203 |
: forall (idxs : list (idx + list idx)) |
| 2204 |
(reg_available : list REG) |
| 2205 |
(runtime_reg : list Z) |
| 2206 |
G s G' s' |
| 2207 |
outputaddrs |
| 2208 |
(H : build_merge_base_addresses_G (dereference_scalar:=dereference_scalar) idxs reg_available runtime_reg (G, s) = Some (outputaddrs, (G', s'))) |
| 2209 |
(d := s.(dag_state)) |
| 2210 |
(Hvals : Forall2 (fun it rv |
| 2211 |
=> match it with |
| 2212 |
| inl idx => if dereference_scalar |
| 2213 |
then True |
| 2214 |
else eval_idx_Z G d idx rv |
| 2215 |
| inr _ => True |
| 2216 |
end) |
| 2217 |
idxs (List.firstn (List.length idxs) runtime_reg)) |
| 2218 |
(Hreg : Nat.min (List.length idxs) (List.length reg_available) <= List.length runtime_reg) |
| 2219 |
(Henough_reg : List.length idxs <= List.length reg_available) |
| 2220 |
(d' := s'.(dag_state)) |
| 2221 |
(r := s.(symbolic_reg_state)) |
| 2222 |
(r' := s'.(symbolic_reg_state)) |
| 2223 |
(f := s.(symbolic_flag_state)) |
| 2224 |
(f' := s'.(symbolic_flag_state)) |
| 2225 |
(m := s.(symbolic_mem_state)) |
| 2226 |
(m' := s'.(symbolic_mem_state)) |
| 2227 |
(Hd : gensym_dag_ok G d) |
| 2228 |
(Hruntime_reg_bounded : Forall (fun v => (0 <= v < 2^64)%Z) runtime_reg) |
| 2229 |
(Hreg_available_wide : Forall (fun reg => let '(rn, lo, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) reg_available), |
| 2230 |
((exists (outputaddrs' : list (idx * option idx + idx * list idx)), |
| 2231 |
let addrs_vals_of := fun base_reg_val addrs' => List.map (fun i => Z.land (base_reg_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length addrs')) in |
| 2232 |
fold_left (fun rst '(r, idx') |
| 2233 |
=> set_reg rst (reg_index r) |
| 2234 |
match idx' with |
| 2235 |
| inl (idx', _) => idx' |
| 2236 |
| inr (base_idx', idxs') => base_idx' |
| 2237 |
end) |
| 2238 |
(List.combine reg_available outputaddrs') |
| 2239 |
r |
| 2240 |
= r' |
| 2241 |
/\ ((* TODO: Is this too specific a spec? *) |
| 2242 |
List.rev (List.flat_map |
| 2243 |
(fun '(idx', idx) |
| 2244 |
=> match idx', idx with |
| 2245 |
| inl (reg_val, Some addr), inl val => if dereference_scalar then [(addr, val)] else [] |
| 2246 |
|
| 2247 |
| inl _, _ | _, inl _ => [] |
| 2248 |
| inr (base', addrs'), inr items |
| 2249 |
=> List.combine addrs' items |
| 2250 |
end) |
| 2251 |
(List.combine outputaddrs' idxs)) |
| 2252 |
++ m) |
| 2253 |
= m' |
| 2254 |
/\ (* outputaddrs' base / array *) |
| 2255 |
Forall2 |
| 2256 |
(fun idx' v => |
| 2257 |
match idx' with |
| 2258 |
| inl (idx', addr') (* scalars *) |
| 2259 |
=> eval_idx_Z G' d' idx' (Z.land v (Z.ones 64)) |
| 2260 |
/\ option_eq (eval_idx_Z G' d') addr' (if dereference_scalar then Some (Z.land v (Z.ones 64)) else None) |
| 2261 |
| inr (base', addrs') |
| 2262 |
(* arrays: (exists base', |
| 2263 |
(* set_reg r rn base' = r' |
| 2264 |
/\ *) eval_idx_Z G' d' base' (Z.land base_reg_val (Z.ones 64))) *) |
| 2265 |
=> eval_idx_Z G' d' base' (Z.land v (Z.ones 64)) |
| 2266 |
/\ (* arrays : Forall2 (eval_idx_Z G' d') addrs addrs_vals *) |
| 2267 |
Forall2 (eval_idx_Z G' d') addrs' (addrs_vals_of v addrs') |
| 2268 |
end) |
| 2269 |
outputaddrs' |
| 2270 |
(List.firstn (List.length outputaddrs') runtime_reg) |
| 2271 |
/\ (* outputaddrs base: |
| 2272 |
arrays: eval_idx_Z G' d' base (Z.land base_reg_val (Z.ones 64)) *) |
| 2273 |
Forall2 |
| 2274 |
(fun idx base_reg_val => |
| 2275 |
match idx with |
| 2276 |
| inr base => eval_idx_Z G' d' base (Z.land base_reg_val (Z.ones 64)) |
| 2277 |
| inl (inr base) => eval_idx_Z G' d' base (Z.land base_reg_val (Z.ones 64)) |
| 2278 |
| inl (inl r) => True |
| 2279 |
end) |
| 2280 |
outputaddrs |
| 2281 |
(List.firstn (List.length outputaddrs) runtime_reg) |
| 2282 |
/\ (* outputaddrs reg: *) |
| 2283 |
Forall2 |
| 2284 |
(fun idx r => |
| 2285 |
match idx with |
| 2286 |
| inl (inl r') => r = r' |
| 2287 |
| inl (inr _) | inr _ => True |
| 2288 |
end) |
| 2289 |
outputaddrs |
| 2290 |
(List.firstn (List.length outputaddrs) reg_available) |
| 2291 |
/\ Forall2 (fun addr idx => |
| 2292 |
match addr, idx with |
| 2293 |
| inl (idx, _), inl val_idx |
| 2294 |
=> if dereference_scalar |
| 2295 |
then True |
| 2296 |
else forall v, (0 <= v < 2^64)%Z -> eval_idx_Z G' d' idx v <-> eval_idx_Z G' d' val_idx v |
| 2297 |
| inr (_, ls), inr ls' => List.length ls = List.length ls' |
| 2298 |
| inl _, inr _ | inr _, inl _ => False |
| 2299 |
end) |
| 2300 |
outputaddrs' idxs |
| 2301 |
/\ Forall2 (fun addr idx => |
| 2302 |
match addr, idx with |
| 2303 |
| inl (inl _), inl (_, None) => dereference_scalar = false |
| 2304 |
| inl (inr addr), inl (_, Some addr') => dereference_scalar = true /\ addr = addr' |
| 2305 |
| inr _, inr _ => True |
| 2306 |
| inl (inl _), inl (_, Some _) | inl (inr _), inl (_, None) => False |
| 2307 |
| inl _, inr _ | inr _, inl _ => False |
| 2308 |
end) |
| 2309 |
outputaddrs outputaddrs') |
| 2310 |
/\ f = f') |
| 2311 |
/\ gensym_dag_ok G' d' |
| 2312 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 2313 |
Proof. |
| 2314 |
induction idxs as [|idx idxs IH], |
| 2315 |
reg_available as [|reg_available reg_availables], |
| 2316 |
runtime_reg as [|runtime_reg runtime_regs]; |
| 2317 |
try specialize (IH reg_availables runtime_regs). |
| 2318 |
all: repeat first [ progress cbn [build_merge_base_addresses_G List.length firstn] in * |
| 2319 |
| progress cbv [Symbolic.bind ErrorT.bind Symbolic.ret G.ret G.lift G.bind Crypto.Util.Option.bind] in * |
| 2320 |
| progress intros |
| 2321 |
| progress inversion_option |
| 2322 |
| progress inversion_prod |
| 2323 |
| progress subst |
| 2324 |
| lia |
| 2325 |
| apply conj; eauto; [] |
| 2326 |
| solve [ exists nil; cbn [List.combine fold_left flat_map rev List.app length firstn]; eauto 10 ] ]. |
| 2327 |
all: [ > ]. (* only one goal left *) |
| 2328 |
all: repeat |
| 2329 |
first [ progress subst |
| 2330 |
| progress inversion_option |
| 2331 |
| progress destruct_head'_prod |
| 2332 |
| progress destruct_head'_unit |
| 2333 |
| progress destruct_head'_and |
| 2334 |
| progress destruct_head'_ex |
| 2335 |
| assumption |
| 2336 |
| progress specialize_by_assumption |
| 2337 |
| progress specialize_by (eapply Forall2_weaken; [ | eassumption ]; intros; break_innermost_match; eauto using lift_eval_idx_Z_impl) |
| 2338 |
| progress cbn [length firstn List.combine fold_left flat_map rev Symbolic.dag_state Symbolic.symbolic_reg_state Symbolic.symbolic_mem_state Symbolic.symbolic_flag_state update_mem_with fst snd] in * |
| 2339 |
| progress destruct_head' symbolic_state |
| 2340 |
| match goal with |
| 2341 |
| [ H : (_, _) = (_, _) |- _ ] => inversion H; clear H |
| 2342 |
| [ H : Forall2 _ (_ :: _) _ |- _ ] => inversion H; clear H |
| 2343 |
| [ H : Forall2 _ [] _ |- _ ] => inversion H; clear H |
| 2344 |
| [ H : Forall _ (_ :: _) |- _ ] => inversion H; clear H |
| 2345 |
| [ H : Forall _ [] |- _ ] => clear H |
| 2346 |
| [ H : context[Nat.min (S _) (S _)] |- _ ] |
| 2347 |
=> rewrite <- Nat.succ_min_distr in H |
| 2348 |
| [ H : S ?x <= S ?y |- _ ] => |
| 2349 |
assert (x <= y) by (clear -H; lia); clear H |
| 2350 |
| [ H : SetReg _ _ _ = _ |- _ ] |
| 2351 |
=> eapply SetReg_ok in H; [ | solve [ eassumption | reflexivity ] .. ] |
| 2352 |
| [ H : SetRegFresh_G _ _ _ = _ |- _ ] |
| 2353 |
=> eapply SetRegFresh_G_ok in H; |
| 2354 |
[ | solve [ reflexivity | assumption | split; assumption | cbv [index_and_shift_and_bitcount_of_reg] in *; congruence ] .. ] |
| 2355 |
| [ H : build_merge_array_addresses _ _ _ = _ |- _ ] |
| 2356 |
=> eapply build_merge_array_addresses_ok in H; |
| 2357 |
[ | solve [ eassumption | reflexivity ] .. ] |
| 2358 |
| [ IH : forall G s G' s' outputaddrs, build_merge_base_addresses_G ?idxs ?reg_availables ?runtime_regs (G, s) = Some (outputaddrs, (G', s')) -> _, |
| 2359 |
H : build_merge_base_addresses_G ?idxs ?reg_availables ?runtime_regs (?Gv, ?sv) = Some (?outputaddrsv, (?G'v, ?s'v)) |
| 2360 |
|- _ ] |
| 2361 |
=> specialize (IH _ _ _ _ _ H); clear H |
| 2362 |
(* |
| 2363 |
| [ H : symbolic_flag_state _ = symbolic_flag_state ?x, H' : symbolic_flag_state ?x = _ |- _ ] |
| 2364 |
=> is_var x; destruct x*) |
| 2365 |
end |
| 2366 |
| break_innermost_match_hyps_step |
| 2367 |
| apply conj; eauto 10; [] |
| 2368 |
| progress cbv [index_and_shift_and_bitcount_of_reg] in * ]. |
| 2369 |
(* above is all reversible, evar-free; below is irreversible *) |
| 2370 |
all: exactly_once ((lazymatch goal with |- context[inr _ :: _] => idtac end; |
| 2371 |
eexists (inr (_, _) :: _)) |
| 2372 |
+ (lazymatch goal with |- context[inl (inl _) :: _] => idtac end; |
| 2373 |
eexists (inl (_, None) :: _)) |
| 2374 |
+ (lazymatch goal with |- context[inl (inr _) :: _] => idtac end; |
| 2375 |
eexists (inl (_, Some _) :: _))). |
| 2376 |
all: repeat |
| 2377 |
repeat |
| 2378 |
first [ progress cbn [length firstn List.combine fold_left flat_map rev] in * |
| 2379 |
| progress intros |
| 2380 |
| rewrite rev_app_distr |
| 2381 |
| rewrite <- ?app_assoc; progress cbn [List.app] |
| 2382 |
| rewrite !app_assoc |
| 2383 |
| rewrite app_nil_r |
| 2384 |
| progress cbv [option_eq] in * |
| 2385 |
| progress inversion_prod |
| 2386 |
| progress subst |
| 2387 |
| progress handle_eval_eval |
| 2388 |
| (* instantiate evars early to avoid wrong instantiations *) |
| 2389 |
match goal with |
| 2390 |
| [ |- context[?x ++ ?y :: ?z = ?x' ++ ?y' :: ?z] ] |
| 2391 |
=> progress unify y y' |
| 2392 |
end |
| 2393 |
| reflexivity |
| 2394 |
| eassumption |
| 2395 |
| solve [ eauto using Forall2_weaken, lift_eval_idx_Z_impl ] |
| 2396 |
| apply conj |
| 2397 |
| match goal with |
| 2398 |
| [ |- Forall2 _ (_ :: _) (_ :: _) ] => constructor |
| 2399 |
| [ |- fold_left ?f _ _ = fold_left ?f _ _ ] => f_equal |
| 2400 |
| [ |- _ :: _ = _ :: _ ] => f_equal |
| 2401 |
| [ |- (_, _) = (_, _) ] => f_equal |
| 2402 |
| [ |- ?x ++ ?y = ?x' ++ ?y ] => f_equal |
| 2403 |
| [ |- ?x ++ ?y = ?x ++ ?y' ] => f_equal |
| 2404 |
| [ |- rev _ = rev _ ] => apply f_equal |
| 2405 |
| [ |- ?x ++ ?y = ?y ] => cut (x = []); [ now intros -> | ] |
| 2406 |
| [ |- ?y ++ ?x = ?y ] => cut (x = []); [ now intros ->; rewrite app_nil_r | ] |
| 2407 |
| [ |- rev ?x = nil ] => cut (x = []); [ now intros -> | ] |
| 2408 |
| [ H : eval_idx_Z _ _ ?i ?v |- eval_idx_Z _ _ ?i ?v' ] |
| 2409 |
=> cut (v' = v); [ intros ->; eapply lift_eval_idx_Z_impl; [ | exact H ]; eauto | rewrite !Z.land_ones, !Z.mod_small by lia ] |
| 2410 |
(*| [ |- match ?e with inl _ => ?v | inr _ => _ end = ?v' ] |
| 2411 |
=> assert_fails unify v v'; instantiate (1:=ltac:(right; repeat exactly_once econstructor)); reflexivity*) |
| 2412 |
| [ H : Forall2 _ ?l (List.map _ (seq 0 (length ?l'))) |- _ ] |
| 2413 |
=> is_var l; is_var l'; assert_fails constr_eq l l'; |
| 2414 |
let H' := fresh in |
| 2415 |
assert (H' : length l' = length l) by (now apply eq_length_Forall2 in H; rewrite H, map_length, seq_length); |
| 2416 |
rewrite H' in * |
| 2417 |
| [ H : Forall2 _ ?l (List.map ?f ?ls) |- Forall2 _ ?l (List.map ?f' ?ls) ] |
| 2418 |
=> erewrite map_ext; [ eapply Forall2_weaken; [ | exact H ] | cbv beta ]; |
| 2419 |
[ eauto using lift_eval_idx_Z_impl |
| 2420 |
| intros; rewrite !Z.land_ones by lia; push_Zmod; pull_Zmod ] |
| 2421 |
end ]. |
| 2422 |
Qed. |
| 2423 |
|
| 2424 |
Lemma compute_stack_ok {opts : symbolic_options_computed_opt} {descr:description} G s s' base |
| 2425 |
(rsp_val : Z) (stack_size : nat) |
| 2426 |
(H : compute_stack_base stack_size s = Success (base, s')) |
| 2427 |
(d := s.(dag_state)) |
| 2428 |
(d' := s'.(dag_state)) |
| 2429 |
(r := s.(symbolic_reg_state)) |
| 2430 |
(r' := s'.(symbolic_reg_state)) |
| 2431 |
(f := s.(symbolic_flag_state)) |
| 2432 |
(f' := s'.(symbolic_flag_state)) |
| 2433 |
(m := s.(symbolic_mem_state)) |
| 2434 |
(m' := s'.(symbolic_mem_state)) |
| 2435 |
(Hd : gensym_dag_ok G d) |
| 2436 |
: exists G', |
| 2437 |
((exists rsp_idx, |
| 2438 |
set_reg r (reg_index rsp) rsp_idx = r' |
| 2439 |
/\ eval_idx_Z G' d' rsp_idx (Z.land rsp_val (Z.ones 64))) |
| 2440 |
/\ f = f' |
| 2441 |
/\ m = m') |
| 2442 |
/\ eval_idx_Z G' d' base (Z.land (rsp_val - 8 * Z.of_nat stack_size) (Z.ones 64)) |
| 2443 |
/\ gensym_dag_ok G' d' |
| 2444 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 2445 |
Proof. |
| 2446 |
eapply compute_stack_base_eq_G in H. |
| 2447 |
destruct H as [G' H]; exists G'. |
| 2448 |
eapply compute_stack_base_G_ok in H; try eassumption. |
| 2449 |
Qed. |
| 2450 |
|
| 2451 |
Lemma build_merge_stack_placeholders_ok {opts : symbolic_options_computed_opt} {descr:description} G s s' |
| 2452 |
(rsp_val : Z) (stack_vals : list Z) stack_base |
| 2453 |
(H : build_merge_stack_placeholders (List.length stack_vals) s = Success (stack_base, s')) |
| 2454 |
(d := s.(dag_state)) |
| 2455 |
(d' := s'.(dag_state)) |
| 2456 |
(r := s.(symbolic_reg_state)) |
| 2457 |
(r' := s'.(symbolic_reg_state)) |
| 2458 |
(f := s.(symbolic_flag_state)) |
| 2459 |
(f' := s'.(symbolic_flag_state)) |
| 2460 |
(m := s.(symbolic_mem_state)) |
| 2461 |
(m' := s'.(symbolic_mem_state)) |
| 2462 |
(Hd : gensym_dag_ok G d) |
| 2463 |
(Hstack_vals_bounded : Forall (fun v : Z => (0 <= v < 2 ^ 64)%Z) stack_vals) |
| 2464 |
(stack_addr_vals := List.map (fun i => Z.land (rsp_val - 8 * Z.of_nat (List.length stack_vals) + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length stack_vals))) |
| 2465 |
: exists G', |
| 2466 |
((exists rsp_idx, |
| 2467 |
set_reg r (reg_index rsp) rsp_idx = r' |
| 2468 |
/\ eval_idx_Z G' d' rsp_idx (Z.land rsp_val (Z.ones 64))) |
| 2469 |
/\ f = f' |
| 2470 |
/\ (exists stack_addr_idxs stack_idxs, |
| 2471 |
Forall2 (eval_idx_Z G' d') stack_addr_idxs stack_addr_vals |
| 2472 |
/\ Forall2 (eval_idx_Z G' d') stack_idxs stack_vals |
| 2473 |
/\ (* TODO: Is this too specific a spec? *) List.rev (List.combine stack_addr_idxs stack_idxs) ++ m = m') |
| 2474 |
/\ eval_idx_Z G' d' stack_base (Z.land (rsp_val - 8 * Z.of_nat (List.length stack_vals)) (Z.ones 64))) |
| 2475 |
/\ gensym_dag_ok G' d' |
| 2476 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 2477 |
Proof. |
| 2478 |
eapply build_merge_stack_placeholders_eq_G in H. |
| 2479 |
destruct H as [G' H]; exists G'. |
| 2480 |
eapply build_merge_stack_placeholders_G_ok in H; try eassumption. |
| 2481 |
Qed. |
| 2482 |
|
| 2483 |
Lemma build_merge_base_addresses_ok |
| 2484 |
{opts : symbolic_options_computed_opt} {descr:description} {dereference_scalar:bool} |
| 2485 |
(idxs : list (idx + list idx)) |
| 2486 |
(reg_available : list REG) |
| 2487 |
(runtime_reg : list Z) |
| 2488 |
G s |
| 2489 |
(d := s.(dag_state)) |
| 2490 |
(Hvals : Forall2 (fun it rv |
| 2491 |
=> match it with |
| 2492 |
| inl idx => if dereference_scalar |
| 2493 |
then True |
| 2494 |
else eval_idx_Z G d idx rv |
| 2495 |
| inr _ => True |
| 2496 |
end) |
| 2497 |
idxs (List.firstn (List.length idxs) runtime_reg)) |
| 2498 |
(Hreg : Nat.min (List.length idxs) (List.length reg_available) <= List.length runtime_reg) |
| 2499 |
(Henough_reg : List.length idxs <= List.length reg_available) |
| 2500 |
s' |
| 2501 |
(d' := s'.(dag_state)) |
| 2502 |
(r := s.(symbolic_reg_state)) |
| 2503 |
(r' := s'.(symbolic_reg_state)) |
| 2504 |
(f := s.(symbolic_flag_state)) |
| 2505 |
(f' := s'.(symbolic_flag_state)) |
| 2506 |
(m := s.(symbolic_mem_state)) |
| 2507 |
(m' := s'.(symbolic_mem_state)) |
| 2508 |
outputaddrs |
| 2509 |
(H : build_merge_base_addresses (dereference_scalar:=dereference_scalar) idxs reg_available s = Success (outputaddrs, s')) |
| 2510 |
(Hd : gensym_dag_ok G d) |
| 2511 |
(Hruntime_reg_bounded : Forall (fun v => (0 <= v < 2^64)%Z) runtime_reg) |
| 2512 |
(Hreg_available_wide : Forall (fun reg => let '(rn, lo, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) reg_available) |
| 2513 |
: exists G', |
| 2514 |
((exists (outputaddrs' : list (idx * option idx + idx * list idx)), |
| 2515 |
let addrs_vals_of := fun base_reg_val addrs' => List.map (fun i => Z.land (base_reg_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length addrs')) in |
| 2516 |
fold_left (fun rst '(r, idx') |
| 2517 |
=> set_reg rst (reg_index r) |
| 2518 |
match idx' with |
| 2519 |
| inl (idx', _) => idx' |
| 2520 |
| inr (base_idx', idxs') => base_idx' |
| 2521 |
end) |
| 2522 |
(List.combine reg_available outputaddrs') |
| 2523 |
r |
| 2524 |
= r' |
| 2525 |
/\ ((* TODO: Is this too specific a spec? *) |
| 2526 |
List.rev (List.flat_map |
| 2527 |
(fun '(idx', idx) |
| 2528 |
=> match idx', idx with |
| 2529 |
| inl (reg_val, Some addr), inl val => if dereference_scalar then [(addr, val)] else [] |
| 2530 |
|
| 2531 |
| inl _, _ | _, inl _ => [] |
| 2532 |
| inr (base', addrs'), inr items |
| 2533 |
=> List.combine addrs' items |
| 2534 |
end) |
| 2535 |
(List.combine outputaddrs' idxs)) |
| 2536 |
++ m) |
| 2537 |
= m' |
| 2538 |
/\ (* outputaddrs' base / array *) |
| 2539 |
Forall2 |
| 2540 |
(fun idx' v => |
| 2541 |
match idx' with |
| 2542 |
| inl (idx', addr') (* scalars *) |
| 2543 |
=> eval_idx_Z G' d' idx' (Z.land v (Z.ones 64)) |
| 2544 |
/\ option_eq (eval_idx_Z G' d') addr' (if dereference_scalar then Some (Z.land v (Z.ones 64)) else None) |
| 2545 |
| inr (base', addrs') |
| 2546 |
(* arrays: (exists base', |
| 2547 |
(* set_reg r rn base' = r' |
| 2548 |
/\ *) eval_idx_Z G' d' base' (Z.land base_reg_val (Z.ones 64))) *) |
| 2549 |
=> eval_idx_Z G' d' base' (Z.land v (Z.ones 64)) |
| 2550 |
/\ (* arrays : Forall2 (eval_idx_Z G' d') addrs addrs_vals *) |
| 2551 |
Forall2 (eval_idx_Z G' d') addrs' (addrs_vals_of v addrs') |
| 2552 |
end) |
| 2553 |
outputaddrs' |
| 2554 |
(List.firstn (List.length outputaddrs') runtime_reg) |
| 2555 |
/\ (* outputaddrs base: |
| 2556 |
arrays: eval_idx_Z G' d' base (Z.land base_reg_val (Z.ones 64)) *) |
| 2557 |
Forall2 |
| 2558 |
(fun idx base_reg_val => |
| 2559 |
match idx with |
| 2560 |
| inr base => eval_idx_Z G' d' base (Z.land base_reg_val (Z.ones 64)) |
| 2561 |
| inl (inr base) => eval_idx_Z G' d' base (Z.land base_reg_val (Z.ones 64)) |
| 2562 |
| inl (inl r) => True |
| 2563 |
end) |
| 2564 |
outputaddrs |
| 2565 |
(List.firstn (List.length outputaddrs) runtime_reg) |
| 2566 |
/\ (* outputaddrs reg: *) |
| 2567 |
Forall2 |
| 2568 |
(fun idx r => |
| 2569 |
match idx with |
| 2570 |
| inl (inl r') => r = r' |
| 2571 |
| inl (inr _) | inr _ => True |
| 2572 |
end) |
| 2573 |
outputaddrs |
| 2574 |
(List.firstn (List.length outputaddrs) reg_available) |
| 2575 |
/\ Forall2 (fun addr idx => |
| 2576 |
match addr, idx with |
| 2577 |
| inl (idx, _), inl val_idx |
| 2578 |
=> if dereference_scalar |
| 2579 |
then True |
| 2580 |
else forall v, (0 <= v < 2^64)%Z -> eval_idx_Z G' d' idx v <-> eval_idx_Z G' d' val_idx v |
| 2581 |
| inr (_, ls), inr ls' => List.length ls = List.length ls' |
| 2582 |
| inl _, inr _ | inr _, inl _ => False |
| 2583 |
end) |
| 2584 |
outputaddrs' idxs |
| 2585 |
/\ Forall2 (fun addr idx => |
| 2586 |
match addr, idx with |
| 2587 |
| inl (inl _), inl (_, None) => dereference_scalar = false |
| 2588 |
| inl (inr addr), inl (_, Some addr') => dereference_scalar = true /\ addr = addr' |
| 2589 |
| inr _, inr _ => True |
| 2590 |
| inl (inl _), inl (_, Some _) | inl (inr _), inl (_, None) => False |
| 2591 |
| inl _, inr _ | inr _, inl _ => False |
| 2592 |
end) |
| 2593 |
outputaddrs outputaddrs') |
| 2594 |
/\ f = f') |
| 2595 |
/\ gensym_dag_ok G' d' |
| 2596 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 2597 |
Proof. |
| 2598 |
eapply build_merge_base_addresses_eq_G in H; [ | eassumption .. ]. |
| 2599 |
destruct H as [G' H]; exists G'. |
| 2600 |
eapply build_merge_base_addresses_G_ok in H; try eassumption. |
| 2601 |
Qed. |
| 2602 |
|
| 2603 |
|
| 2604 |
Lemma mapM_GetReg_ok_bounded {opts : symbolic_options_computed_opt} {descr:description} G |
| 2605 |
: forall regs idxs reg_vals s s' |
| 2606 |
(H : mapM GetReg regs s = Success (idxs, s')) |
| 2607 |
(d := s.(dag_state)) |
| 2608 |
(d' := s'.(dag_state)) |
| 2609 |
(r := s.(symbolic_reg_state)) |
| 2610 |
(r' := s'.(symbolic_reg_state)) |
| 2611 |
(f := s.(symbolic_flag_state)) |
| 2612 |
(f' := s'.(symbolic_flag_state)) |
| 2613 |
(m := s.(symbolic_mem_state)) |
| 2614 |
(m' := s'.(symbolic_mem_state)) |
| 2615 |
(Hd : gensym_dag_ok G d) |
| 2616 |
(Hwide : Forall (fun reg => let '(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) regs) |
| 2617 |
(Hbounded : Forall (fun v => (0 <= v < 2^64)%Z) reg_vals) |
| 2618 |
(Hregval_len : List.length regs = List.length reg_vals) |
| 2619 |
(Hregval : Forall2 (fun idx v => forall idx', idx = Some idx' -> eval_idx_Z G s idx' v) (List.map (get_reg r) (List.map reg_index regs)) reg_vals), |
| 2620 |
((exists reg_idxs, |
| 2621 |
List.map (get_reg r) (List.map reg_index regs) = List.map Some reg_idxs |
| 2622 |
/\ Forall2 (eval_idx_Z G s') reg_idxs reg_vals) |
| 2623 |
/\ Forall2 (eval_idx_Z G s') idxs reg_vals |
| 2624 |
/\ r = r' |
| 2625 |
/\ f = f' |
| 2626 |
/\ m = m') |
| 2627 |
/\ gensym_dag_ok G d' |
| 2628 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 2629 |
Proof. |
| 2630 |
induction regs as [|reg regs IH], |
| 2631 |
idxs as [|idx idxs], |
| 2632 |
reg_vals as [|reg_val reg_vals]; |
| 2633 |
try specialize (IH idxs reg_vals). |
| 2634 |
all: repeat first [ progress cbn [mapM List.map fst snd Symbolic.dag_state Symbolic.symbolic_reg_state Symbolic.symbolic_mem_state Symbolic.symbolic_flag_state List.length] in * |
| 2635 |
| progress cbv [Symbolic.ret Symbolic.bind ErrorT.bind] in * |
| 2636 |
| progress intros |
| 2637 |
| progress destruct_head'_ex |
| 2638 |
| progress destruct_head'_prod |
| 2639 |
| progress destruct_head'_and |
| 2640 |
| progress inversion_option |
| 2641 |
| progress inversion_ErrorT |
| 2642 |
| progress inversion_prod |
| 2643 |
| progress subst |
| 2644 |
| progress inversion_list |
| 2645 |
| progress inversion_nat_eq |
| 2646 |
| progress specialize_by_assumption |
| 2647 |
| progress destruct_head' symbolic_state |
| 2648 |
| progress specialize_by (eapply Forall2_weaken; [ | eassumption ]; cbv [option_eq]; intros *; break_innermost_match; eauto using lift_eval_idx_Z_impl) |
| 2649 |
| exfalso; lia |
| 2650 |
| match goal with |
| 2651 |
| [ H : Forall2 _ (_ :: _) _ |- _ ] => inversion H; clear H |
| 2652 |
| [ H : Forall2 _ [] _ |- _ ] => inversion H; clear H |
| 2653 |
| [ H : Forall _ (_ :: _) |- _ ] => inversion H; clear H |
| 2654 |
| [ |- exists reg_idxs, _ :: _ = List.map Some reg_idxs /\ _ ] |
| 2655 |
=> eexists (_ :: _); cbn [List.map]; split; [ apply f_equal2; eassumption | ] |
| 2656 |
| [ IH : forall s s', mapM ?f ?ls s = Success (?v, s') -> _, H : mapM ?f ?ls _ = Success (?v, _) |- _ ] |
| 2657 |
=> specialize (IH _ _ H) |
| 2658 |
| [ H : forall ls, nil = List.map _ ls -> _ |- _ ] |
| 2659 |
=> specialize (H nil eq_refl) |
| 2660 |
| [ |- exists ls, nil = List.map _ ls /\ _ ] => exists nil |
| 2661 |
| [ H : get_reg ?r ?ri = Some ?i, H' : eval_idx_Z ?G ?d ?i ?v |- _ ] |
| 2662 |
=> unique assert (forall i', get_reg r ri = Some i' -> eval_idx_Z G d i' v) |
| 2663 |
by now intro; rewrite H; inversion 1; subst; assumption |
| 2664 |
| [ H : option_eq _ _ (Some _) |- _ ] => cbv [option_eq] in H |
| 2665 |
| [ H : GetReg _ _ = _ |- _ ] |
| 2666 |
=> eapply GetReg_ok_bounded in H; [ | solve [ eassumption | reflexivity | lia ] .. ] |
| 2667 |
| [ |- Forall2 _ (_ :: _) (_ :: _) ] => constructor |
| 2668 |
end |
| 2669 |
| solve [ eauto 10 using lift_eval_idx_Z_impl ] |
| 2670 |
| break_innermost_match_hyps_step |
| 2671 |
| apply conj; eauto 10 using lift_eval_idx_Z_impl; [] ]. |
| 2672 |
Qed. |
| 2673 |
|
| 2674 |
Lemma reg_all_set_set_reg r regs i v |
| 2675 |
: Forall (fun v => Option.is_Some v = true) (List.map (get_reg r) regs) |
| 2676 |
-> Forall (fun v => Option.is_Some v = true) |
| 2677 |
(List.map (get_reg (set_reg r i v)) regs). |
| 2678 |
Proof. |
| 2679 |
rewrite !Forall_map; apply Forall_impl; intro. |
| 2680 |
rewrite get_reg_set_reg_full. |
| 2681 |
cbv [Option.is_Some]; break_innermost_match; congruence. |
| 2682 |
Qed. |
| 2683 |
|
| 2684 |
Lemma reg_all_set_fold_left {A} r regs f (ls : list A) |
| 2685 |
(Hf : forall rst v, |
| 2686 |
List.In v ls |
| 2687 |
-> Forall (fun v => Option.is_Some v = true) (List.map (get_reg rst) regs) |
| 2688 |
-> Forall (fun v => Option.is_Some v = true) (List.map (get_reg (f rst v)) regs)) |
| 2689 |
: Forall (fun v => Option.is_Some v = true) (List.map (get_reg r) regs) |
| 2690 |
-> Forall (fun v => Option.is_Some v = true) |
| 2691 |
(List.map (get_reg (fold_left f ls r)) |
| 2692 |
regs). |
| 2693 |
Proof. |
| 2694 |
revert r; induction ls as [|x xs IH]; intros; cbn [fold_left]; auto. |
| 2695 |
apply IH; break_innermost_match; intros; apply Hf; cbn [List.In] in *; auto. |
| 2696 |
Qed. |
| 2697 |
|
| 2698 |
Lemma eq_load_remove_iff a s v |
| 2699 |
: load a s = Some v <-> exists ls, fst (remove a s) = v :: ls. |
| 2700 |
Proof. |
| 2701 |
split; cbv [load remove]; revert v. |
| 2702 |
all: induction s as [|?? IH]; intros *; cbn; break_innermost_match; cbn. |
| 2703 |
all: intros; inversion_option; destruct_head'_ex; inversion_list; subst; eauto. |
| 2704 |
Qed. |
| 2705 |
|
| 2706 |
Lemma eq_load_remove a s |
| 2707 |
: load a s = nth_error (fst (remove a s)) 0. |
| 2708 |
Proof. |
| 2709 |
pose proof (eq_load_remove_iff a s). |
| 2710 |
split_iff. |
| 2711 |
destruct (load a s), (fst (remove a s)); cbn. |
| 2712 |
all: try match goal with H : _ |- _ => specialize (H _ eq_refl) end. |
| 2713 |
all: try match goal with H : _ |- _ => specialize (H _ (ex_intro _ _ eq_refl)) end. |
| 2714 |
all: destruct_head'_ex; inversion_list; inversion_option; subst; try reflexivity. |
| 2715 |
Qed. |
| 2716 |
|
| 2717 |
Lemma eq_load_of_remove a s v ls |
| 2718 |
: fst (remove a s) = v :: ls -> load a s = Some v. |
| 2719 |
Proof. |
| 2720 |
intro H; rewrite eq_load_remove, H; reflexivity. |
| 2721 |
Qed. |
| 2722 |
|
| 2723 |
Definition same_reg_some (s1 s2 : Symbolic.reg_state) : Prop |
| 2724 |
:= forall reg, Option.is_Some (get_reg s1 (reg_index reg)) = true -> Option.is_Some (get_reg s2 (reg_index reg)) = true. |
| 2725 |
|
| 2726 |
Local Instance same_reg_some_trans : Transitive same_reg_some | 100. |
| 2727 |
Proof. cbv [Transitive same_reg_some]; eauto. Qed. |
| 2728 |
|
| 2729 |
Class same_reg_some_of_success {T} (f : M T) |
| 2730 |
:= success_same_reg : forall v s s', f s = Success (v, s') -> same_reg_some s s'. |
| 2731 |
|
| 2732 |
Local Ltac same_reg_some_of_success_t_step_normal_nobreak := |
| 2733 |
first [ match goal with |
| 2734 |
| [ H : context[set_reg _ _ ?v] |- _ ] |
| 2735 |
=> tryif is_var v then fail else generalize dependent v; intros |
| 2736 |
end |
| 2737 |
| progress cbv [same_reg_some_of_success] in * |
| 2738 |
| progress intros |
| 2739 |
| reflexivity |
| 2740 |
| assumption |
| 2741 |
| progress inversion_ErrorT |
| 2742 |
| progress destruct_head'_prod |
| 2743 |
| progress cbn [fst snd Symbolic.symbolic_reg_state] in * |
| 2744 |
| progress cbv [update_dag_with update_mem_with update_flag_with update_reg_with Symbolic.bind Symbolic.ret Symbolic.err some_or ErrorT.bind] in * |
| 2745 |
| progress inversion_pair |
| 2746 |
| progress subst |
| 2747 |
| match goal with |
| 2748 |
| [ H : _ = Success _ |- _ ] => apply success_same_reg in H |
| 2749 |
| [ |- same_reg_some _ (set_reg _ _ _) ] |
| 2750 |
=> cbv [same_reg_some] in *; intros; rewrite get_reg_set_reg_full; |
| 2751 |
break_innermost_match |
| 2752 |
end |
| 2753 |
| cbv [same_reg_some] in *; eauto with nocore; congruence ]. |
| 2754 |
|
| 2755 |
Local Ltac same_reg_some_of_success_t_step_normal := |
| 2756 |
first [ same_reg_some_of_success_t_step_normal_nobreak |
| 2757 |
| break_innermost_match_hyps_step ]. |
| 2758 |
|
| 2759 |
Local Ltac same_reg_some_of_success_t_step_unfold := |
| 2760 |
match goal with |
| 2761 |
| [ H : ?f = Success _ |- _ ] |
| 2762 |
=> let f' := head f in |
| 2763 |
(*idtac f';*) |
| 2764 |
progress unfold f' in H |
| 2765 |
end. |
| 2766 |
|
| 2767 |
Local Ltac same_reg_some_of_success_t_step_unfold_go := |
| 2768 |
repeat same_reg_some_of_success_t_step_normal; |
| 2769 |
same_reg_some_of_success_t_step_unfold; |
| 2770 |
repeat same_reg_some_of_success_t_step_normal. |
| 2771 |
|
| 2772 |
Local Ltac same_reg_some_of_success_t := repeat first [ same_reg_some_of_success_t_step_normal | same_reg_some_of_success_t_step_unfold ]. |
| 2773 |
|
| 2774 |
(* TODO: move? *) |
| 2775 |
Local Instance Merge_reg_same {descr:description} x : same_reg_some_of_success (Symbolic.Merge x). |
| 2776 |
Proof. same_reg_some_of_success_t. Qed. |
| 2777 |
|
| 2778 |
(* TODO: move? *) |
| 2779 |
Local Instance App_reg_same {opts : symbolic_options_computed_opt} {descr:description} x : same_reg_some_of_success (Symbolic.App x). |
| 2780 |
Proof. same_reg_some_of_success_t. Qed. |
| 2781 |
|
| 2782 |
(* TODO: move? *) |
| 2783 |
Local Instance GetReg_reg_same {opts : symbolic_options_computed_opt} {descr:description} r : same_reg_some_of_success (GetReg r). |
| 2784 |
Proof. same_reg_some_of_success_t. Qed. |
| 2785 |
|
| 2786 |
(* TODO: move? *) |
| 2787 |
Local Instance Address_reg_same {opts : symbolic_options_computed_opt} {descr:description} {sa:AddressSize} a : same_reg_some_of_success (Address a). |
| 2788 |
Proof. same_reg_some_of_success_t. Qed. |
| 2789 |
|
| 2790 |
(* TODO: move? *) |
| 2791 |
Local Instance GetOperand_reg_same {opts : symbolic_options_computed_opt} {descr:description} {sz:OperationSize} {sa:AddressSize} arg : same_reg_some_of_success (GetOperand arg). |
| 2792 |
Proof. same_reg_some_of_success_t. Qed. |
| 2793 |
|
| 2794 |
(* TODO: move? *) |
| 2795 |
Local Instance GetFlag_reg_same f : same_reg_some_of_success (GetFlag f). |
| 2796 |
Proof. same_reg_some_of_success_t. Qed. |
| 2797 |
|
| 2798 |
(* TODO: move? *) |
| 2799 |
Local Instance SetOperand_reg_same {opts : symbolic_options_computed_opt} {descr:description} {sz:OperationSize} {sa:AddressSize} arg v : same_reg_some_of_success (SetOperand arg v). |
| 2800 |
Proof. same_reg_some_of_success_t. Qed. |
| 2801 |
|
| 2802 |
(* TODO: move? *) |
| 2803 |
Local Instance SetFlag_reg_same f v : same_reg_some_of_success (SetFlag f v). |
| 2804 |
Proof. same_reg_some_of_success_t. Qed. |
| 2805 |
|
| 2806 |
(* TODO: move? *) |
| 2807 |
Local Instance RevealConst_reg_same v : same_reg_some_of_success (RevealConst v). |
| 2808 |
Proof. same_reg_some_of_success_t. Qed. |
| 2809 |
|
| 2810 |
(* TODO: move? *) |
| 2811 |
Local Instance Reveal_reg_same v1 v2 : same_reg_some_of_success (Reveal v1 v2). |
| 2812 |
Proof. same_reg_some_of_success_t. Qed. |
| 2813 |
|
| 2814 |
(* TODO: move? *) |
| 2815 |
Local Instance HavocFlags_reg_same : same_reg_some_of_success HavocFlags. |
| 2816 |
Proof. same_reg_some_of_success_t. Qed. |
| 2817 |
|
| 2818 |
(* TODO: move? *) |
| 2819 |
Local Instance ret_reg_same {T} (v : T) : same_reg_some_of_success (ret v). |
| 2820 |
Proof. same_reg_some_of_success_t. Qed. |
| 2821 |
|
| 2822 |
(* TODO: move? *) |
| 2823 |
Local Instance bind_reg_same {A B} f v |
| 2824 |
{H0 : same_reg_some_of_success v} |
| 2825 |
{H1 : forall v', same_reg_some_of_success (f v')} |
| 2826 |
: same_reg_some_of_success (@Symbolic.bind A B v f). |
| 2827 |
Proof. same_reg_some_of_success_t. Qed. |
| 2828 |
|
| 2829 |
(* TODO: move? *) |
| 2830 |
Local Instance mapM_reg_same {A B} f ls |
| 2831 |
{H1 : forall v', same_reg_some_of_success (f v')} |
| 2832 |
: same_reg_some_of_success (@mapM A B f ls). |
| 2833 |
Proof. |
| 2834 |
induction ls as [|?? IH]; cbn [mapM]; |
| 2835 |
same_reg_some_of_success_t. |
| 2836 |
Defined. |
| 2837 |
|
| 2838 |
(* TODO: move? *) |
| 2839 |
Fixpoint Symeval_reg_same opts descr sz sa args {struct args} : same_reg_some_of_success (@Symeval opts descr sz sa args). |
| 2840 |
Proof. |
| 2841 |
destruct args; cbn [Symeval] in *; typeclasses eauto. |
| 2842 |
Qed. |
| 2843 |
Local Existing Instance Symeval_reg_same. |
| 2844 |
#[global] |
| 2845 |
Typeclasses Opaque Symeval. |
| 2846 |
#[global] |
| 2847 |
Typeclasses Transparent AddressSize OperationSize. |
| 2848 |
|
| 2849 |
(* TODO: move? *) |
| 2850 |
Local Instance SymexNormalInstruction_reg_same {opts : symbolic_options_computed_opt} {descr:description} instr : same_reg_some_of_success (SymexNormalInstruction instr). |
| 2851 |
Proof. |
| 2852 |
destruct instr; cbv [SymexNormalInstruction err Symbolic.bind ret Syntax.op Syntax.args ErrorT.bind same_reg_some_of_success] in *; intros. |
| 2853 |
same_reg_some_of_success_t. |
| 2854 |
all: cbv [same_reg_some] in *; eauto 10 with nocore. |
| 2855 |
all: intro reg; repeat match goal with H : forall r : REG, _ |- _ => specialize (H reg) end. |
| 2856 |
all: rewrite !@get_reg_set_reg_full in *. |
| 2857 |
all: break_innermost_match_hyps. |
| 2858 |
all: cbn [Option.is_Some] in *. |
| 2859 |
all: eauto. |
| 2860 |
Qed. |
| 2861 |
|
| 2862 |
(* TODO: move? *) |
| 2863 |
Local Instance SymexLine_reg_same {opts : symbolic_options_computed_opt} line : same_reg_some_of_success (SymexLine line). |
| 2864 |
Proof. |
| 2865 |
cbv [SymexLine SymexRawLine err ret] in *; break_innermost_match; try congruence. |
| 2866 |
apply SymexNormalInstruction_reg_same. |
| 2867 |
Qed. |
| 2868 |
|
| 2869 |
(* TODO: move? *) |
| 2870 |
Lemma SymexLines_reg_same {opts : symbolic_options_computed_opt} lines s s' |
| 2871 |
(H : SymexLines lines s = Success (tt, s')) |
| 2872 |
: same_reg_some s s'. |
| 2873 |
Proof. |
| 2874 |
revert s H. |
| 2875 |
induction lines as [|line lines IH]; cbn [SymexLines]; cbv [Symbolic.ret Symbolic.bind ErrorT.bind]; intros *; break_innermost_match; intros; inversion_ErrorT; inversion_pair; subst; try now (hnf; eauto with nocore). |
| 2876 |
same_reg_some_of_success_t. |
| 2877 |
Qed. |
| 2878 |
|
| 2879 |
Definition same_mem_addressed (s1 s2 : Symbolic.mem_state) : Prop |
| 2880 |
:= List.map fst s1 = List.map fst s2. |
| 2881 |
|
| 2882 |
Lemma update_nth_fst_mem_same m1 n y |
| 2883 |
: same_mem_addressed m1 (update_nth n (fun ptsto : idx * idx => (fst ptsto, y)) m1). |
| 2884 |
Proof. |
| 2885 |
revert n m1. |
| 2886 |
cbv [same_mem_addressed]; induction n as [|n IH], m1 as [|? m1]; cbn [update_nth List.map fst]; f_equal; eauto. |
| 2887 |
Qed. |
| 2888 |
|
| 2889 |
(* TODO: move? *) |
| 2890 |
Lemma store_mem_same x y m1 m2 (H : store x y m1 = Some m2) |
| 2891 |
: same_mem_addressed m1 m2. |
| 2892 |
Proof. |
| 2893 |
cbv [store Crypto.Util.Option.bind] in *; break_innermost_match_hyps; inversion_option; subst. |
| 2894 |
apply update_nth_fst_mem_same. |
| 2895 |
Qed. |
| 2896 |
|
| 2897 |
Class same_mem_addressed_of_success {T} (f : M T) |
| 2898 |
:= success_same_mem : forall v s s', f s = Success (v, s') -> same_mem_addressed s s'. |
| 2899 |
|
| 2900 |
Local Ltac same_mem_addressed_of_success_t_step_normal_nobreak := |
| 2901 |
first [ progress cbv [same_mem_addressed_of_success] in * |
| 2902 |
| progress intros |
| 2903 |
| reflexivity |
| 2904 |
| assumption |
| 2905 |
| progress inversion_ErrorT |
| 2906 |
| progress destruct_head'_prod |
| 2907 |
| progress cbn [fst snd Symbolic.symbolic_mem_state] in * |
| 2908 |
| progress cbv [update_dag_with update_mem_with update_flag_with update_reg_with Symbolic.bind Symbolic.ret Symbolic.err some_or ErrorT.bind] in * |
| 2909 |
| progress inversion_pair |
| 2910 |
| progress subst |
| 2911 |
| match goal with |
| 2912 |
| [ H : store _ _ _ = Some _ |- _ ] => apply store_mem_same in H |
| 2913 |
| [ H : _ = Success _ |- _ ] => apply success_same_mem in H |
| 2914 |
end |
| 2915 |
| cbv [same_mem_addressed] in *; congruence ]. |
| 2916 |
|
| 2917 |
Local Ltac same_mem_addressed_of_success_t_step_normal := |
| 2918 |
first [ same_mem_addressed_of_success_t_step_normal_nobreak |
| 2919 |
| break_innermost_match_hyps_step ]. |
| 2920 |
|
| 2921 |
Local Ltac same_mem_addressed_of_success_t_step_unfold := |
| 2922 |
match goal with |
| 2923 |
| [ H : ?f = Success _ |- _ ] |
| 2924 |
=> let f' := head f in |
| 2925 |
(*idtac f';*) |
| 2926 |
progress unfold f' in H |
| 2927 |
end. |
| 2928 |
|
| 2929 |
Local Ltac same_mem_addressed_of_success_t_step_unfold_go := |
| 2930 |
repeat same_mem_addressed_of_success_t_step_normal; |
| 2931 |
same_mem_addressed_of_success_t_step_unfold; |
| 2932 |
repeat same_mem_addressed_of_success_t_step_normal. |
| 2933 |
|
| 2934 |
Local Ltac same_mem_addressed_of_success_t := repeat first [ same_mem_addressed_of_success_t_step_normal | same_mem_addressed_of_success_t_step_unfold ]. |
| 2935 |
|
| 2936 |
(* TODO: move? *) |
| 2937 |
Local Instance Merge_mem_same {descr:description} x : same_mem_addressed_of_success (Symbolic.Merge x). |
| 2938 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2939 |
|
| 2940 |
(* TODO: move? *) |
| 2941 |
Local Instance App_mem_same {opts : symbolic_options_computed_opt} {descr:description} x : same_mem_addressed_of_success (Symbolic.App x). |
| 2942 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2943 |
|
| 2944 |
(* TODO: move? *) |
| 2945 |
Local Instance GetReg_mem_same {opts : symbolic_options_computed_opt} {descr:description} r : same_mem_addressed_of_success (GetReg r). |
| 2946 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2947 |
|
| 2948 |
(* TODO: move? *) |
| 2949 |
Local Instance Address_mem_same opts descr sa a : same_mem_addressed_of_success (@Address opts descr sa a). |
| 2950 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2951 |
|
| 2952 |
(* TODO: move? *) |
| 2953 |
Local Instance GetOperand_mem_same opts descr sz sa arg : same_mem_addressed_of_success (@GetOperand opts descr sz sa arg). |
| 2954 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2955 |
|
| 2956 |
(* TODO: move? *) |
| 2957 |
Local Instance GetFlag_mem_same f : same_mem_addressed_of_success (GetFlag f). |
| 2958 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2959 |
|
| 2960 |
(* TODO: move? *) |
| 2961 |
Local Instance SetOperand_mem_same opts descr sz sa arg v : same_mem_addressed_of_success (@SetOperand opts descr sz sa arg v). |
| 2962 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2963 |
|
| 2964 |
(* TODO: move? *) |
| 2965 |
Local Instance SetFlag_mem_same f v : same_mem_addressed_of_success (SetFlag f v). |
| 2966 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2967 |
|
| 2968 |
(* TODO: move? *) |
| 2969 |
Local Instance RevealConst_mem_same v : same_mem_addressed_of_success (RevealConst v). |
| 2970 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2971 |
|
| 2972 |
(* TODO: move? *) |
| 2973 |
Local Instance Reveal_mem_same v1 v2 : same_mem_addressed_of_success (Reveal v1 v2). |
| 2974 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2975 |
|
| 2976 |
(* TODO: move? *) |
| 2977 |
Local Instance HavocFlags_mem_same : same_mem_addressed_of_success HavocFlags. |
| 2978 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2979 |
|
| 2980 |
(* TODO: move? *) |
| 2981 |
Local Instance ret_mem_same {T} (v : T) : same_mem_addressed_of_success (ret v). |
| 2982 |
Proof. same_mem_addressed_of_success_t. Qed. |
| 2983 |
|
| 2984 |
(* TODO: move? *) |
| 2985 |
Local Instance bind_mem_same {A B} f v |
| 2986 |
{H0 : same_mem_addressed_of_success v} |
| 2987 |
{H1 : forall v', same_mem_addressed_of_success (f v')} |
| 2988 |
: same_mem_addressed_of_success (@Symbolic.bind A B v f). |
| 2989 |
Proof. |
| 2990 |
same_mem_addressed_of_success_t. |
| 2991 |
specialize (H0 _ _ _ ltac:(eassumption)). |
| 2992 |
specialize (H1 _ _ _ _ ltac:(eassumption)). |
| 2993 |
cbv [same_mem_addressed] in *; congruence. |
| 2994 |
Qed. |
| 2995 |
|
| 2996 |
(* TODO: move? *) |
| 2997 |
Local Instance mapM_mem_same {A B} f ls |
| 2998 |
{H1 : forall v', same_mem_addressed_of_success (f v')} |
| 2999 |
: same_mem_addressed_of_success (@mapM A B f ls). |
| 3000 |
Proof. |
| 3001 |
induction ls as [|?? IH]; cbn [mapM]; |
| 3002 |
same_mem_addressed_of_success_t. |
| 3003 |
specialize (IH _ _ _ ltac:(eassumption)). |
| 3004 |
specialize (H1 _ _ _ _ ltac:(eassumption)). |
| 3005 |
same_mem_addressed_of_success_t. |
| 3006 |
Defined. |
| 3007 |
|
| 3008 |
(* TODO: move? *) |
| 3009 |
Fixpoint Symeval_mem_same opts descr sz sa args {struct args} : same_mem_addressed_of_success (@Symeval opts descr sz sa args). |
| 3010 |
Proof. |
| 3011 |
destruct args; cbn [Symeval] in *; typeclasses eauto. |
| 3012 |
Qed. |
| 3013 |
Local Existing Instance Symeval_mem_same. |
| 3014 |
#[global] |
| 3015 |
Typeclasses Opaque Symeval. |
| 3016 |
#[global] |
| 3017 |
Typeclasses Transparent AddressSize OperationSize. |
| 3018 |
|
| 3019 |
(* TODO: move? *) |
| 3020 |
Local Instance SymexNormalInstruction_mem_same {opts : symbolic_options_computed_opt} {descr:description} instr : same_mem_addressed_of_success (SymexNormalInstruction instr). |
| 3021 |
Proof. |
| 3022 |
destruct instr; cbv [SymexNormalInstruction err Symbolic.bind ret Syntax.op Syntax.args ErrorT.bind same_mem_addressed_of_success] in *; intros. |
| 3023 |
same_mem_addressed_of_success_t. |
| 3024 |
Qed. |
| 3025 |
|
| 3026 |
(* TODO: move? *) |
| 3027 |
Local Instance SymexLine_mem_same {opts : symbolic_options_computed_opt} line : same_mem_addressed_of_success (SymexLine line). |
| 3028 |
Proof. |
| 3029 |
cbv [SymexLine SymexRawLine err ret] in *; break_innermost_match; try congruence. |
| 3030 |
apply SymexNormalInstruction_mem_same. |
| 3031 |
Qed. |
| 3032 |
|
| 3033 |
(* TODO: move? *) |
| 3034 |
Lemma SymexLines_mem_same {opts : symbolic_options_computed_opt} lines s s' |
| 3035 |
(H : SymexLines lines s = Success (tt, s')) |
| 3036 |
: same_mem_addressed s s'. |
| 3037 |
Proof. |
| 3038 |
revert s H. |
| 3039 |
induction lines as [|line lines IH]; cbn [SymexLines]; cbv [Symbolic.ret Symbolic.bind ErrorT.bind]; intros *; break_innermost_match; intros; inversion_ErrorT; inversion_pair; subst; try reflexivity. |
| 3040 |
specialize (IH _ ltac:(eassumption)). |
| 3041 |
same_mem_addressed_of_success_t. |
| 3042 |
Qed. |
| 3043 |
|
| 3044 |
Lemma same_mem_addressed_alt l1 l2 |
| 3045 |
(H : same_mem_addressed l1 l2) |
| 3046 |
: exists l2b, List.combine (List.map fst l1) l2b = l2 /\ List.length l2b = List.length l1. |
| 3047 |
Proof. |
| 3048 |
cbv [same_mem_addressed] in *. |
| 3049 |
rewrite H. |
| 3050 |
pose proof (split_combine l2) as H'. |
| 3051 |
rewrite split_alt in H'. |
| 3052 |
eexists; split; [ now eauto | ]. |
| 3053 |
apply (f_equal (@List.length _)) in H. |
| 3054 |
rewrite !map_length in *; congruence. |
| 3055 |
Qed. |
| 3056 |
|
| 3057 |
Lemma same_mem_addressed_app_ex_r l1 l2 l3 |
| 3058 |
(H : same_mem_addressed (l1 ++ l2) l3) |
| 3059 |
: exists l3a l3b, l3a ++ l3b = l3 /\ same_mem_addressed l1 l3a /\ same_mem_addressed l2 l3b. |
| 3060 |
Proof. |
| 3061 |
cbv [same_mem_addressed] in *. |
| 3062 |
rewrite map_app in H. |
| 3063 |
exists (firstn (List.length l1) l3), (skipn (List.length l1) l3). |
| 3064 |
rewrite <- firstn_map, <- skipn_map, <- H, firstn_app, skipn_app, map_length, Nat.sub_diag, firstn_O, skipn_O, firstn_skipn, firstn_all, (skipn_all (List.length l1)), app_nil_l, app_nil_r by now rewrite map_length; lia. |
| 3065 |
auto. |
| 3066 |
Qed. |
| 3067 |
|
| 3068 |
Lemma same_mem_addressed_rev_ex_r l1 l2 |
| 3069 |
(H : same_mem_addressed (List.rev l1) l2) |
| 3070 |
: exists l3, List.rev l3 = l2 /\ same_mem_addressed l1 l3. |
| 3071 |
Proof. |
| 3072 |
cbv [same_mem_addressed] in *. |
| 3073 |
exists (List.rev l2). |
| 3074 |
rewrite map_rev, <- H, map_rev, !rev_involutive. |
| 3075 |
auto. |
| 3076 |
Qed. |
| 3077 |
|
| 3078 |
Lemma same_mem_addressed_combine_ex_r_helper1 l1a l1b l2 |
| 3079 |
(H : same_mem_addressed (List.combine l1a l1b) l2) |
| 3080 |
: exists l2b, List.combine (firstn (List.length l1b) l1a) l2b = l2 /\ List.length l2b = List.length l2. |
| 3081 |
Proof. |
| 3082 |
cbv [same_mem_addressed] in *. |
| 3083 |
exists (List.map snd l2); split; [ | distr_length; lia ]. |
| 3084 |
rewrite map_fst_combine in H. |
| 3085 |
pose proof (split_combine l2) as H'. |
| 3086 |
rewrite split_alt, <- H in H'. |
| 3087 |
now eauto. |
| 3088 |
Qed. |
| 3089 |
|
| 3090 |
Lemma same_mem_addressed_combine_ex_r_helper2 l1a l1b l2 |
| 3091 |
(H : same_mem_addressed (List.combine l1a l1b) l2) |
| 3092 |
: exists l2b, List.combine l1a l2b = l2 /\ List.length l2b = Nat.min (List.length l1b) (List.length l1a). |
| 3093 |
Proof. |
| 3094 |
pose proof (f_equal (@List.length _) H). |
| 3095 |
apply same_mem_addressed_combine_ex_r_helper1 in H. |
| 3096 |
destruct H as [l2b [H ?] ]. |
| 3097 |
exists (firstn (List.length l1b) l2b). |
| 3098 |
subst. |
| 3099 |
split; [ | distr_length; lia ]. |
| 3100 |
etransitivity; rewrite combine_truncate_l, combine_truncate_r; [ | reflexivity ]. |
| 3101 |
rewrite ?firstn_app, ?firstn_firstn_min. |
| 3102 |
autorewrite with distr_length in *. |
| 3103 |
do 2 f_equal; lia. |
| 3104 |
Qed. |
| 3105 |
|
| 3106 |
Lemma same_mem_addressed_combine_ex_r l1a l1b l2 |
| 3107 |
(H : same_mem_addressed (List.combine l1a l1b) l2) |
| 3108 |
: exists l2b, List.combine l1a l2b = l2 /\ List.length l2b = List.length l1b. |
| 3109 |
Proof. |
| 3110 |
apply same_mem_addressed_combine_ex_r_helper2 in H. |
| 3111 |
destruct H as [l2b [H H'] ]. |
| 3112 |
exists (l2b ++ skipn (List.length l1a) l1b). |
| 3113 |
autorewrite with distr_length in *. |
| 3114 |
split; [ | lia ]. |
| 3115 |
rewrite combine_truncate_r, firstn_app. |
| 3116 |
rewrite <- (firstn_skipn (List.length l1a) l2b) in H. |
| 3117 |
rewrite H'; subst. |
| 3118 |
do 2 (apply f_equal2; try reflexivity; []). |
| 3119 |
generalize dependent (List.length l1a); clear l1a; intros. |
| 3120 |
revert H'; apply Nat.min_case_strong; intros H0 H1; subst. |
| 3121 |
all: autorewrite with natsimplify. |
| 3122 |
all: rewrite ?firstn_O, ?skipn_all, ?firstn_nil by lia. |
| 3123 |
all: reflexivity. |
| 3124 |
Qed. |
| 3125 |
|
| 3126 |
Lemma same_mem_addressed_nil ls |
| 3127 |
(H : same_mem_addressed nil ls) |
| 3128 |
: nil = ls. |
| 3129 |
Proof. |
| 3130 |
destruct ls; cbv in H; congruence. |
| 3131 |
Qed. |
| 3132 |
|
| 3133 |
Lemma symbolic_mem_state_nil d |
| 3134 |
: symbolic_mem_state (init_symbolic_state d) = nil. |
| 3135 |
Proof. |
| 3136 |
cbv [init_symbolic_state]; break_innermost_match; reflexivity. |
| 3137 |
Qed. |
| 3138 |
|
| 3139 |
Lemma same_mem_addressed_init_symbolic_state d ls |
| 3140 |
(H : same_mem_addressed (init_symbolic_state d) ls) |
| 3141 |
: symbolic_mem_state (init_symbolic_state d) = ls. |
| 3142 |
Proof. |
| 3143 |
rewrite symbolic_mem_state_nil in *. |
| 3144 |
apply same_mem_addressed_nil, H. |
| 3145 |
Qed. |
| 3146 |
|
| 3147 |
Lemma same_mem_addressed_flat_map_combine_addrs_ex_r {dereference_scalar:bool} {T} (ls1 : list (_ + T * _)) ls2 ls' |
| 3148 |
(H : same_mem_addressed (List.flat_map |
| 3149 |
(fun '(idx', idx) |
| 3150 |
=> match idx', idx with |
| 3151 |
| inl addr_or_val, inl val => if dereference_scalar then [(addr_or_val, val)] else [] |
| 3152 |
| inl _, _ | _, inl _ => [] |
| 3153 |
| inr (base', addrs'), inr items |
| 3154 |
=> List.combine addrs' items |
| 3155 |
end) |
| 3156 |
(List.combine ls1 ls2)) |
| 3157 |
ls') |
| 3158 |
: exists ls'2, List.flat_map |
| 3159 |
(fun '(idx', idx) |
| 3160 |
=> match idx', idx with |
| 3161 |
| inl addr_or_val, inl val => if dereference_scalar then [(addr_or_val, val)] else [] |
| 3162 |
| inl _, _ | _, inl _ => [] |
| 3163 |
| inr (base', addrs'), inr items |
| 3164 |
=> List.combine addrs' items |
| 3165 |
end) |
| 3166 |
(List.combine ls1 ls'2) = ls' |
| 3167 |
/\ Forall2 (fun idx idx' => match idx, idx' with |
| 3168 |
| inl val, inl val' |
| 3169 |
=> if dereference_scalar then True else val = val' |
| 3170 |
| inr items, inr items' => List.length items = List.length items' |
| 3171 |
| inl _, inr _ | inr _, inl _ => False |
| 3172 |
end) ls2 ls'2. |
| 3173 |
Proof. |
| 3174 |
revert dependent ls'. |
| 3175 |
revert ls1 ls2. |
| 3176 |
induction ls1 as [|?? IH], ls2 as [|? ls2]; |
| 3177 |
try specialize (IH ls2); |
| 3178 |
cbn [List.combine List.map List.flat_map]; intros. |
| 3179 |
all: repeat first [ progress subst |
| 3180 |
| progress destruct_head'_and |
| 3181 |
| progress destruct_head'_ex |
| 3182 |
| (now exists nil) |
| 3183 |
| reflexivity |
| 3184 |
| specialize (IH _ ltac:(eassumption)) |
| 3185 |
| match goal with |
| 3186 |
| [ H : same_mem_addressed nil _ |- _ ] => apply same_mem_addressed_nil in H |
| 3187 |
| [ H : same_mem_addressed (_ ++ _) _ |- _ ] => apply same_mem_addressed_app_ex_r in H |
| 3188 |
| [ H : nil = List.map _ ?ls |- _ ] => is_var ls; destruct ls |
| 3189 |
| [ H : same_mem_addressed match _ with _ => _ end ?x |- _ ] |
| 3190 |
=> is_var x; apply same_mem_addressed_alt in H |
| 3191 |
| [ |- exists ls, nil = nil /\ Forall2 _ ?lsv ls ] |
| 3192 |
=> exists lsv; split; [ | apply Reflexive_forall2; intro; break_innermost_match ] |
| 3193 |
end ]. |
| 3194 |
eexists (_ :: _); cbn [flat_map]; split; [ apply f_equal2; [ | reflexivity ] | constructor; [ | assumption ] ]. |
| 3195 |
2: lazymatch goal with |
| 3196 |
| [ |- match ?s with |
| 3197 |
| inl _ |
| 3198 |
=> match ?ev with |
| 3199 |
| inr _ => False |
| 3200 |
| _ => _ |
| 3201 |
end |
| 3202 |
| _ => _ |
| 3203 |
end ] |
| 3204 |
=> let __ := open_constr:(eq_refl : ev = match s with inl val => inl (if dereference_scalar then _ else _) | inr items => inr (firstn (List.length items) ?[ls] ++ skipn (List.length ?ls) items)%list end) in |
| 3205 |
break_innermost_match; autorewrite with distr_length; try exact I; try reflexivity; set_evars; lia |
| 3206 |
end. |
| 3207 |
repeat first [ match goal with |
| 3208 |
| [ |- match ?x with _ => _ end = List.combine (List.map _ match ?x with _ => _ end) _ ] |
| 3209 |
=> destruct x |
| 3210 |
| [ |- match ?ev with inl _ => _ | inr _ => nil end = List.combine (List.map _ match ?x with inl _ => _ | inr _ => nil end) _ ] |
| 3211 |
=> is_evar ev; |
| 3212 |
is_var x; |
| 3213 |
let __ := open_constr:(eq_refl : ev = match x with inl _ => inl _ | inr _ => inr _ end) in |
| 3214 |
destruct x |
| 3215 |
| [ |- [(_, ?ev)] = match ?x with _ => _ end ] |
| 3216 |
=> is_evar ev; |
| 3217 |
is_var x; |
| 3218 |
let __ := open_constr:(eq_refl : ev = match x with nil => _ | _ :: _ => _ end) in |
| 3219 |
destruct x |
| 3220 |
| [ |- List.combine ?ls ?ev = List.combine (firstn (List.length ?ls') ?ls) ?x ] |
| 3221 |
=> is_evar ev; unify ev (firstn (List.length ls') x) |
| 3222 |
end |
| 3223 |
| exfalso; discriminate |
| 3224 |
| reflexivity |
| 3225 |
| rewrite map_fst_combine |
| 3226 |
| progress cbn [List.map List.combine fst List.length] in * |
| 3227 |
| break_innermost_match_step ]. |
| 3228 |
match goal with |
| 3229 |
| [ |- List.combine _ (firstn _ ?ev ++ skipn _ _) = List.combine _ ?ls ] |
| 3230 |
=> is_evar ev; unify ev ls |
| 3231 |
end. |
| 3232 |
etransitivity; rewrite combine_truncate_l, combine_truncate_r; [ | reflexivity ]. |
| 3233 |
rewrite ?firstn_app, ?firstn_firstn_min. |
| 3234 |
autorewrite with distr_length in *. |
| 3235 |
repeat first [ rewrite Nat.sub_diag |
| 3236 |
| rewrite firstn_O |
| 3237 |
| rewrite app_nil_r |
| 3238 |
| reflexivity |
| 3239 |
| match goal with |
| 3240 |
| [ |- context[(Nat.min ?x ?y + (?x - ?y))%nat] ] |
| 3241 |
=> replace (Nat.min x y + (x - y))%nat with x by lia |
| 3242 |
| [ |- context[Nat.min (Nat.min ?x ?y) ?x] ] |
| 3243 |
=> replace (Nat.min (Nat.min x y) x) with (Nat.min x y) by lia |
| 3244 |
| [ H : ?x = Nat.min ?y ?z |- context[Nat.min ?x ?z] ] |
| 3245 |
=> replace (Nat.min x z) with (Nat.min y z) by lia |
| 3246 |
| [ H : ?x = Nat.min ?y ?z |- context[Nat.min ?z ?x] ] |
| 3247 |
=> replace (Nat.min z x) with (Nat.min y z) by lia |
| 3248 |
| [ |- context[(Nat.min ?x ?y - Nat.min ?y ?x)%nat] ] |
| 3249 |
=> rewrite (Nat.min_comm x y) |
| 3250 |
| [ |- context[firstn ?n ?x] ] |
| 3251 |
=> rewrite (@firstn_all _ n x) by lia |
| 3252 |
| [ |- context[firstn (Nat.min (List.length ?x) _) ?x] ] |
| 3253 |
=> rewrite (Nat.min_comm (List.length x)), <- (@firstn_firstn_min _ (List.length x) _ x) |
| 3254 |
end ]. |
| 3255 |
Unshelve. |
| 3256 |
constructor. |
| 3257 |
Qed. |
| 3258 |
|
| 3259 |
Lemma update_update_dag_with s d0 d1 |
| 3260 |
: update_dag_with (update_dag_with s d0) d1 = update_dag_with s (fun d => d1 (d0 d)). |
| 3261 |
Proof. reflexivity. Qed. |
| 3262 |
|
| 3263 |
Definition same_index G d i1 i2 : Prop |
| 3264 |
:= i1 = i2 \/ exists v, eval_idx_Z G d i1 v /\ eval_idx_Z G d i2 v. |
| 3265 |
|
| 3266 |
Local Instance same_index_Reflexive {G d} : Reflexive (same_index G d) | 100. |
| 3267 |
Proof. cbv [same_index]; repeat intro; firstorder eauto. Qed. |
| 3268 |
|
| 3269 |
Local Instance same_index_Symmetric {G d} : Symmetric (same_index G d) | 100. |
| 3270 |
Proof. cbv [same_index]; repeat intro; firstorder eauto. Qed. |
| 3271 |
|
| 3272 |
Local Instance same_index_Transitive {G d} : Transitive (same_index G d) | 100. |
| 3273 |
Proof. cbv [same_index]; repeat intro; firstorder (handle_eval_eval; subst; eauto). Qed. |
| 3274 |
|
| 3275 |
Lemma same_index_of_eq {G d i1 i2} : i1 = i2 -> same_index G d i1 i2. |
| 3276 |
Proof. intros; subst; left; reflexivity. Qed. |
| 3277 |
|
| 3278 |
Lemma lift_same_index_impl G d G' d' (H : forall e v, eval G d e v -> eval G' d' e v) |
| 3279 |
: forall i1 i2, same_index G d i1 i2 -> same_index G' d' i1 i2. |
| 3280 |
Proof. |
| 3281 |
cbv [same_index eval_idx_Z]; repeat intro; firstorder (handle_eval_eval; subst; eauto). |
| 3282 |
Qed. |
| 3283 |
|
| 3284 |
Definition unique_mem_addressed G d (s : mem_state) : Prop |
| 3285 |
:= NoDupA (fun v1 v2 => same_index G d (fst v1) (fst v2)) s. |
| 3286 |
|
| 3287 |
Lemma Load64_ok G s s' addr addr_val idx |
| 3288 |
(H : Load64 addr s = Success (idx, s')) |
| 3289 |
(d := s.(dag_state)) |
| 3290 |
(d' := s'.(dag_state)) |
| 3291 |
(r := s.(symbolic_reg_state)) |
| 3292 |
(r' := s'.(symbolic_reg_state)) |
| 3293 |
(f := s.(symbolic_flag_state)) |
| 3294 |
(f' := s'.(symbolic_flag_state)) |
| 3295 |
(m := s.(symbolic_mem_state)) |
| 3296 |
(m' := s'.(symbolic_mem_state)) |
| 3297 |
(Hd : gensym_dag_ok G d) |
| 3298 |
(Haddr : eval_idx_Z G d addr addr_val) |
| 3299 |
: (load addr s' = Some idx |
| 3300 |
/\ r = r' |
| 3301 |
/\ f = f' |
| 3302 |
/\ m = m') |
| 3303 |
/\ gensym_dag_ok G d' |
| 3304 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 3305 |
Proof. |
| 3306 |
cbv [Load64 some_or] in *; break_innermost_match_hyps; inversion_ErrorT; inversion_pair; subst; intuition eauto. |
| 3307 |
Qed. |
| 3308 |
|
| 3309 |
Lemma Remove64_ok G s s' addr idx |
| 3310 |
(H : Remove64 addr s = Success (idx, s')) |
| 3311 |
(d := s.(dag_state)) |
| 3312 |
(d' := s'.(dag_state)) |
| 3313 |
(r := s.(symbolic_reg_state)) |
| 3314 |
(r' := s'.(symbolic_reg_state)) |
| 3315 |
(f := s.(symbolic_flag_state)) |
| 3316 |
(f' := s'.(symbolic_flag_state)) |
| 3317 |
(m := s.(symbolic_mem_state)) |
| 3318 |
(m' := s'.(symbolic_mem_state)) |
| 3319 |
(Hd : gensym_dag_ok G d) |
| 3320 |
: (r = r' |
| 3321 |
/\ f = f' |
| 3322 |
/\ Permutation m ((addr, idx) :: m') |
| 3323 |
/\ Forall (fun x => (fst x =? addr)%N = false) m') |
| 3324 |
/\ gensym_dag_ok G d' |
| 3325 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 3326 |
Proof. |
| 3327 |
cbv [Remove64 some_or remove] in *; break_innermost_match_hyps; inversion_ErrorT; inversion_pair; subst. |
| 3328 |
subst m m'. |
| 3329 |
rewrite <- List.partition_permutation by eassumption. |
| 3330 |
let H := match goal with H : partition _ _ = _ |- _ => H end in |
| 3331 |
pose proof H as H'; apply List.Forall_partition in H'. |
| 3332 |
intuition eauto. |
| 3333 |
match goal with |- Permutation ?x ?y => cut (x = y); [ intros ->; reflexivity | ] end. |
| 3334 |
repeat first [ progress destruct_head'_and |
| 3335 |
| progress cbn [List.map List.app fst snd] in * |
| 3336 |
| progress inversion_list |
| 3337 |
| progress subst |
| 3338 |
| progress invlist Forall |
| 3339 |
| progress reflect_hyps |
| 3340 |
| progress destruct_head'_prod |
| 3341 |
| reflexivity |
| 3342 |
| match goal with |
| 3343 |
| [ H : List.map _ ?s = _ :: _ |- _ ] => is_var s; destruct s |
| 3344 |
| [ H : List.map _ ?s = [] |- _ ] => is_var s; destruct s |
| 3345 |
end ]. |
| 3346 |
Qed. |
| 3347 |
|
| 3348 |
Local Existing Instance Permutation_cons | 0. |
| 3349 |
Lemma LoadArray_ok {opts : symbolic_options_computed_opt} {descr:description} G s s' base base_val len idxs |
| 3350 |
(H : LoadArray base len s = Success (idxs, s')) |
| 3351 |
(d := s.(dag_state)) |
| 3352 |
(d' := s'.(dag_state)) |
| 3353 |
(r := s.(symbolic_reg_state)) |
| 3354 |
(r' := s'.(symbolic_reg_state)) |
| 3355 |
(f := s.(symbolic_flag_state)) |
| 3356 |
(f' := s'.(symbolic_flag_state)) |
| 3357 |
(m := s.(symbolic_mem_state)) |
| 3358 |
(m' := s'.(symbolic_mem_state)) |
| 3359 |
(Hd : gensym_dag_ok G d) |
| 3360 |
(Hbase : eval_idx_Z G d base base_val) |
| 3361 |
: ((exists addrs : list idx, |
| 3362 |
Permutation m (List.combine addrs idxs ++ m') |
| 3363 |
/\ List.length idxs = len |
| 3364 |
/\ Forall2 (eval_idx_Z G d') addrs (List.map (fun i => Z.land (base_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 len)) |
| 3365 |
/\ Forall (fun addr => Forall (fun x => (fst x =? addr)%N = false) m') addrs) |
| 3366 |
/\ r = r' |
| 3367 |
/\ f = f') |
| 3368 |
/\ gensym_dag_ok G d' |
| 3369 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 3370 |
Proof. |
| 3371 |
cbv [LoadArray Symbolic.bind ErrorT.bind] in H. |
| 3372 |
subst d d' r r' f f' m m'. |
| 3373 |
revert H Hd Hbase. |
| 3374 |
replace (List.length idxs = len) with (List.length idxs = List.length (seq 0 len)) by now rewrite seq_length. |
| 3375 |
generalize (seq 0 len); intros indices; clear len. |
| 3376 |
revert indices idxs s s'. |
| 3377 |
induction indices as [|i indices IH], idxs as [|idx idxs]; |
| 3378 |
try specialize (IH idxs). |
| 3379 |
all: repeat first [ progress intros |
| 3380 |
| progress inversion_ErrorT |
| 3381 |
| progress inversion_prod |
| 3382 |
| progress subst |
| 3383 |
| progress cbn [List.map Crypto.Util.Option.bind fold_right mapM fst snd Symbolic.dag_state Symbolic.symbolic_reg_state Symbolic.symbolic_mem_state Symbolic.symbolic_flag_state update_mem_with List.combine List.length] in * |
| 3384 |
| progress cbv [Symbolic.ret Option.List.lift Symbolic.bind ErrorT.bind Load64 some_or] in * |
| 3385 |
| discriminate |
| 3386 |
| solve [ eauto 10 |
| 3387 |
| exists nil; cbn [List.map]; eauto ] |
| 3388 |
| progress destruct_head'_prod |
| 3389 |
| progress destruct_head'_and |
| 3390 |
| progress destruct_head'_ex |
| 3391 |
| progress specialize_by_assumption |
| 3392 |
| progress specialize_by eauto using lift_eval_idx_Z_impl |
| 3393 |
| match goal with |
| 3394 |
| [ H : _ :: _ = _ :: _ |- _ ] => inversion H; clear H |
| 3395 |
| [ H : Remove64 _ _ = _ |- _ ] |
| 3396 |
=> eapply Remove64_ok in H; [ | eassumption .. ] |
| 3397 |
| [ H : compute_array_address _ _ _ = _ |- _ ] |
| 3398 |
=> eapply compute_array_address_ok in H; [ | eassumption .. ] |
| 3399 |
| [ IH : forall s s', mapM ?f ?ls s = Success (?v, s') -> _, H : mapM ?f ?ls _ = Success (?v, _) |- _ ] |
| 3400 |
=> specialize (IH _ _ H) |
| 3401 |
| [ H : symbolic_flag_state _ = symbolic_flag_state ?x, H' : symbolic_flag_state ?x = _ |- _ ] |
| 3402 |
=> is_var x; destruct x |
| 3403 |
| [ H : load _ ?s = Some _ |- context[load _ ?s] ] => rewrite H |
| 3404 |
| [ H : Some _ = fold_right _ _ _ |- _ ] => rewrite <- H |
| 3405 |
| [ |- Forall2 _ (_ :: _) (_ :: _) ] => constructor |
| 3406 |
| [ H : Permutation ?s _ |- context[?s] ] => rewrite H |
| 3407 |
| [ H : Permutation ?s _, H' : Forall _ ?ls |- _ ] |
| 3408 |
=> lazymatch ls with |
| 3409 |
| context[?s] => rewrite H in H' |
| 3410 |
end |
| 3411 |
end |
| 3412 |
| apply conj; (idtac + refine (reflexivity _)); eauto using lift_eval_idx_Z_impl; [] |
| 3413 |
| break_innermost_match_hyps_step |
| 3414 |
| progress break_match_hyps |
| 3415 |
| solve [ eauto using lift_eval_idx_Z_impl ] |
| 3416 |
| rewrite Forall2_cons_cons_iff |
| 3417 |
| rewrite Forall_cons_iff |
| 3418 |
| eexists (_ :: _) |
| 3419 |
| match goal with |
| 3420 |
| [ H : Forall _ (_ ++ _) |- _ ] => apply Forall_app in H |
| 3421 |
end ]. |
| 3422 |
Qed. |
| 3423 |
|
| 3424 |
(* TODO: move? *) |
| 3425 |
Ltac saturate_lengths_step := |
| 3426 |
let do_with ls := |
| 3427 |
lazymatch ls with |
| 3428 |
| @List.map ?A ?B ?f ?x |
| 3429 |
=> unique pose proof (@map_length A B f x) |
| 3430 |
| @firstn ?A ?n ?x |
| 3431 |
=> unique pose proof (@firstn_length A n x) |
| 3432 |
| @skipn ?A ?n ?x |
| 3433 |
=> unique pose proof (@skipn_length A n x) |
| 3434 |
| @seq ?start ?len |
| 3435 |
=> unique pose proof (@seq_length start len) |
| 3436 |
| @List.app ?A ?l ?l' |
| 3437 |
=> unique pose proof (@app_length A l l') |
| 3438 |
| @List.combine ?A ?B ?l1 ?l2 |
| 3439 |
=> unique pose proof (@combine_length A B l1 l2) |
| 3440 |
end in |
| 3441 |
match goal with |
| 3442 |
| [ |- context[length (?x ++ ?y)] ] |
| 3443 |
=> rewrite (app_length x y) |
| 3444 |
| [ H : Forall2 _ ?y ?x |- _ ] |
| 3445 |
=> unique assert (length y = length x) by eapply eq_length_Forall2, H |
| 3446 |
| [ |- context[length ?ls] ] |
| 3447 |
=> do_with ls |
| 3448 |
| [ H : context[length ?ls] |- _ ] |
| 3449 |
=> do_with ls |
| 3450 |
| [ H : nth_error _ _ = None |- _ ] |
| 3451 |
=> unique pose proof (@nth_error_error_length _ _ _ H) |
| 3452 |
| [ H : nth_error _ _ = Some _ |- _ ] |
| 3453 |
=> unique pose proof (@nth_error_value_length _ _ _ _ H) |
| 3454 |
end. |
| 3455 |
Ltac saturate_lengths := repeat saturate_lengths_step. |
| 3456 |
|
| 3457 |
Lemma LoadOutputs_ok {opts : symbolic_options_computed_opt} {descr:description} {dereference_scalar:bool} G s s' outputaddrs output_types output_vals idxs |
| 3458 |
(H : LoadOutputs (dereference_scalar:=dereference_scalar) outputaddrs output_types s = Success (Success idxs, s')) |
| 3459 |
(d := s.(dag_state)) |
| 3460 |
(d' := s'.(dag_state)) |
| 3461 |
(r := s.(symbolic_reg_state)) |
| 3462 |
(r' := s'.(symbolic_reg_state)) |
| 3463 |
(f := s.(symbolic_flag_state)) |
| 3464 |
(f' := s'.(symbolic_flag_state)) |
| 3465 |
(m := s.(symbolic_mem_state)) |
| 3466 |
(m' := s'.(symbolic_mem_state)) |
| 3467 |
(Hd : gensym_dag_ok G d) |
| 3468 |
(Houtputaddrs_array : Forall2 (fun idx val |
| 3469 |
=> match idx with |
| 3470 |
| inl (inr idx) |
| 3471 |
| inr idx |
| 3472 |
=> eval_idx_Z G d idx (Z.land val (Z.ones 64)) |
| 3473 |
| _ => True |
| 3474 |
end) outputaddrs output_vals) |
| 3475 |
(Houtputaddrs_reg : Forall (fun idx |
| 3476 |
=> match idx with |
| 3477 |
| inl (inl r) |
| 3478 |
=> (let '(rn, lo, sz) := index_and_shift_and_bitcount_of_reg r in sz = 64%N) |
| 3479 |
/\ (exists v, forall idx', get_reg s (reg_index r) = Some idx' -> eval_idx_Z G s idx' (Z.land v (Z.ones 64))) |
| 3480 |
/\ dereference_scalar = false |
| 3481 |
| inl (inr _) => dereference_scalar = true |
| 3482 |
| _ => True |
| 3483 |
end) outputaddrs) |
| 3484 |
: ((exists outputaddrs' : list (idx + list idx), |
| 3485 |
Forall2 (fun idxs '((base, len), base_val) |
| 3486 |
=> match idxs, base, len with |
| 3487 |
| inr idxs, inr base, Some len |
| 3488 |
=> Forall2 (eval_idx_Z G d') idxs (List.map (fun i => Z.land (base_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 len)) |
| 3489 |
| inl idx, inl (inl r), None |
| 3490 |
=> (exists idx', |
| 3491 |
get_reg s' (reg_index r) = Some idx' |
| 3492 |
/\ (exists v, (0 <= v < 2^64)%Z /\ eval_idx_Z G d' idx v /\ eval_idx_Z G d' idx' v)) |
| 3493 |
/\ dereference_scalar = false |
| 3494 |
| inl idx, inl (inr addr), None |
| 3495 |
=> idx = addr |
| 3496 |
/\ eval_idx_Z G d' addr (Z.land base_val (Z.ones 64)) |
| 3497 |
/\ dereference_scalar = true |
| 3498 |
| _, _, _ => False |
| 3499 |
end) |
| 3500 |
outputaddrs' |
| 3501 |
(List.combine (List.combine outputaddrs output_types) output_vals) |
| 3502 |
/\ Permutation m (List.flat_map |
| 3503 |
(fun '(addrs, idxs) |
| 3504 |
=> match addrs, idxs with |
| 3505 |
| inr addrs, inr idxs |
| 3506 |
=> List.combine addrs idxs |
| 3507 |
| inl addr, inl idx |
| 3508 |
=> if dereference_scalar |
| 3509 |
then [(addr, idx)] |
| 3510 |
else [] |
| 3511 |
| inl _, inr _ | inr _, inl _ => [] |
| 3512 |
end) |
| 3513 |
(List.combine outputaddrs' idxs) |
| 3514 |
++ m') |
| 3515 |
/\ Forall2 |
| 3516 |
(fun addrs idxs |
| 3517 |
=> match addrs, idxs with |
| 3518 |
| inl idx, inl val_idx |
| 3519 |
=> if dereference_scalar |
| 3520 |
then True |
| 3521 |
else idx = val_idx (*forall v, (0 <= v < 2^64)%Z -> eval_idx_Z G d' idx v <-> eval_idx_Z G d' val_idx v*) |
| 3522 |
| inr addrs, inr idxs |
| 3523 |
=> List.length idxs = List.length addrs |
| 3524 |
| inl _, inr _ | inr _, inl _ => False |
| 3525 |
end) |
| 3526 |
outputaddrs' |
| 3527 |
idxs |
| 3528 |
/\ Forall (fun addrs => match addrs with |
| 3529 |
| inl idx => True |
| 3530 |
| inr addrs |
| 3531 |
=> Forall (fun addr => Forall (fun x => (fst x =? addr)%N = false) m') addrs |
| 3532 |
end) |
| 3533 |
outputaddrs' |
| 3534 |
/\ List.length output_types = List.length outputaddrs) |
| 3535 |
/\ r = r' |
| 3536 |
/\ f = f') |
| 3537 |
/\ gensym_dag_ok G d' |
| 3538 |
/\ (forall e n, eval G d e n -> eval G d' e n). |
| 3539 |
Proof. |
| 3540 |
cbv [LoadOutputs LoadOutputs_internal Symbolic.bind Symbolic.ret ErrorT.bind] in H. |
| 3541 |
destruct mapM eqn:?, (_ =? _)%nat eqn:?. |
| 3542 |
all: repeat first [ progress inversion_ErrorT |
| 3543 |
| progress inversion_prod |
| 3544 |
| progress subst |
| 3545 |
| progress destruct_head'_prod |
| 3546 |
| progress cbn [fst snd] in * |
| 3547 |
| progress reflect_hyps |
| 3548 |
| match goal with |
| 3549 |
| [ H := _ |- _ ] => subst H |
| 3550 |
end ]. |
| 3551 |
match goal with |
| 3552 |
| [ H : mapM _ _ _ = Success (?l, ?s) |- _ ] |
| 3553 |
=> rename l into idxs; rename s into s' |
| 3554 |
end. |
| 3555 |
revert Hd Houtputaddrs_array Houtputaddrs_reg. |
| 3556 |
revert dependent s'; intro s'. |
| 3557 |
revert s s'. |
| 3558 |
revert dependent outputaddrs; intro outputaddrs. |
| 3559 |
revert outputaddrs output_types output_vals idxs. |
| 3560 |
induction outputaddrs as [|outputaddr outputaddrs IH], |
| 3561 |
output_types as [|output_type output_types], |
| 3562 |
output_vals as [|output_val output_vals], |
| 3563 |
idxs as [|idx idxs]; |
| 3564 |
try specialize (IH output_types output_vals idxs). |
| 3565 |
all: repeat first [ progress cbn [fst snd mapM List.map List.flat_map List.app List.combine option_map fold_right Crypto.Util.Option.bind Symbolic.dag_state Symbolic.symbolic_reg_state Symbolic.symbolic_mem_state Symbolic.symbolic_flag_state List.length] in * |
| 3566 |
| progress cbv [Symbolic.ret] in * |
| 3567 |
| progress intros |
| 3568 |
| progress subst |
| 3569 |
| progress inversion_ErrorT |
| 3570 |
| progress inversion_prod |
| 3571 |
| discriminate |
| 3572 |
| solve [ eauto 10 ] |
| 3573 |
| progress destruct_head'_prod |
| 3574 |
| progress destruct_head'_ex |
| 3575 |
| progress destruct_head'_and |
| 3576 |
| progress specialize_by_assumption |
| 3577 |
| rewrite Forall2_cons_cons_iff |
| 3578 |
| set_evars; rewrite Forall_cons_iff; subst_evars |
| 3579 |
| rewrite !app_assoc |
| 3580 |
| progress cbv [Symbolic.bind ErrorT.bind] in * |
| 3581 |
| progress inversion_list |
| 3582 |
| progress inversion_nat_eq |
| 3583 |
| progress specialize_by |
| 3584 |
(first [ eapply Forall2_weaken | eapply Forall_impl ]; [ | multimatch goal with H : _ |- _ => exact H end ]; |
| 3585 |
cbv beta; intros *; |
| 3586 |
destruct_head' symbolic_state; cbn [Symbolic.dag_state Symbolic.symbolic_reg_state Symbolic.symbolic_mem_state Symbolic.symbolic_flag_state] in *; |
| 3587 |
subst; |
| 3588 |
break_innermost_match; |
| 3589 |
cbv [eval_idx_Z] in *; |
| 3590 |
now intuition (destruct_head'_ex; eauto)) |
| 3591 |
| match goal with |
| 3592 |
| [ H : Forall _ (_ :: _) |- _ ] => inversion H; clear H |
| 3593 |
| [ H : Forall2 _ (_ :: _) _ |- _ ] => inversion H; clear H |
| 3594 |
| [ H : Forall2 _ [] _ |- _ ] => inversion H; clear H |
| 3595 |
| [ H : _ :: _ = _ :: _ |- _ ] => inversion H; clear H |
| 3596 |
| [ IH : forall s s', mapM ?f ?ls s = Success (?v, s') -> _, H : mapM ?f ?ls _ = Success (?v, _) |- _ ] |
| 3597 |
=> specialize (IH _ _ H) |
| 3598 |
| [ H : LoadArray _ _ _ = _ |- _ ] |
| 3599 |
=> eapply LoadArray_ok in H; [ | eassumption .. ] |
| 3600 |
| [ H : Remove64 _ _ = _ |- _ ] |
| 3601 |
=> eapply Remove64_ok in H; [ | eassumption .. ] |
| 3602 |
| [ H : GetReg _ _ = _ |- _ ] |
| 3603 |
=> eapply GetReg_ok in H; [ | (reflexivity + eassumption) .. ] |
| 3604 |
| [ H : Permutation ?s _, H' : Forall _ ?s |- _ ] => rewrite H in H' |
| 3605 |
| [ H : Forall _ (_ ++ _) |- _ ] => apply Forall_app in H |
| 3606 |
end |
| 3607 |
| break_innermost_match_hyps_step |
| 3608 |
| progress break_match_hyps |
| 3609 |
| apply conj; eauto; [] |
| 3610 |
| progress destruct_head' symbolic_state ]. |
| 3611 |
all: lazymatch goal with |
| 3612 |
| [ |- context[Forall2 _ _ ((inr _, _, _) :: _)] ] |
| 3613 |
=> eexists (inr _ :: _) |
| 3614 |
| [ |- context[Forall2 _ _ ((inl _, _, _) :: _)] ] |
| 3615 |
=> eexists (inl _ :: _) |
| 3616 |
end. |
| 3617 |
all: repeat first [ rewrite Forall2_cons_cons_iff |
| 3618 |
| match goal with |
| 3619 |
| [ |- context[?x = ?y] ] |
| 3620 |
=> first [ is_evar x | is_evar y ]; progress unify x y |
| 3621 |
| [ |- Permutation _ _ /\ _ ] => split |
| 3622 |
| [ H : Permutation ?s _ |- Permutation ?s _ ] => rewrite H |
| 3623 |
| [ H : Permutation ?s _ |- Permutation (_ :: ?s) _ ] => rewrite H |
| 3624 |
| [ H : Permutation ?s _ |- Permutation (_ ++ ?s) _ ] => rewrite H |
| 3625 |
| [ |- context[@Forall ?A ?P (?x :: ?xs)] ] |
| 3626 |
=> rewrite (@Forall_cons_iff A P x xs) |
| 3627 |
| [ |- ?A /\ ?B ] => tryif (has_evar A; has_evar B) then fail else split |
| 3628 |
| [ |- List.length ?A = List.length ?B ] |
| 3629 |
=> tryif (has_evar A; has_evar B) then fail else (saturate_lengths; congruence) |
| 3630 |
| [ |- Forall (fun v => Forall (@?P v) (@?y v)) ?x ] |
| 3631 |
=> tryif first [ has_evar P | has_evar x | has_evar y ] |
| 3632 |
then fail |
| 3633 |
else (eapply Forall_impl; [ | eassumption ]; cbv beta) |
| 3634 |
end |
| 3635 |
| rewrite !List.app_assoc |
| 3636 |
| progress cbn [List.combine flat_map List.app] |
| 3637 |
| progress repeat rewrite <- ?and_assoc, <- (and_comm (Permutation _ _)); |
| 3638 |
rewrite ?and_assoc |
| 3639 |
| match goal with |
| 3640 |
| [ |- Permutation _ _ ] => reflexivity |
| 3641 |
end |
| 3642 |
| solve [ cbv [eval_idx_Z] in *; eauto 10 ] |
| 3643 |
| (rewrite -> ?and_assoc + rewrite <- ?and_assoc); |
| 3644 |
apply conj; |
| 3645 |
match goal with |
| 3646 |
| [ |- ?G ] => tryif has_evar G then auto else eauto |
| 3647 |
end; |
| 3648 |
[] |
| 3649 |
| match goal with |
| 3650 |
| [ |- context[?A /\ ?B] ] |
| 3651 |
=> lazymatch A with |
| 3652 |
| context[Forall2 _ _ _] |
| 3653 |
=> lazymatch B with |
| 3654 |
| context[Forall2 _ _ _] |
| 3655 |
=> fail 1 |
| 3656 |
end |
| 3657 |
end |
| 3658 |
| _ => idtac |
| 3659 |
end; |
| 3660 |
rewrite <- ?and_assoc, <- (and_comm (Forall2 _ _ _)), ?and_assoc |
| 3661 |
| rewrite <- ?and_assoc, <- (and_comm (false = false)), ?and_assoc ]. |
| 3662 |
all: repeat first [ progress intros |
| 3663 |
| progress destruct_head'_and |
| 3664 |
| progress subst |
| 3665 |
| assumption |
| 3666 |
| reflexivity |
| 3667 |
| match goal with |
| 3668 |
| [ |- ?A /\ ?B ] => tryif (has_evar A; has_evar B) then fail else split |
| 3669 |
| [ |- Forall (fun v => Forall (@?P v) (@?y v)) ?x ] |
| 3670 |
=> tryif first [ has_evar P | has_evar x | has_evar y ] |
| 3671 |
then fail |
| 3672 |
else (eapply Forall_impl; [ | eassumption ]; cbv beta) |
| 3673 |
| [ H : Permutation ?s _, H' : Forall _ ?s |- _ ] => rewrite H in H' |
| 3674 |
| [ H : Forall _ (_ ++ _) |- _ ] => apply Forall_app in H |
| 3675 |
| [ |- List.length _ = List.length _ ] => saturate_lengths; congruence |
| 3676 |
| [ H : forall idx, ?x = Some idx -> _, H' : ?x = Some _ |- _ ] |
| 3677 |
=> specialize (H _ H') |
| 3678 |
| [ H : ?x = Some _ |- context[?x] ] => rewrite H |
| 3679 |
| [ |- exists v, Some ?x = Some v /\ _ ] => exists x |
| 3680 |
end |
| 3681 |
| rewrite !@Forall2_map_r_iff in * |
| 3682 |
| eapply Forall2_weaken; [ | eassumption ]; cbv beta |
| 3683 |
| rewrite !Z.land_ones in * by lia |
| 3684 |
| rewrite Z.mod_mod in * by (clear; lia) |
| 3685 |
| progress handle_eval_eval |
| 3686 |
| match goal with |
| 3687 |
| [ H : eval_idx_Z _ _ ?i ?v |- eval_idx_Z _ _ ?i ?v' ] |
| 3688 |
=> cut (v' = v); [ intros ->; eapply lift_eval_idx_Z_impl; [ | exact H ]; now eauto | ]; |
| 3689 |
push_Zmod; pull_Zmod |
| 3690 |
| [ |- _ <-> _ ] => split |
| 3691 |
| [ H : context[(?x mod ?y)%Z] |- _ ] |
| 3692 |
=> unique assert (0 <= x mod y)%Z by now apply Z.mod_pos_bound; clear; lia |
| 3693 |
| [ H : context[(?x mod ?y)%Z] |- _ ] |
| 3694 |
=> unique assert (x mod y < y)%Z by now apply Z.mod_pos_bound; clear; lia |
| 3695 |
end |
| 3696 |
| apply conj; eauto; [] |
| 3697 |
| rewrite <- ?and_assoc, <- (and_comm (Forall2 _ _ _)), ?and_assoc |
| 3698 |
| solve [ cbv [eval_idx_Z] in *; eauto 10 ] ]. |
| 3699 |
Qed. |