-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImp.v
More file actions
1471 lines (1313 loc) · 38.6 KB
/
Copy pathImp.v
File metadata and controls
1471 lines (1313 loc) · 38.6 KB
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
(** * Imp: Simple Imperative Programs *)
Set Warnings "-notation-overridden,-parsing".
From Coq Require Import Bool.Bool.
From Coq Require Import Init.Nat.
From Coq Require Import Arith.Arith.
From Coq Require Import Arith.EqNat.
From Coq Require Import Lia.
From Coq Require Import Lists.List.
From Coq Require Import Strings.String.
Import ListNotations.
From QuickChick Require Import QuickChick.
Import QcNotation QcDefaultNotation.
Require Export ExtLib.Structures.Monads.
Export MonadNotation. Open Scope monad_scope.
Open Scope qc_scope.
Require Import
Coq.FSets.FMapList
Coq.Structures.OrderedTypeEx
Coq.FSets.FMapFacts.
Module Import M := FMapList.Make(String_as_OT).
(* Instantiate a map type using strings as keys *)
Module P := WProperties_fun String_as_OT M.
Module F := P.F.
(* Optionals are moands *)
Instance option_monad : Monad option :=
{
ret X v := @Some X v;
bind T U mt f :=
match mt with
| Some v => f v
| None => None
end
}.
(* some list combinators *)
Fixpoint foldr {X B}
(f : X -> B -> B) (b : B) (l : list X) : B :=
match l with
| [] => b
| (x::l) => (f x (foldr f b l))
end.
Definition andmap {X} (p : X -> bool) :=
foldr (fun x b => (p x) && b) true.
Definition ormap {X} (p : X -> bool) :=
foldr (fun x b => (p x) || b) false.
Fixpoint zip {X Y} (l1 : list X) (l2 : list Y)
: list (X * Y) :=
match l1, l2 with
| _, [] => []
| [], _ => []
| (x::xs), (y::ys) => (x,y) :: (zip xs ys)
end.
Open Scope string_scope.
Instance string_arb : Gen string :=
{
arbitrary := ret "random"
}.
Instance shrink_str : Shrink string :=
{
shrink s := []
}.
Close Scope string_scope.
(* ================================================================= *)
(* Labels, for controlling priviledge *)
Inductive label : Type :=
| Secret
| Public.
Derive (Arbitrary, Show) for label.
Instance eq_dec_label (l1 l2 : label) : Dec (l1 = l2).
Proof. dec_eq. Defined.
(* Reflection setup *)
Definition label_eqb l1 l2 :=
match l1, l2 with
| Secret, Secret => true
| Public, Public => true
| _, _ => false
end.
Definition label_eqb_eq : forall l1 l2,
label_eqb l1 l2 = true -> l1 = l2.
Proof.
intros.
destruct l1; destruct l2; auto; inversion H.
Qed.
Definition label_eqb_neq : forall l1 l2,
label_eqb l1 l2 = false -> l1 <> l2.
Proof.
intros.
destruct l1; destruct l2; auto; congruence.
Qed.
Definition merge (l1 l2 : label) : label :=
match l1, l2 with
| Public, Public => Public
| _, _ => Secret
end.
(* Conjecture merge_secret : forall l, merge l Secret = Secret. *)
(* QuickChick merge_secret. *)
Theorem merge_secret : forall l1,
merge l1 Secret = Secret.
Proof.
intros. destruct l1; auto.
Qed.
Theorem merge_public : forall l1,
merge l1 Public = l1.
Proof.
intros.
destruct l1; auto.
Qed.
Theorem merge_commutative : forall l1 l2,
merge l1 l2 = merge l2 l1.
Proof.
destruct l1; destruct l2; auto.
Qed.
(* This class drives the entire implementation of IFC-Imp *)
Class Distinguishable X :=
{
indist : X -> X -> Prop
}.
Notation "x1 == x2" := (indist x1 x2)
(at level 40).
Definition value (X:Type) : Type := (X * label).
Instance value_indist {X} : Distinguishable (value X) :=
{
indist x1 x2 :=
match x1, x2 with
| (_, Secret), (_, Secret) => True
| (n1, Public), (n2, Public) => n1 = n2
| _, _ => False
end
}.
Definition nat_value : Type := value nat.
Instance dec_value_indist (v1 v2 : nat_value) :
Dec (v1 == v2).
Proof.
constructor. unfold ssrbool.decidable.
destruct v1. destruct v2.
destruct l; destruct l0;
try (right; auto; fail).
- left. simpl. tauto.
- destruct (Nat.eqb n n0) eqn:Heq.
+ apply Nat.eqb_eq in Heq.
subst. left. simpl. auto.
+ apply Nat.eqb_neq in Heq. right. simpl. auto.
Qed.
Definition bool_value : Type := value bool.
Instance bool_value_indist (v1 v2 : bool_value) :
Dec (v1 == v2).
Proof.
constructor. unfold ssrbool.decidable.
destruct v1. destruct v2.
destruct l; destruct l0;
try (right; auto; fail).
- left. simpl. tauto.
- destruct (Bool.eqb b b0) eqn:Heq.
+ assert (b = b0).
apply Bool.eqb_eq. unfold Is_true. rewrite Heq. tauto.
subst. left. simpl. auto.
+ assert (b <> b0).
apply Bool.eqb_false_iff. assumption.
right. simpl. assumption.
Qed.
Definition state := M.t nat_value.
Close Scope string_scope.
Instance indist_state : Distinguishable state :=
{
indist s1 s2 := forall (x1 : string) (v1 v2: nat_value),
(MapsTo x1 v1 s1 ->
(exists (v2' : nat_value),
MapsTo x1 v2' s2 /\ v1 == v2'))
/\
(MapsTo x1 v2 s2 ->
exists (v1' : nat_value),
MapsTo x1 v1' s1 /\ v1' == v2)
}.
Theorem indist_comm : forall (st1 st2 : state),
st1 == st2 -> st2 == st1.
Proof.
intros.
unfold "==". simpl.
intros.
split.
- intros.
simpl "==" in H. simpl in H.
destruct H with
(x1 := x1)
(v1 := v2)
(v2 := v1).
destruct H2 as [v1']. assumption.
clear H1. clear H.
destruct H2.
exists v1'. split.
+ assumption.
+ destruct v1'. destruct v1.
destruct l; destruct l0;
try assumption; auto.
- intros.
simpl "==" in H.
destruct H with
(x1 := x1)
(v1 := v2)
(v2 := v1).
clear H2. clear H.
destruct H1 as [v2']. assumption.
destruct H.
exists v2'. split.
+ assumption.
+ destruct v2'. destruct v2.
destruct l; destruct l0;
try assumption; auto.
Qed.
Theorem indist_trans : forall (st1 st2 st3 : state),
st1 == st2 ->
st2 == st3 ->
st1 == st3.
Proof.
intros.
simpl "==". intros.
split.
- intros.
simpl "==" in *.
specialize (H x1 v1 v2).
destruct H. destruct H as [v2']. assumption.
destruct H. clear H2.
specialize (H0 x1 v2' v2).
destruct H0. destruct H0 as [ v2'0 ]. assumption.
destruct H0. exists v2'0. split.
+ assumption.
+ destruct v1. destruct v2'0. destruct v2.
destruct v2'.
destruct l; destruct l0;
destruct l1; destruct l2; try assumption;
try tauto; try (exfalso; assumption);
subst; auto.
- intros.
simpl "==" in *.
specialize (H0 x1 v1 v2).
destruct H0. destruct H2 as [v1']. assumption.
destruct H2.
specialize (H x1 v1 v1').
destruct H. destruct H4 as [v2']. assumption.
destruct H4. exists v2'. split.
+ assumption.
+ destruct v2'. destruct v2. destruct v1'. destruct v1.
destruct l; destruct l0; destruct l1; destruct l2;
try tauto;
try (exfalso; assumption);
subst; auto.
Qed.
Inductive aexp : Type :=
| ANum (n : nat)
| AId (x : string)
| APlus (a1 a2 : aexp)
| AMinus (a1 a2 : aexp)
| AMult (a1 a2 : aexp).
Instance aexp_dec_eq (a1 a2 : aexp) :
Dec (a1 = a2).
Proof. dec_eq. Defined.
Derive (Show, Shrink, Arbitrary) for aexp.
Definition W : string := "W".
Definition X : string := "X".
Definition Y : string := "Y".
Definition Z : string := "Z".
Inductive bexp : Type :=
| BTrue
| BFalse
| BEq (a1 a2 : aexp)
| BLe (a1 a2 : aexp)
| BNot (b : bexp)
| BAnd (b1 b2 : bexp).
Instance bexp_eq_dec (b1 b2 : bexp) :
Dec (b1 = b2).
Proof. dec_eq. Defined.
Derive (Show, Shrink, Arbitrary) for bexp.
Coercion AId : string >-> aexp.
Coercion ANum : nat >-> aexp.
Declare Custom Entry com.
Declare Scope com_scope.
Notation "<{ e }>" := e (at level 0, e custom com at level 99) : com_scope.
Notation "( x )" := x (in custom com, x at level 99) : com_scope.
Notation "x" := x (in custom com at level 0, x constr at level 0) : com_scope.
Notation "f x .. y" := (.. (f x) .. y)
(in custom com at level 0, only parsing,
f constr at level 0, x constr at level 9,
y constr at level 9) : com_scope.
Notation "x + y" := (APlus x y) (in custom com at level 50, left associativity).
Notation "x - y" := (AMinus x y) (in custom com at level 50, left associativity).
Notation "x * y" := (AMult x y) (in custom com at level 40, left associativity).
Notation "'true'" := true (at level 1).
Notation "'true'" := BTrue (in custom com at level 0).
Notation "'false'" := false (at level 1).
Notation "'false'" := BFalse (in custom com at level 0).
Notation "x <= y" := (BLe x y) (in custom com at level 70, no associativity).
Notation "x = y" := (BEq x y) (in custom com at level 70, no associativity).
Notation "x && y" := (BAnd x y) (in custom com at level 80, left associativity).
Notation "'~' b" := (BNot b) (in custom com at level 75, right associativity).
Open Scope com_scope.
Definition example_aexp : aexp := <{ 3 + (X * 2) }>.
Definition example_bexp : bexp := <{ true && ~(X <= 4) }>.
Close Scope string_scope.
(* ================================================================= *)
(** ** Evaluation *)
Inductive aevalR : state -> aexp -> nat_value -> Prop :=
| A_Value : forall state (n : nat),
aevalR state (ANum n) (n, Public)
| A_Id : forall state x v,
MapsTo x v state ->
aevalR state (AId x) v
| A_EPlus : forall state a1 a2 n1 n2 l1 l2,
aevalR state a1 (n1, l1) ->
aevalR state a2 (n2, l2) ->
aevalR state <{a1 + a2}> (n1 + n2, merge l1 l2)
| A_EMinus : forall state a1 a2 n1 n2 l1 l2,
aevalR state a1 (n1, l1) ->
aevalR state a2 (n2, l2) ->
aevalR state <{a1 - a2}> (n1 - n2, merge l1 l2)
| A_EMult : forall state a1 a2 n1 n2 l1 l2,
aevalR state a1 (n1, l1) ->
aevalR state a2 (n2, l2) ->
aevalR state <{a1 * a2}> (n1 * n2, merge l1 l2).
Fixpoint aeval (fuel : nat) (st : state) (a : aexp) : option nat_value :=
match fuel with
| 0 => None
| S n' => match a with
| (ANum n) => Some (n, Public)
| (AId x) => find x st
| (APlus a1 a2) =>
'(n1, l1) <- aeval n' st a1;;
'(n2, l2) <- aeval n' st a2;;
ret (n1 + n2, merge l1 l2)
| (AMinus a1 a2) =>
'(n1, l1) <- aeval n' st a1;;
'(n2, l2) <- aeval n' st a2;;
ret (n1 - n2, merge l1 l2)
| (AMult a1 a2) =>
'(n1, l1) <- aeval n' st a1;;
'(n2, l2) <- aeval n' st a2;;
ret (n1 * n2, merge l1 l2)
end
end.
Theorem aeval_implies_aevalR : forall fuel st a v,
aeval fuel st a = Some v ->
aevalR st a v.
Proof.
intros.
generalize dependent st.
generalize dependent fuel.
generalize dependent v.
induction a; intros;
try destruct fuel; try (inversion H).
- apply A_Value.
- apply A_Id. apply find_2. assumption.
- destruct (aeval fuel st a1) eqn:Heq1.
+ destruct n as [n1 l2].
destruct (aeval fuel st a2) eqn:Heq2.
* destruct n as [n2 l3].
injection H1. intros. subst.
apply A_EPlus.
-- eapply IHa1. eauto.
-- eapply IHa2. eauto.
* congruence.
+ congruence.
- destruct (aeval fuel st a1) eqn:Heq1.
+ destruct n as [n1 l2].
destruct (aeval fuel st a2) eqn:Heq2.
* destruct n as [n2 l3].
injection H1. intros. subst.
apply A_EMinus.
-- eapply IHa1. eauto.
-- eapply IHa2. eauto.
* congruence.
+ congruence.
- destruct (aeval fuel st a1) eqn:Heq1.
+ destruct n as [n1 l2].
destruct (aeval fuel st a2) eqn:Heq2.
* destruct n as [n2 l3].
injection H1. intros. subst.
apply A_EMult.
-- eapply IHa1. eauto.
-- eapply IHa2. eauto.
* congruence.
+ congruence.
Qed.
Inductive bevalR : state -> bexp -> bool_value -> Prop :=
| E_True : forall st,
bevalR st <{true}> (true, Public)
| E_False : forall st,
bevalR st <{false}> (false, Public)
| E_Eq : forall st a1 a2 n1 n2 l1 l2,
aevalR st a1 (n1, l1) ->
aevalR st a2 (n2, l2) ->
bevalR st <{a1 = a2}> (n1 =? n2, merge l1 l2)
| E_Lt : forall st a1 a2 n1 n2 l1 l2,
aevalR st a1 (n1, l1) ->
aevalR st a2 (n2, l2) ->
bevalR st <{a1 <= a2}> (n1 <=? n2, merge l1 l2)
| E_not__true : forall st b l1,
bevalR st b (true, l1) ->
bevalR st <{~ b}> (false, l1)
| E_not__false : forall st b l1,
bevalR st b (false, l1) ->
bevalR st <{~ b}> (true, l1)
| E_and : forall st b1 b2 bool1 bool2 l1 l2,
bevalR st b1 (bool1, l1) ->
bevalR st b2 (bool2, l2) ->
bevalR st <{b1 && b2}> (bool1 && bool2, merge l1 l2).
Definition empty_st : state := (M.empty nat_value).
Notation "x '!->' v" := (add x v (M.empty nat_value))
(at level 100).
Example aexp0 :
aevalR (X !-> (5, Public)) <{ X * 2 }> (10, Public).
Proof.
replace (10, Public) with (5 * 2, merge Public Public).
apply A_EMult.
apply A_Id.
apply find_2. auto.
apply A_Value.
auto.
Qed.
Example aexp1 :
aevalR (X !-> (5, Public)) <{ (3 + (X * 2))}> (13, Public).
Proof.
replace (13, Public) with (3 + 10, merge Public Public).
apply A_EPlus.
apply A_Value.
replace (10, Public) with (5 * 2, merge Public Public).
apply A_EMult.
apply A_Id.
apply find_2. auto.
apply A_Value.
auto.
auto.
Qed.
Example bexp1 :
bevalR (X !-> (5, Secret)) <{ true && ~(X <= 4)}>
(true, Secret).
Proof.
replace (true, Secret) with
(true && true, merge Public Secret).
apply E_and.
apply E_True.
apply E_not__false.
replace (false, Secret) with
(5 <=? 4, merge Secret Public).
apply E_Lt.
apply A_Id. apply find_2. auto.
apply A_Value.
auto.
auto.
Qed.
(* ################################################################# *)
(** * Commands *)
(* ================================================================= *)
(** ** Syntax *)
Inductive com : Type :=
| CSkip
| CAss (x : string) (a : aexp) (l : label)
| CSeq (c1 c2 : com)
| CIf (b : bexp) (c1 c2 : com).
(* | CWhile (b : bexp) (c : com). *)
Fixpoint com_indist_f c1 c2 :=
match c1, c2 with
| CSkip, CSkip => True
| CAss x1 a1 Public, CAss x2 a2 Public =>
x1 = x2 /\ a1 = a2
| CAss x1 a1 Secret, CAss x2 a2 Secret => x1 = x2
| CSeq c1 c2, CSeq c1' c2' =>
com_indist_f c1 c1' /\ com_indist_f c2 c2'
| CIf b c1 c2, CIf b' c1' c2' =>
b = b' /\ com_indist_f c1 c1' /\ com_indist_f c2 c2'
| _, _ => False
end.
Instance com_indist : Distinguishable com :=
{
indist := com_indist_f
}.
Notation "'skip'" :=
CSkip (in custom com at level 0) : com_scope.
Notation "x ':=' y ',' l" :=
(CAss x y l)
(in custom com at level 0,
x constr at level 0,
y at level 85,
no associativity) : com_scope.
Notation "x ; y" :=
(CSeq x y)
(in custom com at level 90, right associativity) : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" :=
(CIf x y z)
(in custom com at level 89, x at level 99,
y at level 99, z at level 99) : com_scope.
(* Notation "'while' x 'do' y 'end'" := *)
(* (CWhile x y) *)
(* (in custom com at level 89, x at level 99, y at level 99) : com_scope. *)
Reserved Notation
"st '=[' c ']=>' st'"
(at level 40, c custom com at level 99,
st constr, st' constr at next level).
Definition sec_context : Type := (state * label).
Instance sec_context_indist : Distinguishable sec_context :=
{
indist c1 c2 :=
match c1, c2 with
| (s1, l1), (s2, l2) => l2 = l1 /\ indist s1 s2
end
}.
Definition merge_list (l : list label) : label :=
foldr (fun l1 l2 => merge l1 l2) Public l.
Definition writing_valid (x : string) (st : state) (l : label)
: Prop :=
(exists (n' : nat) (l' : label),
l = l' /\
MapsTo x (n', l') st).
Inductive ceval : com -> sec_context -> sec_context -> Prop :=
| E_Skip : forall ctxt,
ctxt=[ skip ]=> ctxt
| E_Ass : forall (st : M.t nat_value) a n x l l__v l__s,
aevalR st a (n, l__v) ->
In x st ->
writing_valid x st (merge l__v l) ->
(st,l) =[ x := a , l__s ]=>
(add x (n, merge_list [l__v;l__s; l]) st, l)
| E_Seq : forall c1 c2 ctxt ctxt' ctxt'',
ctxt =[ c1 ]=> ctxt' ->
ctxt' =[ c2 ]=> ctxt'' ->
ctxt =[ c1 ; c2 ]=> ctxt''
| E_IfTrue : forall st st' b l__guard l__begin c1 c2,
bevalR st b (true, l__guard) ->
(st, merge l__begin l__guard) =[ c1 ]=>
(st', l__begin ) ->
(st,l__begin) =[ if b then c1 else c2 end]=> (st', l__begin)
| E_IfFalse : forall st st' l__guard l__begin b c1 c2,
bevalR st b (false, l__guard) ->
(st, merge l__begin l__guard) =[ c2 ]=>
(st', merge l__begin l__guard) ->
(st, l__begin) =[ if b then c1 else c2 end]=> (st', l__begin)
where "st =[ c ]=> st'" := (ceval c st st').
(* | E_WhileFalse : forall b st c, *)
(* beval st b = false -> *)
(* st =[ while b do c end ]=> st *)
(* | E_WhileTrue : forall st st' st'' b c, *)
(* beval st b = true -> *)
(* st =[ c ]=> st' -> *)
(* st' =[ while b do c end ]=> st'' -> *)
(* st =[ while b do c end ]=> st'' *)
Definition default_ctxt (xs : list (string * label)) :
sec_context :=
let st :=
foldr (fun '(x, l) st => add x (0, l) st)
(M.empty nat_value) xs in
(st, Public).
Definition v_eq (v1 v2 : nat_value) : bool :=
match v1, v2 with
| (n1, l1), (n2, l2) => (l1 = l2 ?) && (n1 =? n2)
end.
Definition build_st (entries : list (string * (nat * label)))
: M.t nat_value :=
foldr
(fun '(key, value) m' => add key value m')
(empty nat_value)
entries.
Definition ceval_example1state : sec_context :=
default_ctxt [(X, Public); (Y, Public); (Z, Public)].
Example ceval_example1:
ceval_example1state =[
X := 2, Public;
if (X <= 1)
then Y := 3, Public
else Z := 4, Public
end
]=> (build_st [(Z, (4, Public));(X, (2, Public)); (Y, (0, Public))], Public).
Proof.
remember (add X (0, Public) (add Y (0, Public) (Z !-> (0, Public)))).
eapply E_Seq.
- eapply E_Ass.
+ simpl. rewrite <- Heqt0.
exact (A_Value t0 2).
+ simpl. apply mem_2. auto.
+ simpl. rewrite <- Heqt0.
unfold writing_valid.
Admitted.
Lemma public_indist : forall {X} (n : X),
(n, Public) == (n, Public).
Proof.
intros.
unfold indist. simpl.
reflexivity.
Qed.
Lemma secret_indist : forall {X} (n1 n2 : X),
(n1, Secret) == (n2, Secret).
Proof.
intros.
unfold indist. simpl. tauto.
Qed.
Lemma diff_dist : forall {X} (n1 n2 : X),
not ((n1, Secret) == (n2, Public)).
Proof.
intros. unfold not. intros.
unfold indist in H. simpl in H.
auto.
Qed.
Lemma indist_commut :
forall {X:Type} `{forall (x1 x2 : X), Dec (x1 = x2)}
(n1 n2 : X) (l1 l2 : label),
(n1, l1) == (n2, l2) ->
(n2, l2) == (n1, l1).
Proof.
intros.
unfold indist in H. simpl in H0.
destruct l1; destruct l2.
- apply secret_indist.
- apply H0.
- apply H0.
- specialize (H n1 n2). destruct H.
destruct dec.
+ rewrite H0. apply public_indist.
+ congruence.
Qed.
Lemma MapsTo_eq :
forall (st : state) (x : string) (v1 v2 : nat_value),
MapsTo x v1 st ->
MapsTo x v2 st ->
v1 = v2.
Proof.
intros.
apply find_1 in H.
apply find_1 in H0. rewrite H0 in H. injection H.
intros. auto.
Qed.
Lemma map_add_eq :
forall (st : state) (x : string) (v1 v2 : nat_value),
MapsTo x v1 (add x v2 st) ->
v1 = v2.
Proof.
intros.
assert (MapsTo x v2 (add x v2 st)).
{
apply add_1. reflexivity.
}
apply MapsTo_eq with (st := add x v2 st) (x := x);
assumption.
Qed.
Lemma indist_implies_indist :
forall (st1 st2 : state) (x : string) (v1 v2 : nat_value),
st1 == st2 ->
MapsTo x v1 st1 ->
MapsTo x v2 st2 ->
v1 == v2.
Proof.
intros. simpl "==" in H.
specialize (H x v1 v2).
destruct H. destruct H as [v2']. assumption.
destruct H.
replace v2' with v2 in *.
- destruct v1. destruct v2.
destruct l; destruct l0;
try tauto;
try (exfalso; assumption);
subst; auto.
- apply MapsTo_eq with (st := st2) (x := x);
assumption.
Qed.
Theorem indist_split : forall {X} (n1 n2 : X) (l1 l2 : label),
(n1, l1) == (n2, l2) ->
(l1 = Secret /\ l2 = Secret) \/
(l1 = Public /\ l2 = Public /\ n1 = n2).
Proof.
intros.
destruct l1; destruct l2;
auto;
contradict H.
Qed.
Theorem aexp_EENI :
forall (st1 st2 : state) (a : aexp) (v1 v2 : nat_value),
st1 == st2 ->
aevalR st1 a v1 ->
aevalR st2 a v2 ->
v1 == v2.
Proof.
intros.
generalize dependent v1.
generalize dependent v2.
induction a; intros;
inversion H0; inversion H1; subst;
try (
assert ((n1, l1) == (n0, l0)); auto;
assert ((n2, l2) == (n3, l3)); auto;
apply indist_split in H2;
apply indist_split in H3;
destruct H2; destruct H3;
destruct H2 as [Hl1 Hl0];
destruct H3 as [Hl2 Hl3];
subst; simpl; try tauto;
destruct Hl0; subst; simpl; try tauto;
destruct Hl3; subst; simpl; tauto).
- apply public_indist.
- eapply indist_implies_indist.
apply H. apply H4. apply H8.
Qed.
Theorem bexp_EENI :
forall (st1 st2 : state) (b : bexp) (v1 v2 : bool_value),
st1 == st2 ->
bevalR st1 b v1 ->
bevalR st2 b v2 ->
v1 == v2.
Proof.
intros.
generalize dependent v1.
generalize dependent v2.
induction b; intros;
inversion H0; inversion H1; subst;
try (apply public_indist).
-
assert ((n1, l1) == (n0, l0)).
apply aexp_EENI with
(st1 := st1)
(st2 := st2)
(a := a1); assumption.
assert ((n2, l2) == (n3, l3)).
apply aexp_EENI with
(st1 := st1)
(st2 := st2)
(a := a2); assumption.
apply indist_split in H2.
apply indist_split in H3.
destruct H2; destruct H3;
destruct H2; destruct H3; subst; simpl; try tauto;
destruct H4; subst; simpl; try tauto;
destruct H6; subst; auto; tauto.
- assert ((n1, l1) == (n0, l0)).
apply aexp_EENI with
(st1 := st1)
(st2 := st2)
(a := a1); assumption.
assert ((n2, l2) == (n3, l3)).
apply aexp_EENI with
(st1 := st1)
(st2 := st2)
(a := a2); assumption.
apply indist_split in H2.
apply indist_split in H3.
destruct H2; destruct H3;
destruct H2; destruct H3; subst; simpl; try tauto;
destruct H4; subst; simpl; try tauto;
destruct H6; subst; simpl; tauto.
- assert ((true, l1) == (true, l0)).
apply IHb; assumption.
apply indist_split in H2.
destruct H2.
+ destruct H2. subst. apply secret_indist.
+ destruct H2. destruct H3. subst. apply public_indist.
- assert ((true, l1) == (false, l0)).
apply IHb; assumption.
apply indist_split in H2.
destruct H2.
+ destruct H2. subst. apply secret_indist.
+ destruct H2 as [_ [_ Contra]]. congruence.
- assert ((false, l1) == (true, l0)).
apply IHb; assumption.
apply indist_split in H2.
destruct H2.
+ destruct H2. subst. apply secret_indist.
+ destruct H2 as [_ [_ Contra]]. congruence.
- assert ((false, l1) == (false, l0)).
apply IHb; assumption.
apply indist_split in H2.
destruct H2.
+ destruct H2. subst. apply secret_indist.
+ destruct H2 as [H21 [H22 H23]].
subst. simpl. tauto.
- assert ((bool1, l1) == (bool0, l0)).
apply IHb1; assumption.
assert ((bool2, l2) == (bool3, l3)).
apply IHb2; assumption.
apply indist_split in H2.
apply indist_split in H3.
destruct H2; destruct H3;
destruct H2; destruct H3; subst; simpl; try tauto;
destruct H4; subst; simpl; try tauto.
destruct H6. subst. simpl. tauto.
Qed.
Theorem secret_doms : forall (ls : list label),
List.In Secret ls ->
merge_list ls = Secret.
Proof.
intros.
unfold merge_list.
induction ls as [| l ls' IH].
- inversion H.
- destruct l.
+ auto.
+ assert (List.In Secret ls').
{
inversion H.
+ congruence.
+ assumption.
}
simpl.
assert
((foldr (fun l1 l2 : label => merge l1 l2) Public ls')
= Secret).
apply IH. assumption.
rewrite H1. reflexivity.
Qed.
Theorem map_add_congruence : forall (l1 l2 : label) (n1 n2 : nat)
(st : state) (x : string),
l1 <> l2 ->
MapsTo x (n1, l1) (add x (n2, l2) st) ->
False.
Proof.
intros.
assert (MapsTo x (n2, l2) (add x (n2, l2) st)).
{
apply add_1. auto.
}
assert ((n1, l1) = (n2, l2)).
{
apply MapsTo_eq with
(st := (add x (n2, l2) st))
(x := x); auto.
}
injection H2. intros. congruence.
Qed.
(* the no wrote down rule is sound*)
Theorem add_secret :
forall (x : string) (n : nat) (st : state),
writing_valid x st Secret ->
st == (add x (n, Secret) st).
Proof.
intros.
unfold writing_valid in H.
destruct H as [n']. destruct H as [l']. destruct H.
subst. simpl "==". intros.
destruct (String.eqb x x1) eqn:Heq.
- apply String.eqb_eq in Heq.
split.
+ intros. exists (n, Secret).
split; subst.
* apply add_1. auto.
* destruct v1. destruct l.
-- tauto.
-- assert ((n', Secret) = (n0, Public)).
{
apply MapsTo_eq with (st := st) (x := x1);
assumption.
}
injection H1. intros. congruence.
+ intros. subst.
exists (n', Secret). destruct v1. destruct v2.
split.
* assumption.
* destruct l; destruct l0;
try tauto; try (exfalso; assumption);
apply map_add_congruence with
(l1 := Public)
(l2 := Secret)
(n1 := n1)
(n2 := n)
(st := st)
(x := x1); auto; discriminate.
- apply String.eqb_neq in Heq.
split.
+ intros. exists v1. split.
* apply add_2; assumption.
* destruct v1. destruct l; auto.
+ intros. exists v2. split.
* apply add_3 with (x := x) (e' := (n, Secret)); auto.
* destruct v2. destruct l; auto.
Qed.
Theorem add_indist :
forall (x : string) (v1 v2 : nat_value) (st1 st2 : state),
st1 == st2 ->
v1 == v2 ->
(add x v1 st1) == (add x v2 st2).
Proof.
intros x v1 v2 st1 st2 Hstindist Hvindist.