-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory_cmp_impl_soundness.v
1185 lines (962 loc) · 55.8 KB
/
memory_cmp_impl_soundness.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import Arith.
Require Import Nat.
Require Import Bool.
Require Import bbv.Word.
Require Import Coq.NArith.NArith.
Require Import List.
Import ListNotations.
Require Import FORVES.constants.
Import Constants.
Require Import FORVES.program.
Import Program.
Require Import FORVES.execution_state.
Import ExecutionState.
Require Import FORVES.stack_operation_instructions.
Import StackOpInstrs.
Require Import FORVES.misc.
Import Misc.
Require Import FORVES.symbolic_state.
Import SymbolicState.
Require Import FORVES.symbolic_state_eval.
Import SymbolicStateEval.
Require Import FORVES.symbolic_state_eval_facts.
Import SymbolicStateEvalFacts.
Require Import FORVES.valid_symbolic_state.
Import ValidSymbolicState.
Require Import FORVES.symbolic_state_cmp.
Import SymbolicStateCmp.
Require Import FORVES.memory_cmp_impl.
Import MemoryCmpImpl.
Require Import FORVES.eval_common.
Import EvalCommon.
Require Import FORVES.concrete_interpreter.
Import ConcreteInterpreter.
Require Import Coq.Logic.FunctionalExtensionality.
Require Import FORVES.memory_ops_solvers_impl_soundness.
Import MemoryOpsSolversImplSoundness.
Module MemoryCmpImplSoundness.
Theorem trivial_memory_cmp_snd:
safe_smemory_cmp_ext_wrt_sstack_value_cmp trivial_memory_cmp.
Proof.
unfold safe_smemory_cmp_ext_wrt_sstack_value_cmp.
unfold safe_sstack_val_cmp_ext_1_d.
unfold safe_smemory_cmp_ext_d.
unfold safe_smemory_cmp.
unfold trivial_memory_cmp.
intros.
destruct smem1; destruct smem2; try discriminate.
exists mem.
auto.
Qed.
Theorem basic_memory_cmp_snd:
safe_smemory_cmp_ext_wrt_sstack_value_cmp basic_memory_cmp.
Proof.
unfold safe_smemory_cmp_ext_wrt_sstack_value_cmp.
intros d sstack_val_cmp H_sstack_val_cmp_snd.
unfold safe_smemory_cmp_ext_d.
intros d' H_d'_le_d.
unfold safe_smemory_cmp.
intros smem1 smem2 maxidx1 sb1 maxidx2 sb2 instk_height ops H_valid_sb1 H_valid_sb2.
revert smem2.
revert smem1.
induction smem1 as [|u1 smem1' IHsmem1'].
+ intros smem2 H_valid_smem1 H_valid_smem2 H_basic_mem_cmp stk mem strg exts H_stk_len.
destruct smem2; try discriminate.
exists mem.
unfold eval_smemory.
simpl.
split; reflexivity.
+ intros smem2 H_valid_smem1 H_valid_smem2 H_basic_mem_cmp stk mem strg exts H_stk_len.
destruct smem2 as [|u2 smem2'] eqn:H_smem2.
++ simpl in H_basic_mem_cmp.
destruct u1; try discriminate.
++ simpl in H_basic_mem_cmp.
destruct u1 as [soffset1 svalue1|soffset1 svalue1] eqn:E_u1; destruct u2 as [soffset2 svalue2|soffset2 svalue2] eqn:E_u2; try discriminate.
+++ destruct (sstack_val_cmp d' soffset1 soffset2 maxidx1 sb1 maxidx2 sb2 instk_height ops) eqn:E_cmp_soffset1_soffset2; try discriminate.
destruct (sstack_val_cmp d' svalue1 svalue2 maxidx1 sb1 maxidx2 sb2 instk_height ops) eqn:E_cmp_svalue1_svalue2; try discriminate.
simpl in H_valid_smem1.
destruct H_valid_smem1 as [ [H_valid_soffset1 H_valid_svalue1] H_valid_smem1'].
simpl in H_valid_smem2.
destruct H_valid_smem2 as [ [H_valid_soffset2 H_valid_svalue2] H_valid_smem2'].
pose proof (IHsmem1' smem2' H_valid_smem1' H_valid_smem2' H_basic_mem_cmp stk mem strg exts H_stk_len) as IHsmem1'_0.
destruct IHsmem1'_0 as [mem' [IHsmem1'_0 IHsmem1'_1]].
unfold safe_sstack_val_cmp_ext_1_d in H_sstack_val_cmp_snd.
pose proof (H_sstack_val_cmp_snd d' H_d'_le_d) as H_sstack_val_cmp_snd_d'.
unfold safe_sstack_val_cmp in H_sstack_val_cmp_snd_d'.
pose proof(H_sstack_val_cmp_snd_d' soffset1 soffset2 maxidx1 sb1 maxidx2 sb2 instk_height ops H_valid_soffset1 H_valid_soffset2 H_valid_sb1 H_valid_sb2 E_cmp_soffset1_soffset2 stk mem strg exts H_stk_len) as H_eval_soffset1_soffset2.
destruct H_eval_soffset1_soffset2 as [soffset_1_2_v [H_eval_soffset1 H_eval_soffset2]].
pose proof(H_sstack_val_cmp_snd_d' svalue1 svalue2 maxidx1 sb1 maxidx2 sb2 instk_height ops H_valid_svalue1 H_valid_svalue2 H_valid_sb1 H_valid_sb2 E_cmp_svalue1_svalue2 stk mem strg exts H_stk_len) as H_eval_svalue1_svalue2.
destruct H_eval_svalue1_svalue2 as [svalue_1_2_v [H_eval_svalue1 H_eval_svalue2]].
exists (mstore mem' svalue_1_2_v soffset_1_2_v).
unfold eval_smemory in IHsmem1'_0.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx1 sb1 ops)) smem1') as [updates1|] eqn:H_mo_smem1'; try discriminate.
injection IHsmem1'_0 as IHsmem1'_0.
unfold eval_smemory in IHsmem1'_1.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx2 sb2 ops)) smem2') as [updates2|] eqn:H_mo_smem2'; try discriminate.
injection IHsmem1'_1 as IHsmem1'_1.
unfold eval_smemory.
unfold map_option.
repeat rewrite <- map_option_ho.
unfold instantiate_memory_update at 1.
rewrite H_eval_soffset1.
rewrite H_eval_svalue1.
unfold instantiate_memory_update at 2.
rewrite H_eval_soffset2.
rewrite H_eval_svalue2.
rewrite H_mo_smem1'.
rewrite H_mo_smem2'.
unfold update_memory.
fold update_memory.
rewrite IHsmem1'_0.
rewrite IHsmem1'_1.
simpl.
split; reflexivity.
(* copy of previous item *)
+++ destruct (sstack_val_cmp d' soffset1 soffset2 maxidx1 sb1 maxidx2 sb2 instk_height ops) eqn:E_cmp_soffset1_soffset2; try discriminate.
destruct (sstack_val_cmp d' svalue1 svalue2 maxidx1 sb1 maxidx2 sb2 instk_height ops) eqn:E_cmp_svalue1_svalue2; try discriminate.
simpl in H_valid_smem1.
destruct H_valid_smem1 as [ [H_valid_soffset1 H_valid_svalue1] H_valid_smem1'].
simpl in H_valid_smem2.
destruct H_valid_smem2 as [ [H_valid_soffset2 H_valid_svalue2] H_valid_smem2'].
pose proof (IHsmem1' smem2' H_valid_smem1' H_valid_smem2' H_basic_mem_cmp stk mem strg exts H_stk_len) as IHsmem1'_0.
destruct IHsmem1'_0 as [mem' [IHsmem1'_0 IHsmem1'_1]].
unfold safe_sstack_val_cmp_ext_1_d in H_sstack_val_cmp_snd.
pose proof (H_sstack_val_cmp_snd d' H_d'_le_d) as H_sstack_val_cmp_snd_d'.
unfold safe_sstack_val_cmp in H_sstack_val_cmp_snd_d'.
pose proof(H_sstack_val_cmp_snd_d' soffset1 soffset2 maxidx1 sb1 maxidx2 sb2 instk_height ops H_valid_soffset1 H_valid_soffset2 H_valid_sb1 H_valid_sb2 E_cmp_soffset1_soffset2 stk mem strg exts H_stk_len) as H_eval_soffset1_soffset2.
destruct H_eval_soffset1_soffset2 as [soffset_1_2_v [H_eval_soffset1 H_eval_soffset2]].
pose proof(H_sstack_val_cmp_snd_d' svalue1 svalue2 maxidx1 sb1 maxidx2 sb2 instk_height ops H_valid_svalue1 H_valid_svalue2 H_valid_sb1 H_valid_sb2 E_cmp_svalue1_svalue2 stk mem strg exts H_stk_len) as H_eval_svalue1_svalue2.
destruct H_eval_svalue1_svalue2 as [svalue_1_2_v [H_eval_svalue1 H_eval_svalue2]].
exists (mstore mem' (split1_byte (svalue_1_2_v: word ((S (pred BytesInEVMWord))*8))) soffset_1_2_v).
unfold eval_smemory in IHsmem1'_0.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx1 sb1 ops)) smem1') as [updates1|] eqn:H_mo_smem1'; try discriminate.
injection IHsmem1'_0 as IHsmem1'_0.
unfold eval_smemory in IHsmem1'_1.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx2 sb2 ops)) smem2') as [updates2|] eqn:H_mo_smem2'; try discriminate.
injection IHsmem1'_1 as IHsmem1'_1.
unfold eval_smemory.
unfold map_option.
repeat rewrite <- map_option_ho.
unfold instantiate_memory_update at 1.
rewrite H_eval_soffset1.
rewrite H_eval_svalue1.
unfold instantiate_memory_update at 2.
rewrite H_eval_soffset2.
rewrite H_eval_svalue2.
rewrite H_mo_smem1'.
rewrite H_mo_smem2'.
unfold update_memory.
fold update_memory.
rewrite IHsmem1'_0.
rewrite IHsmem1'_1.
simpl.
split; reflexivity.
Qed.
Lemma reorder_updates'_valid:
forall maxidx sb instk_height ops,
valid_bindings instk_height maxidx sb ops ->
forall d smem b smem_r,
valid_smemory instk_height maxidx smem ->
reorder_updates' d smem maxidx sb = (b,smem_r) ->
valid_smemory instk_height maxidx smem_r.
Proof.
intros maxidx sb instk_height ops H_valid_sb.
induction d as [|d'' IHd'].
+ intros smem b smem_r H_valid_smem H_reorder'.
simpl in H_reorder'.
injection H_reorder' as H_b H_smem_r.
rewrite <- H_smem_r.
apply H_valid_smem.
+ intros smem b smem_r H_valid_smem H_reorder'.
simpl in H_reorder'.
destruct smem as [|u1 smem'] eqn:H_smem.
++ injection H_reorder' as H_b H_smem_r.
rewrite <- H_smem_r.
simpl.
auto.
++ destruct smem' as [|u2 smem''] eqn:H_smem'.
+++ injection H_reorder' as H_b H_smem_r.
rewrite <- H_smem_r.
simpl.
split; try auto.
apply H_valid_smem.
+++ destruct (swap_memory_update u1 u2 maxidx sb) eqn:E_swap.
++++ destruct (reorder_updates' d'' (u1 :: smem'')) eqn:E_reorder'_rec.
injection H_reorder' as H_b H_smem_r.
rewrite <- H_smem_r.
simpl in H_valid_smem.
destruct H_valid_smem as [H_valid_u1 [H_valid_u2 H_valid_smem'']].
pose proof (IHd' (u1 :: smem'') b0 s (valid_smemory_when_extended_with_valid_update instk_height maxidx u1 smem'' H_valid_u1 H_valid_smem'') E_reorder'_rec) as IHd'_0.
simpl.
split.
+++++ apply H_valid_u2.
+++++ apply IHd'_0.
++++ destruct (reorder_updates' d'' (u2 :: smem'')) eqn:E_reorder'_rec.
injection H_reorder' as H_b H_smem_r.
rewrite <- H_smem_r.
simpl in H_valid_smem.
destruct H_valid_smem as [H_valid_u1 [H_valid_u2 H_valid_smem'']].
pose proof (IHd' (u2 :: smem'') b0 s (valid_smemory_when_extended_with_valid_update instk_height maxidx u2 smem'' H_valid_u2 H_valid_smem'') E_reorder'_rec) as IHd'_0.
simpl.
split.
+++++ apply H_valid_u1.
+++++ apply IHd'_0.
Qed.
Lemma x_eq_offset_false_implied:
forall n offset1 offset2 x,
(offset1+(N.of_nat (S n)) <=? offset2)%N = true ->
(x =? offset1)%N = true ->
(x =? offset2)%N = false.
Proof.
intros n offset1 offset2 x H_no_overlap H_x_offset1.
simpl in H_no_overlap.
rewrite N.eqb_eq in H_x_offset1.
rewrite H_x_offset1.
rewrite N.eqb_neq.
apply N.lt_neq.
pose proof (N.leb_le (offset1 + N.pos (Pos.of_succ_nat n))%N offset2) as H_leb_le.
rewrite H_leb_le in H_no_overlap.
apply Nlt_in.
intuition.
Qed.
Lemma n_le_m_implies_n_lt_m_plus_1:
forall n m,
(n <=? m = true -> n <? m+1 = true)%N.
Proof.
intros n m H_n_le_m.
rewrite N.ltb_lt.
rewrite N.leb_le in H_n_le_m.
apply Nlt_in.
intuition.
Qed.
Lemma n_le_m_implies_n_le_m_plus_1:
forall n m,
(n <=? m = true -> n <=? m+1 = true)%N.
Proof.
intros n m H_n_le_m.
rewrite N.leb_le.
rewrite N.leb_le in H_n_le_m.
apply N.le_lteq.
left.
apply Nlt_in.
intuition.
Qed.
Lemma Nle_in:
forall n m : N, N.to_nat n <= N.to_nat m -> (n <= m)%N.
Proof.
intros n m H.
apply Nat.le_lteq in H.
destruct H.
+ apply Nlt_in in H.
apply N.le_lteq.
left.
apply H.
+ apply N.le_lteq.
right.
apply nat_of_N_eq in H.
apply H.
Qed.
Lemma n_lt_m_implies_n_plus_1_le_m:
forall n m,
(n <? m = true -> n+1 <=? m = true)%N.
Proof.
intros n m H_n_lt_m.
rewrite N.leb_le.
pose proof Nlt_in.
rewrite N.ltb_lt in H_n_lt_m.
apply Nlt_out in H_n_lt_m.
apply Nle_in.
intuition.
Qed.
Lemma update_same_address_mstore'_aux:
forall n m mem (value1: word (S n*8)) (value2: word (m*8)) offset1 offset2 x,
(offset1+(N.of_nat (S n)) <=? offset2)%N = true ->
(x =? offset1)%N = true ->
mstore'
(fun offset' : N =>
if (offset' =? offset1)%N
then split1_byte value1
else mstore' mem (split2_byte value1) (offset1 + 1) offset') value2 offset2
x = split1_byte value1.
Proof.
intros n m.
revert m n.
induction m as [|m'].
+ intros n mem value1 value2 offset1 offset2 x H_no_overlap H_x_eq_offset1.
simpl.
rewrite H_x_eq_offset1.
reflexivity.
+ intros n mem value1 value2 offset1 offset2 x H_no_overlap H_x_eq_offset1.
simpl.
assert(H_x_offset2: (x =? offset2)%N = false). apply (x_eq_offset_false_implied n offset1 offset2 x H_no_overlap H_x_eq_offset1).
assert(H_no_overlap': (offset1 + N.of_nat (S n) <=? offset2+1)%N = true).
apply (n_le_m_implies_n_le_m_plus_1 (offset1 + N.of_nat (S n))%N offset2 H_no_overlap).
rewrite H_x_offset2.
pose proof (IHm' n mem value1 (split2_byte value2) offset1 (offset2+1)%N x H_no_overlap' H_x_eq_offset1) as IHm'_0.
apply IHm'_0.
Qed.
Lemma update_same_address_mstore'_aux'':
forall n m mem (value1: word (S n*8)) (value2: word (m*8)) offset1 offset2 x,
(offset1+(N.of_nat (S n)) <=? offset2)%N = true ->
(x =? offset1)%N = false ->
mstore'
(fun offset' : N =>
if (offset' =? offset1)%N
then split1_byte value1
else mstore' mem (split2_byte value1) (offset1 + 1) offset') value2 offset2
x = mstore'
(mstore' mem (split2_byte value1) (offset1 + 1)) value2 offset2 x.
Proof.
intros n m.
revert m n.
induction m as [|m' IHm'].
+ intros n nmem value1 value2 offset1 offset2 x H_no_overlap H_x_offset1.
simpl.
rewrite H_x_offset1.
reflexivity.
+ intros n mem value1 value2 offset1 offset2 x H_no_overlap H_x_offset1.
simpl.
destruct (x =? offset2)%N eqn:E_x_offset2; try reflexivity.
assert(H_no_overlap': (offset1 + N.of_nat (S n) <=? offset2+1)%N = true).
apply (n_le_m_implies_n_le_m_plus_1 (offset1 + N.of_nat (S n))%N offset2 H_no_overlap).
pose proof (IHm' n mem value1 (split2_byte value2) offset1 (offset2+1)%N x H_no_overlap' H_x_offset1) as IHm'_0.
apply IHm'_0.
Qed.
Lemma mstore_non_overlap_updates:
forall n m mem offset1 (value1 : word (n*8)) offset2 (value2: word (m*8)),
(offset1+(N.of_nat n) <=? offset2)%N = true ->
mstore' (mstore' mem value1 offset1) value2 offset2 =
mstore' (mstore' mem value2 offset2) value1 offset1.
Proof.
induction n as [|n' IHn'].
+ intros m mem offset1 value1 offset2 value2 H_no_overlap.
unfold mstore' at 2.
unfold mstore' at 2.
reflexivity.
+ intros m mem offset1 value1 offset2 value2 H_no_overlap.
apply functional_extensionality.
intro x.
simpl.
destruct (x =? offset1)%N eqn:E_x_offset1.
++ rewrite update_same_address_mstore'_aux.
reflexivity.
apply H_no_overlap.
apply E_x_offset1.
++ rewrite update_same_address_mstore'_aux''.
assert (H_no_overlap': (offset1 + 1 + N.of_nat n' <=? offset2)%N = true). rewrite <- N_x_nat_of_Si. apply H_no_overlap.
pose proof (IHn' m mem (offset1 + 1)%N (split2_byte value1) offset2 value2 H_no_overlap') as IHn'_0.
rewrite IHn'_0.
reflexivity.
apply H_no_overlap.
apply E_x_offset1.
Qed.
Lemma swap_memory_update_snd:
forall smem u1 u2 maxidx sb instk_height ops,
valid_smemory instk_height maxidx smem ->
valid_smemory_update instk_height maxidx u1 ->
valid_smemory_update instk_height maxidx u2 ->
valid_bindings instk_height maxidx sb ops ->
swap_memory_update u1 u2 maxidx sb = true ->
forall stk mem strg exts,
length stk = instk_height ->
exists mem' : memory,
eval_smemory (u1::u2::smem) maxidx sb stk mem strg exts ops = Some mem' /\
eval_smemory (u2::u1::smem) maxidx sb stk mem strg exts ops = Some mem'.
Proof.
intros smem u1 u2 maxidx sb instk_height ops.
intros H_valid_smem H_valid_u1 H_valid_u2 H_valid_sb H_swap_mem_u.
intros stk mem strg exts.
intros H_stk_len.
pose proof (valid_smemory_when_extended_with_valid_update instk_height maxidx u1 smem H_valid_u1 H_valid_smem) as H_valid_u1_smem.
pose proof (valid_smemory_when_extended_with_valid_update instk_height maxidx u2 (u1::smem) H_valid_u2 H_valid_u1_smem) as H_valid_u2_u1_smem.
pose proof (valid_smemory_when_extended_with_valid_update instk_height maxidx u2 smem H_valid_u2 H_valid_smem) as H_valid_u2_smem.
pose proof (valid_smemory_when_extended_with_valid_update instk_height maxidx u1 (u2::smem) H_valid_u1 H_valid_u2_smem) as H_valid_u1_u2_smem.
pose proof (eval_smemory_succ instk_height maxidx sb stk mem strg exts ops (u1::u2::smem) (eq_sym H_stk_len) H_valid_u1_u2_smem H_valid_sb) as H_eval_u1_u2_smem.
pose proof (eval_smemory_succ instk_height maxidx sb stk mem strg exts ops (u2::u1::smem) (eq_sym H_stk_len) H_valid_u2_u1_smem H_valid_sb) as H_eval_u2_u1_smem.
destruct H_eval_u1_u2_smem as [mem1 H_eval_u1_u2_smem].
destruct H_eval_u2_u1_smem as [mem2 H_eval_u2_u1_smem].
unfold eval_smemory in H_eval_u1_u2_smem.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops)) (u1 :: u2 :: smem)) as [updates1|] eqn:E_mo1; try discriminate.
injection H_eval_u1_u2_smem as H_eval_u1_u2_smem.
unfold eval_smemory in H_eval_u2_u1_smem.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops)) (u2 :: u1 :: smem)) as [updates2|] eqn:E_mo2; try discriminate.
injection H_eval_u2_u1_smem as H_eval_u2_u1_smem.
pose proof (map_option_split_2 (memory_update sstack_val) (memory_update EVMWord) (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops)) smem updates1 u1 u2 E_mo1) as E_mo1_split.
destruct E_mo1_split as [u1v1 [u2v1 [updates1' [H_updates1 [H_u1v1 [H_u2v1 H_mo1_0]]]]]].
pose proof (map_option_split_2 (memory_update sstack_val) (memory_update EVMWord) (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops)) smem updates2 u2 u1 E_mo2) as E_mo2_split.
destruct E_mo2_split as [u2v2 [u1v2 [updates2' [H_updates2 [H_u2v2 [H_u1v2 H_mo2_0]]]]]].
assert(H_updates1'_eq_updates2': updates1' = updates2'). rewrite H_mo2_0 in H_mo1_0. injection H_mo1_0 as H_mo1_0. rewrite H_mo1_0. reflexivity.
assert(H_u1v1_u1v2: u1v1=u1v2). rewrite H_u1v2 in H_u1v1. injection H_u1v1 as H_u1v1. rewrite H_u1v1. reflexivity.
assert(H_u2v1_u2v2: u2v1=u2v2). rewrite H_u2v2 in H_u2v1. injection H_u2v1 as H_u2v1. rewrite H_u2v1. reflexivity.
rewrite <- H_updates1'_eq_updates2' in H_updates2.
rewrite <- H_u1v1_u1v2 in H_updates2.
rewrite <- H_u2v1_u2v2 in H_updates2.
rewrite H_updates1 in H_eval_u1_u2_smem.
rewrite H_updates2 in H_eval_u2_u1_smem.
unfold update_memory in H_eval_u1_u2_smem.
fold update_memory in H_eval_u1_u2_smem.
unfold update_memory in H_eval_u2_u1_smem.
fold update_memory in H_eval_u2_u1_smem.
destruct u1 as [soffset1 svalue1|soffset1 svalue1] eqn:E_u1;
destruct u2 as [soffset2 svalue2|soffset2 svalue2] eqn:E_u2.
+ unfold instantiate_memory_update in H_u1v1.
destruct (eval_sstack_val soffset1 stk mem strg exts maxidx sb ops) as [offset1_v|] eqn:E_eval_soffset1; try discriminate.
destruct (eval_sstack_val svalue1 stk mem strg exts maxidx sb ops) as [svalue1_v|] eqn:E_eval_svalue1; try discriminate.
injection H_u1v1 as H_u1v1.
rewrite <- H_u1v1 in H_eval_u1_u2_smem.
rewrite <- H_u1v1 in H_eval_u2_u1_smem.
unfold instantiate_memory_update in H_u2v1.
destruct (eval_sstack_val soffset2 stk mem strg exts maxidx sb ops) as [offset2_v|] eqn:E_eval_soffset2; try discriminate.
destruct (eval_sstack_val svalue2 stk mem strg exts maxidx sb ops) as [svalue2_v|] eqn:E_eval_svalue2; try discriminate.
injection H_u2v1 as H_u2v1.
rewrite <- H_u2v1 in H_eval_u2_u1_smem.
rewrite <- H_u2v1 in H_eval_u1_u2_smem.
simpl in H_eval_u1_u2_smem.
simpl in H_eval_u2_u1_smem.
unfold mstore in H_eval_u1_u2_smem.
unfold mstore in H_eval_u2_u1_smem.
unfold eval_smemory.
rewrite E_mo1.
rewrite E_mo2.
rewrite H_updates1.
rewrite H_updates2.
rewrite <- H_u1v1.
rewrite <- H_u2v1.
unfold update_memory.
fold update_memory.
unfold update_memory'.
unfold mstore.
unfold swap_memory_update in H_swap_mem_u.
destruct (follow_in_smap soffset1 maxidx sb) eqn:E_follow_in_smap_soffset1; try discriminate.
destruct f; try discriminate.
destruct smv; try discriminate.
destruct val; try discriminate.
destruct (follow_in_smap soffset2 maxidx sb) eqn:E_follow_in_smap_soffset2; try discriminate.
destruct f; try discriminate.
destruct smv; try discriminate.
destruct val0; try discriminate.
assert(E_eval_soffset1_aux := E_eval_soffset1).
unfold eval_sstack_val in E_eval_soffset1_aux.
unfold eval_sstack_val' in E_eval_soffset1_aux.
fold eval_sstack_val' in E_eval_soffset1_aux.
rewrite E_follow_in_smap_soffset1 in E_eval_soffset1_aux.
injection E_eval_soffset1_aux as E_eval_soffset1_aux.
assert(E_eval_soffset2_aux := E_eval_soffset2).
unfold eval_sstack_val in E_eval_soffset2_aux.
unfold eval_sstack_val' in E_eval_soffset2_aux.
fold eval_sstack_val' in E_eval_soffset2_aux.
rewrite E_follow_in_smap_soffset2 in E_eval_soffset2_aux.
injection E_eval_soffset2_aux as E_eval_soffset2_aux.
rewrite E_eval_soffset1_aux in H_swap_mem_u.
rewrite E_eval_soffset2_aux in H_swap_mem_u.
assert( H_swap_mem_u_le: (wordToN offset2_v + 32 <=? wordToN offset1_v)%N = true).
pose proof (n_lt_m_implies_n_plus_1_le_m (wordToN offset2_v + 31) (wordToN offset1_v) H_swap_mem_u) as H_swap_mem_u_le_aux.
rewrite <- N.add_assoc in H_swap_mem_u_le_aux.
simpl in H_swap_mem_u_le_aux.
apply H_swap_mem_u_le_aux.
pose proof (mstore_non_overlap_updates BytesInEVMWord BytesInEVMWord (update_memory mem updates1') (wordToN offset2_v) svalue2_v (wordToN offset1_v) svalue1_v H_swap_mem_u_le) as H_mstore_non_overlap_updates.
rewrite H_mstore_non_overlap_updates.
exists mem2.
split; rewrite H_eval_u2_u1_smem; reflexivity.
+ unfold instantiate_memory_update in H_u1v1.
destruct (eval_sstack_val soffset1 stk mem strg exts maxidx sb ops) as [offset1_v|] eqn:E_eval_soffset1; try discriminate.
destruct (eval_sstack_val svalue1 stk mem strg exts maxidx sb ops) as [svalue1_v|] eqn:E_eval_svalue1; try discriminate.
injection H_u1v1 as H_u1v1.
rewrite <- H_u1v1 in H_eval_u1_u2_smem.
rewrite <- H_u1v1 in H_eval_u2_u1_smem.
unfold instantiate_memory_update in H_u2v1.
destruct (eval_sstack_val soffset2 stk mem strg exts maxidx sb ops) as [offset2_v|] eqn:E_eval_soffset2; try discriminate.
destruct (eval_sstack_val svalue2 stk mem strg exts maxidx sb ops) as [svalue2_v|] eqn:E_eval_svalue2; try discriminate.
injection H_u2v1 as H_u2v1.
rewrite <- H_u2v1 in H_eval_u2_u1_smem.
rewrite <- H_u2v1 in H_eval_u1_u2_smem.
simpl in H_eval_u1_u2_smem.
simpl in H_eval_u2_u1_smem.
unfold mstore in H_eval_u1_u2_smem.
unfold mstore in H_eval_u2_u1_smem.
unfold eval_smemory.
rewrite E_mo1.
rewrite E_mo2.
rewrite H_updates1.
rewrite H_updates2.
rewrite <- H_u1v1.
rewrite <- H_u2v1.
unfold update_memory.
fold update_memory.
unfold update_memory'.
unfold mstore.
unfold swap_memory_update in H_swap_mem_u.
destruct (follow_in_smap soffset1 maxidx sb) eqn:E_follow_in_smap_soffset1; try discriminate.
destruct f; try discriminate.
destruct smv; try discriminate.
destruct val; try discriminate.
destruct (follow_in_smap soffset2 maxidx sb) eqn:E_follow_in_smap_soffset2; try discriminate.
destruct f; try discriminate.
destruct smv; try discriminate.
destruct val0; try discriminate.
assert(E_eval_soffset1_aux := E_eval_soffset1).
unfold eval_sstack_val in E_eval_soffset1_aux.
unfold eval_sstack_val' in E_eval_soffset1_aux.
fold eval_sstack_val' in E_eval_soffset1_aux.
rewrite E_follow_in_smap_soffset1 in E_eval_soffset1_aux.
injection E_eval_soffset1_aux as E_eval_soffset1_aux.
assert(E_eval_soffset2_aux := E_eval_soffset2).
unfold eval_sstack_val in E_eval_soffset2_aux.
unfold eval_sstack_val' in E_eval_soffset2_aux.
fold eval_sstack_val' in E_eval_soffset2_aux.
rewrite E_follow_in_smap_soffset2 in E_eval_soffset2_aux.
injection E_eval_soffset2_aux as E_eval_soffset2_aux.
rewrite E_eval_soffset1_aux in H_swap_mem_u.
rewrite E_eval_soffset2_aux in H_swap_mem_u.
assert( H_swap_mem_u_le: (wordToN offset2_v + 1 <=? wordToN offset1_v)%N = true).
apply n_lt_m_implies_n_plus_1_le_m. apply H_swap_mem_u.
pose proof (mstore_non_overlap_updates 1 BytesInEVMWord (update_memory mem updates1') (wordToN offset2_v) (split1_byte (svalue2_v : word ((S (pred BytesInEVMWord))*8))) (wordToN offset1_v) svalue1_v H_swap_mem_u_le) as H_mstore_non_overlap_updates.
rewrite H_mstore_non_overlap_updates.
exists mem2.
split; rewrite <- H_eval_u2_u1_smem; reflexivity.
+ unfold instantiate_memory_update in H_u1v1.
destruct (eval_sstack_val soffset1 stk mem strg exts maxidx sb ops) as [offset1_v|] eqn:E_eval_soffset1; try discriminate.
destruct (eval_sstack_val svalue1 stk mem strg exts maxidx sb ops) as [svalue1_v|] eqn:E_eval_svalue1; try discriminate.
injection H_u1v1 as H_u1v1.
rewrite <- H_u1v1 in H_eval_u1_u2_smem.
rewrite <- H_u1v1 in H_eval_u2_u1_smem.
unfold instantiate_memory_update in H_u2v1.
destruct (eval_sstack_val soffset2 stk mem strg exts maxidx sb ops) as [offset2_v|] eqn:E_eval_soffset2; try discriminate.
destruct (eval_sstack_val svalue2 stk mem strg exts maxidx sb ops) as [svalue2_v|] eqn:E_eval_svalue2; try discriminate.
injection H_u2v1 as H_u2v1.
rewrite <- H_u2v1 in H_eval_u2_u1_smem.
rewrite <- H_u2v1 in H_eval_u1_u2_smem.
simpl in H_eval_u1_u2_smem.
simpl in H_eval_u2_u1_smem.
unfold mstore in H_eval_u1_u2_smem.
unfold mstore in H_eval_u2_u1_smem.
unfold eval_smemory.
rewrite E_mo1.
rewrite E_mo2.
rewrite H_updates1.
rewrite H_updates2.
rewrite <- H_u1v1.
rewrite <- H_u2v1.
unfold update_memory.
fold update_memory.
unfold update_memory'.
unfold mstore.
unfold swap_memory_update in H_swap_mem_u.
destruct (follow_in_smap soffset1 maxidx sb) eqn:E_follow_in_smap_soffset1; try discriminate.
destruct f; try discriminate.
destruct smv; try discriminate.
destruct val; try discriminate.
destruct (follow_in_smap soffset2 maxidx sb) eqn:E_follow_in_smap_soffset2; try discriminate.
destruct f; try discriminate.
destruct smv; try discriminate.
destruct val0; try discriminate.
assert(E_eval_soffset1_aux := E_eval_soffset1).
unfold eval_sstack_val in E_eval_soffset1_aux.
unfold eval_sstack_val' in E_eval_soffset1_aux.
fold eval_sstack_val' in E_eval_soffset1_aux.
rewrite E_follow_in_smap_soffset1 in E_eval_soffset1_aux.
injection E_eval_soffset1_aux as E_eval_soffset1_aux.
assert(E_eval_soffset2_aux := E_eval_soffset2).
unfold eval_sstack_val in E_eval_soffset2_aux.
unfold eval_sstack_val' in E_eval_soffset2_aux.
fold eval_sstack_val' in E_eval_soffset2_aux.
rewrite E_follow_in_smap_soffset2 in E_eval_soffset2_aux.
injection E_eval_soffset2_aux as E_eval_soffset2_aux.
rewrite E_eval_soffset1_aux in H_swap_mem_u.
rewrite E_eval_soffset2_aux in H_swap_mem_u.
assert( H_swap_mem_u_le: (wordToN offset2_v + 32 <=? wordToN offset1_v)%N = true).
pose proof (n_lt_m_implies_n_plus_1_le_m (wordToN offset2_v + 31) (wordToN offset1_v) H_swap_mem_u) as H_swap_mem_u_le_aux.
rewrite <- N.add_assoc in H_swap_mem_u_le_aux.
simpl in H_swap_mem_u_le_aux.
apply H_swap_mem_u_le_aux.
pose proof (mstore_non_overlap_updates BytesInEVMWord 1 (update_memory mem updates1') (wordToN offset2_v) svalue2_v (wordToN offset1_v) (split1_byte (svalue1_v : word ((S (pred BytesInEVMWord))*8))) H_swap_mem_u_le) as H_mstore_non_overlap_updates.
rewrite H_mstore_non_overlap_updates.
exists mem2.
split; rewrite <- H_eval_u2_u1_smem; reflexivity.
+ unfold instantiate_memory_update in H_u1v1.
destruct (eval_sstack_val soffset1 stk mem strg exts maxidx sb ops) as [offset1_v|] eqn:E_eval_soffset1; try discriminate.
destruct (eval_sstack_val svalue1 stk mem strg exts maxidx sb ops) as [svalue1_v|] eqn:E_eval_svalue1; try discriminate.
injection H_u1v1 as H_u1v1.
rewrite <- H_u1v1 in H_eval_u1_u2_smem.
rewrite <- H_u1v1 in H_eval_u2_u1_smem.
unfold instantiate_memory_update in H_u2v1.
destruct (eval_sstack_val soffset2 stk mem strg exts maxidx sb ops) as [offset2_v|] eqn:E_eval_soffset2; try discriminate.
destruct (eval_sstack_val svalue2 stk mem strg exts maxidx sb ops) as [svalue2_v|] eqn:E_eval_svalue2; try discriminate.
injection H_u2v1 as H_u2v1.
rewrite <- H_u2v1 in H_eval_u2_u1_smem.
rewrite <- H_u2v1 in H_eval_u1_u2_smem.
simpl in H_eval_u1_u2_smem.
simpl in H_eval_u2_u1_smem.
unfold mstore in H_eval_u1_u2_smem.
unfold mstore in H_eval_u2_u1_smem.
unfold eval_smemory.
rewrite E_mo1.
rewrite E_mo2.
rewrite H_updates1.
rewrite H_updates2.
rewrite <- H_u1v1.
rewrite <- H_u2v1.
unfold update_memory.
fold update_memory.
unfold update_memory'.
unfold mstore.
unfold swap_memory_update in H_swap_mem_u.
destruct (follow_in_smap soffset1 maxidx sb) eqn:E_follow_in_smap_soffset1; try discriminate.
destruct f; try discriminate.
destruct smv; try discriminate.
destruct val; try discriminate.
destruct (follow_in_smap soffset2 maxidx sb) eqn:E_follow_in_smap_soffset2; try discriminate.
destruct f; try discriminate.
destruct smv; try discriminate.
destruct val0; try discriminate.
assert(E_eval_soffset1_aux := E_eval_soffset1).
unfold eval_sstack_val in E_eval_soffset1_aux.
unfold eval_sstack_val' in E_eval_soffset1_aux.
fold eval_sstack_val' in E_eval_soffset1_aux.
rewrite E_follow_in_smap_soffset1 in E_eval_soffset1_aux.
injection E_eval_soffset1_aux as E_eval_soffset1_aux.
assert(E_eval_soffset2_aux := E_eval_soffset2).
unfold eval_sstack_val in E_eval_soffset2_aux.
unfold eval_sstack_val' in E_eval_soffset2_aux.
fold eval_sstack_val' in E_eval_soffset2_aux.
rewrite E_follow_in_smap_soffset2 in E_eval_soffset2_aux.
injection E_eval_soffset2_aux as E_eval_soffset2_aux.
rewrite E_eval_soffset1_aux in H_swap_mem_u.
rewrite E_eval_soffset2_aux in H_swap_mem_u.
assert( H_swap_mem_u_le: (wordToN offset2_v + 1 <=? wordToN offset1_v)%N = true).
apply n_lt_m_implies_n_plus_1_le_m. apply H_swap_mem_u.
pose proof (mstore_non_overlap_updates 1 1 (update_memory mem updates1') (wordToN offset2_v) (split1_byte (svalue2_v : word ((S (pred BytesInEVMWord))*8))) (wordToN offset1_v) (split1_byte (svalue1_v : word ((S (pred BytesInEVMWord))*8))) H_swap_mem_u_le) as H_mstore_non_overlap_updates.
rewrite H_mstore_non_overlap_updates.
exists mem2.
split; rewrite <- H_eval_u2_u1_smem; reflexivity.
Qed.
Lemma reorder_updates'_snd:
forall maxidx sb instk_height ops,
valid_bindings instk_height maxidx sb ops ->
forall d smem b smem_r,
valid_smemory instk_height maxidx smem ->
reorder_updates' d smem maxidx sb = (b,smem_r) ->
forall stk mem strg exts,
length stk = instk_height ->
exists mem' : memory,
eval_smemory smem maxidx sb stk mem strg exts ops = Some mem' /\
eval_smemory smem_r maxidx sb stk mem strg exts ops = Some mem'.
Proof.
intros maxidx sb instk_height ops H_valid_sb.
induction d as [|d' IHd'].
+ intros smem b smem_r H_valid_smem H_reorder'.
intros stk mem strg exts H_stk_len.
simpl in H_reorder'.
injection H_reorder' as H_b H_smem_r.
rewrite <- H_smem_r.
pose proof (eval_smemory_succ instk_height maxidx sb stk mem strg exts ops smem (eq_sym H_stk_len) H_valid_smem H_valid_sb) as H_eval_smem.
destruct H_eval_smem as [strg' H_eval_smem].
exists strg'.
split; apply H_eval_smem.
+ intros smem b smem_r H_valid_smem H_reorder'.
intros stk mem strg exts H_stk_len.
simpl in H_reorder'.
destruct smem as [|u1 smem'] eqn:E_smem.
++ injection H_reorder' as H_b H_smem_r.
rewrite <- H_smem_r.
exists mem.
unfold eval_smemory.
simpl.
split; reflexivity.
++ destruct smem' as [|u2 smem''] eqn:E_smem'.
+++ injection H_reorder' as H_b H_smem_r.
rewrite <- H_smem_r.
destruct u1 as [soffset1 svalue1|soffset1 svalue1] eqn:E_u1.
++++ simpl in H_valid_smem.
destruct H_valid_smem as [[H_valid_soffset1 H_valid_svalue1] _].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height soffset1 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_soffset1 H_valid_sb (gt_Sn_n maxidx)) as E_eval_soffset1.
destruct E_eval_soffset1 as [soffset1_v E_eval_soffset1].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height svalue1 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_svalue1 H_valid_sb (gt_Sn_n maxidx)) as E_eval_svalue1.
destruct E_eval_svalue1 as [svalue1_v E_eval_svalue1].
(* exists (fun key => if (key =? wordToN soffset1_v)%N then svalue1_v else mem key). *)
unfold eval_smemory.
unfold map_option.
unfold instantiate_memory_update.
unfold eval_sstack_val.
rewrite E_eval_soffset1.
rewrite E_eval_svalue1.
unfold update_memory.
exists (update_memory' mem (U_MSTORE EVMWord soffset1_v svalue1_v)).
split; reflexivity.
++++ simpl in H_valid_smem.
destruct H_valid_smem as [[H_valid_soffset1 H_valid_svalue1] _].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height soffset1 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_soffset1 H_valid_sb (gt_Sn_n maxidx)) as E_eval_soffset1.
destruct E_eval_soffset1 as [soffset1_v E_eval_soffset1].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height svalue1 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_svalue1 H_valid_sb (gt_Sn_n maxidx)) as E_eval_svalue1.
destruct E_eval_svalue1 as [svalue1_v E_eval_svalue1].
unfold eval_smemory.
unfold map_option.
unfold instantiate_memory_update.
unfold eval_sstack_val.
rewrite E_eval_soffset1.
rewrite E_eval_svalue1.
unfold update_memory.
exists (update_memory' mem (U_MSTORE8 EVMWord soffset1_v svalue1_v)).
split; reflexivity.
+++ destruct (swap_memory_update u1 u2 maxidx sb) eqn:E_swap.
++++ destruct (reorder_updates' d' (u1 :: smem'') maxidx sb) as [b' smem_r'] eqn:E_reorder'_rec.
injection H_reorder' as E_b E_smem_r.
simpl in H_valid_smem.
destruct H_valid_smem as [H_valid_u1 [H_valid_u2 H_valid_smem'']].
pose proof (swap_memory_update_snd smem'' u1 u2 maxidx sb instk_height ops H_valid_smem'' H_valid_u1 H_valid_u2 H_valid_sb E_swap stk mem strg exts H_stk_len) as H_swap_memory_update_snd.
destruct H_swap_memory_update_snd as [strg_aux [H_eval_u1_u2_smem'' H_eval_u2_u1_smem'' ]].
rewrite <- H_eval_u2_u1_smem'' in H_eval_u1_u2_smem''.
rewrite H_eval_u1_u2_smem''.
pose proof (IHd' (u1 :: smem'') b' smem_r' (valid_smemory_when_extended_with_valid_update instk_height maxidx u1 smem'' H_valid_u1 H_valid_smem'') E_reorder'_rec stk mem strg exts H_stk_len) as IHd'_0.
destruct IHd'_0 as [mem' [H_eval_u1_smem'' H_eval_smem_r']].
rewrite <- E_smem_r.
unfold eval_smemory in H_eval_u1_smem''.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops)) (u1 :: smem'')) as [updates1|] eqn:E_mo_1; try discriminate.
unfold eval_smemory in H_eval_smem_r'.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops)) smem_r') as [updates2|] eqn:E_mo_2; try discriminate.
destruct u2 as [soffset2 svalue2|soffset2 svalue2] eqn:E_u2.
+++++ simpl in H_valid_u2.
destruct H_valid_u2 as [H_valid_soffset2 H_valid_svalue2].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height soffset2 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_soffset2 H_valid_sb (gt_Sn_n maxidx)) as E_eval_soffset2.
destruct E_eval_soffset2 as [soffset2_v E_eval_soffset2].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height svalue2 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_svalue2 H_valid_sb (gt_Sn_n maxidx)) as E_eval_svalue2.
destruct E_eval_svalue2 as [svalue2_v E_eval_svalue2].
unfold eval_smemory.
unfold map_option.
repeat rewrite <- map_option_ho.
unfold instantiate_memory_update at 1.
unfold eval_sstack_val at 1.
rewrite E_eval_soffset2.
unfold eval_sstack_val at 1.
rewrite E_eval_svalue2.
unfold instantiate_memory_update at 3.
unfold eval_sstack_val at 3.
rewrite E_eval_soffset2.
unfold eval_sstack_val at 3.
rewrite E_eval_svalue2.
unfold map_option in E_mo_1.
destruct (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops) u1) as [elem_val|] eqn:E_elem_val; try discriminate.
repeat rewrite <- map_option_ho in E_mo_1.
rewrite E_mo_1.
rewrite E_mo_2.
unfold update_memory.
fold update_memory.
injection H_eval_u1_smem'' as H_eval_u1_smem''.
rewrite H_eval_u1_smem''.
injection H_eval_smem_r' as H_eval_smem_r'.
rewrite H_eval_smem_r'.
exists (update_memory' mem' (U_MSTORE EVMWord soffset2_v svalue2_v)).
split; reflexivity.
+++++ simpl in H_valid_u2.
destruct H_valid_u2 as [H_valid_soffset2 H_valid_svalue2].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height soffset2 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_soffset2 H_valid_sb (gt_Sn_n maxidx)) as E_eval_soffset2.
destruct E_eval_soffset2 as [soffset2_v E_eval_soffset2].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height svalue2 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_svalue2 H_valid_sb (gt_Sn_n maxidx)) as E_eval_svalue2.
destruct E_eval_svalue2 as [svalue2_v E_eval_svalue2].
unfold eval_smemory.
unfold map_option.
repeat rewrite <- map_option_ho.
unfold instantiate_memory_update at 1.
unfold eval_sstack_val at 1.
rewrite E_eval_soffset2.
unfold eval_sstack_val at 1.
rewrite E_eval_svalue2.
unfold instantiate_memory_update at 3.
unfold eval_sstack_val at 3.
rewrite E_eval_soffset2.
unfold eval_sstack_val at 3.
rewrite E_eval_svalue2.
unfold map_option in E_mo_1.
destruct (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops) u1) as [elem_val|] eqn:E_elem_val; try discriminate.
repeat rewrite <- map_option_ho in E_mo_1.
rewrite E_mo_1.
rewrite E_mo_2.
unfold update_memory.
fold update_memory.
injection H_eval_u1_smem'' as H_eval_u1_smem''.
rewrite H_eval_u1_smem''.
injection H_eval_smem_r' as H_eval_smem_r'.
rewrite H_eval_smem_r'.
exists (update_memory' mem' (U_MSTORE8 EVMWord soffset2_v svalue2_v)).
split; reflexivity.
++++ destruct (reorder_updates' d' (u2 :: smem'') maxidx sb) as [b' smem_r'] eqn:E_reorder'_rec.
simpl in H_valid_smem.
destruct H_valid_smem as [H_valid_u1 [H_valid_u2 H_valid_smem'']].
pose proof (IHd' (u2 :: smem'') b' smem_r' (valid_smemory_when_extended_with_valid_update instk_height maxidx u2 smem'' H_valid_u2 H_valid_smem'') E_reorder'_rec stk mem strg exts H_stk_len) as IHd'_0.
destruct IHd'_0 as [strg' [H_eval_u2_smem'' H_eval_smem_r']].
injection H_reorder' as H_b' H_smem_r'.
rewrite <- H_smem_r'.
unfold eval_smemory in H_eval_u2_smem''.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops)) (u2 :: smem'')) as [updates1|] eqn:E_mo_1; try discriminate.
unfold eval_smemory in H_eval_smem_r'.
destruct (map_option (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops)) smem_r') as [updates2|] eqn:E_mo_2; try discriminate.
destruct u1 as [soffset1 svalue1|soffset1 svalue1] eqn:E_u1.
+++++ simpl in H_valid_u1.
destruct H_valid_u1 as [H_valid_soffset1 H_valid_svalue1].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height soffset1 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_soffset1 H_valid_sb (gt_Sn_n maxidx)) as E_eval_soffset1.
destruct E_eval_soffset1 as [soffset1_v E_eval_soffset1].
pose proof (eval_sstack_val'_succ (S maxidx) instk_height svalue1 stk mem strg exts maxidx sb ops (eq_sym H_stk_len) H_valid_svalue1 H_valid_sb (gt_Sn_n maxidx)) as E_eval_svalue1.
destruct E_eval_svalue1 as [svalue1_v E_eval_svalue1].
unfold eval_smemory.
unfold map_option.
repeat rewrite <- map_option_ho.
unfold instantiate_memory_update at 1.
unfold eval_sstack_val at 1.
rewrite E_eval_soffset1.
unfold eval_sstack_val at 1.
rewrite E_eval_svalue1.
unfold instantiate_memory_update at 3.
unfold eval_sstack_val at 3.
rewrite E_eval_soffset1.
unfold eval_sstack_val at 3.
rewrite E_eval_svalue1.
unfold map_option in E_mo_1.
destruct (instantiate_memory_update (fun sv : sstack_val => eval_sstack_val sv stk mem strg exts maxidx sb ops) u2) as [elem_val|] eqn:E_elem_val; try discriminate.
repeat rewrite <- map_option_ho in E_mo_1.
rewrite E_mo_1.
rewrite E_mo_2.