-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cert0.v
1227 lines (1042 loc) · 28.7 KB
/
Cert0.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
(** The contents of this file will be either integrated with Cert.v or moved elsewhere. *)
From iVM Require Import DSet Mono.
Import DSetNotations.
Unset Suggest Proof Using.
Open Scope vector.
(* TODO: Place inside section or module. *)
Import OpCodes.
Open Scope Z.
(***)
Lemma next_1_helper (op: Z) (Hop: 0 <= op < 256) :
(Vector.map toB8 [op] : Bytes 1) = Z.to_N op :> N.
Proof.
simp map.
remember (toB8 op) as u eqn:Hu.
dependent elimination u as [[b0; b1; b2; b3; b4; b5; b6; b7]].
simp bytesToBits. simpl. rewrite Hu.
unfold bitsToN. f_equal.
apply fromBits_toBits. cbn. lia.
Qed.
Lemma step_match_helper (op: Z) (Hop: 0 <= op < 256) :
Z.of_N (bitsToN (bytesToBits (cells_to_bytes (Vector.map toB8 [op])))) = op.
Proof.
unfold cells_to_bytes, id.
rewrite next_1_helper;
[ rewrite Z2N.id | ];
lia.
Qed.
Proposition shiftin_spec [X n] [x: X] [u: vector X n] (H: (n + 1 = S n)%nat) :
shiftin x u = rew H in (u ++ [x]).
Proof.
induction u.
- revert H. apply EqDec.UIP_K. reflexivity.
- cbn in *.
set (HH := Nat.succ_inj _ _ H). rewrite (rew_cons HH).
f_equal. exact (IHu HH).
Qed.
(** We would have preferred to call this [shiftout_shiftin], but we stick
to the same naming pattern of VectorSpec. *)
Proposition shiftin_shiftout
[X n] (x: X) (u: vector X n) :
shiftout (shiftin x u) = u.
Proof.
induction u.
- reflexivity.
- cbn. rewrite IHu. reflexivity.
Qed.
Proposition last_shiftout_shifting [X n] (u: vector X (S n)) :
shiftin (Vector.last u) (shiftout u) = u.
Proof.
induction n.
- dependent elimination u as [[x]]. reflexivity.
- dependent elimination u as [x :: u].
cbn. f_equal. exact (IHn u).
Qed.
(** Cf. [List.rev_ind]. *)
Proposition vec_rev_ind
[A : Type]
(P : forall {n}, vector A n -> Prop)
(H_nil: P [])
(H_cons: forall {n} (u: vector A n) x, P u -> P (shiftin x u))
{n} (u: vector A n) : P u.
Proof.
induction n.
- dependent elimination u. exact H_nil.
- specialize (H_cons n (shiftout u) (Vector.last u) (IHn (shiftout u))).
rewrite last_shiftout_shifting in H_cons. exact H_cons.
Qed.
Corollary vec_rev_ind'
[A : Type]
(P : forall {n}, vector A n -> Prop)
(H_nil: P [])
(H_cons: forall {n} (u: vector A n) x, P u -> P (u ++ [x]))
{n} (u: vector A n) : P u.
Proof.
induction u using vec_rev_ind.
- exact H_nil.
- set (H := Nat.add_1_r n).
rewrite (shiftin_spec H).
destruct H.
exact (H_cons n u x IHu).
Qed.
(***)
Instance chain_propr : PropR chain.
Proof.
intros u u' Hu v v' Hv.
unfold chain.
apply (bind_propr _ _ _).
- exact Hu.
- intros x x' Hx.
cbv in Hx.
subst x.
destruct x'.
+ exact Hv.
+ crush.
Qed.
Proposition id_equation_1 X (x: X) : id x = x.
Proof. reflexivity. Qed.
(** * Certified programs *)
Definition Cert (spec: M bool) :=
exists (f: State -> nat), spec ⊑ let* s := get in
nSteps (f s).
(** In most cases we know exactly how many steps are needed. *)
Definition nCert n (spec: M bool) := spec ⊑ nSteps n.
Proposition nCert_is_Cert n (spec: M bool) : nCert n spec -> Cert spec.
Proof.
unfold nCert, Cert.
intros H.
exists (fun _ => n).
smon_rewrite.
exact H.
Qed.
Local Notation not_terminated := (ret true) (only parsing).
Local Notation terminated := (ret false) (only parsing).
Lemma cert_id : nCert 0 not_terminated.
Proof.
unfold nCert.
simp nSteps.
crush.
Qed.
Lemma ncert_comp m n (u: M bool) {Cu: nCert m u} (v: M bool) {Cv: nCert n v} :
nCert (m + n) (chain u v).
Proof.
unfold nCert in *.
rewrite nSteps_action.
apply chain_propr.
- exact Cu.
- exact Cv.
Qed.
Definition nCertN n {X} (mx: M X) := nCert n (mx;; not_terminated).
(** *** Asserting next operations and move PC *)
Definition swallow1 (op: Z) : M unit :=
let* pc := get' PC in
let* x := load pc in
assume' (x = toB8 op);;
put' PC (offset 1 pc).
Equations swallow {n} (ops: vector Z n) : M unit :=
swallow [] := ret tt;
swallow (op :: rest) :=
swallow1 op;;
swallow rest.
(* TODO: Move *)
Lemma assume_cons' {A} (EA: EqDec A) (a a': A) n (u u': vector A n) {X} (mx: M X) :
assume' (a :: u = a' :: u');;
mx = assume' (a = a');;
assume' (u = u');;
mx.
Proof.
repeat setoid_rewrite <- simplify_assume.
apply assume_cons.
Qed.
Lemma swallow_spec {n} (ops: vector Z n) :
swallow ops = let* pc := get' PC in
let* u := loadMany n pc in
assume' (u = Vector.map toB8 ops);;
put' PC (offset n pc).
Proof.
(* TODO: Simplify *)
induction n.
- dependent elimination ops.
simp swallow map.
simp_loadMany.
unfold offset.
smon_rewrite. setoid_rewrite toBits_ofN_bitsToN.
smon_rewrite.
- dependent elimination ops as [ @Vector.cons z n ops ].
simp swallow map. unfold swallow1. rewrite IHn.
simp_loadMany.
smon_rewrite.
apply bind_extensional. intros pc.
apply bind_extensional. intros op.
do 3 setoid_rewrite postpone_assume'.
smon_rewrite.
setoid_rewrite <- confined_put;
[ | apply (confined_neutral (m':=MEM));
typeclasses eauto ].
apply bind_extensional. intros u.
simpl Vector.map.
unfold Cells. (* TODO: How can we avoid having to remember this everywhere? *)
setoid_rewrite assume_cons'.
destruct (decide (op = toB8 z)) as [Hop|Hop]; [ | smon_rewrite ].
subst op.
destruct (decide _) as [Hu|Hu]; [ | smon_rewrite ].
subst u.
smon_rewrite.
setoid_rewrite <- Z_action_add.
do 2 f_equal.
lia.
Qed.
Instance confined_swallow {n} (ops: vector Z n) :
Confined (MEM * PC) (swallow ops).
Proof.
rewrite swallow_spec.
typeclasses eauto.
Qed.
Lemma swallow_action {m n} (o1: vector Z m) (o2: vector Z n) :
swallow (o1 ++ o2) = swallow o1;; swallow o2.
Proof.
induction m.
- dependent elimination o1.
simp swallow.
smon_rewrite.
- dependent elimination o1 as [ @Vector.cons x m o1 ].
simpl (swallow _).
simp swallow.
rewrite (IHm o1).
rewrite bind_assoc.
reflexivity.
Qed.
Instance swallow_propr {n} (ops: vector Z n) : PropR (swallow ops).
Proof.
rewrite swallow_spec.
crush.
Qed.
Proposition swallow1' (op: Z) n (ops: vector Z n) :
swallow (op :: ops) = swallow ([op] ++ ops).
Proof.
reflexivity.
Qed.
Lemma swallow_lemma {n} {ops: vector Z n} {X} {u: M X} {f: Bytes n -> M X} :
u ⊑ f (Vector.map toB8 ops) -> swallow ops;; u ⊑ next n >>= f.
Proof.
intros H.
repeat setoid_rewrite bind_assoc.
revert ops u f H.
induction n; intros ops u f H; simp next.
- dependent elimination ops. simp swallow.
setoid_rewrite ret_bind. exact H.
- dependent elimination ops as [Vector.cons (n:=n) x r].
simp swallow.
unfold swallow1.
repeat setoid_rewrite bind_assoc.
crush.
smon_rewrite.
subst.
exact (IHn r u (fun v => f (toB8 x :: v)) H).
Qed.
Proposition swallow_equation_3 (n : nat) (z : Z) (u : vector Z n) :
swallow (shiftin z u) = swallow u;;
swallow1 z.
Proof.
set (H := Nat.add_1_r n).
rewrite (shiftin_spec H).
destruct H. cbn.
rewrite swallow_action. simp swallow.
setoid_rewrite bind_ret_tt.
reflexivity.
Qed.
Hint Rewrite swallow_equation_3 : swallow.
(** ** EXIT *)
Ltac cert_start :=
unfold nCert;
simp nSteps;
unfold chain, oneStep;
repeat setoid_rewrite bind_assoc;
apply swallow_lemma;
setoid_rewrite next_1_helper; [ | lia ];
try (unfold cells_to_bytes, id;
rewrite next_1_helper; try lia);
simpl;
repeat rewrite ret_bind;
crush.
Proposition cert_EXIT : nCert 1 (swallow [EXIT];;
terminated).
Proof. cert_start. Qed.
Proposition cert_NOP : nCertN 1 (swallow [NOP]).
Proof. unfold nCertN. cert_start. Qed.
(**********************)
Proposition andb_spec (x y: bool) : x && y <-> x /\ y.
Proof.
destruct x, y; now cbn.
Qed.
Proposition Is_true_spec (b: bool) : b <-> b = true.
Proof.
destruct b; now cbn.
Qed.
Proposition ltb_spec (x y: Z) : x <? y <-> x < y.
Proof.
rewrite Is_true_spec, Z.ltb_lt. tauto.
Qed.
Notation isStdOp op := (0 < op < 256).
Proposition isStdOp_toB8 {op} (Hop: isStdOp op) : toB8 op = op :> Z.
Proof.
rewrite ofN_bitsToN.
apply fromBits_toBits.
lia.
Qed.
Proposition isStdOp_match0 {op} (Hop: isStdOp op) {X} (u v: X) :
match op with 0 => u | _ => v end = v.
Proof.
now destruct op.
Qed.
(** ** Not exit *)
(* TODO: Expand and maybe move *)
Ltac binary_simpl_tac :=
unfold cells_to_bytes;
try simp map;
try rewrite id_equation_1;
try rewrite bytesToBits_equation_3.
Lemma swallow_step_lemma n op n' (ops: vector Z n') mb
(Hop: isStdOp op)
(H: swallow ops;; mb ⊑ oneStep' op;; nSteps n) :
nCert (S n) (swallow (op :: ops);; mb).
Proof.
unfold nCert.
simp nSteps.
unfold chain.
unfold oneStep.
rewrite swallow1'.
rewrite swallow_action.
smon_rewrite.
apply swallow_lemma.
simp map.
binary_simpl_tac.
setoid_rewrite (isStdOp_toB8 Hop).
setoid_rewrite (isStdOp_match0 Hop).
smon_rewrite.
setoid_rewrite (isStdOp_toB8 Hop).
exact H.
Qed.
Corollary swallow_step_lemma' n op mb
(Hop: isStdOp op)
(H: mb ⊑ oneStep' op;; nSteps n) :
nCert (S n) (swallow [op];; mb).
Proof.
apply swallow_step_lemma; [ exact Hop | ].
simp swallow.
smon_rewrite.
exact H.
Qed.
Corollary swallow_step_lemma_N n op n' (ops: vector Z n') X (mx: M X)
(Hop: isStdOp op)
(H: (swallow ops;; mx);; not_terminated ⊑ oneStep' op;; nSteps n) :
nCertN (S n) (swallow (op :: ops);; mx).
Proof.
unfold nCertN. revert H.
smon_rewrite. intros H.
apply swallow_step_lemma; assumption.
Qed.
Corollary swallow_step_lemma_N' n op X (mx: M X)
(Hop: isStdOp op)
(H: mx;; not_terminated ⊑ oneStep' op;; nSteps n) :
nCertN (S n) (swallow [op];; mx).
Proof.
apply swallow_step_lemma_N; [ exact Hop | ].
simp swallow.
smon_rewrite.
exact H.
Qed.
Ltac swallow1_tac :=
unfold nCertN;
apply swallow_step_lemma_N';
[ lia | ];
simp nSteps;
apply (bind_propr _ _ _);
[ simp oneStep' | ];
crush.
(** ** Instructions with no operands *)
Proposition cert_JUMP : nCertN 1 (
swallow [JUMP];;
let* a := pop64 in
put' PC a).
Proof. swallow1_tac. Qed.
Proposition cert_SET_SP : nCertN 1 (
swallow [SET_SP];;
let* a := pop64 in
put' SP a).
Proof. swallow1_tac. Qed.
Proposition cert_GET_PC : nCertN 1 (
swallow [GET_PC];;
let* a := get' PC in
pushZ a).
Proof. swallow1_tac. Qed.
Proposition cert_GET_SP : nCertN 1 (
swallow [GET_SP];;
let* a := get' SP in
pushZ a).
Proof. swallow1_tac. Qed.
(* For the other push instructions, see below. *)
Proposition cert_PUSH0 : nCertN 1 (
swallow [PUSH0];;
pushZ 0).
Proof. swallow1_tac. Qed.
Proposition cert_LOAD1 : nCertN 1 (
swallow [LOAD1];;
let* a := pop64 in
let* x := loadMany 1 a in
pushZ x).
Proof. swallow1_tac. Qed.
Proposition cert_LOAD2 : nCertN 1 (
swallow [LOAD2];;
let* a := pop64 in
let* x := loadMany 2 a in
pushZ x).
Proof. swallow1_tac. Qed.
Proposition cert_LOAD4 : nCertN 1 (
swallow [LOAD4];;
let* a := pop64 in
let* x := loadMany 4 a in
pushZ x).
Proof. swallow1_tac. Qed.
Proposition cert_LOAD8 : nCertN 1 (
swallow [LOAD8];;
let* a := pop64 in
let* x := loadMany 8 a in
pushZ x).
Proof. swallow1_tac. Qed.
Proposition cert_STORE1 : nCertN 1 (
swallow [STORE1];;
let* a := pop64 in
let* x := pop64 in
storeZ 1 a x).
Proof. swallow1_tac. Qed.
Proposition cert_STORE2 : nCertN 1 (
swallow [STORE2];;
let* a := pop64 in
let* x := pop64 in
storeZ 2 a x).
Proof. swallow1_tac. Qed.
Proposition cert_STORE4 : nCertN 1 (
swallow [STORE4];;
let* a := pop64 in
let* x := pop64 in
storeZ 4 a x).
Proof. swallow1_tac. Qed.
Proposition cert_STORE8 : nCertN 1 (
swallow [STORE8];;
let* a := pop64 in
let* x := pop64 in
storeZ 8 a x).
Proof. swallow1_tac. Qed.
Proposition cert_ADD : nCertN 1 (
swallow [ADD];;
let* x := pop64 in
let* y := pop64 in
pushZ (x + y)).
Proof. swallow1_tac. Qed.
Proposition cert_MULT : nCertN 1 (
swallow [MULT];;
let* x := pop64 in
let* y := pop64 in
pushZ (x * y)).
Proof. swallow1_tac. Qed.
Proposition cert_DIV : nCertN 1 (
swallow [DIV];;
let* x := pop64 in
let* y := pop64 in
pushZ (if decide (x = 0 :> Z) then 0 else y / x)).
Proof. swallow1_tac. Qed.
Proposition cert_REM : nCertN 1 (
swallow [REM];;
let* x := pop64 in
let* y := pop64 in
pushZ (if decide (x = 0 :> Z) then 0 else y mod x)).
Proof. swallow1_tac. Qed.
Proposition cert_LT : nCertN 1 (
swallow [LT];;
let* x := pop64 in
let* y := pop64 in
pushZ (if decide (y < x) then -1 else 0)).
Proof. swallow1_tac. Qed.
Proposition cert_AND : nCertN 1 (
swallow [AND];;
let* x := pop64 in
let* y := pop64 in
pushZ (Vector.map2 andb x y : B64)).
Proof. swallow1_tac. Qed.
Proposition cert_OR : nCertN 1 (
swallow [OR];;
let* x := pop64 in
let* y := pop64 in
pushZ (Vector.map2 orb x y : B64)).
Proof. swallow1_tac. Qed.
Proposition cert_NOT : nCertN 1 (
swallow [NOT];;
let* x := pop64 in
pushZ (Vector.map negb x : B64)).
Proof. swallow1_tac. Qed.
Proposition cert_XOR : nCertN 1 (
swallow [XOR];;
let* x := pop64 in
let* y := pop64 in
pushZ (Vector.map2 xorb x y : B64)).
Proof. swallow1_tac. Qed.
Proposition cert_POW2 : nCertN 1 (
swallow [POW2];;
let* x := pop64 in
pushZ (2 ^ x)).
Proof. swallow1_tac. Qed.
(******************)
Proposition cert_READ_FRAME : nCertN 1 (
swallow [READ_FRAME];;
let* i := pop64 in
let* pair := readFrame i in
pushZ (fst pair);;
pushZ (snd pair)).
Proof. swallow1_tac. rewrite readFrame_spec. crush. Qed.
Proposition cert_READ_PIXEL : nCertN 1 (
swallow [READ_PIXEL];;
let* y := pop64 in
let* x := pop64 in
let* c := readPixel x y in
pushZ c).
Proof. swallow1_tac. Qed.
Proposition cert_NEW_FRAME : nCertN 1 (
swallow [NEW_FRAME];;
let* r := pop64 in
let* h := pop64 in
let* w := pop64 in
newFrame w h r).
Proof. swallow1_tac. Qed.
Proposition cert_SET_PIXEL : nCertN 1 (
swallow [SET_PIXEL];;
let* b := pop64 in
let* g := pop64 in
let* r := pop64 in
let* y := pop64 in
let* x := pop64 in
setPixel x y (r, g, b)).
Proof. swallow1_tac. Qed.
Proposition cert_ADD_SAMPLE : nCertN 1 (
swallow [ADD_SAMPLE];;
let* r := pop64 in
let* l := pop64 in
addSample (toB16 l) (toB16 r)).
Proof. swallow1_tac. rewrite addSample_spec. crush. Qed.
Proposition cert_PUT_CHAR : nCertN 1 (
swallow [PUT_CHAR];;
let* c := pop64 in
putChar (toB32 c)).
Proof. swallow1_tac. rewrite putChar_spec. crush. Qed.
Proposition cert_PUT_BYTE : nCertN 1 (
swallow [PUT_BYTE];;
let* b := pop64 in
putByte (toB8 b)).
Proof. swallow1_tac. rewrite putByte_spec. crush. Qed.
(********************)
Ltac cert_operand_tac :=
apply swallow_step_lemma_N; [ lia | ];
simp nSteps;
apply (bind_propr _ _ _); [ | crush ];
apply swallow_lemma;
binary_simpl_tac.
Proposition cert_JZ_FWD o : nCertN 1 (
swallow [JZ_FWD; o];;
let* x := pop64 in
(if (decide (x = 0 :> Z))
then
let* pc := get' PC in
put' PC (offset (toB8 o) pc)
else
ret tt)).
Proof. cert_operand_tac. crush. Qed.
Proposition cert_JZ_BACK o : nCertN 1 (
swallow [JZ_BACK; o];;
let* x := pop64 in
(if (decide (x = 0 :> Z))
then
let* pc := get' PC in
put' PC (offset (-(1 + toB8 o)) pc)
else
ret tt)).
Proof. cert_operand_tac. crush. Qed.
(*************************)
Ltac cert_push_tac :=
cert_operand_tac;
apply pushZ_propr.
Definition zVecToZ {n} (u : vector Z n) : Z :=
Vector.map toB8 u : Bytes n.
Proposition cert_PUSH1 (u: vector Z 1) : nCertN 1 (
swallow (PUSH1 :: u);;
pushZ (zVecToZ u)
).
Proof. cert_push_tac. Qed.
Corollary cert_PUSH1' x : nCertN 1 (
swallow [PUSH1; x];;
pushZ (toB8 x)
).
Proof. apply (cert_PUSH1 [x]). Qed.
Proposition cert_PUSH2 (u: vector Z 2) : nCertN 1 (
swallow (PUSH2 :: u);;
pushZ (zVecToZ u)
).
Proof. cert_push_tac. Qed.
Proposition cert_PUSH4 (u: vector Z 4) : nCertN 1 (
swallow (PUSH4 :: u);;
pushZ (zVecToZ u)
).
Proof. cert_push_tac. Qed.
Proposition cert_PUSH8 (u: vector Z 8) : nCertN 1 (
swallow (PUSH8 :: u);;
pushZ (zVecToZ u)
).
Proof. cert_push_tac. Qed.
(******************************************)
Definition isPow2 (z:Z) : Prop := exists (n: nat), z = 2 ^ n.
Proposition isPow2_spec z : isPow2 z <-> exists (n: nat), z = (2 ^ n)%nat.
Proof.
unfold isPow2.
change 2 with (Z.of_N (N.of_nat 2)).
setoid_rewrite <- N2Z.inj_pow.
setoid_rewrite Nat2N_inj_pow.
reflexivity.
Qed.
Proposition isPow2_spec' z :
isPow2 z <-> 0 < z /\ exists (y: nat), (y < Z.to_nat z)%nat /\ z = 2 ^ y.
Proof.
split.
- intros [n H].
subst z.
split; [ apply pow2_pos | ].
exists n.
split; [ | reflexivity ].
rewrite <- Z_N_nat.
change 2 with (Z.of_N (N.of_nat 2)).
rewrite <- N2Z.inj_pow.
rewrite <- Nat2N_inj_pow.
rewrite N2Z.id.
rewrite Nnat.Nat2N.id.
apply Nat.pow_gt_lin_r.
lia.
- intros [H0 [n [Hn Hz]]].
exists n.
exact Hz.
Qed.
Instance isPow2_decidable z : Decidable (isPow2 z).
Proof.
exact (decidable_transfer (isPow2_spec' z)).
Defined.
(***)
Definition toBytes' n z : vector Z n :=
Vector.map Z.of_N (Vector.map bitsToN (toBytes n z)).
Proposition toBytes'_spec n z i : Vector.nth (toBytes' n z) i = Vector.nth (toBytes n z) i.
Proof.
unfold toBytes'.
now do 2 rewrite (nth_map _ _ i i eq_refl).
Qed.
Proposition toBytes_eq {n x y} (H: cong (n * 8) x y) : toBytes n x = toBytes n y.
Proof.
apply bytesToBits_injective.
setoid_rewrite bytesToBits_bitsToBytes.
apply toBits_cong.
exact H.
Qed.
(**********************)
Proposition nAfter_nonempty n a :
nAfter (S n) a = (nAfter n a) ∪ !{ offset n a }.
Proof.
apply extensionality. intros x.
unfold nAfter, singleton, union.
repeat setoid_rewrite def_spec.
split.
- intros [i [Hi Hx]].
by_lia (i < n \/ i = n) as Hor.
destruct Hor as [Hor|Hor].
+ left. exists i. intuition.
+ right. subst i. exact Hx.
- intros [[i [Hi Hx]] | Hor].
+ exists i. split; [lia | exact Hx].
+ exists n. split; [lia | exact Hor].
Qed.
Corollary disjoint_nAfter_nonempty u n a :
u # nAfter (S n) a <-> u # nAfter n a /\ ~ offset n a ∈ u.
Proof.
rewrite nAfter_nonempty.
rewrite disjoint_union_spec.
setoid_rewrite disjoint_not_member'.
tauto.
Qed.
(********************************)
(** The following holds in the initial smonad, see MonoExtras.v. *)
Parameter err_less_eq :
forall {X} {RX: Rel X} (mx: M X) (Hmx: mx ⊑ err), mx = err.
Parameter RM_transitive :
forall X (RX: Rel X) (RXT: Transitive RX),
Transitive (RM X RX).
Parameter RM_antisymmetric :
forall X (RX: Rel X) (RXT: Antisymmetric X eq RX),
Antisymmetric (M X) eq (RM X RX).
Existing Instance RM_transitive.
Existing Instance RM_antisymmetric.
(**************************)
(* TODO: Move to StateRel.v ? *)
Instance update_MEM_propR : PropR (@update _ _ MEM).
Proof.
crush.
srel_destruct Hst.
unfold rel, state_relation, lens_relation, and_relation.
lens_rewrite.
repeat split; assumption.
Qed.
(******)
Section assume_rel_section.
Ltac assume_rel_tac P H :=
destruct (decide P) as [p|_];
smon_rewrite;
[ apply H
| crush ].
Proposition assume_rel
P {DP: Decidable P} {X} (f g: P -> M X)
(H : forall (p:P), f p ⊑ g p) :
let* p := assume P in f p ⊑
let* p := assume P in g p.
Proof. assume_rel_tac P H. Qed.
Proposition assume_rel'
P {DP: Decidable P} {X} (f: P -> M X) (mx: M X)
(H : forall (p:P), f p ⊑ mx) :
let* p := assume P in f p ⊑ mx.
Proof. assume_rel_tac P H. Qed.
End assume_rel_section.
Proposition confined'
{mix: Mixer State} {mx: M unit} (Cmx: Confined mix mx)
(my: M unit) {Hmy: Neutral mix my} :
my;; mx = mx;; my.
Proof.
enough (my;; mx;; ret tt = mx;; my;; ret tt) as HH.
- revert HH. smon_rewrite. tauto.
- rewrite Cmx.
+ reflexivity.
+ typeclasses eauto.
Qed.
Instance confined_swallow' pc {n} (ops: vector Z n) :
Confined (MEM' (nAfter n pc) * PC) (
put' PC pc;;
swallow ops
).
Proof.
rewrite swallow_spec, lens_put_get.
typeclasses eauto.
Qed.
(************************)
(* Finitely enumerable, equivalent to Coq.Logic.FinFun.Finite. *)
Class SFinite X : Type :=
{
SFinite_n : N;
SFinite_f : forall i, (i < SFinite_n)%N -> X;
SFinite_p : forall x, exists i Hi, SFinite_f i Hi = x;
}.
#[refine]
Instance bits_sfinite n : SFinite (Bits n) :=
{
SFinite_n := 2 ^ n;
SFinite_f i _ := toBits n i;
}.
Proof.
intros x. exists x, (bitsToN_bound x).
apply fromBits_toBits'.
Qed.
(****)
Instance sfinite_decidable_all
{X} {SF: SFinite X}
(P: X -> Prop) {DP: forall x, Decidable (P x)} :
Decidable (forall x, P x).
Proof.
enough (
(forall x, P x) <->
(forall i (Hi: (i < SFinite_n)%N),
P (SFinite_f i Hi))) as H;
[ unshelve eapply (decidable_transfer H) | ].
split.
- intros H i Hi. apply H.
- intros H x.
destruct (SFinite_p x) as [i [Hi Hp]].
specialize (H i Hi).
rewrite Hp in H.
exact H.
Qed.
Definition isWiped (u: DSet Addr) (m: Memory) :=
forall a (Hau: a ∈ u), exists (Ha: available a), m a Ha = None.
(********)
(** ** Mark memory as undefined *)
Definition wipe (u: DSet Addr) : M unit :=
assume' (u ⊆ available);;
put' (MEM' u) (fun _ _ _ => None).
Definition wipe_spec := unfolded_eq (@wipe).
Proposition wipe_empty : wipe ∅ = ret tt.
Proof.
unfold wipe.
rewrite put_empty.
destruct (decide _) as [H|H].
- now rewrite ret_bind.
- contradict H. unfold subset. intros a Ha. contradict Ha.
Qed.
Proposition wipe_mono u v (H: u ⊆ v) : wipe v ⊑ wipe u.
Proof.
unfold wipe. do 2 rewrite put_spec. cbn.
unfold restr. crush; [ | contradict HR; now transitivity v ].
apply update_MEM_propR; [ exact Hst | ].
crush.
specialize (H a). cbv. cbv in H.
destruct (v a) eqn:Hva; [ exact I | ].
destruct (u a) eqn:Hua.
- exfalso. exact (H I).
- srel_destruct Hst.
apply Hst_mem.
Qed.
Corollary wipe_less u : wipe u ⊑ ret tt.
Proof.
(* This is also provable without assuming RM_transitive. *)
transitivity (wipe ∅).
- apply wipe_mono. apply empty_initial.
- rewrite wipe_empty. crush.
Qed.
Instance confined_wipe u : Confined (MEM' u) (wipe u).
Proof.
typeclasses eauto.
Qed.
#[global] Opaque wipe.
(***********)
Lemma wipe_swallow_reordering'
u {n} (ops: vector Z n) pc (Hdis: u # nAfter n pc) :
put' PC pc;;
wipe u;;
swallow ops =
put' PC pc;;
swallow ops;;
wipe u.
Proof.
setoid_rewrite confined_wipe; [ | typeclasses eauto .. ].
setoid_rewrite <- bind_assoc at 2.
setoid_rewrite (confined' (confined_wipe u)); [ reflexivity | ].
unshelve eapply (confined_neutral (m':= MEM' (nAfter n pc) * PC ));
try typeclasses eauto.
apply independent_backward.
setoid_rewrite prodLens_prodMixer.
apply independent_prod; [ | typeclasses eauto ].
(* TODO: Why doesn't 'typeclasses eauto' solve this as well? *)
now apply independent_backward,
composite_independent_r,
separate_independent.
Qed.
Corollary wipe_swallow_reordering u {n} (ops: vector Z n) :
let* pc := get' PC in
assume (u # nAfter n pc);;
wipe u;;
swallow ops =
let* pc := get' PC in
assume (u # nAfter n pc);;
swallow ops;;
wipe u.
Proof.