file_name
stringlengths 5
52
| name
stringlengths 4
95
| original_source_type
stringlengths 0
23k
| source_type
stringlengths 9
23k
| source_definition
stringlengths 9
57.9k
| source
dict | source_range
dict | file_context
stringlengths 0
721k
| dependencies
dict | opens_and_abbrevs
listlengths 2
94
| vconfig
dict | interleaved
bool 1
class | verbose_type
stringlengths 1
7.42k
| effect
stringclasses 118
values | effect_flags
sequencelengths 0
2
| mutual_with
sequencelengths 0
11
| ideal_premises
sequencelengths 0
236
| proof_features
sequencelengths 0
1
| is_simple_lemma
bool 2
classes | is_div
bool 2
classes | is_proof
bool 2
classes | is_simply_typed
bool 2
classes | is_type
bool 2
classes | partial_definition
stringlengths 5
3.99k
| completed_definiton
stringlengths 1
1.63M
| isa_cross_project_example
bool 1
class |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Sec2.HIFC.fst | Sec2.HIFC.triple | val triple : Type0 | let triple = label & label & flows | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 439,
"start_col": 0,
"start_line": 439
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple3",
"Sec2.HIFC.label",
"Sec2.HIFC.flows"
] | [] | false | false | false | true | true | let triple =
| label & label & flows | false |
|
Hacl.FFDHE.fst | Hacl.FFDHE.ffdhe_shared_secret | val ffdhe_shared_secret: a:S.ffdhe_alg ->
DH.ffdhe_shared_secret_st t_limbs a (DH.ffdhe_len a) (ke a) | val ffdhe_shared_secret: a:S.ffdhe_alg ->
DH.ffdhe_shared_secret_st t_limbs a (DH.ffdhe_len a) (ke a) | let ffdhe_shared_secret a sk pk ss =
let len = DH.ffdhe_len a in
DH.ffdhe_shared_secret a len (ke a) (ffdhe_shared_secret_precomp a) (ffdhe_precomp_p a) sk pk ss | {
"file_name": "code/ffdhe/Hacl.FFDHE.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 98,
"end_line": 69,
"start_col": 0,
"start_line": 67
} | module Hacl.FFDHE
open Lib.IntTypes
module S = Spec.FFDHE
module DH = Hacl.Impl.FFDHE
module BD = Hacl.Bignum.Definitions
module BE = Hacl.Bignum.Exponentiation
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
inline_for_extraction noextract
let t_limbs = U64
inline_for_extraction noextract
let ke (a:S.ffdhe_alg) =
BE.mk_runtime_exp #t_limbs (BD.blocks (DH.ffdhe_len a) (size (numbytes t_limbs)))
private
[@CInline]
let ffdhe_precomp_p (a:S.ffdhe_alg) : DH.ffdhe_precomp_p_st t_limbs a (DH.ffdhe_len a) (ke a) =
DH.ffdhe_precomp_p a (DH.ffdhe_len a) (ke a)
private
[@CInline]
let ffdhe_check_pk (a:S.ffdhe_alg) : DH.ffdhe_check_pk_st t_limbs a (DH.ffdhe_len a) =
DH.ffdhe_check_pk #t_limbs a (DH.ffdhe_len a)
private
[@CInline]
let ffdhe_compute_exp (a:S.ffdhe_alg) : DH.ffdhe_compute_exp_st t_limbs a (DH.ffdhe_len a) (ke a) =
DH.ffdhe_compute_exp a (DH.ffdhe_len a) (ke a)
let ffdhe_len (a:S.ffdhe_alg) : DH.size_pos = DH.ffdhe_len a
val new_ffdhe_precomp_p: a:S.ffdhe_alg ->
DH.new_ffdhe_precomp_p_st t_limbs a (ffdhe_len a) (ke a)
let new_ffdhe_precomp_p a =
DH.new_ffdhe_precomp_p a (DH.ffdhe_len a) (ke a) (ffdhe_precomp_p a)
val ffdhe_secret_to_public_precomp: a:S.ffdhe_alg ->
DH.ffdhe_secret_to_public_precomp_st t_limbs a (DH.ffdhe_len a) (ke a)
let ffdhe_secret_to_public_precomp a p_r2_n sk pk =
let len = DH.ffdhe_len a in
DH.ffdhe_secret_to_public_precomp a len (ke a) (ffdhe_compute_exp a) p_r2_n sk pk
val ffdhe_secret_to_public: a:S.ffdhe_alg ->
DH.ffdhe_secret_to_public_st t_limbs a (DH.ffdhe_len a) (ke a)
let ffdhe_secret_to_public a sk pk =
let len = DH.ffdhe_len a in
DH.ffdhe_secret_to_public a len (ke a) (ffdhe_secret_to_public_precomp a) (ffdhe_precomp_p a) sk pk
val ffdhe_shared_secret_precomp: a:S.ffdhe_alg ->
DH.ffdhe_shared_secret_precomp_st t_limbs a (DH.ffdhe_len a) (ke a)
let ffdhe_shared_secret_precomp a p_r2_n sk pk ss =
let len = DH.ffdhe_len a in
DH.ffdhe_shared_secret_precomp a len (ke a) (ffdhe_check_pk a) (ffdhe_compute_exp a) p_r2_n sk pk ss
val ffdhe_shared_secret: a:S.ffdhe_alg -> | {
"checked_file": "/",
"dependencies": [
"Spec.FFDHE.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Impl.FFDHE.fst.checked",
"Hacl.Bignum.Exponentiation.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.FFDHE.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Bignum.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.FFDHE",
"short_module": "DH"
},
{
"abbrev": true,
"full_module": "Spec.FFDHE",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Spec.FFDHE.ffdhe_alg
-> Hacl.Impl.FFDHE.ffdhe_shared_secret_st Hacl.FFDHE.t_limbs
a
(Hacl.Impl.FFDHE.ffdhe_len a)
(Hacl.FFDHE.ke a) | Prims.Tot | [
"total"
] | [] | [
"Spec.FFDHE.ffdhe_alg",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Hacl.Impl.FFDHE.ffdhe_len",
"Hacl.Impl.FFDHE.ffdhe_shared_secret",
"Hacl.FFDHE.t_limbs",
"Hacl.FFDHE.ke",
"Hacl.FFDHE.ffdhe_shared_secret_precomp",
"Hacl.FFDHE.ffdhe_precomp_p",
"Hacl.Bignum.Definitions.limb",
"Hacl.Impl.FFDHE.size_pos",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.l_or",
"Lib.IntTypes.range",
"Lib.IntTypes.U32",
"Prims.l_and",
"Prims.op_GreaterThan",
"Prims.op_LessThanOrEqual",
"Prims.op_Subtraction",
"Prims.pow2",
"Lib.IntTypes.v",
"Lib.IntTypes.PUB",
"Spec.FFDHE.ffdhe_len"
] | [] | false | false | false | false | false | let ffdhe_shared_secret a sk pk ss =
| let len = DH.ffdhe_len a in
DH.ffdhe_shared_secret a len (ke a) (ffdhe_shared_secret_precomp a) (ffdhe_precomp_p a) sk pk ss | false |
Sec2.HIFC.fst | Sec2.HIFC.unit_triple | val unit_triple : (Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list _ | let unit_triple = bot, bot, [] | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 440,
"start_col": 0,
"start_line": 440
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | (Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list _ | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.Mktuple3",
"Sec2.HIFC.label",
"Prims.list",
"Sec2.HIFC.bot",
"Prims.Nil",
"FStar.Pervasives.Native.tuple3"
] | [] | false | false | false | true | false | let unit_triple =
| bot, bot, [] | false |
|
Sec2.HIFC.fst | Sec2.HIFC.ifc_triple | val ifc_triple : _: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) ->
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow)
-> (FStar.Set.set Sec2.HIFC.loc * FStar.Set.set Sec2.HIFC.loc) * Prims.list Sec2.HIFC.flow | let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 111,
"end_line": 441,
"start_col": 0,
"start_line": 441
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) ->
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow)
-> (FStar.Set.set Sec2.HIFC.loc * FStar.Set.set Sec2.HIFC.loc) * Prims.list Sec2.HIFC.flow | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple3",
"Sec2.HIFC.label",
"Prims.list",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Pervasives.Native.Mktuple3",
"FStar.Set.set",
"Sec2.HIFC.loc",
"Sec2.HIFC.union",
"FStar.List.Tot.Base.op_At",
"Sec2.HIFC.add_source",
"Prims.Cons",
"Sec2.HIFC.bot"
] | [] | false | false | false | true | false | let ifc_triple (w0, r0, fs0) (w1, r1, fs1) =
| (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1) :: fs1))) | false |
|
Sec2.HIFC.fst | Sec2.HIFC.has_flow_append | val has_flow_append (from to: loc) (fs fs': flows)
: Lemma (has_flow from to fs ==> has_flow from to (fs @ fs') /\ has_flow from to (fs' @ fs)) | val has_flow_append (from to: loc) (fs fs': flows)
: Lemma (has_flow from to fs ==> has_flow from to (fs @ fs') /\ has_flow from to (fs' @ fs)) | let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
() | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 6,
"end_line": 266,
"start_col": 0,
"start_line": 252
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | from: Sec2.HIFC.loc -> to: Sec2.HIFC.loc -> fs: Sec2.HIFC.flows -> fs': Sec2.HIFC.flows
-> FStar.Pervasives.Lemma
(ensures
Sec2.HIFC.has_flow from to fs ==>
Sec2.HIFC.has_flow from to (fs @ fs') /\ Sec2.HIFC.has_flow from to (fs' @ fs)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.loc",
"Sec2.HIFC.flows",
"Sec2.HIFC.flow",
"Prims.unit",
"FStar.List.Tot.Base.memP",
"Prims.squash",
"Prims.l_and",
"FStar.List.Tot.Base.op_At",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil",
"Sec2.HIFC.memP_append_or",
"Prims.l_True",
"Prims.l_imp",
"Sec2.HIFC.has_flow"
] | [] | false | false | true | false | false | let has_flow_append (from to: loc) (fs fs': flows)
: Lemma (has_flow from to fs ==> has_flow from to (fs @ fs') /\ has_flow from to (fs' @ fs)) =
| let aux (rs: _)
: Lemma (requires List.Tot.memP rs fs)
(ensures List.Tot.memP rs (fs @ fs') /\ List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)] =
memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
() | false |
Sec2.HIFC.fst | Sec2.HIFC.label_equiv | val label_equiv : s0: Sec2.HIFC.label -> s1: Sec2.HIFC.label -> Type0 | let label_equiv (s0 s1:label) = Set.equal s0 s1 | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 47,
"end_line": 443,
"start_col": 0,
"start_line": 443
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s0: Sec2.HIFC.label -> s1: Sec2.HIFC.label -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.label",
"FStar.Set.equal",
"Sec2.HIFC.loc"
] | [] | false | false | false | true | true | let label_equiv (s0 s1: label) =
| Set.equal s0 s1 | false |
|
Sec2.HIFC.fst | Sec2.HIFC.memP_append_or | val memP_append_or (#a: Type) (x: a) (l0 l1: list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0) | val memP_append_or (#a: Type) (x: a) (l0 l1: list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0) | let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1 | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 250,
"start_col": 0,
"start_line": 244
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> l0: Prims.list a -> l1: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.memP x (l0 @ l1) <==>
FStar.List.Tot.Base.memP x l0 \/ FStar.List.Tot.Base.memP x l1) (decreases l0) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"Sec2.HIFC.memP_append_or",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"FStar.List.Tot.Base.memP",
"FStar.List.Tot.Base.op_At",
"Prims.l_or",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec memP_append_or (#a: Type) (x: a) (l0 l1: list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0) =
| match l0 with
| [] -> ()
| _ :: tl -> memP_append_or x tl l1 | false |
Sec2.HIFC.fst | Sec2.HIFC.triple_equiv | val triple_equiv : _: ((Sec2.HIFC.label * Sec2.HIFC.label) * Sec2.HIFC.flows) ->
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Sec2.HIFC.flows)
-> Prims.logical | let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1 | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 104,
"end_line": 444,
"start_col": 0,
"start_line": 444
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Sec2.HIFC.flows) ->
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Sec2.HIFC.flows)
-> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple3",
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"FStar.Pervasives.Native.Mktuple2",
"Prims.l_and",
"Sec2.HIFC.label_equiv",
"Sec2.HIFC.flows_equiv",
"Prims.logical"
] | [] | false | false | false | true | true | let triple_equiv (w0, r0, f0) (w1, r1, f1) =
| label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1 | false |
|
ContextPollution.fst | ContextPollution.warmup1 | val warmup1 : x: Prims.bool{x == true} -> Prims.unit | let warmup1 (x:bool { x == true }) = assert x | {
"file_name": "share/steel/tests/ContextPollution.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 45,
"end_line": 12,
"start_col": 0,
"start_line": 12
} | module ContextPollution
let warmup (x:bool { x == true }) = assert x
//SNIPPET_START: context_test1$
module T = FStar.Tactics
module B = LowStar.Buffer
module SA = Steel.Array
open FStar.Seq | {
"checked_file": "/",
"dependencies": [
"Steel.Array.fsti.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "ContextPollution.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Array",
"short_module": "SA"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.Tactics",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.bool{x == true} -> Prims.unit | Prims.Tot | [
"total"
] | [] | [
"Prims.bool",
"Prims.eq2",
"Prims._assert",
"Prims.b2t",
"Prims.unit"
] | [] | false | false | false | false | false | let warmup1 (x: bool{x == true}) =
| assert x | false |
|
Sec2.HIFC.fst | Sec2.HIFC.add_source_monotonic | val add_source_monotonic (from to: loc) (r: label) (fs: flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs)) | val add_source_monotonic (from to: loc) (r: label) (fs: flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs)) | let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 48,
"end_line": 290,
"start_col": 0,
"start_line": 286
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | from: Sec2.HIFC.loc -> to: Sec2.HIFC.loc -> r: Sec2.HIFC.label -> fs: Sec2.HIFC.flows
-> FStar.Pervasives.Lemma
(ensures
Sec2.HIFC.has_flow from to fs ==> Sec2.HIFC.has_flow from to (Sec2.HIFC.add_source r fs)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.loc",
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.flow",
"Prims.list",
"Sec2.HIFC.add_source_monotonic",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"Sec2.HIFC.has_flow",
"Sec2.HIFC.add_source",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec add_source_monotonic (from to: loc) (r: label) (fs: flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs)) =
| match fs with
| [] -> ()
| _ :: tl -> add_source_monotonic from to r tl | false |
Sec2.HIFC.fst | Sec2.HIFC.elim_has_flow_seq | val elim_has_flow_seq (from to: loc) (r0 r1 w1: label) (fs0 fs1: flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1) :: fs1)))))
(ensures
(~(has_flow from to fs0) /\ (~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1)))) | val elim_has_flow_seq (from to: loc) (r0 r1 w1: label) (fs0 fs1: flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1) :: fs1)))))
(ensures
(~(has_flow from to fs0) /\ (~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1)))) | let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 58,
"end_line": 284,
"start_col": 0,
"start_line": 268
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
from: Sec2.HIFC.loc ->
to: Sec2.HIFC.loc ->
r0: Sec2.HIFC.label ->
r1: Sec2.HIFC.label ->
w1: Sec2.HIFC.label ->
fs0: Sec2.HIFC.flows ->
fs1: Sec2.HIFC.flows
-> FStar.Pervasives.Lemma
(requires
~(Sec2.HIFC.has_flow from to (fs0 @ Sec2.HIFC.add_source r0 ((Sec2.HIFC.bot, w1) :: fs1))))
(ensures
~(Sec2.HIFC.has_flow from to fs0) /\ (~(FStar.Set.mem from r0) \/ ~(FStar.Set.mem to w1)) /\
~(Sec2.HIFC.has_flow from to (Sec2.HIFC.add_source r0 fs1))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.loc",
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.has_flow_append",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Prims.Nil",
"Sec2.HIFC.add_source",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Prims.list",
"FStar.List.Tot.Base.op_At",
"Prims.l_not",
"Sec2.HIFC.has_flow",
"FStar.Set.equal",
"FStar.Set.union",
"Sec2.HIFC.bot",
"Prims.squash",
"Prims.l_and",
"Prims.l_or",
"Prims.b2t",
"FStar.Set.mem",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let elim_has_flow_seq (from to: loc) (r0 r1 w1: label) (fs0 fs1: flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1) :: fs1)))))
(ensures
(~(has_flow from to fs0) /\ (~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1)))) =
| assert (add_source r0 ((bot, w1) :: fs1) == (Set.union r0 bot, w1) :: add_source r0 fs1);
assert ((Set.union r0 bot) `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1) :: add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1) :: add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1) :: add_source r0 fs1))));
assert ((r0, w1) :: add_source r0 fs1 == [r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1) | false |
Sec2.HIFC.fst | Sec2.HIFC.pre_bind | val pre_bind
(a b: Type)
(w0 r0 w1 r1: label)
(fs0 fs1: flows)
(#p #q #r #s: _)
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b
(union r0 r1)
(union w0 w1)
(fs0 @ add_source r0 ((bot, w1) :: fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2)) | val pre_bind
(a b: Type)
(w0 r0 w1 r1: label)
(fs0 fs1: flows)
(#p #q #r #s: _)
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b
(union r0 r1)
(union w0 w1)
(fs0 @ add_source r0 ((bot, w1) :: fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2)) | let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 433,
"start_col": 0,
"start_line": 421
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Type ->
b: Type ->
w0: Sec2.HIFC.label ->
r0: Sec2.HIFC.label ->
w1: Sec2.HIFC.label ->
r1: Sec2.HIFC.label ->
fs0: Sec2.HIFC.flows ->
fs1: Sec2.HIFC.flows ->
x: Sec2.HIFC.hifc a r0 w0 fs0 p q ->
y: (x: a -> Sec2.HIFC.hifc b r1 w1 fs1 (r x) (s x))
-> Sec2.HIFC.hifc b
(Sec2.HIFC.union r0 r1)
(Sec2.HIFC.union w0 w1)
(fs0 @ Sec2.HIFC.add_source r0 ((Sec2.HIFC.bot, w1) :: fs1))
(fun s0 -> p s0 /\ (forall (x: a) (s1: Sec2.HIFC.store). q s0 x s1 ==> r x s1))
(fun s0 r s2 -> exists (x: a) (s1: Sec2.HIFC.store). q s0 x s1 /\ s x s1 r s2) | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Prims.unit",
"Sec2.HIFC.bind_ifc_flows_ok",
"Sec2.HIFC.bind_ifc_writes_ok",
"Sec2.HIFC.bind_ifc_reads_ok",
"Sec2.HIFC.hst",
"Sec2.HIFC.store",
"Prims.l_and",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.l_Exists",
"Sec2.HIFC.bind_ifc'",
"Sec2.HIFC.union",
"FStar.List.Tot.Base.op_At",
"Sec2.HIFC.flow",
"Sec2.HIFC.add_source",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.bot"
] | [] | false | false | false | false | false | let pre_bind
(a: Type)
(b: Type)
(w0: label)
(r0: label)
(w1: label)
(r1: label)
(fs0: flows)
(fs1: flows)
#p
#q
#r
#s
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b
(union r0 r1)
(union w0 w1)
(fs0 @ add_source r0 ((bot, w1) :: fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2)) =
| let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f | false |
Sec2.HIFC.fst | Sec2.HIFC.bind_ifc_reads_ok | val bind_ifc_reads_ok
(#a #b: Type)
(#w0 #r0 #w1 #r1: label)
(#fs0 #fs1: flows)
(#p #q #r #s: _)
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1)) | val bind_ifc_reads_ok
(#a #b: Type)
(#w0 #r0 #w1 #r1: label)
(#fs0 #fs1: flows)
(#p #q #r #s: _)
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1)) | let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
() | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 6,
"end_line": 230,
"start_col": 0,
"start_line": 202
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Sec2.HIFC.hifc a r0 w0 fs0 p q -> y: (x: a -> Sec2.HIFC.hifc b r1 w1 fs1 (r x) (s x))
-> FStar.Pervasives.Lemma
(ensures Sec2.HIFC.reads (Sec2.HIFC.bind_ifc' x y) (Sec2.HIFC.union r0 r1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Sec2.HIFC.store",
"Prims.unit",
"Sec2.HIFC.agree_on",
"Prims.squash",
"Sec2.HIFC.related_runs",
"Prims.l_and",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.l_Exists",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.logical",
"Prims.Nil",
"Prims._assert",
"Prims.eq2",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.fst",
"FStar.Pervasives.Native.snd",
"Sec2.HIFC.loc",
"Prims.b2t",
"FStar.Set.mem",
"Prims.int",
"Sec2.HIFC.sel",
"Sec2.HIFC.reads_ok_preserves_equal_locs",
"Sec2.HIFC.weaken_reads_ok",
"FStar.Set.set",
"Sec2.HIFC.union",
"Sec2.HIFC.hst",
"Sec2.HIFC.bind_ifc'",
"Prims.l_True",
"Sec2.HIFC.reads"
] | [] | false | false | true | false | false | let bind_ifc_reads_ok
(#a: Type)
(#b: Type)
(#w0: label)
(#r0: label)
(#w1: label)
(#r1: label)
(#fs0: flows)
(#fs1: flows)
#p
#q
#r
#s
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1)) =
| let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0: store{p_f s0}) (s0': store{p_f s0'})
: Lemma (requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat (agree_on reads s0 s0')] =
let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
() | false |
Sec2.HIFC.fst | Sec2.HIFC.bind | val bind
(a b: Type)
(r0 w0: label)
(fs0: flows)
(p: pre)
(q: post a)
(r1 w1: label)
(fs1: flows)
(r: (a -> pre))
(s: (a -> post b))
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b
(union r0 r1)
(union w0 w1)
(fs0 @ add_source r0 ((bot, w1) :: fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 ->
(exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2))) | val bind
(a b: Type)
(r0 w0: label)
(fs0: flows)
(p: pre)
(q: post a)
(r1 w1: label)
(fs1: flows)
(r: (a -> pre))
(s: (a -> post b))
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b
(union r0 r1)
(union w0 w1)
(fs0 @ add_source r0 ((bot, w1) :: fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 ->
(exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2))) | let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a))) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 79,
"end_line": 582,
"start_col": 0,
"start_line": 571
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame` | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Type ->
b: Type ->
r0: Sec2.HIFC.label ->
w0: Sec2.HIFC.label ->
fs0: Sec2.HIFC.flows ->
p: Sec2.HIFC.pre ->
q: Sec2.HIFC.post a ->
r1: Sec2.HIFC.label ->
w1: Sec2.HIFC.label ->
fs1: Sec2.HIFC.flows ->
r: (_: a -> Sec2.HIFC.pre) ->
s: (_: a -> Sec2.HIFC.post b) ->
x: Sec2.HIFC.hifc a r0 w0 fs0 p q ->
y: (x: a -> Sec2.HIFC.hifc b r1 w1 fs1 (r x) (s x))
-> Sec2.HIFC.hifc b
(Sec2.HIFC.union r0 r1)
(Sec2.HIFC.union w0 w1)
(fs0 @ Sec2.HIFC.add_source r0 ((Sec2.HIFC.bot, w1) :: fs1))
(fun s0 ->
p s0 /\
(forall (x: a) (s1: Sec2.HIFC.store). q s0 x s1 /\ Sec2.HIFC.modifies w0 s0 s1 ==> r x s1)
)
(fun s0 r s2 ->
exists (x: a) (s1: Sec2.HIFC.store).
q s0 x s1 /\ Sec2.HIFC.modifies w0 s0 s1 /\ s x s1 r s2 /\ Sec2.HIFC.modifies w1 s1 s2) | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Sec2.HIFC.pre_bind",
"Sec2.HIFC.store",
"Prims.l_and",
"Sec2.HIFC.modifies",
"Sec2.HIFC.frame",
"Sec2.HIFC.union",
"FStar.List.Tot.Base.op_At",
"Sec2.HIFC.flow",
"Sec2.HIFC.add_source",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.bot",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.l_Exists"
] | [] | false | false | false | false | false | let bind
(a b: Type)
(r0 w0: label)
(fs0: flows)
(p: pre)
(q: post a)
(r1 w1: label)
(fs1: flows)
(r: (a -> pre))
(s: (a -> post b))
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b
(union r0 r1)
(union w0 w1)
(fs0 @ add_source r0 ((bot, w1) :: fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 ->
(exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2))) =
| (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a))) | false |
Hacl.Impl.Frodo.KEM.Decaps.fst | Hacl.Impl.Frodo.KEM.Decaps.crypto_kem_dec_kp_s_cond | val crypto_kem_dec_kp_s_cond:
a:FP.frodo_alg
-> bp_matrix:matrix_t params_nbar (params_n a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h bp_matrix /\ live h bpp_matrix /\
live h c_matrix /\ live h cp_matrix)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s_cond a (as_matrix h0 bp_matrix) (as_matrix h0 bpp_matrix)
(as_matrix h0 c_matrix) (as_matrix h0 cp_matrix)) | val crypto_kem_dec_kp_s_cond:
a:FP.frodo_alg
-> bp_matrix:matrix_t params_nbar (params_n a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h bp_matrix /\ live h bpp_matrix /\
live h c_matrix /\ live h cp_matrix)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s_cond a (as_matrix h0 bp_matrix) (as_matrix h0 bpp_matrix)
(as_matrix h0 c_matrix) (as_matrix h0 cp_matrix)) | let crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix =
let b1 = matrix_eq bp_matrix bpp_matrix in
let b2 = matrix_eq c_matrix cp_matrix in
b1 &. b2 | {
"file_name": "code/frodo/Hacl.Impl.Frodo.KEM.Decaps.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 10,
"end_line": 166,
"start_col": 0,
"start_line": 163
} | module Hacl.Impl.Frodo.KEM.Decaps
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Hacl.Impl.Matrix
open Hacl.Impl.Frodo.Params
open Hacl.Impl.Frodo.KEM
open Hacl.Impl.Frodo.KEM.Encaps
open Hacl.Impl.Frodo.Encode
open Hacl.Impl.Frodo.Pack
open Hacl.Impl.Frodo.Sample
open Hacl.Frodo.Random
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module FP = Spec.Frodo.Params
module KG = Hacl.Impl.Frodo.KEM.KeyGen
module S = Spec.Frodo.KEM.Decaps
module M = Spec.Matrix
#set-options "--z3rlimit 100 --fuel 0 --ifuel 0"
inline_for_extraction noextract
val get_bp_c_matrices:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h ct /\ live h bp_matrix /\ live h c_matrix /\
disjoint bp_matrix ct /\ disjoint c_matrix ct /\ disjoint bp_matrix c_matrix)
(ensures fun h0 _ h1 -> modifies (loc bp_matrix |+| loc c_matrix) h0 h1 /\
(as_matrix h1 bp_matrix, as_matrix h1 c_matrix) == S.get_bp_c_matrices a (as_seq h0 ct))
let get_bp_c_matrices a ct bp_matrix c_matrix =
let c1 = sub ct 0ul (ct1bytes_len a) in
let c2 = sub ct (ct1bytes_len a) (ct2bytes_len a) in
frodo_unpack params_nbar (params_n a) (params_logq a) c1 bp_matrix;
frodo_unpack params_nbar params_nbar (params_logq a) c2 c_matrix
inline_for_extraction noextract
val frodo_mu_decode:
a:FP.frodo_alg
-> s_bytes:lbytes (secretmatrixbytes_len a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h s_bytes /\ live h bp_matrix /\
live h c_matrix /\ live h mu_decode /\
disjoint mu_decode s_bytes /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0))
(ensures fun h0 _ h1 -> modifies (loc mu_decode) h0 h1 /\
as_seq h1 mu_decode ==
S.frodo_mu_decode a (as_seq h0 s_bytes) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let frodo_mu_decode a s_bytes bp_matrix c_matrix mu_decode =
push_frame();
let s_matrix = matrix_create (params_n a) params_nbar in
let m_matrix = matrix_create params_nbar params_nbar in
matrix_from_lbytes s_bytes s_matrix;
matrix_mul_s bp_matrix s_matrix m_matrix;
matrix_sub c_matrix m_matrix;
frodo_key_decode (params_logq a) (params_extracted_bits a) params_nbar m_matrix mu_decode;
clear_matrix s_matrix;
clear_matrix m_matrix;
pop_frame()
inline_for_extraction noextract
val get_bpp_cp_matrices_:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> sp_matrix:matrix_t params_nbar (params_n a)
-> ep_matrix:matrix_t params_nbar (params_n a)
-> epp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
live h sp_matrix /\ live h ep_matrix /\ live h epp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc sk; loc bpp_matrix;
loc cp_matrix; loc sp_matrix; loc ep_matrix; loc epp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices_ a gen_a (as_seq h0 mu_decode) (as_seq h0 sk)
(as_matrix h0 sp_matrix) (as_matrix h0 ep_matrix) (as_matrix h0 epp_matrix))
let get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix =
FP.expand_crypto_secretkeybytes a;
FP.expand_crypto_publickeybytes a;
let pk = sub sk (crypto_bytes a) (crypto_publickeybytes a) in
let seed_a = sub pk 0ul bytes_seed_a in
let b = sub pk bytes_seed_a (crypto_publickeybytes a -! bytes_seed_a) in
frodo_mul_add_sa_plus_e a gen_a seed_a sp_matrix ep_matrix bpp_matrix;
frodo_mul_add_sb_plus_e_plus_mu a mu_decode b sp_matrix epp_matrix cp_matrix;
mod_pow2 (params_logq a) bpp_matrix;
mod_pow2 (params_logq a) cp_matrix
#push-options "--z3rlimit 150"
inline_for_extraction noextract
val get_bpp_cp_matrices:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> seed_se:lbytes (crypto_bytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h seed_se /\ live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc seed_se; loc sk; loc bpp_matrix; loc cp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices a gen_a (as_seq h0 mu_decode) (as_seq h0 seed_se) (as_seq h0 sk))
let get_bpp_cp_matrices a gen_a mu_decode seed_se sk bpp_matrix cp_matrix =
push_frame ();
let sp_matrix = matrix_create params_nbar (params_n a) in
let ep_matrix = matrix_create params_nbar (params_n a) in
let epp_matrix = matrix_create params_nbar params_nbar in
get_sp_ep_epp_matrices a seed_se sp_matrix ep_matrix epp_matrix;
get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix;
clear_matrix3 a sp_matrix ep_matrix epp_matrix;
pop_frame ()
#pop-options
inline_for_extraction noextract
val crypto_kem_dec_kp_s_cond:
a:FP.frodo_alg
-> bp_matrix:matrix_t params_nbar (params_n a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h bp_matrix /\ live h bpp_matrix /\
live h c_matrix /\ live h cp_matrix)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s_cond a (as_matrix h0 bp_matrix) (as_matrix h0 bpp_matrix)
(as_matrix h0 c_matrix) (as_matrix h0 cp_matrix)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Params.fst.checked",
"Spec.Frodo.KEM.Decaps.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Matrix.fst.checked",
"Hacl.Impl.Frodo.Sample.fst.checked",
"Hacl.Impl.Frodo.Params.fst.checked",
"Hacl.Impl.Frodo.Pack.fst.checked",
"Hacl.Impl.Frodo.KEM.KeyGen.fst.checked",
"Hacl.Impl.Frodo.KEM.Encaps.fst.checked",
"Hacl.Impl.Frodo.KEM.fst.checked",
"Hacl.Impl.Frodo.Encode.fst.checked",
"Hacl.Frodo.Random.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Frodo.KEM.Decaps.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.KEM.Decaps",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Frodo.KEM.KeyGen",
"short_module": "KG"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.Params",
"short_module": "FP"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Frodo.Random",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Sample",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Pack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Encode",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM.Encaps",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Params",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Matrix",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Spec.Frodo.Params.frodo_alg ->
bp_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
(Hacl.Impl.Frodo.Params.params_n a) ->
bpp_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
(Hacl.Impl.Frodo.Params.params_n a) ->
c_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
Hacl.Impl.Frodo.Params.params_nbar ->
cp_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
Hacl.Impl.Frodo.Params.params_nbar
-> FStar.HyperStack.ST.Stack Lib.IntTypes.uint16 | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Frodo.Params.frodo_alg",
"Hacl.Impl.Matrix.matrix_t",
"Hacl.Impl.Frodo.Params.params_nbar",
"Hacl.Impl.Frodo.Params.params_n",
"Lib.IntTypes.op_Amp_Dot",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Lib.IntTypes.uint16",
"Lib.IntTypes.int_t",
"Hacl.Impl.Matrix.matrix_eq"
] | [] | false | true | false | false | false | let crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix =
| let b1 = matrix_eq bp_matrix bpp_matrix in
let b2 = matrix_eq c_matrix cp_matrix in
b1 &. b2 | false |
Sec2.HIFC.fst | Sec2.HIFC.bind_ifc_flows_ok | val bind_ifc_flows_ok
(#a #b: Type)
(#w0 #r0 #w1 #r1: label)
(#fs0 #fs1: flows)
(#p #q #r #s: _)
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1) :: fs1))) | val bind_ifc_flows_ok
(#a #b: Type)
(#w0 #r0 #w1 #r1: label)
(#fs0 #fs1: flows)
(#p #q #r #s: _)
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1) :: fs1))) | let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
() | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 6,
"end_line": 417,
"start_col": 0,
"start_line": 393
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Sec2.HIFC.hifc a r0 w0 fs0 p q -> y: (x: a -> Sec2.HIFC.hifc b r1 w1 fs1 (r x) (s x))
-> FStar.Pervasives.Lemma
(ensures
Sec2.HIFC.respects (Sec2.HIFC.bind_ifc' x y)
(fs0 @ Sec2.HIFC.add_source r0 ((Sec2.HIFC.bot, w1) :: fs1))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Sec2.HIFC.loc",
"Prims.unit",
"Prims.l_and",
"Prims.b2t",
"Prims.op_disEquality",
"Prims.l_not",
"Sec2.HIFC.has_flow",
"Prims.squash",
"Sec2.HIFC.no_leakage",
"Sec2.HIFC.store",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.l_Exists",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.logical",
"Prims.Nil",
"Prims.int",
"Sec2.HIFC.upd",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel",
"Sec2.HIFC.bind_hst_no_leakage",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.fst",
"FStar.Pervasives.Native.snd",
"Prims.list",
"Sec2.HIFC.flow",
"FStar.List.Tot.Base.op_At",
"Sec2.HIFC.add_source",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.bot",
"Sec2.HIFC.hst",
"Sec2.HIFC.bind_ifc'",
"Sec2.HIFC.respects"
] | [] | false | false | true | false | false | let bind_ifc_flows_ok
(#a: Type)
(#b: Type)
(#w0: label)
(#r0: label)
(#w1: label)
(#r1: label)
(#fs0: flows)
(#fs1: flows)
#p
#q
#r
#s
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1) :: fs1))) =
| let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1) :: fs1)) in
let respects_flows_lemma (from to: loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)] =
let aux (s0: store{p_f s0}) (k: _{p_f (upd s0 from k)})
: Lemma
(let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to) [SMTPat (upd s0 from k)] =
bind_hst_no_leakage x y from to s0 k
in
()
in
() | false |
Sec2.HIFC.fst | Sec2.HIFC.if_then_else | val if_then_else : a: Type ->
r0: Sec2.HIFC.label ->
w0: Sec2.HIFC.label ->
f0: Sec2.HIFC.flows ->
p0: Sec2.HIFC.pre ->
q0: Sec2.HIFC.post a ->
r1: Sec2.HIFC.label ->
w1: Sec2.HIFC.label ->
f1: Sec2.HIFC.flows ->
p1: Sec2.HIFC.pre ->
q1: Sec2.HIFC.post a ->
c_then: Sec2.HIFC.hifc a r0 w0 f0 p0 q0 ->
c_else: Sec2.HIFC.hifc a r1 w1 f1 p1 q1 ->
b: Prims.bool
-> Type | let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 65,
"end_line": 661,
"start_col": 0,
"start_line": 653
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Type ->
r0: Sec2.HIFC.label ->
w0: Sec2.HIFC.label ->
f0: Sec2.HIFC.flows ->
p0: Sec2.HIFC.pre ->
q0: Sec2.HIFC.post a ->
r1: Sec2.HIFC.label ->
w1: Sec2.HIFC.label ->
f1: Sec2.HIFC.flows ->
p1: Sec2.HIFC.pre ->
q1: Sec2.HIFC.post a ->
c_then: Sec2.HIFC.hifc a r0 w0 f0 p0 q0 ->
c_else: Sec2.HIFC.hifc a r1 w1 f1 p1 q1 ->
b: Prims.bool
-> Type | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Prims.bool",
"Sec2.HIFC.union",
"FStar.List.Tot.Base.op_At",
"Sec2.HIFC.flow",
"Sec2.HIFC.store"
] | [] | false | false | false | false | true | let if_then_else
(a: Type)
r0
w0
f0
p0
q0
r1
w1
f1
p1
q1
(c_then: hifc a r0 w0 f0 p0 q0)
(c_else: hifc a r1 w1 f1 p1 q1)
(b: bool)
=
| hifc a
(r0 `union` r1)
(w0 `union` w1)
(f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1) | false |
|
Sec2.HIFC.fst | Sec2.HIFC.add_source_bot | val add_source_bot (f: flows) : Lemma ((add_source bot f) `flows_equiv` f) | val add_source_bot (f: flows) : Lemma ((add_source bot f) `flows_equiv` f) | let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 453,
"start_col": 0,
"start_line": 449
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: Sec2.HIFC.flows
-> FStar.Pervasives.Lemma (ensures Sec2.HIFC.flows_equiv (Sec2.HIFC.add_source Sec2.HIFC.bot f) f) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.flows",
"Sec2.HIFC.flow",
"Prims.list",
"Sec2.HIFC.add_source_bot",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Sec2.HIFC.flows_equiv",
"Sec2.HIFC.add_source",
"Sec2.HIFC.bot",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec add_source_bot (f: flows) : Lemma ((add_source bot f) `flows_equiv` f) =
| match f with
| [] -> ()
| _ :: tl -> add_source_bot tl | false |
Sec2.HIFC.fst | Sec2.HIFC.has_flow_soundness | val has_flow_soundness
(#a #r #w #fs #p #q: _)
(f: hifc a r w fs p q)
(from to: loc)
(s: store{p s})
(k: int{p (upd s from k)})
: Lemma
(requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\ sel s1 to <> sel s1' to)) (ensures has_flow from to fs) | val has_flow_soundness
(#a #r #w #fs #p #q: _)
(f: hifc a r w fs p q)
(from to: loc)
(s: store{p s})
(k: int{p (upd s from k)})
: Lemma
(requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\ sel s1 to <> sel s1' to)) (ensures has_flow from to fs) | let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
() | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 8,
"end_line": 308,
"start_col": 0,
"start_line": 293
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
f: Sec2.HIFC.hifc a r w fs p q ->
from: Sec2.HIFC.loc ->
to: Sec2.HIFC.loc ->
s: Sec2.HIFC.store{p s} ->
k: Prims.int{p (Sec2.HIFC.upd s from k)}
-> FStar.Pervasives.Lemma
(requires
(let _ = f s in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ _ s1 = _ in
let _ = f (Sec2.HIFC.upd s from k) in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ _ s1' = _ in
from <> to /\ Sec2.HIFC.sel s1 to <> Sec2.HIFC.sel s1' to)
<:
Type0)
<:
Type0)) (ensures Sec2.HIFC.has_flow from to fs) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Sec2.HIFC.loc",
"Sec2.HIFC.store",
"Prims.int",
"Sec2.HIFC.upd",
"Prims.unit",
"Prims.l_not",
"Sec2.HIFC.has_flow",
"Prims.squash",
"Prims.l_False",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil",
"Prims._assert",
"Sec2.HIFC.no_leakage",
"Sec2.HIFC.respects",
"Prims.l_and",
"Prims.b2t",
"Prims.op_disEquality",
"Sec2.HIFC.sel",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.fst",
"FStar.Pervasives.Native.snd"
] | [] | false | false | true | false | false | let has_flow_soundness
#a
#r
#w
#fs
#p
#q
(f: hifc a r w fs p q)
(from: loc)
(to: loc)
(s: store{p s})
(k: int{p (upd s from k)})
: Lemma
(requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\ sel s1 to <> sel s1' to)) (ensures has_flow from to fs) =
| let aux () : Lemma (requires (~(has_flow from to fs))) (ensures False) [SMTPat ()] =
assert (respects f fs);
assert (no_leakage f from to)
in
() | false |
Sec2.HIFC.fst | Sec2.HIFC.flows_equiv_append | val flows_equiv_append (f0 f1 g0 g1: flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0 @ f1) (g0 @ g1)) | val flows_equiv_append (f0 f1 g0 g1: flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0 @ f1) (g0 @ g1)) | let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1 | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 473,
"start_col": 0,
"start_line": 469
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f0: Sec2.HIFC.flows -> f1: Sec2.HIFC.flows -> g0: Sec2.HIFC.flows -> g1: Sec2.HIFC.flows
-> FStar.Pervasives.Lemma (requires Sec2.HIFC.flows_equiv f0 g0 /\ Sec2.HIFC.flows_equiv f1 g1)
(ensures Sec2.HIFC.flows_equiv (f0 @ f1) (g0 @ g1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.flows",
"Sec2.HIFC.flows_included_append",
"Prims.unit",
"Prims.l_and",
"Sec2.HIFC.flows_equiv",
"Prims.squash",
"FStar.List.Tot.Base.op_At",
"Sec2.HIFC.flow",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let flows_equiv_append (f0 f1 g0 g1: flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0 @ f1) (g0 @ g1)) =
| flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1 | false |
Sec2.HIFC.fst | Sec2.HIFC.left_unit | val left_unit : _: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) -> Prims.unit | let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f)) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 68,
"end_line": 484,
"start_col": 0,
"start_line": 481
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) -> Prims.unit | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple3",
"Sec2.HIFC.label",
"Prims.list",
"Sec2.HIFC.flow",
"Prims._assert",
"Sec2.HIFC.triple_equiv",
"Sec2.HIFC.ifc_triple",
"Sec2.HIFC.unit_triple",
"FStar.Pervasives.Native.Mktuple3",
"Sec2.HIFC.flows",
"Prims.unit",
"Sec2.HIFC.add_source_bot",
"FStar.Set.equal",
"Sec2.HIFC.loc",
"Sec2.HIFC.union",
"Sec2.HIFC.bot"
] | [] | false | false | false | true | false | let left_unit (w, r, f) =
| assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert ((ifc_triple unit_triple (w, r, f)) `triple_equiv` (w, r, f)) | false |
|
Sec2.HIFC.fst | Sec2.HIFC.right_unit | val right_unit : _: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) -> Prims.unit | let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f)) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 69,
"end_line": 494,
"start_col": 0,
"start_line": 485
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f; | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) -> Prims.unit | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple3",
"Sec2.HIFC.label",
"Prims.list",
"Sec2.HIFC.flow",
"Prims._assert",
"Sec2.HIFC.triple_equiv",
"Sec2.HIFC.ifc_triple",
"FStar.Pervasives.Native.Mktuple3",
"Sec2.HIFC.unit_triple",
"Sec2.HIFC.flows",
"Prims.unit",
"Sec2.HIFC.append_nil_r",
"Sec2.HIFC.flows_equiv_append",
"Sec2.HIFC.add_source",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.bot",
"Prims.Nil",
"Sec2.HIFC.flows_equiv",
"FStar.Calc.calc_finish",
"FStar.Set.set",
"Sec2.HIFC.loc",
"Prims.eq2",
"Sec2.HIFC.union",
"FStar.List.Tot.Base.op_At",
"FStar.Preorder.relation",
"FStar.Calc.calc_step",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash"
] | [] | false | false | false | true | false | let right_unit (w, r, f) =
| calc ( == ) {
ifc_triple (w, r, f) unit_triple;
( == ) { () }
(w `union` bot, r `union` bot, f @ add_source r ([(bot, bot)]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert ((ifc_triple (w, r, f) unit_triple) `triple_equiv` (w, r, f)) | false |
|
Sec2.HIFC.fst | Sec2.HIFC.bind_hst_no_leakage | val bind_hst_no_leakage
(#a #b: Type)
(#w0 #r0 #w1 #r1: label)
(#fs0 #fs1: flows)
(#p #q #r #s: _)
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to: loc)
(s0: store)
(k: _)
: Lemma
(requires
(let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\ p_f s0' /\ from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1) :: fs1)))))
(ensures
(let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)) | val bind_hst_no_leakage
(#a #b: Type)
(#w0 #r0 #w1 #r1: label)
(#fs0 #fs1: flows)
(#p #q #r #s: _)
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to: loc)
(s0: store)
(k: _)
: Lemma
(requires
(let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\ p_f s0' /\ from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1) :: fs1)))))
(ensures
(let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)) | let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
() | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 10,
"end_line": 386,
"start_col": 0,
"start_line": 311
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: Sec2.HIFC.hifc a r0 w0 fs0 p q ->
y: (x: a -> Sec2.HIFC.hifc b r1 w1 fs1 (r x) (s x)) ->
from: Sec2.HIFC.loc ->
to: Sec2.HIFC.loc ->
s0: Sec2.HIFC.store ->
k: Prims.int
-> FStar.Pervasives.Lemma
(requires
(let p_f s0 = p s0 /\ (forall (x: a) (s1: Sec2.HIFC.store). q s0 x s1 ==> r x s1) in
let s0' = Sec2.HIFC.upd s0 from k in
p_f s0 /\ p_f s0' /\ from <> to /\
~(Sec2.HIFC.has_flow from to (fs0 @ Sec2.HIFC.add_source r0 ((Sec2.HIFC.bot, w1) :: fs1)))
))
(ensures
(let f = Sec2.HIFC.bind_ifc' x y in
let s0' = Sec2.HIFC.upd s0 from k in
let _ = f s0 in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ _ s2 = _ in
let _ = f s0' in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ _ s2' = _ in
Sec2.HIFC.sel s2 to == Sec2.HIFC.sel s2' to)
<:
Type0)
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Sec2.HIFC.loc",
"Sec2.HIFC.store",
"Prims.int",
"FStar.Set.mem",
"Prims.op_Equality",
"Sec2.HIFC.sel",
"Prims._assert",
"FStar.Map.equal",
"Prims.bool",
"Sec2.HIFC.add_source_monotonic",
"Prims.unit",
"Sec2.HIFC.has_flow",
"Sec2.HIFC.has_flow_soundness",
"Prims.l_or",
"Prims.b2t",
"Prims.op_disEquality",
"Sec2.HIFC.reads",
"Sec2.HIFC.upd",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.eq2",
"Sec2.HIFC.does_not_read_loc_v",
"Sec2.HIFC.does_not_read_loc",
"Sec2.HIFC.reads_ok_does_not_read_loc",
"Prims.l_not",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.fst",
"FStar.Pervasives.Native.snd",
"Sec2.HIFC.no_leakage",
"Sec2.HIFC.respects",
"Sec2.HIFC.elim_has_flow_seq",
"Prims.list",
"Sec2.HIFC.flow",
"FStar.List.Tot.Base.op_At",
"Sec2.HIFC.add_source",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"Prims.l_Exists",
"Prims.l_and",
"Sec2.HIFC.hst",
"Sec2.HIFC.bind_ifc'",
"Sec2.HIFC.bot",
"Prims.logical",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let bind_hst_no_leakage
(#a: Type)
(#b: Type)
(#w0: label)
(#r0: label)
(#w1: label)
(#r1: label)
(#fs0: flows)
(#fs1: flows)
#p
#q
#r
#s
(x: hifc a r0 w0 fs0 p q)
(y: (x: a -> hifc b r1 w1 fs1 (r x) (s x)))
(from: loc)
(to: loc)
(s0: store)
(k: _)
: Lemma
(requires
(let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\ p_f s0' /\ from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1) :: fs1)))))
(ensures
(let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)) =
| let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1) :: fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
if Set.mem to w1
then
(assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then assert (Map.equal s1 s1')
else
(assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else
(assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1))) | false |
Sec2.HIFC.fst | Sec2.HIFC.ref | val ref : l: Sec2.HIFC.label -> Type0 | let ref (l:label) = r:loc {r `Set.mem` l} | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 754,
"start_col": 0,
"start_line": 754
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.label -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.loc",
"Prims.b2t",
"FStar.Set.mem"
] | [] | false | false | false | true | true | let ref (l: label) =
| r: loc{r `Set.mem` l} | false |
|
Sec2.HIFC.fst | Sec2.HIFC.append_nil_r | val append_nil_r (#a: _) (l: list a) : Lemma (l @ [] == l) | val append_nil_r (#a: _) (l: list a) : Lemma (l @ [] == l) | let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 478,
"start_col": 0,
"start_line": 474
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1; | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list a -> FStar.Pervasives.Lemma (ensures l @ [] == l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Sec2.HIFC.append_nil_r",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.List.Tot.Base.op_At",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec append_nil_r #a (l: list a) : Lemma (l @ [] == l) =
| match l with
| [] -> ()
| _ :: tl -> append_nil_r tl | false |
Sec2.HIFC.fst | Sec2.HIFC.flows_included_append | val flows_included_append (f0 f1 g0 g1: flows)
: Lemma (requires flows_included_in f0 g0 /\ flows_included_in f1 g1)
(ensures flows_included_in (f0 @ f1) (g0 @ g1)) | val flows_included_append (f0 f1 g0 g1: flows)
: Lemma (requires flows_included_in f0 g0 /\ flows_included_in f1 g1)
(ensures flows_included_in (f0 @ f1) (g0 @ g1)) | let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
() | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 6,
"end_line": 468,
"start_col": 0,
"start_line": 454
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f0: Sec2.HIFC.flows -> f1: Sec2.HIFC.flows -> g0: Sec2.HIFC.flows -> g1: Sec2.HIFC.flows
-> FStar.Pervasives.Lemma
(requires Sec2.HIFC.flows_included_in f0 g0 /\ Sec2.HIFC.flows_included_in f1 g1)
(ensures Sec2.HIFC.flows_included_in (f0 @ f1) (g0 @ g1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.flows",
"Sec2.HIFC.flow",
"Sec2.HIFC.loc",
"Prims.unit",
"Prims.l_and",
"FStar.List.Tot.Base.memP",
"FStar.List.Tot.Base.op_At",
"Prims.b2t",
"Prims.op_disEquality",
"Sec2.HIFC.has_flow_1",
"Prims.squash",
"Prims.l_Exists",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.logical",
"Prims.Nil",
"FStar.Classical.forall_intro",
"Prims.l_iff",
"Prims.l_or",
"Sec2.HIFC.memP_append_or",
"Prims._assert",
"Sec2.HIFC.flows_included_in"
] | [] | false | false | true | false | false | let flows_included_append (f0 f1 g0 g1: flows)
: Lemma (requires flows_included_in f0 g0 /\ flows_included_in f1 g1)
(ensures flows_included_in (f0 @ f1) (g0 @ g1)) =
| let aux (f from to: _)
: Lemma (requires List.Tot.memP f (f0 @ f1) /\ from <> to /\ has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0 @ g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)] =
memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
() | false |
Sec2.HIFC.fst | Sec2.HIFC.lref | val lref : Type0 | let lref = ref low | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 18,
"end_line": 757,
"start_col": 0,
"start_line": 757
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.ref",
"Sec2.HIFC.low"
] | [] | false | false | false | true | true | let lref =
| ref low | false |
|
Sec2.HIFC.fst | Sec2.HIFC.low | val low:label | val low:label | let low : label = Set.complement high | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 756,
"start_col": 0,
"start_line": 756
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l} | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Sec2.HIFC.label | Prims.Tot | [
"total"
] | [] | [
"FStar.Set.complement",
"Sec2.HIFC.loc",
"Sec2.HIFC.high"
] | [] | false | false | false | true | false | let low:label =
| Set.complement high | false |
Sec2.HIFC.fst | Sec2.HIFC.href | val href : Type0 | let href = ref high | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 758,
"start_col": 0,
"start_line": 758
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.ref",
"Sec2.HIFC.high"
] | [] | false | false | false | true | true | let href =
| ref high | false |
|
Sec2.HIFC.fst | Sec2.HIFC.frame | val frame (a: Type) (r w: label) (fs: flows) (#p #q: _) (f: hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) | val frame (a: Type) (r w: label) (fs: flows) (#p #q: _) (f: hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) | let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 565,
"start_col": 0,
"start_line": 537
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Type ->
r: Sec2.HIFC.label ->
w: Sec2.HIFC.label ->
fs: Sec2.HIFC.flows ->
f: Sec2.HIFC.hifc a r w fs p q
-> Sec2.HIFC.hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ Sec2.HIFC.modifies w s0 s1) | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Prims.unit",
"Prims._assert",
"Sec2.HIFC.respects",
"Sec2.HIFC.store",
"Prims.l_and",
"Sec2.HIFC.modifies",
"Sec2.HIFC.loc",
"Prims.l_not",
"Sec2.HIFC.has_flow",
"Prims.b2t",
"Prims.op_disEquality",
"Prims.squash",
"Sec2.HIFC.no_leakage",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.logical",
"Prims.Nil",
"Sec2.HIFC.writes",
"Sec2.HIFC.reads",
"FStar.Set.mem",
"Sec2.HIFC.does_not_read_loc",
"Sec2.HIFC.reads_ok_does_not_read_loc",
"Sec2.HIFC.hst",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.fst",
"FStar.Pervasives.Native.snd",
"Prims.l_True",
"Prims.eq2",
"Prims.int",
"Sec2.HIFC.sel"
] | [] | false | false | false | false | false | let frame (a: Type) (r: label) (w: label) (fs: flows) #p #q (f: hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) =
| let aux (s0: store{p s0}) (l: loc{~(Set.mem l w)})
: Lemma
(let x, s1 = f s0 in
sel s0 l == sel s1 l) [SMTPat ()] =
()
in
let g:hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l: loc) (s: store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat (does_not_read_loc g r l s)] =
reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to: _)
: Lemma (requires ~(has_flow from to fs) /\ from <> to)
(ensures (no_leakage g from to))
[SMTPat (has_flow from to fs)] =
assert (no_leakage f from to)
in
assert (respects g fs);
g | false |
Sec2.HIFC.fst | Sec2.HIFC.consequence | val consequence (a: Type) (r0 w0: label) (p q p' q': _) (fs0: flows) (f: hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\ (forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True) | val consequence (a: Type) (r0 w0: label) (p q p' q': _) (fs0: flows) (f: hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\ (forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True) | let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 612,
"start_col": 0,
"start_line": 606
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Type ->
r0: Sec2.HIFC.label ->
w0: Sec2.HIFC.label ->
p: Sec2.HIFC.pre ->
q: Sec2.HIFC.post a ->
p': (_: Sec2.HIFC.store -> Prims.logical) ->
q': (_: Sec2.HIFC.store -> _: a -> _: Sec2.HIFC.store -> Prims.logical) ->
fs0: Sec2.HIFC.flows ->
f: Sec2.HIFC.hifc a r0 w0 fs0 p q
-> Prims.Pure (Sec2.HIFC.hst a p' q') | Prims.Pure | [] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.store",
"Prims.logical",
"Sec2.HIFC.flows",
"Sec2.HIFC.hifc",
"Sec2.HIFC.hst",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.fst",
"FStar.Pervasives.Native.snd",
"Prims.l_and",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.l_True"
] | [] | false | false | false | false | false | let consequence (a: Type) (r0: label) (w0: label) p q p' q' (fs0: flows) (f: hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\ (forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True) =
| let g:hst a p' q' = fun s -> f s in
g | false |
Sec2.HIFC.fst | Sec2.HIFC.append_memP | val append_memP (#a: _) (x: a) (l0 l1: list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1)) | val append_memP (#a: _) (x: a) (l0 l1: list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1)) | let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1 | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 39,
"end_line": 667,
"start_col": 0,
"start_line": 663
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> l0: Prims.list a -> l1: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.memP x (l0 @ l1) <==>
FStar.List.Tot.Base.memP x l0 \/ FStar.List.Tot.Base.memP x l1) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Sec2.HIFC.append_memP",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"FStar.List.Tot.Base.memP",
"FStar.List.Tot.Base.op_At",
"Prims.l_or",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec append_memP #a (x: a) (l0: list a) (l1: list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1)) =
| match l0 with
| [] -> ()
| hd :: tl -> append_memP x tl l1 | false |
Sec2.HIFC.fst | Sec2.HIFC.weaken_flows_append | val weaken_flows_append (fs fs': flows)
: Lemma
(ensures
(norm [delta; iota; zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta; iota; zeta] (fs' `flows_included_in` (fs @ fs'))))) [SMTPat ()] | val weaken_flows_append (fs fs': flows)
: Lemma
(ensures
(norm [delta; iota; zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta; iota; zeta] (fs' `flows_included_in` (fs @ fs'))))) [SMTPat ()] | let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs')) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 54,
"end_line": 691,
"start_col": 0,
"start_line": 672
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | fs: Sec2.HIFC.flows -> fs': Sec2.HIFC.flows
-> FStar.Pervasives.Lemma
(ensures
FStar.Pervasives.norm [FStar.Pervasives.delta; FStar.Pervasives.iota; FStar.Pervasives.zeta]
(Sec2.HIFC.flows_included_in fs (fs @ fs')) /\
FStar.Pervasives.norm [FStar.Pervasives.delta; FStar.Pervasives.iota; FStar.Pervasives.zeta]
(Sec2.HIFC.flows_included_in fs' (fs @ fs'))) [SMTPat ()] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Sec2.HIFC.flows",
"Sec2.HIFC.norm_spec_inv",
"Sec2.HIFC.flows_included_in",
"FStar.List.Tot.Base.op_At",
"Sec2.HIFC.flow",
"Prims.unit",
"FStar.List.Tot.Base.memP",
"Prims.squash",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil",
"Sec2.HIFC.append_memP",
"Prims.l_True",
"Prims.l_and",
"FStar.Pervasives.norm",
"FStar.Pervasives.norm_step",
"FStar.Pervasives.delta",
"FStar.Pervasives.iota",
"FStar.Pervasives.zeta",
"Prims.logical"
] | [] | false | false | true | false | false | let weaken_flows_append (fs fs': flows)
: Lemma
(ensures
(norm [delta; iota; zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta; iota; zeta] (fs' `flows_included_in` (fs @ fs'))))) [SMTPat ()] =
| let aux (f0: flow)
: Lemma (requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat (f0 `List.Tot.memP` (fs @ fs'))] =
append_memP f0 fs fs'
in
let aux (f0: flow)
: Lemma (requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat (f0 `List.Tot.memP` (fs @ fs'))] =
append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs')) | false |
Sec2.HIFC.fst | Sec2.HIFC.sub_hifc | val sub_hifc
(a: Type)
(r0 w0: label)
(fs0: flows)
(p q: _)
(r1 w1: label)
(fs1: flows)
(#p' #q': _)
(f: hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\ label_inclusion w0 w1 /\
(norm [delta; iota; zeta] (fs0 `flows_included_in` fs1)) /\ (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True) | val sub_hifc
(a: Type)
(r0 w0: label)
(fs0: flows)
(p q: _)
(r1 w1: label)
(fs1: flows)
(#p' #q': _)
(f: hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\ label_inclusion w0 w1 /\
(norm [delta; iota; zeta] (fs0 `flows_included_in` fs1)) /\ (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True) | let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 650,
"start_col": 0,
"start_line": 625
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Type ->
r0: Sec2.HIFC.label ->
w0: Sec2.HIFC.label ->
fs0: Sec2.HIFC.flows ->
p: Sec2.HIFC.pre ->
q: Sec2.HIFC.post a ->
r1: Sec2.HIFC.label ->
w1: Sec2.HIFC.label ->
fs1: Sec2.HIFC.flows ->
f: Sec2.HIFC.hifc a r0 w0 fs0 p q
-> Prims.Pure (Sec2.HIFC.hifc a r1 w1 fs1 p' q') | Prims.Pure | [] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.store",
"Prims.logical",
"Sec2.HIFC.hifc",
"Prims.unit",
"Prims._assert",
"Sec2.HIFC.respects",
"Sec2.HIFC.loc",
"Prims.l_and",
"Prims.l_not",
"Sec2.HIFC.has_flow",
"Prims.b2t",
"Prims.op_disEquality",
"Prims.squash",
"Sec2.HIFC.no_leakage",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil",
"Sec2.HIFC.writes",
"Sec2.HIFC.reads",
"Sec2.HIFC.weaken_reads_ok",
"Sec2.HIFC.hst",
"Sec2.HIFC.consequence",
"Sec2.HIFC.flows_included_in",
"Sec2.HIFC.norm_spec",
"Sec2.HIFC.label_inclusion",
"FStar.Pervasives.norm",
"FStar.Pervasives.norm_step",
"FStar.Pervasives.delta",
"FStar.Pervasives.iota",
"FStar.Pervasives.zeta",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.l_True"
] | [] | false | false | false | false | false | let sub_hifc
(a: Type)
(r0: label)
(w0: label)
(fs0: flows)
p
q
(r1: label)
(w1: label)
(fs1: flows)
#p'
#q'
(f: hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\ label_inclusion w0 w1 /\
(norm [delta; iota; zeta] (fs0 `flows_included_in` fs1)) /\ (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True) =
| let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f:hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to: _)
: Lemma (requires ~(has_flow from to fs1) /\ from <> to)
(ensures (no_leakage f from to))
[SMTPat (no_leakage f from to)] =
assert (no_leakage forig from to)
in
assert (respects f fs1);
f | false |
Sec2.HIFC.fst | Sec2.HIFC.refine_flow_hifc | val refine_flow_hifc (#a #w #r #f #fs #p #q: _) (c: hifc a r w (f :: fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\ from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\ p (upd s0 from v) /\ q s0 x s1 /\ modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\ modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True) | val refine_flow_hifc (#a #w #r #f #fs #p #q: _) (c: hifc a r w (f :: fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\ from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\ p (upd s0 from v) /\ q s0 x s1 /\ modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\ modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True) | let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 603,
"start_col": 0,
"start_line": 587
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | c: Sec2.HIFC.hifc a r w (f :: fs) p q -> Prims.Pure (Sec2.HIFC.hifc a r w fs p q) | Prims.Pure | [] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flow",
"Prims.list",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Prims.Cons",
"Prims.l_Forall",
"Sec2.HIFC.loc",
"Prims.int",
"Prims.l_imp",
"Prims.l_and",
"Sec2.HIFC.has_flow_1",
"Prims.b2t",
"Prims.op_disEquality",
"Sec2.HIFC.store",
"Sec2.HIFC.upd",
"Sec2.HIFC.modifies",
"Prims.eq2",
"Sec2.HIFC.sel",
"Prims.l_True"
] | [] | false | false | false | false | false | let refine_flow_hifc #a #w #r #f #fs #p #q (c: hifc a r w (f :: fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\ from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\ p (upd s0 from v) /\ q s0 x s1 /\ modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\ modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True) =
| c | false |
Sec2.HIFC.fst | Sec2.HIFC.write | val write (l: loc) (x: int)
: HIFC unit bot (single l) [] (requires fun _ -> True) (ensures fun _ _ s1 -> sel s1 l == x) | val write (l: loc) (x: int)
: HIFC unit bot (single l) [] (requires fun _ -> True) (ensures fun _ _ s1 -> sel s1 l == x) | let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 720,
"start_col": 0,
"start_line": 716
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.loc -> x: Prims.int -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.loc",
"Prims.int",
"Sec2.HIFC.iwrite",
"Prims.unit",
"Sec2.HIFC.bot",
"Sec2.HIFC.single",
"Prims.Nil",
"Sec2.HIFC.flow",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let write (l: loc) (x: int)
: HIFC unit bot (single l) [] (requires fun _ -> True) (ensures fun _ _ s1 -> sel s1 l == x) =
| HIFC?.reflect (iwrite l x) | false |
Sec2.HIFC.fst | Sec2.HIFC.refine_flow | val refine_flow (#a #w #r #f #fs #p #q: _) ($c: (unit -> HIFC a r w (f :: fs) p q))
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\ from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\ p (upd s0 from v) /\ q s0 x s1 /\ modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\ modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True) | val refine_flow (#a #w #r #f #fs #p #q: _) ($c: (unit -> HIFC a r w (f :: fs) p q))
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\ from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\ p (upd s0 from v) /\ q s0 x s1 /\ modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\ modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True) | let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c())))) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 63,
"end_line": 750,
"start_col": 0,
"start_line": 734
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | $c: (_: Prims.unit -> Sec2.HIFC.HIFC a) -> Prims.Pure (_: Prims.unit -> Sec2.HIFC.HIFC a) | Prims.Pure | [] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flow",
"Prims.list",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Prims.unit",
"Prims.Cons",
"Sec2.HIFC.refine_flow_hifc",
"Prims.l_Forall",
"Sec2.HIFC.loc",
"Prims.int",
"Prims.l_imp",
"Prims.l_and",
"Sec2.HIFC.has_flow_1",
"Prims.b2t",
"Prims.op_disEquality",
"Sec2.HIFC.store",
"Sec2.HIFC.upd",
"Sec2.HIFC.modifies",
"Prims.eq2",
"Sec2.HIFC.sel",
"Prims.l_True"
] | [] | false | false | false | false | false | let refine_flow #a #w #r #f #fs #p #q ($c: (unit -> HIFC a r w (f :: fs) p q))
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\ from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\ p (upd s0 from v) /\ q s0 x s1 /\ modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\ modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True) =
| (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c ())))) | false |
Sec2.HIFC.fst | Sec2.HIFC.read | val read (l: loc)
: HIFC int (single l) bot [] (requires fun _ -> True) (ensures fun s0 x s1 -> x == sel s0 l) | val read (l: loc)
: HIFC int (single l) bot [] (requires fun _ -> True) (ensures fun s0 x s1 -> x == sel s0 l) | let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 715,
"start_col": 0,
"start_line": 711
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
} | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.loc -> Sec2.HIFC.HIFC Prims.int | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.loc",
"Sec2.HIFC.iread",
"Prims.int",
"Sec2.HIFC.single",
"Sec2.HIFC.bot",
"Prims.Nil",
"Sec2.HIFC.flow",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let read (l: loc)
: HIFC int (single l) bot [] (requires fun _ -> True) (ensures fun s0 x s1 -> x == sel s0 l) =
| HIFC?.reflect (iread l) | false |
Sec2.HIFC.fst | Sec2.HIFC.lift_PURE_HIFC | val lift_PURE_HIFC (a: Type) (wp: pure_wp a) (f: (unit -> PURE a wp))
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True) | val lift_PURE_HIFC (a: Type) (wp: pure_wp a) (f: (unit -> PURE a wp))
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True) | let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ()) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 729,
"start_col": 0,
"start_line": 724
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Type -> wp: Prims.pure_wp a -> f: (_: Prims.unit -> Prims.PURE a)
-> Prims.Pure
(Sec2.HIFC.hifc a
Sec2.HIFC.bot
Sec2.HIFC.bot
[]
(fun _ -> Prims.l_True)
(fun s0 _ s1 -> s0 == s1)) | Prims.Pure | [] | [] | [
"Prims.pure_wp",
"Prims.unit",
"Sec2.HIFC.return",
"FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall",
"Sec2.HIFC.hifc",
"Sec2.HIFC.bot",
"Prims.Nil",
"Sec2.HIFC.flow",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2"
] | [] | false | false | false | false | false | let lift_PURE_HIFC (a: Type) (wp: pure_wp a) (f: (unit -> PURE a wp))
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True) =
| FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ()) | false |
LowParse.Low.Writers.Instances.fst | LowParse.Low.Writers.Instances.swrite_bounded_vlgenbytes | val swrite_bounded_vlgenbytes
(h0: HS.mem)
(sout:
slice (srel_of_buffer_srel (B.trivial_preorder _))
(srel_of_buffer_srel (B.trivial_preorder _)))
(pout_from0: U32.t)
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#kk: parser_kind)
(#pk: parser kk (bounded_int32 min max))
(#sk: serializer pk {kk.parser_kind_subkind == Some ParserStrong})
(wk: leaf_writer_strong sk)
(len: U32.t{min <= U32.v len /\ U32.v len <= max})
(b:
B.buffer U8.t
{ B.live h0 b /\ B.length b == U32.v len /\
B.loc_disjoint (B.loc_buffer b) (loc_slice_from sout pout_from0) })
: Tot
(w:
swriter (serialize_bounded_vlgenbytes min max sk) h0 0 sout pout_from0
{swvalue w == FB.hide (B.as_seq h0 b)}) | val swrite_bounded_vlgenbytes
(h0: HS.mem)
(sout:
slice (srel_of_buffer_srel (B.trivial_preorder _))
(srel_of_buffer_srel (B.trivial_preorder _)))
(pout_from0: U32.t)
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#kk: parser_kind)
(#pk: parser kk (bounded_int32 min max))
(#sk: serializer pk {kk.parser_kind_subkind == Some ParserStrong})
(wk: leaf_writer_strong sk)
(len: U32.t{min <= U32.v len /\ U32.v len <= max})
(b:
B.buffer U8.t
{ B.live h0 b /\ B.length b == U32.v len /\
B.loc_disjoint (B.loc_buffer b) (loc_slice_from sout pout_from0) })
: Tot
(w:
swriter (serialize_bounded_vlgenbytes min max sk) h0 0 sout pout_from0
{swvalue w == FB.hide (B.as_seq h0 b)}) | let swrite_bounded_vlgenbytes
(h0: HS.mem)
(sout: slice (srel_of_buffer_srel (B.trivial_preorder _)) (srel_of_buffer_srel (B.trivial_preorder _)))
(pout_from0: U32.t)
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 })
(#kk: parser_kind)
(#pk: parser kk (bounded_int32 min max))
(#sk: serializer pk { kk.parser_kind_subkind == Some ParserStrong })
(wk: leaf_writer_strong sk)
(len: U32.t { min <= U32.v len /\ U32.v len <= max })
(b: B.buffer U8.t {
B.live h0 b /\
B.length b == U32.v len /\
B.loc_disjoint (B.loc_buffer b) (loc_slice_from sout pout_from0)
})
: Tot (w: swriter (serialize_bounded_vlgenbytes min max sk) h0 0 sout pout_from0 {
swvalue w == FB.hide (B.as_seq h0 b)
})
= SWriter (Ghost.hide (FB.hide (B.as_seq h0 b))) (fun pout_from ->
serialized_length_eq (serialize_bounded_vlgenbytes min max sk) (FB.hide (B.as_seq h0 b));
length_serialize_bounded_vlgenbytes min max sk (FB.hide (B.as_seq h0 b));
serialized_length_eq sk len;
let pout_payload = swrite (swrite_leaf wk h0 sout pout_from0 len) pout_from in
let payload = B.sub sout.base pout_payload len in
B.blit b 0ul payload 0ul len;
let h = HST.get () in
valid_bounded_vlgenbytes min max pk sout pout_from h;
pout_payload `U32.add` len
) | {
"file_name": "src/lowparse/LowParse.Low.Writers.Instances.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 246,
"start_col": 0,
"start_line": 217
} | module LowParse.Low.Writers.Instances
include LowParse.Low.Writers
include LowParse.Low.Combinators
include LowParse.Low.Bytes
include LowParse.Low.BitSum
module HS = FStar.HyperStack
module B = LowStar.Buffer
module U32 = FStar.UInt32
module HST = FStar.HyperStack.ST
inline_for_extraction
noextract
let swrite_weaken
(#h0: HS.mem)
(#sout: slice (srel_of_buffer_srel (B.trivial_preorder _)) (srel_of_buffer_srel (B.trivial_preorder _)))
(#pout_from0: U32.t)
(#k1: parser_kind)
(#t1: Type)
(#p1: parser k1 t1)
(#s1: serializer p1 { k1.parser_kind_subkind == Some ParserStrong })
(#space_beyond: nat)
(k2: parser_kind)
(w1: swriter s1 h0 space_beyond sout pout_from0 {
(k2 `is_weaker_than` k1) /\
k2.parser_kind_subkind == Some ParserStrong
})
: Tot (w2: swriter (serialize_weaken k2 s1) h0 space_beyond sout pout_from0 {
swvalue w2 == swvalue w1
})
= SWriter (Ghost.hide (swvalue w1)) (fun pout_from ->
serialized_length_eq s1 (swvalue w1);
serialized_length_eq (serialize_weaken k2 s1) (swvalue w1);
let res = swrite w1 pout_from in
let h = HST.get () in
valid_weaken k2 p1 h sout pout_from;
res
)
inline_for_extraction
noextract
let swrite_nondep_then'
(#h0: HS.mem)
(#sout: slice (srel_of_buffer_srel (B.trivial_preorder _)) (srel_of_buffer_srel (B.trivial_preorder _)))
(#pout_from0: U32.t)
(#k1: parser_kind)
(#t1: Type)
(#p1: parser k1 t1)
(#s1: serializer p1 { k1.parser_kind_subkind == Some ParserStrong })
(#space_beyond: nat)
(w1: swriter s1 h0 space_beyond sout pout_from0)
(#k2: parser_kind)
(#t2: Type)
(#p2: parser k2 t2)
(#s2: serializer p2 { k2.parser_kind_subkind == Some ParserStrong })
(w2: swriter s2 h0 space_beyond sout pout_from0)
: Tot (w: swriter (s1 `serialize_nondep_then` s2) h0 space_beyond sout pout_from0 {
swvalue w == (swvalue w1, swvalue w2)
})
= SWriter (Ghost.hide (swvalue w1, swvalue w2)) (fun pout_from ->
serialized_length_eq (s1 `serialize_nondep_then` s2) (swvalue w1, swvalue w2);
serialize_nondep_then_eq s1 s2 (swvalue w1, swvalue w2);
serialized_length_eq s1 (swvalue w1);
serialized_length_eq s2 (swvalue w2);
let pos1 = swrite w1 pout_from in
let pos2 = swrite w2 pos1 in
let h' = HST.get () in
valid_nondep_then h' p1 p2 sout pout_from;
pos2
)
let max (x1 x2: nat) : Tot nat = if x1 > x2 then x1 else x2
inline_for_extraction
noextract
let swrite_nondep_then
(#h0: HS.mem)
(#sout: slice (srel_of_buffer_srel (B.trivial_preorder _)) (srel_of_buffer_srel (B.trivial_preorder _)))
(#pout_from0: U32.t)
(#k1: parser_kind)
(#t1: Type)
(#p1: parser k1 t1)
(#s1: serializer p1 { k1.parser_kind_subkind == Some ParserStrong })
(#space_beyond1: nat)
(w1: swriter s1 h0 space_beyond1 sout pout_from0)
(#k2: parser_kind)
(#t2: Type)
(#p2: parser k2 t2)
(#s2: serializer p2 { k2.parser_kind_subkind == Some ParserStrong })
(#space_beyond2: nat)
(w2: swriter s2 h0 space_beyond2 sout pout_from0)
: Tot (w: swriter (s1 `serialize_nondep_then` s2) h0 (space_beyond1 `max` space_beyond2) sout pout_from0 {
swvalue w == (swvalue w1, swvalue w2)
})
= [@inline_let]
let sbmax = space_beyond1 `max` space_beyond2 in
weaken_swriter w1 h0 sbmax pout_from0 `swrite_nondep_then'` weaken_swriter w2 h0 sbmax pout_from0
inline_for_extraction
noextract
let swrite_filter
(#h0: HS.mem)
(#sout: slice (srel_of_buffer_srel (B.trivial_preorder _)) (srel_of_buffer_srel (B.trivial_preorder _)))
(#pout_from0: U32.t)
(#k1: parser_kind)
(#t1: Type)
(#p1: parser k1 t1)
(#s1: serializer p1 { k1.parser_kind_subkind == Some ParserStrong })
(#space_beyond: nat)
(cond: (t1 -> GTot bool))
(w1: swriter s1 h0 space_beyond sout pout_from0 { cond (swvalue w1) } )
: Tot (w2: swriter (serialize_filter s1 cond) h0 space_beyond sout pout_from0 {
swvalue w2 == swvalue w1
})
= SWriter (Ghost.hide (swvalue w1)) (fun pout_from ->
serialized_length_eq (serialize_filter s1 cond) (swvalue w1);
serialized_length_eq s1 (swvalue w1);
let res = swrite w1 pout_from in
let h = HST.get () in
valid_filter h p1 cond sout pout_from;
res
)
inline_for_extraction
noextract
let swrite_synth
(#h0: HS.mem)
(#sout: slice (srel_of_buffer_srel (B.trivial_preorder _)) (srel_of_buffer_srel (B.trivial_preorder _)))
(#pout_from0: U32.t)
(#k1: parser_kind)
(#t1: Type)
(#p1: parser k1 t1)
(#s1: serializer p1 { k1.parser_kind_subkind == Some ParserStrong })
(#space_beyond: nat)
(w1: swriter s1 h0 space_beyond sout pout_from0)
(#t2: Type)
(f12: (t1 -> GTot t2))
(f21: (t2 -> GTot t1))
(prf: squash (
synth_injective f12 /\
synth_inverse f12 f21
))
: Tot (w2: swriter (serialize_synth p1 f12 s1 f21 ()) h0 space_beyond sout pout_from0 {
swvalue w2 == f12 (swvalue w1) /\
swvalue w1 == f21 (swvalue w2)
})
= [@inline_let] let _ =
serialize_synth_eq p1 f12 s1 f21 () (f12 (swvalue w1));
synth_injective_synth_inverse_synth_inverse_recip f12 f21 ()
in
SWriter (Ghost.hide (f12 (swvalue w1))) (fun pout_from ->
serialized_length_eq (serialize_synth p1 f12 s1 f21 ()) (f12 (swvalue w1));
serialized_length_eq s1 (swvalue w1);
let res = swrite w1 pout_from in
let h = HST.get () in
valid_synth h p1 f12 sout pout_from;
res
)
module U8 = FStar.UInt8
module FB = FStar.Bytes
#push-options "--z3rlimit 16"
inline_for_extraction
noextract
let swrite_flbytes
(h0: HS.mem)
(sout: slice (srel_of_buffer_srel (B.trivial_preorder _)) (srel_of_buffer_srel (B.trivial_preorder _)))
(pout_from0: U32.t)
(len: U32.t)
(b: B.buffer U8.t {
B.live h0 b /\
B.length b == U32.v len /\
B.loc_disjoint (B.loc_buffer b) (loc_slice_from sout pout_from0)
})
: Tot (w: swriter (serialize_flbytes (U32.v len)) h0 0 sout pout_from0 {
swvalue w == FB.hide (B.as_seq h0 b)
})
= SWriter (Ghost.hide (FB.hide (B.as_seq h0 b))) (fun pout_from ->
serialized_length_eq (serialize_flbytes (U32.v len)) (FB.hide (B.as_seq h0 b));
let payload = B.sub sout.base pout_from len in
B.blit b 0ul payload 0ul len;
let h = HST.get () in
valid_flbytes_intro h (U32.v len) sout pout_from;
pout_from `U32.add` len
)
inline_for_extraction
noextract
let swrite_bounded_vlbytes
(h0: HS.mem)
(sout: slice (srel_of_buffer_srel (B.trivial_preorder _)) (srel_of_buffer_srel (B.trivial_preorder _)))
(pout_from0: U32.t)
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 })
(len: U32.t { min <= U32.v len /\ U32.v len <= max })
(b: B.buffer U8.t {
B.length b == U32.v len /\
B.live h0 b /\
B.loc_disjoint (B.loc_buffer b) (loc_slice_from sout pout_from0)
})
: Tot (w: swriter (serialize_bounded_vlbytes min max) h0 0 sout pout_from0 {
swvalue w == FB.hide (B.as_seq h0 b)
})
= SWriter (Ghost.hide (FB.hide (B.as_seq h0 b))) (fun pout_from ->
serialized_length_eq (serialize_bounded_vlbytes min max) (FB.hide (B.as_seq h0 b));
length_serialize_bounded_vlbytes min max (FB.hide (B.as_seq h0 b));
let pout_payload = pout_from `U32.add` U32.uint_to_t (log256' max) in
let payload = B.sub sout.base pout_payload len in
B.blit b 0ul payload 0ul len;
finalize_bounded_vlbytes min max sout pout_from len
)
inline_for_extraction | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Endianness.fst.checked",
"LowStar.Buffer.fst.checked",
"LowParse.Low.Writers.fst.checked",
"LowParse.Low.Combinators.fsti.checked",
"LowParse.Low.Bytes.fst.checked",
"LowParse.Low.BitSum.fst.checked",
"LowParse.Endianness.BitFields.fst.checked",
"LowParse.BitFields.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Bytes.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Low.Writers.Instances.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Bytes",
"short_module": "FB"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowParse.Low.BitSum",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low.Bytes",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low.Combinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low.Writers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low.Writers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low.Writers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 16,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h0: FStar.Monotonic.HyperStack.mem ->
sout:
LowParse.Slice.slice (LowParse.Slice.srel_of_buffer_srel (LowStar.Buffer.trivial_preorder LowParse.Bytes.byte
))
(LowParse.Slice.srel_of_buffer_srel (LowStar.Buffer.trivial_preorder LowParse.Bytes.byte)) ->
pout_from0: FStar.UInt32.t ->
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
wk: LowParse.Low.Base.leaf_writer_strong sk ->
len: FStar.UInt32.t{min <= FStar.UInt32.v len /\ FStar.UInt32.v len <= max} ->
b:
LowStar.Buffer.buffer FStar.UInt8.t
{ LowStar.Monotonic.Buffer.live h0 b /\
LowStar.Monotonic.Buffer.length b == FStar.UInt32.v len /\
LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Monotonic.Buffer.loc_buffer b)
(LowParse.Slice.loc_slice_from sout pout_from0) }
-> w:
LowParse.Low.Writers.swriter (LowParse.Spec.Bytes.serialize_bounded_vlgenbytes min max sk)
h0
0
sout
pout_from0
{LowParse.Low.Writers.swvalue w == FStar.Bytes.hide (LowStar.Monotonic.Buffer.as_seq h0 b)} | Prims.Tot | [
"total"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"LowParse.Slice.slice",
"LowParse.Slice.srel_of_buffer_srel",
"LowParse.Bytes.byte",
"LowStar.Buffer.trivial_preorder",
"FStar.UInt32.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.BoundedInt.bounded_int32",
"LowParse.Spec.Base.serializer",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.parser_subkind",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.ParserStrong",
"LowParse.Low.Base.leaf_writer_strong",
"FStar.UInt32.v",
"LowStar.Buffer.buffer",
"FStar.UInt8.t",
"LowStar.Monotonic.Buffer.live",
"Prims.int",
"Prims.l_or",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt.size",
"FStar.UInt32.n",
"LowStar.Monotonic.Buffer.length",
"LowStar.Monotonic.Buffer.loc_disjoint",
"LowStar.Monotonic.Buffer.loc_buffer",
"LowParse.Slice.loc_slice_from",
"LowParse.Low.Writers.SWriter",
"LowParse.Spec.VLGen.parse_bounded_vlgen_kind",
"LowParse.Spec.Bytes.parse_all_bytes_kind",
"LowParse.Spec.Bytes.parse_bounded_vlbytes_t",
"LowParse.Spec.Bytes.parse_bounded_vlgenbytes",
"LowParse.Spec.Bytes.serialize_bounded_vlgenbytes",
"FStar.Ghost.hide",
"FStar.Bytes.hide",
"LowStar.Monotonic.Buffer.as_seq",
"FStar.UInt32.add",
"Prims.unit",
"LowParse.Low.Bytes.valid_bounded_vlgenbytes",
"FStar.HyperStack.ST.get",
"LowStar.Monotonic.Buffer.blit",
"FStar.UInt32.__uint_to_t",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Buffer.sub",
"LowParse.Slice.__proj__Mkslice__item__base",
"LowParse.Low.Writers.swrite",
"LowParse.Low.Writers.swrite_leaf",
"LowParse.Low.Base.Spec.serialized_length_eq",
"LowParse.Spec.Bytes.length_serialize_bounded_vlgenbytes",
"LowParse.Low.Writers.swriter",
"FStar.Bytes.bytes",
"LowParse.Low.Writers.swvalue"
] | [] | false | false | false | false | false | let swrite_bounded_vlgenbytes
(h0: HS.mem)
(sout:
slice (srel_of_buffer_srel (B.trivial_preorder _))
(srel_of_buffer_srel (B.trivial_preorder _)))
(pout_from0: U32.t)
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#kk: parser_kind)
(#pk: parser kk (bounded_int32 min max))
(#sk: serializer pk {kk.parser_kind_subkind == Some ParserStrong})
(wk: leaf_writer_strong sk)
(len: U32.t{min <= U32.v len /\ U32.v len <= max})
(b:
B.buffer U8.t
{ B.live h0 b /\ B.length b == U32.v len /\
B.loc_disjoint (B.loc_buffer b) (loc_slice_from sout pout_from0) })
: Tot
(w:
swriter (serialize_bounded_vlgenbytes min max sk) h0 0 sout pout_from0
{swvalue w == FB.hide (B.as_seq h0 b)}) =
| SWriter (Ghost.hide (FB.hide (B.as_seq h0 b)))
(fun pout_from ->
serialized_length_eq (serialize_bounded_vlgenbytes min max sk) (FB.hide (B.as_seq h0 b));
length_serialize_bounded_vlgenbytes min max sk (FB.hide (B.as_seq h0 b));
serialized_length_eq sk len;
let pout_payload = swrite (swrite_leaf wk h0 sout pout_from0 len) pout_from in
let payload = B.sub sout.base pout_payload len in
B.blit b 0ul payload 0ul len;
let h = HST.get () in
valid_bounded_vlgenbytes min max pk sout pout_from h;
pout_payload `U32.add` len) | false |
Sec2.HIFC.fst | Sec2.HIFC.test | val test (l: lref) (h: href)
: HIFC unit
(union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) | val test (l: lref) (h: href)
: HIFC unit
(union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) | let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 13,
"end_line": 767,
"start_col": 0,
"start_line": 760
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.write",
"Prims.unit",
"Prims.int",
"Sec2.HIFC.read",
"Sec2.HIFC.union",
"Sec2.HIFC.single",
"Sec2.HIFC.bot",
"Sec2.HIFC.add_source",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test (l: lref) (h: href)
: HIFC unit
(union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) =
| let x = read l in
write h x | false |
Hacl.Impl.Frodo.KEM.Decaps.fst | Hacl.Impl.Frodo.KEM.Decaps.get_bp_c_matrices | val get_bp_c_matrices:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h ct /\ live h bp_matrix /\ live h c_matrix /\
disjoint bp_matrix ct /\ disjoint c_matrix ct /\ disjoint bp_matrix c_matrix)
(ensures fun h0 _ h1 -> modifies (loc bp_matrix |+| loc c_matrix) h0 h1 /\
(as_matrix h1 bp_matrix, as_matrix h1 c_matrix) == S.get_bp_c_matrices a (as_seq h0 ct)) | val get_bp_c_matrices:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h ct /\ live h bp_matrix /\ live h c_matrix /\
disjoint bp_matrix ct /\ disjoint c_matrix ct /\ disjoint bp_matrix c_matrix)
(ensures fun h0 _ h1 -> modifies (loc bp_matrix |+| loc c_matrix) h0 h1 /\
(as_matrix h1 bp_matrix, as_matrix h1 c_matrix) == S.get_bp_c_matrices a (as_seq h0 ct)) | let get_bp_c_matrices a ct bp_matrix c_matrix =
let c1 = sub ct 0ul (ct1bytes_len a) in
let c2 = sub ct (ct1bytes_len a) (ct2bytes_len a) in
frodo_unpack params_nbar (params_n a) (params_logq a) c1 bp_matrix;
frodo_unpack params_nbar params_nbar (params_logq a) c2 c_matrix | {
"file_name": "code/frodo/Hacl.Impl.Frodo.KEM.Decaps.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 66,
"end_line": 48,
"start_col": 0,
"start_line": 44
} | module Hacl.Impl.Frodo.KEM.Decaps
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Hacl.Impl.Matrix
open Hacl.Impl.Frodo.Params
open Hacl.Impl.Frodo.KEM
open Hacl.Impl.Frodo.KEM.Encaps
open Hacl.Impl.Frodo.Encode
open Hacl.Impl.Frodo.Pack
open Hacl.Impl.Frodo.Sample
open Hacl.Frodo.Random
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module FP = Spec.Frodo.Params
module KG = Hacl.Impl.Frodo.KEM.KeyGen
module S = Spec.Frodo.KEM.Decaps
module M = Spec.Matrix
#set-options "--z3rlimit 100 --fuel 0 --ifuel 0"
inline_for_extraction noextract
val get_bp_c_matrices:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h ct /\ live h bp_matrix /\ live h c_matrix /\
disjoint bp_matrix ct /\ disjoint c_matrix ct /\ disjoint bp_matrix c_matrix)
(ensures fun h0 _ h1 -> modifies (loc bp_matrix |+| loc c_matrix) h0 h1 /\
(as_matrix h1 bp_matrix, as_matrix h1 c_matrix) == S.get_bp_c_matrices a (as_seq h0 ct)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Params.fst.checked",
"Spec.Frodo.KEM.Decaps.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Matrix.fst.checked",
"Hacl.Impl.Frodo.Sample.fst.checked",
"Hacl.Impl.Frodo.Params.fst.checked",
"Hacl.Impl.Frodo.Pack.fst.checked",
"Hacl.Impl.Frodo.KEM.KeyGen.fst.checked",
"Hacl.Impl.Frodo.KEM.Encaps.fst.checked",
"Hacl.Impl.Frodo.KEM.fst.checked",
"Hacl.Impl.Frodo.Encode.fst.checked",
"Hacl.Frodo.Random.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Frodo.KEM.Decaps.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.KEM.Decaps",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Frodo.KEM.KeyGen",
"short_module": "KG"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.Params",
"short_module": "FP"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Frodo.Random",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Sample",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Pack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Encode",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM.Encaps",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Params",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Matrix",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Spec.Frodo.Params.frodo_alg ->
ct: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_ciphertextbytes a) ->
bp_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
(Hacl.Impl.Frodo.Params.params_n a) ->
c_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
Hacl.Impl.Frodo.Params.params_nbar
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Frodo.Params.frodo_alg",
"Hacl.Impl.Matrix.lbytes",
"Hacl.Impl.Frodo.Params.crypto_ciphertextbytes",
"Hacl.Impl.Matrix.matrix_t",
"Hacl.Impl.Frodo.Params.params_nbar",
"Hacl.Impl.Frodo.Params.params_n",
"Hacl.Impl.Frodo.Pack.frodo_unpack",
"Hacl.Impl.Frodo.Params.params_logq",
"Prims.unit",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"Hacl.Impl.Frodo.Params.ct2bytes_len",
"Lib.Buffer.sub",
"Lib.IntTypes.uint8",
"Hacl.Impl.Frodo.Params.ct1bytes_len",
"FStar.UInt32.__uint_to_t"
] | [] | false | true | false | false | false | let get_bp_c_matrices a ct bp_matrix c_matrix =
| let c1 = sub ct 0ul (ct1bytes_len a) in
let c2 = sub ct (ct1bytes_len a) (ct2bytes_len a) in
frodo_unpack params_nbar (params_n a) (params_logq a) c1 bp_matrix;
frodo_unpack params_nbar params_nbar (params_logq a) c2 c_matrix | false |
Sec2.HIFC.fst | Sec2.HIFC.test3_1 | val test3_1 (l: lref) (h: href) (x: int)
: HIFC int
(single l)
(single h)
[]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l) | val test3_1 (l: lref) (h: href) (x: int)
: HIFC int
(single l)
(single h)
[]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l) | let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 10,
"end_line": 797,
"start_col": 0,
"start_line": 792
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> x: Prims.int -> Sec2.HIFC.HIFC Prims.int | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Prims.int",
"Sec2.HIFC.read",
"Prims.unit",
"Sec2.HIFC.write",
"Sec2.HIFC.single",
"Prims.Nil",
"Sec2.HIFC.flow",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.l_and",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test3_1 (l: lref) (h: href) (x: int)
: HIFC int
(single l)
(single h)
[]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l) =
| write h 0;
read l | false |
Sec2.HIFC.fst | Sec2.HIFC.lift_ist_hst | val lift_ist_hst (a r w fs p q: _) (x: hifc a r w fs p q)
: hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) | val lift_ist_hst (a r w fs p q: _) (x: hifc a r w fs p q)
: hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) | let lift_ist_hst a r w fs p q (x:hifc a r w fs p q)
: hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= frame _ _ _ _ x | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 948,
"start_col": 0,
"start_line": 946
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise
//This one still reports a leakage, even though it doesn't really leak h
let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x
let refine_test9 (l:lref) (h:href)
: (unit -> HIFC unit (union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l))
= refine_flow (fun () -> test9 l h)
assume
val cw0 : label
assume
val cr0 : label
assume
val c0 (_:unit) : IFC unit cr0 cw0 []
assume
val cw1 : label
assume
val cr1 : label
assume
val c1 (_:unit) : IFC unit cr1 cw1 []
assume
val cw2 : label
assume
val cr2 : label
assume
val c2 (_:unit) : IFC unit cr2 cw2 []
let test10 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0
((bot, union cw1 cw2)::
(add_source cr1 [bot, cw2])))
= c0 (); (c1();c2())
let test12 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2);
(union cr0 cr1, cw2)]
= c0 (); (c1();c2())
let test12_1 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, cw1); (union cr0 cr1, cw2)]
= c0 (); (c1();c2())
let test13 ()
: IFC unit (union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
(add_source cr0 [bot, cw1] @
add_source (union cr0 cr1) [bot, cw2])
= (c0 (); c1());c2()
let test14 ()
: IFC unit (union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
([cr0, cw1;
union cr0 cr1, cw2])
= (c0 (); c1()); c2()
let test15 (l:lref)
: HIFC unit (single l) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s0 l == sel s1 l)
= write l (read l)
let ist_exn a w r fs (p:pre) (q:post a) =
unit -> HIFC (option a) w r fs p
(fun s0 x s1 ->
match x with
| None -> True
| Some x -> q s0 x s1) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Type ->
r: Sec2.HIFC.label ->
w: Sec2.HIFC.label ->
fs: Sec2.HIFC.flows ->
p: Sec2.HIFC.pre ->
q: Sec2.HIFC.post a ->
x: Sec2.HIFC.hifc a r w fs p q
-> Sec2.HIFC.hst a p (fun s0 x s1 -> q s0 x s1 /\ Sec2.HIFC.modifies w s0 s1) | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Sec2.HIFC.hifc",
"Sec2.HIFC.frame",
"Sec2.HIFC.hst",
"Sec2.HIFC.store",
"Prims.l_and",
"Sec2.HIFC.modifies"
] | [] | false | false | false | false | false | let lift_ist_hst a r w fs p q (x: hifc a r w fs p q)
: hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) =
| frame _ _ _ _ x | false |
Sec2.HIFC.fst | Sec2.HIFC.test4 | val test4 (l: lref) (h: href) (x: int)
: HIFC int
(single h)
(single l)
[single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h) | val test4 (l: lref) (h: href) (x: int)
: HIFC int
(single h)
(single l)
[single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h) | let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 10,
"end_line": 805,
"start_col": 0,
"start_line": 800
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> x: Prims.int -> Sec2.HIFC.HIFC Prims.int | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Prims.int",
"Sec2.HIFC.read",
"Prims.unit",
"Sec2.HIFC.write",
"Sec2.HIFC.single",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Sec2.HIFC.bot",
"Prims.Nil",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.l_and",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test4 (l: lref) (h: href) (x: int)
: HIFC int
(single h)
(single l)
[single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h) =
| write l x;
read h | false |
Sec2.HIFC.fst | Sec2.HIFC.test5 | val test5 (l: lref) (h: href) (x: int)
: HIFC int
(single h)
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h) | val test5 (l: lref) (h: href) (x: int)
: HIFC int
(single h)
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h) | let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 10,
"end_line": 812,
"start_col": 0,
"start_line": 807
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> x: Prims.int -> Sec2.HIFC.HIFC Prims.int | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Prims.int",
"Sec2.HIFC.read",
"Prims.unit",
"Sec2.HIFC.write",
"Sec2.HIFC.single",
"Prims.Nil",
"Sec2.HIFC.flow",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.l_and",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test5 (l: lref) (h: href) (x: int)
: HIFC int
(single h)
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h) =
| write l x;
read h | false |
Sec2.HIFC.fst | Sec2.HIFC.test3 | val test3 (l: lref) (h: href)
: HIFC unit
(single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) | val test3 (l: lref) (h: href)
: HIFC unit
(single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) | let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 20,
"end_line": 784,
"start_col": 0,
"start_line": 778
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.write",
"Prims.unit",
"Prims.int",
"Sec2.HIFC.read",
"Sec2.HIFC.single",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test3 (l: lref) (h: href)
: HIFC unit
(single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) =
| write h (read l) | false |
Sec2.HIFC.fst | Sec2.HIFC.assoc_hst | val assoc_hst : _: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) ->
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) ->
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow)
-> Prims.unit | let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
} | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 532,
"start_col": 0,
"start_line": 496
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) ->
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow) ->
_: ((Sec2.HIFC.label * Sec2.HIFC.label) * Prims.list Sec2.HIFC.flow)
-> Prims.unit | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple3",
"Sec2.HIFC.label",
"Prims.list",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple3",
"FStar.Calc.calc_finish",
"FStar.Set.set",
"Sec2.HIFC.loc",
"Prims.eq2",
"Sec2.HIFC.ifc_triple",
"Sec2.HIFC.union",
"FStar.List.Tot.Base.op_At",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.add_source",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"Sec2.HIFC.bot",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"Prims._assert",
"Prims.l_Forall",
"FStar.Set.equal"
] | [] | false | false | false | true | false | let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
| calc ( == ) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2));
( == ) { () }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2) :: fs2)));
( == ) { () }
(union w0 (union w1 w2),
union r0 (union r1 r2),
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2) :: fs2)))));
( == ) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2) :: fs2)))));
( == ) { () }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2) :: fs2)))));
( == ) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) :: add_source r1 fs2))));
};
calc ( == ) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
( == ) { () }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1) :: fs1))) (w2, r2, fs2);
( == ) { () }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1) :: fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
( == ) { () }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1) :: add_source r0 fs1)) @
((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
( == ) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1) :: add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
} | false |
|
Sec2.HIFC.fst | Sec2.HIFC.test6 | val test6 (l: lref) (h: href)
: HIFC unit
low
high
[low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l) | val test6 (l: lref) (h: href)
: HIFC unit
low
high
[low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l) | let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 13,
"end_line": 819,
"start_col": 0,
"start_line": 814
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.write",
"Prims.unit",
"Prims.int",
"Sec2.HIFC.read",
"Sec2.HIFC.low",
"Sec2.HIFC.high",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test6 (l: lref) (h: href)
: HIFC unit
low
high
[low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l) =
| let x = read l in
write h x | false |
Sec2.HIFC.fst | Sec2.HIFC.refine_test8 | val refine_test8: l: lref -> h: href -> unit
-> HIFC unit
(union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1) | val refine_test8: l: lref -> h: href -> unit
-> HIFC unit
(union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1) | let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 850,
"start_col": 0,
"start_line": 846
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> _: Prims.unit -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.refine_flow",
"Prims.unit",
"Sec2.HIFC.single",
"Sec2.HIFC.union",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil",
"Sec2.HIFC.flow",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Prims.int",
"Sec2.HIFC.sel",
"Prims.op_Addition",
"Sec2.HIFC.test8"
] | [] | false | true | false | false | false | let refine_test8 (l: lref) (h: href)
: unit
-> HIFC unit
(union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1) =
| refine_flow (fun () -> test8 l h) | false |
Sec2.HIFC.fst | Sec2.HIFC.test2 | val test2 (l: lref) (h: href)
: HIFC unit
(single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) | val test2 (l: lref) (h: href)
: HIFC unit
(single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) | let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 13,
"end_line": 776,
"start_col": 0,
"start_line": 769
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.write",
"Prims.unit",
"Prims.int",
"Sec2.HIFC.read",
"Sec2.HIFC.single",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test2 (l: lref) (h: href)
: HIFC unit
(single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) =
| let x = read l in
write h x | false |
Sec2.HIFC.fst | Sec2.HIFC.test3_lab | val test3_lab (l: lref) (h: href)
: HIFC unit
low
high
[low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) | val test3_lab (l: lref) (h: href)
: HIFC unit
low
high
[low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) | let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 20,
"end_line": 790,
"start_col": 0,
"start_line": 786
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.write",
"Prims.unit",
"Prims.int",
"Sec2.HIFC.read",
"Sec2.HIFC.low",
"Sec2.HIFC.high",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test3_lab (l: lref) (h: href)
: HIFC unit
low
high
[low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l) =
| write h (read l) | false |
Sec2.HIFC.fst | Sec2.HIFC.refine_test9 | val refine_test9 (l: lref) (h: href)
: (unit
-> HIFC unit
(union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)) | val refine_test9 (l: lref) (h: href)
: (unit
-> HIFC unit
(union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)) | let refine_test9 (l:lref) (h:href)
: (unit -> HIFC unit (union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l))
= refine_flow (fun () -> test9 l h) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 870,
"start_col": 0,
"start_line": 864
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise
//This one still reports a leakage, even though it doesn't really leak h
let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> _: Prims.unit -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.refine_flow",
"Prims.unit",
"Sec2.HIFC.single",
"Sec2.HIFC.union",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil",
"Sec2.HIFC.flow",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Prims.int",
"Sec2.HIFC.sel",
"Sec2.HIFC.test9"
] | [] | false | true | false | false | false | let refine_test9 (l: lref) (h: href)
: (unit
-> HIFC unit
(union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)) =
| refine_flow (fun () -> test9 l h) | false |
Sec2.HIFC.fst | Sec2.HIFC.test15 | val test15 (l: lref)
: HIFC unit
(single l)
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s0 l == sel s1 l) | val test15 (l: lref)
: HIFC unit
(single l)
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s0 l == sel s1 l) | let test15 (l:lref)
: HIFC unit (single l) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s0 l == sel s1 l)
= write l (read l) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 20,
"end_line": 936,
"start_col": 0,
"start_line": 932
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise
//This one still reports a leakage, even though it doesn't really leak h
let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x
let refine_test9 (l:lref) (h:href)
: (unit -> HIFC unit (union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l))
= refine_flow (fun () -> test9 l h)
assume
val cw0 : label
assume
val cr0 : label
assume
val c0 (_:unit) : IFC unit cr0 cw0 []
assume
val cw1 : label
assume
val cr1 : label
assume
val c1 (_:unit) : IFC unit cr1 cw1 []
assume
val cw2 : label
assume
val cr2 : label
assume
val c2 (_:unit) : IFC unit cr2 cw2 []
let test10 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0
((bot, union cw1 cw2)::
(add_source cr1 [bot, cw2])))
= c0 (); (c1();c2())
let test12 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2);
(union cr0 cr1, cw2)]
= c0 (); (c1();c2())
let test12_1 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, cw1); (union cr0 cr1, cw2)]
= c0 (); (c1();c2())
let test13 ()
: IFC unit (union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
(add_source cr0 [bot, cw1] @
add_source (union cr0 cr1) [bot, cw2])
= (c0 (); c1());c2()
let test14 ()
: IFC unit (union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
([cr0, cw1;
union cr0 cr1, cw2])
= (c0 (); c1()); c2() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.write",
"Prims.unit",
"Prims.int",
"Sec2.HIFC.read",
"Sec2.HIFC.single",
"Prims.Nil",
"Sec2.HIFC.flow",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test15 (l: lref)
: HIFC unit
(single l)
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s0 l == sel s1 l) =
| write l (read l) | false |
Sec2.HIFC.fst | Sec2.HIFC.ist_exn | val ist_exn : a: Type ->
w: Sec2.HIFC.label ->
r: Sec2.HIFC.label ->
fs: Sec2.HIFC.flows ->
p: Sec2.HIFC.pre ->
q: Sec2.HIFC.post a
-> Type | let ist_exn a w r fs (p:pre) (q:post a) =
unit -> HIFC (option a) w r fs p
(fun s0 x s1 ->
match x with
| None -> True
| Some x -> q s0 x s1) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 39,
"end_line": 943,
"start_col": 0,
"start_line": 938
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise
//This one still reports a leakage, even though it doesn't really leak h
let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x
let refine_test9 (l:lref) (h:href)
: (unit -> HIFC unit (union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l))
= refine_flow (fun () -> test9 l h)
assume
val cw0 : label
assume
val cr0 : label
assume
val c0 (_:unit) : IFC unit cr0 cw0 []
assume
val cw1 : label
assume
val cr1 : label
assume
val c1 (_:unit) : IFC unit cr1 cw1 []
assume
val cw2 : label
assume
val cr2 : label
assume
val c2 (_:unit) : IFC unit cr2 cw2 []
let test10 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0
((bot, union cw1 cw2)::
(add_source cr1 [bot, cw2])))
= c0 (); (c1();c2())
let test12 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2);
(union cr0 cr1, cw2)]
= c0 (); (c1();c2())
let test12_1 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, cw1); (union cr0 cr1, cw2)]
= c0 (); (c1();c2())
let test13 ()
: IFC unit (union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
(add_source cr0 [bot, cw1] @
add_source (union cr0 cr1) [bot, cw2])
= (c0 (); c1());c2()
let test14 ()
: IFC unit (union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
([cr0, cw1;
union cr0 cr1, cw2])
= (c0 (); c1()); c2()
let test15 (l:lref)
: HIFC unit (single l) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s0 l == sel s1 l)
= write l (read l) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Type ->
w: Sec2.HIFC.label ->
r: Sec2.HIFC.label ->
fs: Sec2.HIFC.flows ->
p: Sec2.HIFC.pre ->
q: Sec2.HIFC.post a
-> Type | Prims.Tot | [
"total"
] | [] | [
"Sec2.HIFC.label",
"Sec2.HIFC.flows",
"Sec2.HIFC.pre",
"Sec2.HIFC.post",
"Prims.unit",
"FStar.Pervasives.Native.option",
"Sec2.HIFC.store",
"Prims.l_True"
] | [] | false | false | false | true | true | let ist_exn a w r fs (p: pre) (q: post a) =
| unit
-> HIFC (option a)
w
r
fs
p
(fun s0 x s1 ->
match x with
| None -> True
| Some x -> q s0 x s1) | false |
|
Sec2.HIFC.fst | Sec2.HIFC.test10 | val test10: Prims.unit
-> IFC unit
(union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0 ((bot, union cw1 cw2) :: (add_source cr1 [bot, cw2]))) | val test10: Prims.unit
-> IFC unit
(union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0 ((bot, union cw1 cw2) :: (add_source cr1 [bot, cw2]))) | let test10 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0
((bot, union cw1 cw2)::
(add_source cr1 [bot, cw2])))
= c0 (); (c1();c2()) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 901,
"start_col": 0,
"start_line": 895
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise
//This one still reports a leakage, even though it doesn't really leak h
let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x
let refine_test9 (l:lref) (h:href)
: (unit -> HIFC unit (union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l))
= refine_flow (fun () -> test9 l h)
assume
val cw0 : label
assume
val cr0 : label
assume
val c0 (_:unit) : IFC unit cr0 cw0 []
assume
val cw1 : label
assume
val cr1 : label
assume
val c1 (_:unit) : IFC unit cr1 cw1 []
assume
val cw2 : label
assume
val cr2 : label
assume
val c2 (_:unit) : IFC unit cr2 cw2 [] | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> Sec2.HIFC.IFC Prims.unit | Sec2.HIFC.IFC | [] | [] | [
"Prims.unit",
"Sec2.HIFC.c2",
"Sec2.HIFC.c1",
"Sec2.HIFC.c0",
"Sec2.HIFC.union",
"Sec2.HIFC.cr0",
"Sec2.HIFC.cr1",
"Sec2.HIFC.cr2",
"Sec2.HIFC.cw0",
"Sec2.HIFC.cw1",
"Sec2.HIFC.cw2",
"Sec2.HIFC.add_source",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Sec2.HIFC.bot",
"Prims.Nil"
] | [] | false | true | false | false | false | let test10 ()
: IFC unit
(union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0 ((bot, union cw1 cw2) :: (add_source cr1 [bot, cw2]))) =
| c0 ();
(c1 ();
c2 ()) | false |
Sec2.HIFC.fst | Sec2.HIFC.test13 | val test13: Prims.unit
-> IFC unit
(union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
(add_source cr0 [bot, cw1] @ add_source (union cr0 cr1) [bot, cw2]) | val test13: Prims.unit
-> IFC unit
(union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
(add_source cr0 [bot, cw1] @ add_source (union cr0 cr1) [bot, cw2]) | let test13 ()
: IFC unit (union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
(add_source cr0 [bot, cw1] @
add_source (union cr0 cr1) [bot, cw2])
= (c0 (); c1());c2() | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 923,
"start_col": 0,
"start_line": 918
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise
//This one still reports a leakage, even though it doesn't really leak h
let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x
let refine_test9 (l:lref) (h:href)
: (unit -> HIFC unit (union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l))
= refine_flow (fun () -> test9 l h)
assume
val cw0 : label
assume
val cr0 : label
assume
val c0 (_:unit) : IFC unit cr0 cw0 []
assume
val cw1 : label
assume
val cr1 : label
assume
val c1 (_:unit) : IFC unit cr1 cw1 []
assume
val cw2 : label
assume
val cr2 : label
assume
val c2 (_:unit) : IFC unit cr2 cw2 []
let test10 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0
((bot, union cw1 cw2)::
(add_source cr1 [bot, cw2])))
= c0 (); (c1();c2())
let test12 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2);
(union cr0 cr1, cw2)]
= c0 (); (c1();c2())
let test12_1 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, cw1); (union cr0 cr1, cw2)]
= c0 (); (c1();c2()) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> Sec2.HIFC.IFC Prims.unit | Sec2.HIFC.IFC | [] | [] | [
"Prims.unit",
"Sec2.HIFC.c2",
"Sec2.HIFC.c1",
"Sec2.HIFC.c0",
"Sec2.HIFC.union",
"Sec2.HIFC.cr0",
"Sec2.HIFC.cr1",
"Sec2.HIFC.cr2",
"Sec2.HIFC.cw0",
"Sec2.HIFC.cw1",
"Sec2.HIFC.cw2",
"FStar.List.Tot.Base.op_At",
"Sec2.HIFC.flow",
"Sec2.HIFC.add_source",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Sec2.HIFC.bot",
"Prims.Nil"
] | [] | false | true | false | false | false | let test13 ()
: IFC unit
(union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
(add_source cr0 [bot, cw1] @ add_source (union cr0 cr1) [bot, cw2]) =
| (c0 ();
c1 ());
c2 () | false |
Sec2.HIFC.fst | Sec2.HIFC.test7 | val test7 (l: lref) (h: href)
: HIFC unit
(single h)
(single l)
[high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h) | val test7 (l: lref) (h: href)
: HIFC unit
(single h)
(single l)
[high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h) | let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 13,
"end_line": 827,
"start_col": 0,
"start_line": 822
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.write",
"Prims.unit",
"Prims.int",
"Sec2.HIFC.read",
"Sec2.HIFC.single",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Sec2.HIFC.high",
"Sec2.HIFC.low",
"Prims.Nil",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test7 (l: lref) (h: href)
: HIFC unit
(single h)
(single l)
[high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h) =
| let x = read h in
write l x | false |
Sec2.HIFC.fst | Sec2.HIFC.test8 | val test8 (l: lref) (h: href)
: HIFC unit
(union (single h) (single l))
(single l)
[(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1) | val test8 (l: lref) (h: href)
: HIFC unit
(union (single h) (single l))
(single l)
[(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1) | let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 837,
"start_col": 0,
"start_line": 831
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.write",
"Prims.op_Addition",
"Prims.unit",
"Prims.int",
"Sec2.HIFC.read",
"Sec2.HIFC.union",
"Sec2.HIFC.single",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test8 (l: lref) (h: href)
: HIFC unit
(union (single h) (single l))
(single l)
[(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1) =
| let x0 = read h in
let x = read l in
write l (x + 1) | false |
Sec2.HIFC.fst | Sec2.HIFC.test14 | val test14: Prims.unit
-> IFC unit
(union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
([cr0, cw1; union cr0 cr1, cw2]) | val test14: Prims.unit
-> IFC unit
(union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
([cr0, cw1; union cr0 cr1, cw2]) | let test14 ()
: IFC unit (union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
([cr0, cw1;
union cr0 cr1, cw2])
= (c0 (); c1()); c2() | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 23,
"end_line": 930,
"start_col": 0,
"start_line": 925
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise
//This one still reports a leakage, even though it doesn't really leak h
let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x
let refine_test9 (l:lref) (h:href)
: (unit -> HIFC unit (union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l))
= refine_flow (fun () -> test9 l h)
assume
val cw0 : label
assume
val cr0 : label
assume
val c0 (_:unit) : IFC unit cr0 cw0 []
assume
val cw1 : label
assume
val cr1 : label
assume
val c1 (_:unit) : IFC unit cr1 cw1 []
assume
val cw2 : label
assume
val cr2 : label
assume
val c2 (_:unit) : IFC unit cr2 cw2 []
let test10 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0
((bot, union cw1 cw2)::
(add_source cr1 [bot, cw2])))
= c0 (); (c1();c2())
let test12 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2);
(union cr0 cr1, cw2)]
= c0 (); (c1();c2())
let test12_1 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, cw1); (union cr0 cr1, cw2)]
= c0 (); (c1();c2())
let test13 ()
: IFC unit (union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
(add_source cr0 [bot, cw1] @
add_source (union cr0 cr1) [bot, cw2])
= (c0 (); c1());c2() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> Sec2.HIFC.IFC Prims.unit | Sec2.HIFC.IFC | [] | [] | [
"Prims.unit",
"Sec2.HIFC.c2",
"Sec2.HIFC.c1",
"Sec2.HIFC.c0",
"Sec2.HIFC.union",
"Sec2.HIFC.cr0",
"Sec2.HIFC.cr1",
"Sec2.HIFC.cr2",
"Sec2.HIFC.cw0",
"Sec2.HIFC.cw1",
"Sec2.HIFC.cw2",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil"
] | [] | false | true | false | false | false | let test14 ()
: IFC unit
(union (union cr0 cr1) cr2)
(union (union cw0 cw1) cw2)
([cr0, cw1; union cr0 cr1, cw2]) =
| (c0 ();
c1 ());
c2 () | false |
Hacl.Impl.Frodo.KEM.Decaps.fst | Hacl.Impl.Frodo.KEM.Decaps.crypto_kem_dec_mu | val crypto_kem_dec_mu:
a:FP.frodo_alg
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h sk /\ live h mu_decode /\
live h bp_matrix /\ live h c_matrix /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0) /\
loc_pairwise_disjoint [loc sk; loc mu_decode; loc bp_matrix; loc c_matrix])
(ensures fun h0 _ h1 -> modifies (loc mu_decode) h0 h1 /\
as_seq h1 mu_decode == S.crypto_kem_dec_mu a (as_seq h0 sk)
(as_matrix h0 bp_matrix) (as_matrix h0 c_matrix)) | val crypto_kem_dec_mu:
a:FP.frodo_alg
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h sk /\ live h mu_decode /\
live h bp_matrix /\ live h c_matrix /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0) /\
loc_pairwise_disjoint [loc sk; loc mu_decode; loc bp_matrix; loc c_matrix])
(ensures fun h0 _ h1 -> modifies (loc mu_decode) h0 h1 /\
as_seq h1 mu_decode == S.crypto_kem_dec_mu a (as_seq h0 sk)
(as_matrix h0 bp_matrix) (as_matrix h0 c_matrix)) | let crypto_kem_dec_mu a sk bp_matrix c_matrix mu_decode =
FP.expand_crypto_secretkeybytes a;
let s_bytes = sub sk (crypto_bytes a +! crypto_publickeybytes a) (secretmatrixbytes_len a) in
frodo_mu_decode a s_bytes bp_matrix c_matrix mu_decode | {
"file_name": "code/frodo/Hacl.Impl.Frodo.KEM.Decaps.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 56,
"end_line": 330,
"start_col": 0,
"start_line": 327
} | module Hacl.Impl.Frodo.KEM.Decaps
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Hacl.Impl.Matrix
open Hacl.Impl.Frodo.Params
open Hacl.Impl.Frodo.KEM
open Hacl.Impl.Frodo.KEM.Encaps
open Hacl.Impl.Frodo.Encode
open Hacl.Impl.Frodo.Pack
open Hacl.Impl.Frodo.Sample
open Hacl.Frodo.Random
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module FP = Spec.Frodo.Params
module KG = Hacl.Impl.Frodo.KEM.KeyGen
module S = Spec.Frodo.KEM.Decaps
module M = Spec.Matrix
#set-options "--z3rlimit 100 --fuel 0 --ifuel 0"
inline_for_extraction noextract
val get_bp_c_matrices:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h ct /\ live h bp_matrix /\ live h c_matrix /\
disjoint bp_matrix ct /\ disjoint c_matrix ct /\ disjoint bp_matrix c_matrix)
(ensures fun h0 _ h1 -> modifies (loc bp_matrix |+| loc c_matrix) h0 h1 /\
(as_matrix h1 bp_matrix, as_matrix h1 c_matrix) == S.get_bp_c_matrices a (as_seq h0 ct))
let get_bp_c_matrices a ct bp_matrix c_matrix =
let c1 = sub ct 0ul (ct1bytes_len a) in
let c2 = sub ct (ct1bytes_len a) (ct2bytes_len a) in
frodo_unpack params_nbar (params_n a) (params_logq a) c1 bp_matrix;
frodo_unpack params_nbar params_nbar (params_logq a) c2 c_matrix
inline_for_extraction noextract
val frodo_mu_decode:
a:FP.frodo_alg
-> s_bytes:lbytes (secretmatrixbytes_len a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h s_bytes /\ live h bp_matrix /\
live h c_matrix /\ live h mu_decode /\
disjoint mu_decode s_bytes /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0))
(ensures fun h0 _ h1 -> modifies (loc mu_decode) h0 h1 /\
as_seq h1 mu_decode ==
S.frodo_mu_decode a (as_seq h0 s_bytes) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let frodo_mu_decode a s_bytes bp_matrix c_matrix mu_decode =
push_frame();
let s_matrix = matrix_create (params_n a) params_nbar in
let m_matrix = matrix_create params_nbar params_nbar in
matrix_from_lbytes s_bytes s_matrix;
matrix_mul_s bp_matrix s_matrix m_matrix;
matrix_sub c_matrix m_matrix;
frodo_key_decode (params_logq a) (params_extracted_bits a) params_nbar m_matrix mu_decode;
clear_matrix s_matrix;
clear_matrix m_matrix;
pop_frame()
inline_for_extraction noextract
val get_bpp_cp_matrices_:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> sp_matrix:matrix_t params_nbar (params_n a)
-> ep_matrix:matrix_t params_nbar (params_n a)
-> epp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
live h sp_matrix /\ live h ep_matrix /\ live h epp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc sk; loc bpp_matrix;
loc cp_matrix; loc sp_matrix; loc ep_matrix; loc epp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices_ a gen_a (as_seq h0 mu_decode) (as_seq h0 sk)
(as_matrix h0 sp_matrix) (as_matrix h0 ep_matrix) (as_matrix h0 epp_matrix))
let get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix =
FP.expand_crypto_secretkeybytes a;
FP.expand_crypto_publickeybytes a;
let pk = sub sk (crypto_bytes a) (crypto_publickeybytes a) in
let seed_a = sub pk 0ul bytes_seed_a in
let b = sub pk bytes_seed_a (crypto_publickeybytes a -! bytes_seed_a) in
frodo_mul_add_sa_plus_e a gen_a seed_a sp_matrix ep_matrix bpp_matrix;
frodo_mul_add_sb_plus_e_plus_mu a mu_decode b sp_matrix epp_matrix cp_matrix;
mod_pow2 (params_logq a) bpp_matrix;
mod_pow2 (params_logq a) cp_matrix
#push-options "--z3rlimit 150"
inline_for_extraction noextract
val get_bpp_cp_matrices:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> seed_se:lbytes (crypto_bytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h seed_se /\ live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc seed_se; loc sk; loc bpp_matrix; loc cp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices a gen_a (as_seq h0 mu_decode) (as_seq h0 seed_se) (as_seq h0 sk))
let get_bpp_cp_matrices a gen_a mu_decode seed_se sk bpp_matrix cp_matrix =
push_frame ();
let sp_matrix = matrix_create params_nbar (params_n a) in
let ep_matrix = matrix_create params_nbar (params_n a) in
let epp_matrix = matrix_create params_nbar params_nbar in
get_sp_ep_epp_matrices a seed_se sp_matrix ep_matrix epp_matrix;
get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix;
clear_matrix3 a sp_matrix ep_matrix epp_matrix;
pop_frame ()
#pop-options
inline_for_extraction noextract
val crypto_kem_dec_kp_s_cond:
a:FP.frodo_alg
-> bp_matrix:matrix_t params_nbar (params_n a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h bp_matrix /\ live h bpp_matrix /\
live h c_matrix /\ live h cp_matrix)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s_cond a (as_matrix h0 bp_matrix) (as_matrix h0 bpp_matrix)
(as_matrix h0 c_matrix) (as_matrix h0 cp_matrix))
let crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix =
let b1 = matrix_eq bp_matrix bpp_matrix in
let b2 = matrix_eq c_matrix cp_matrix in
b1 &. b2
inline_for_extraction noextract
val crypto_kem_dec_kp_s:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> seed_se:lbytes (crypto_bytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h mu_decode /\ live h bp_matrix /\ live h seed_se /\
live h c_matrix /\ live h sk /\
loc_pairwise_disjoint [loc mu_decode; loc seed_se; loc sk])
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s a gen_a (as_seq h0 mu_decode) (as_seq h0 seed_se)
(as_seq h0 sk) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let crypto_kem_dec_kp_s a gen_a mu_decode seed_se sk bp_matrix c_matrix =
push_frame ();
let bpp_matrix = matrix_create params_nbar (params_n a) in
let cp_matrix = matrix_create params_nbar params_nbar in
get_bpp_cp_matrices a gen_a mu_decode seed_se sk bpp_matrix cp_matrix;
let mask = crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix in
pop_frame ();
mask
inline_for_extraction noextract
val crypto_kem_dec_ss0:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> mask:uint16{v mask == 0 \/ v mask == v (ones U16 SEC)}
-> kp:lbytes (crypto_bytes a)
-> s:lbytes (crypto_bytes a)
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h ct /\ live h kp /\ live h s /\ live h ss /\
disjoint ss ct /\ disjoint ss kp /\ disjoint ss s /\
disjoint kp s)
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss0 a (as_seq h0 ct) mask (as_seq h0 kp) (as_seq h0 s))
let crypto_kem_dec_ss0 a ct mask kp s ss =
push_frame ();
let kp_s = create (crypto_bytes a) (u8 0) in
Lib.ByteBuffer.buf_mask_select kp s (to_u8 mask) kp_s;
let ss_init_len = crypto_ciphertextbytes a +! crypto_bytes a in
let ss_init = create ss_init_len (u8 0) in
concat2 (crypto_ciphertextbytes a) ct (crypto_bytes a) kp_s ss_init;
frodo_shake a ss_init_len ss_init (crypto_bytes a) ss;
clear_words_u8 ss_init;
clear_words_u8 kp_s;
pop_frame ()
inline_for_extraction noextract
val crypto_kem_dec_seed_se_k:
a:FP.frodo_alg
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> seed_se_k:lbytes (2ul *! crypto_bytes a)
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\ live h seed_se_k /\
disjoint mu_decode seed_se_k /\ disjoint sk seed_se_k)
(ensures fun h0 _ h1 -> modifies (loc seed_se_k) h0 h1 /\
as_seq h1 seed_se_k == S.crypto_kem_dec_seed_se_k a (as_seq h0 mu_decode) (as_seq h0 sk))
let crypto_kem_dec_seed_se_k a mu_decode sk seed_se_k =
push_frame ();
let pkh_mu_decode_len = bytes_pkhash a +! bytes_mu a in
let pkh_mu_decode = create pkh_mu_decode_len (u8 0) in
let pkh = sub sk (crypto_secretkeybytes a -! bytes_pkhash a) (bytes_pkhash a) in
concat2 (bytes_pkhash a) pkh (bytes_mu a) mu_decode pkh_mu_decode;
frodo_shake a pkh_mu_decode_len pkh_mu_decode (2ul *! crypto_bytes a) seed_se_k;
pop_frame ()
inline_for_extraction noextract
val crypto_kem_dec_ss1:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> ct:lbytes (crypto_ciphertextbytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> mu_decode:lbytes (bytes_mu a)
-> seed_se_k:lbytes (2ul *! crypto_bytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h bp_matrix /\ live h seed_se_k /\
live h c_matrix /\ live h sk /\ live h ct /\ live h ss /\
loc_pairwise_disjoint [loc ct; loc sk; loc seed_se_k;
loc mu_decode; loc bp_matrix; loc c_matrix; loc ss])
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss1 a gen_a (as_seq h0 ct) (as_seq h0 sk)
(as_seq h0 mu_decode) (as_seq h0 seed_se_k) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let crypto_kem_dec_ss1 a gen_a ct sk mu_decode seed_se_k bp_matrix c_matrix ss =
let seed_se = sub seed_se_k 0ul (crypto_bytes a) in
let kp = sub seed_se_k (crypto_bytes a) (crypto_bytes a) in
let s = sub sk 0ul (crypto_bytes a) in
let mask = crypto_kem_dec_kp_s a gen_a mu_decode seed_se sk bp_matrix c_matrix in
crypto_kem_dec_ss0 a ct mask kp s ss
inline_for_extraction noextract
val crypto_kem_dec_ss2:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> ct:lbytes (crypto_ciphertextbytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> mu_decode:lbytes (bytes_mu a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h bp_matrix /\
live h c_matrix /\ live h sk /\ live h ct /\ live h ss /\
loc_pairwise_disjoint [loc ct; loc sk;
loc mu_decode; loc bp_matrix; loc c_matrix; loc ss])
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss2 a gen_a (as_seq h0 ct) (as_seq h0 sk)
(as_seq h0 mu_decode) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let crypto_kem_dec_ss2 a gen_a ct sk mu_decode bp_matrix c_matrix ss =
push_frame ();
let seed_se_k = create (2ul *! crypto_bytes a) (u8 0) in
crypto_kem_dec_seed_se_k a mu_decode sk seed_se_k;
crypto_kem_dec_ss1 a gen_a ct sk mu_decode seed_se_k bp_matrix c_matrix ss;
clear_words_u8 seed_se_k;
pop_frame ()
inline_for_extraction noextract
val crypto_kem_dec_mu:
a:FP.frodo_alg
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h sk /\ live h mu_decode /\
live h bp_matrix /\ live h c_matrix /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0) /\
loc_pairwise_disjoint [loc sk; loc mu_decode; loc bp_matrix; loc c_matrix])
(ensures fun h0 _ h1 -> modifies (loc mu_decode) h0 h1 /\
as_seq h1 mu_decode == S.crypto_kem_dec_mu a (as_seq h0 sk)
(as_matrix h0 bp_matrix) (as_matrix h0 c_matrix)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Params.fst.checked",
"Spec.Frodo.KEM.Decaps.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Matrix.fst.checked",
"Hacl.Impl.Frodo.Sample.fst.checked",
"Hacl.Impl.Frodo.Params.fst.checked",
"Hacl.Impl.Frodo.Pack.fst.checked",
"Hacl.Impl.Frodo.KEM.KeyGen.fst.checked",
"Hacl.Impl.Frodo.KEM.Encaps.fst.checked",
"Hacl.Impl.Frodo.KEM.fst.checked",
"Hacl.Impl.Frodo.Encode.fst.checked",
"Hacl.Frodo.Random.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Frodo.KEM.Decaps.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.KEM.Decaps",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Frodo.KEM.KeyGen",
"short_module": "KG"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.Params",
"short_module": "FP"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Frodo.Random",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Sample",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Pack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Encode",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM.Encaps",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Params",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Matrix",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Spec.Frodo.Params.frodo_alg ->
sk: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_secretkeybytes a) ->
bp_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
(Hacl.Impl.Frodo.Params.params_n a) ->
c_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
Hacl.Impl.Frodo.Params.params_nbar ->
mu_decode: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.bytes_mu a)
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Frodo.Params.frodo_alg",
"Hacl.Impl.Matrix.lbytes",
"Hacl.Impl.Frodo.Params.crypto_secretkeybytes",
"Hacl.Impl.Matrix.matrix_t",
"Hacl.Impl.Frodo.Params.params_nbar",
"Hacl.Impl.Frodo.Params.params_n",
"Hacl.Impl.Frodo.Params.bytes_mu",
"Hacl.Impl.Frodo.KEM.Decaps.frodo_mu_decode",
"Prims.unit",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"Hacl.Impl.Frodo.Params.secretmatrixbytes_len",
"Lib.Buffer.sub",
"Lib.IntTypes.uint8",
"Lib.IntTypes.op_Plus_Bang",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Impl.Frodo.Params.crypto_bytes",
"Hacl.Impl.Frodo.Params.crypto_publickeybytes",
"Spec.Frodo.Params.expand_crypto_secretkeybytes"
] | [] | false | true | false | false | false | let crypto_kem_dec_mu a sk bp_matrix c_matrix mu_decode =
| FP.expand_crypto_secretkeybytes a;
let s_bytes = sub sk (crypto_bytes a +! crypto_publickeybytes a) (secretmatrixbytes_len a) in
frodo_mu_decode a s_bytes bp_matrix c_matrix mu_decode | false |
Sec2.HIFC.fst | Sec2.HIFC.test9 | val test9 (l: lref) (h: href)
: HIFC unit
(union (single h) (single l))
(single l)
[((single l) `union` (single h), single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l) | val test9 (l: lref) (h: href)
: HIFC unit
(union (single h) (single l))
(single l)
[((single l) `union` (single h), single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l) | let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 13,
"end_line": 862,
"start_col": 0,
"start_line": 854
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Sec2.HIFC.lref -> h: Sec2.HIFC.href -> Sec2.HIFC.HIFC Prims.unit | Sec2.HIFC.HIFC | [] | [] | [
"Sec2.HIFC.lref",
"Sec2.HIFC.href",
"Sec2.HIFC.write",
"Prims.unit",
"Prims.int",
"Sec2.HIFC.read",
"Sec2.HIFC.union",
"Sec2.HIFC.single",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil",
"Sec2.HIFC.store",
"Prims.l_True",
"Prims.eq2",
"Sec2.HIFC.sel"
] | [] | false | true | false | false | false | let test9 (l: lref) (h: href)
: HIFC unit
(union (single h) (single l))
(single l)
[((single l) `union` (single h), single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l) =
| let x =
(let x0 = read h in
read l)
in
write l x | false |
Sec2.HIFC.fst | Sec2.HIFC.test12 | val test12: Prims.unit
-> IFC unit
(union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2); (union cr0 cr1, cw2)] | val test12: Prims.unit
-> IFC unit
(union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2); (union cr0 cr1, cw2)] | let test12 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2);
(union cr0 cr1, cw2)]
= c0 (); (c1();c2()) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 909,
"start_col": 0,
"start_line": 904
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise
//This one still reports a leakage, even though it doesn't really leak h
let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x
let refine_test9 (l:lref) (h:href)
: (unit -> HIFC unit (union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l))
= refine_flow (fun () -> test9 l h)
assume
val cw0 : label
assume
val cr0 : label
assume
val c0 (_:unit) : IFC unit cr0 cw0 []
assume
val cw1 : label
assume
val cr1 : label
assume
val c1 (_:unit) : IFC unit cr1 cw1 []
assume
val cw2 : label
assume
val cr2 : label
assume
val c2 (_:unit) : IFC unit cr2 cw2 []
let test10 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0
((bot, union cw1 cw2)::
(add_source cr1 [bot, cw2])))
= c0 (); (c1();c2()) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> Sec2.HIFC.IFC Prims.unit | Sec2.HIFC.IFC | [] | [] | [
"Prims.unit",
"Sec2.HIFC.c2",
"Sec2.HIFC.c1",
"Sec2.HIFC.c0",
"Sec2.HIFC.union",
"Sec2.HIFC.cr0",
"Sec2.HIFC.cr1",
"Sec2.HIFC.cr2",
"Sec2.HIFC.cw0",
"Sec2.HIFC.cw1",
"Sec2.HIFC.cw2",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil"
] | [] | false | true | false | false | false | let test12 ()
: IFC unit
(union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2); (union cr0 cr1, cw2)] =
| c0 ();
(c1 ();
c2 ()) | false |
Hacl.Spec.K256.Qinv.fst | Hacl.Spec.K256.Qinv.qinv_lemma | val qinv_lemma: f:S.qelem -> Lemma (qinv f == M.pow f (S.q - 2) % S.q) | val qinv_lemma: f:S.qelem -> Lemma (qinv f == M.pow f (S.q - 2) % S.q) | let qinv_lemma f =
let x_1 = f in
let x_10 = qsquare_times f 1 in
qsquare_times_lemma f 1;
assert_norm (pow2 1 = 0x2);
assert (x_10 == M.pow f 0x2 % S.q);
let x_11 = S.qmul x_10 x_1 in
lemma_pow_mod_1 f;
lemma_pow_mod_mul f 0x2 0x1;
assert (x_11 == M.pow f 0x3 % S.q);
let x_101 = S.qmul x_10 x_11 in
lemma_pow_mod_mul f 0x2 0x3;
assert (x_101 == M.pow f 0x5 % S.q);
let x_111 = S.qmul x_10 x_101 in
lemma_pow_mod_mul f 0x2 0x5;
assert (x_111 == M.pow f 0x7 % S.q);
let x_1001 = S.qmul x_10 x_111 in
lemma_pow_mod_mul f 0x2 0x7;
assert (x_1001 == M.pow f 0x9 % S.q);
let x_1011 = S.qmul x_10 x_1001 in
lemma_pow_mod_mul f 0x2 0x9;
assert (x_1011 == M.pow f 0xb % S.q);
let x_1101 = S.qmul x_10 x_1011 in
lemma_pow_mod_mul f 0x2 0xb;
assert (x_1101 == M.pow f 0xd % S.q);
let x6 = S.qmul (qsquare_times x_1101 2) x_1011 in
qsquare_times_lemma x_1101 2;
assert_norm (pow2 2 = 0x4);
lemma_pow_pow_mod_mul f 0xd 0x4 0xb;
assert (x6 == M.pow f 0x3f % S.q);
let x8 = S.qmul (qsquare_times x6 2) x_11 in
qsquare_times_lemma x6 2;
lemma_pow_pow_mod_mul f 0x3f 0x4 0x3;
assert (x8 == M.pow f 0xff % S.q);
let x14 = S.qmul (qsquare_times x8 6) x6 in
qsquare_times_lemma x8 6;
assert_norm (pow2 6 = 0x40);
lemma_pow_pow_mod_mul f 0xff 0x40 0x3f;
assert (x14 == M.pow f 0x3fff % S.q);
let x28 = S.qmul (qsquare_times x14 14) x14 in
qsquare_times_lemma x14 14;
assert_norm (pow2 14 = 0x4000);
lemma_pow_pow_mod_mul f 0x3fff 0x4000 0x3fff;
assert (x28 == M.pow f 0xfffffff % S.q);
let x56 = S.qmul (qsquare_times x28 28) x28 in
qsquare_times_lemma x28 28;
assert_norm (pow2 28 = 0x10000000);
lemma_pow_pow_mod_mul f 0xfffffff 0x10000000 0xfffffff;
assert (x56 == M.pow f 0xffffffffffffff % S.q);
let r0 = S.qmul (qsquare_times x56 56) x56 in
qsquare_times_lemma x56 56;
assert_norm (pow2 56 = 0x100000000000000);
lemma_pow_pow_mod_mul f 0xffffffffffffff 0x100000000000000 0xffffffffffffff;
assert (r0 == M.pow f 0xffffffffffffffffffffffffffff % S.q);
let r1 = S.qmul (qsquare_times r0 14) x14 in
qsquare_times_lemma r0 14;
lemma_pow_pow_mod_mul f 0xffffffffffffffffffffffffffff 0x4000 0x3fff;
assert (r1 == M.pow f 0x3fffffffffffffffffffffffffffffff % S.q);
let r2 = S.qmul (qsquare_times r1 3) x_101 in
qsquare_times_lemma r1 3;
assert_norm (pow2 3 = 0x8);
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffff 0x8 0x5;
assert (r2 == M.pow f 0x1fffffffffffffffffffffffffffffffd % S.q);
let r3 = S.qmul (qsquare_times r2 4) x_111 in
qsquare_times_lemma r2 4;
assert_norm (pow2 4 = 0x10);
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd 0x10 0x7;
assert (r3 == M.pow f 0x1fffffffffffffffffffffffffffffffd7 % S.q);
let r4 = S.qmul (qsquare_times r3 4) x_101 in
qsquare_times_lemma r3 4;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd7 0x10 0x5;
assert (r4 == M.pow f 0x1fffffffffffffffffffffffffffffffd75 % S.q);
let r5 = S.qmul (qsquare_times r4 5) x_1011 in
qsquare_times_lemma r4 5;
assert_norm (pow2 5 = 0x20);
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd75 0x20 0xb;
assert (r5 == M.pow f 0x3fffffffffffffffffffffffffffffffaeab % S.q);
let r6 = S.qmul (qsquare_times r5 4) x_1011 in
qsquare_times_lemma r5 4;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeab 0x10 0xb;
assert (r6 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb % S.q);
let r7 = S.qmul (qsquare_times r6 4) x_111 in
qsquare_times_lemma r6 4;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb 0x10 0x7;
assert (r7 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb7 % S.q);
let r8 = S.qmul (qsquare_times r7 5) x_111 in
qsquare_times_lemma r7 5;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb7 0x20 0x7;
assert (r8 == M.pow f 0x7fffffffffffffffffffffffffffffff5d576e7 % S.q);
let r9 = S.qmul (qsquare_times r8 6) x_1101 in
qsquare_times_lemma r8 6;
lemma_pow_pow_mod_mul f 0x7fffffffffffffffffffffffffffffff5d576e7 0x40 0xd;
assert (r9 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd % S.q);
let r10 = S.qmul (qsquare_times r9 4) x_101 in
qsquare_times_lemma r9 4;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd 0x10 0x5;
assert (r10 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5 % S.q);
let r11 = S.qmul (qsquare_times r10 3) x_111 in
qsquare_times_lemma r10 3;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5 0x8 0x7;
assert (r11 == M.pow f 0xfffffffffffffffffffffffffffffffebaaedce6af % S.q);
let r12 = S.qmul (qsquare_times r11 5) x_1001 in
qsquare_times_lemma r11 5;
lemma_pow_pow_mod_mul f 0xfffffffffffffffffffffffffffffffebaaedce6af 0x20 0x9;
assert (r12 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9 % S.q);
let r13 = S.qmul (qsquare_times r12 6) x_101 in
qsquare_times_lemma r12 6;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9 0x40 0x5;
assert (r13 == M.pow f 0x7fffffffffffffffffffffffffffffff5d576e7357a45 % S.q);
let r14 = S.qmul (qsquare_times r13 10) x_111 in
qsquare_times_lemma r13 10;
assert_norm (pow2 10 = 0x400);
lemma_pow_pow_mod_mul f 0x7fffffffffffffffffffffffffffffff5d576e7357a45 0x400 0x7;
assert (r14 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e91407 % S.q);
let r15 = S.qmul (qsquare_times r14 4) x_111 in
qsquare_times_lemma r14 4;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e91407 0x10 0x7;
assert (r15 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e914077 % S.q);
let r16 = S.qmul (qsquare_times r15 9) x8 in
qsquare_times_lemma r15 9;
assert_norm (pow2 9 = 0x200);
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e914077 0x200 0xff;
assert (r16 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff % S.q);
let r17 = S.qmul (qsquare_times r16 5) x_1001 in
qsquare_times_lemma r16 5;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff 0x20 0x9;
assert (r17 == M.pow f 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe9 % S.q);
let r18 = S.qmul (qsquare_times r17 6) x_1011 in
qsquare_times_lemma r17 6;
lemma_pow_pow_mod_mul f 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe9 0x40 0xb;
assert (r18 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9140777fa4b % S.q);
let r19 = S.qmul (qsquare_times r18 4) x_1101 in
qsquare_times_lemma r18 4;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9140777fa4b 0x10 0xd;
assert (r19 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9140777fa4bd % S.q);
let r20 = S.qmul (qsquare_times r19 5) x_11 in
qsquare_times_lemma r19 5;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9140777fa4bd 0x20 0x3;
assert (r20 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3 % S.q);
let r21 = S.qmul (qsquare_times r20 6) x_1101 in
qsquare_times_lemma r20 6;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3 0x40 0xd;
assert (r21 == M.pow f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd % S.q);
let r22 = S.qmul (qsquare_times r21 10) x_1101 in
qsquare_times_lemma r21 10;
lemma_pow_pow_mod_mul f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd 0x400 0xd;
assert (r22 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3340d % S.q);
let r23 = S.qmul (qsquare_times r22 4) x_1001 in
qsquare_times_lemma r22 4;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3340d 0x10 0x9;
assert (r23 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3340d9 % S.q);
let r24 = S.qmul (qsquare_times r23 6) x_1 in
qsquare_times_lemma r23 6;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3340d9 0x40 0x1;
assert (r24 == M.pow f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd03641 % S.q);
let r25 = S.qmul (qsquare_times r24 8) x6 in
qsquare_times_lemma r24 8;
assert_norm (pow2 8 = 0x100);
lemma_pow_pow_mod_mul f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd03641 0x100 0x3f;
assert (r25 == M.pow f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036413f % S.q) | {
"file_name": "code/k256/Hacl.Spec.K256.Qinv.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 98,
"end_line": 371,
"start_col": 0,
"start_line": 174
} | module Hacl.Spec.K256.Qinv
open FStar.Mul
module SE = Spec.Exponentiation
module LE = Lib.Exponentiation
module M = Lib.NatMod
module S = Spec.K256
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
let nat_mod_comm_monoid = M.mk_nat_mod_comm_monoid S.q
let mk_to_nat_mod_comm_monoid : SE.to_comm_monoid S.qelem = {
SE.a_spec = S.qelem;
SE.comm_monoid = nat_mod_comm_monoid;
SE.refl = (fun (x:S.qelem) -> x);
}
val one_mod : SE.one_st S.qelem mk_to_nat_mod_comm_monoid
let one_mod _ = 1
val mul_mod : SE.mul_st S.qelem mk_to_nat_mod_comm_monoid
let mul_mod x y = S.qmul x y
val sqr_mod : SE.sqr_st S.qelem mk_to_nat_mod_comm_monoid
let sqr_mod x = S.qmul x x
let mk_nat_mod_concrete_ops : SE.concrete_ops S.qelem = {
SE.to = mk_to_nat_mod_comm_monoid;
SE.one = one_mod;
SE.mul = mul_mod;
SE.sqr = sqr_mod;
}
let qsquare_times (a:S.qelem) (b:nat) : S.qelem =
SE.exp_pow2 mk_nat_mod_concrete_ops a b
val qsquare_times_lemma: a:S.qelem -> b:nat ->
Lemma (qsquare_times a b == M.pow a (pow2 b) % S.q)
let qsquare_times_lemma a b =
SE.exp_pow2_lemma mk_nat_mod_concrete_ops a b;
LE.exp_pow2_lemma nat_mod_comm_monoid a b;
assert (qsquare_times a b == LE.pow nat_mod_comm_monoid a (pow2 b));
M.lemma_pow_nat_mod_is_pow #S.q a (pow2 b)
(**
The algorithm is taken from
https://briansmith.org/ecc-inversion-addition-chains-01
*)
val qinv_r0_r1 (x14: S.qelem) : S.qelem
let qinv_r0_r1 x14 =
let x28 = S.qmul (qsquare_times x14 14) x14 in
let x56 = S.qmul (qsquare_times x28 28) x28 in
let r0 = S.qmul (qsquare_times x56 56) x56 in
let r1 = S.qmul (qsquare_times r0 14) x14 in
r1
val qinv_r2_r8 (r1 x_101 x_111 x_1011: S.qelem) : S.qelem
let qinv_r2_r8 r1 x_101 x_111 x_1011 =
let r2 = S.qmul (qsquare_times r1 3) x_101 in
let r3 = S.qmul (qsquare_times r2 4) x_111 in
let r4 = S.qmul (qsquare_times r3 4) x_101 in
let r5 = S.qmul (qsquare_times r4 5) x_1011 in
let r6 = S.qmul (qsquare_times r5 4) x_1011 in
let r7 = S.qmul (qsquare_times r6 4) x_111 in
let r8 = S.qmul (qsquare_times r7 5) x_111 in
r8
val qinv_r9_r15 (r8 x_101 x_111 x_1001 x_1101: S.qelem) : S.qelem
let qinv_r9_r15 r8 x_101 x_111 x_1001 x_1101 =
let r9 = S.qmul (qsquare_times r8 6) x_1101 in
let r10 = S.qmul (qsquare_times r9 4) x_101 in
let r11 = S.qmul (qsquare_times r10 3) x_111 in
let r12 = S.qmul (qsquare_times r11 5) x_1001 in
let r13 = S.qmul (qsquare_times r12 6) x_101 in
let r14 = S.qmul (qsquare_times r13 10) x_111 in
let r15 = S.qmul (qsquare_times r14 4) x_111 in
r15
val qinv_r16_r23 (r15 x8 x_11 x_1001 x_1011 x_1101: S.qelem) : S.qelem
let qinv_r16_r23 r15 x8 x_11 x_1001 x_1011 x_1101 =
let r16 = S.qmul (qsquare_times r15 9) x8 in
let r17 = S.qmul (qsquare_times r16 5) x_1001 in
let r18 = S.qmul (qsquare_times r17 6) x_1011 in
let r19 = S.qmul (qsquare_times r18 4) x_1101 in
let r20 = S.qmul (qsquare_times r19 5) x_11 in
let r21 = S.qmul (qsquare_times r20 6) x_1101 in
let r22 = S.qmul (qsquare_times r21 10) x_1101 in
let r23 = S.qmul (qsquare_times r22 4) x_1001 in
r23
val qinv_r24_r25 (r23 x_1 x6: S.qelem) : S.qelem
let qinv_r24_r25 r23 x_1 x6 =
let r24 = S.qmul (qsquare_times r23 6) x_1 in
let r25 = S.qmul (qsquare_times r24 8) x6 in
r25
val qinv_r0_r25 (x_1 x_11 x_101 x_111 x_1001 x_1011 x_1101: S.qelem) : S.qelem
let qinv_r0_r25 x_1 x_11 x_101 x_111 x_1001 x_1011 x_1101 =
let x6 = S.qmul (qsquare_times x_1101 2) x_1011 in
let x8 = S.qmul (qsquare_times x6 2) x_11 in
let x14 = S.qmul (qsquare_times x8 6) x6 in
let r1 = qinv_r0_r1 x14 in
let r8 = qinv_r2_r8 r1 x_101 x_111 x_1011 in
let r15 = qinv_r9_r15 r8 x_101 x_111 x_1001 x_1101 in
let r23 = qinv_r16_r23 r15 x8 x_11 x_1001 x_1011 x_1101 in
qinv_r24_r25 r23 x_1 x6
val qinv: f:S.qelem -> S.qelem
let qinv f =
let x_1 = f in
let x_10 = qsquare_times f 1 in
let x_11 = S.qmul x_10 x_1 in
let x_101 = S.qmul x_10 x_11 in
let x_111 = S.qmul x_10 x_101 in
let x_1001 = S.qmul x_10 x_111 in
let x_1011 = S.qmul x_10 x_1001 in
let x_1101 = S.qmul x_10 x_1011 in
qinv_r0_r25 x_1 x_11 x_101 x_111 x_1001 x_1011 x_1101
val lemma_pow_mod_1: f:S.qelem -> Lemma (f == M.pow f 1 % S.q)
let lemma_pow_mod_1 f =
M.lemma_pow1 f;
Math.Lemmas.small_mod f S.q;
assert_norm (pow2 0 = 1);
assert (f == M.pow f 1 % S.q)
val lemma_pow_mod_mul: f:S.qelem -> a:nat -> b:nat ->
Lemma (S.qmul (M.pow f a % S.q) (M.pow f b % S.q) == M.pow f (a + b) % S.q)
let lemma_pow_mod_mul f a b =
calc (==) {
S.qmul (M.pow f a % S.q) (M.pow f b % S.q);
(==) {
Math.Lemmas.lemma_mod_mul_distr_l (M.pow f a) (M.pow f b % S.q) S.q;
Math.Lemmas.lemma_mod_mul_distr_r (M.pow f a) (M.pow f b) S.q }
M.pow f a * M.pow f b % S.q;
(==) { M.lemma_pow_add f a b }
M.pow f (a + b) % S.q;
}
val lemma_pow_pow_mod_mul: f:S.qelem -> a:nat -> b:nat -> c:nat ->
Lemma (S.qmul (M.pow (M.pow f a % S.q) b % S.q) (M.pow f c % S.q) == M.pow f (a * b + c) % S.q)
let lemma_pow_pow_mod_mul f a b c =
calc (==) {
S.qmul (M.pow (M.pow f a % S.q) b % S.q) (M.pow f c % S.q);
(==) {
M.lemma_pow_mod_base (M.pow f a) b S.q;
Math.Lemmas.lemma_mod_mul_distr_l (M.pow (M.pow f a) b) (M.pow f c % S.q) S.q;
Math.Lemmas.lemma_mod_mul_distr_r (M.pow (M.pow f a) b) (M.pow f c) S.q }
M.pow (M.pow f a) b * M.pow f c % S.q;
(==) { M.lemma_pow_mul f a b }
M.pow f (a * b) * M.pow f c % S.q;
(==) { M.lemma_pow_add f (a * b) c }
M.pow f (a * b + c) % S.q;
}
// S.q - 2 = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036413f | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"Spec.Exponentiation.fsti.checked",
"prims.fst.checked",
"Lib.NatMod.fsti.checked",
"Lib.Exponentiation.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.K256.Qinv.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.NatMod",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.Exponentiation",
"short_module": "LE"
},
{
"abbrev": true,
"full_module": "Spec.Exponentiation",
"short_module": "SE"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.K256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.K256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: Spec.K256.PointOps.qelem
-> FStar.Pervasives.Lemma
(ensures
Hacl.Spec.K256.Qinv.qinv f ==
Lib.NatMod.pow f (Spec.K256.PointOps.q - 2) % Spec.K256.PointOps.q) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Spec.K256.PointOps.qelem",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"Prims.op_Modulus",
"Lib.NatMod.pow",
"Spec.K256.PointOps.q",
"Prims.unit",
"Hacl.Spec.K256.Qinv.lemma_pow_pow_mod_mul",
"FStar.Pervasives.assert_norm",
"Prims.b2t",
"Prims.op_Equality",
"Prims.pow2",
"Hacl.Spec.K256.Qinv.qsquare_times_lemma",
"Spec.K256.PointOps.qmul",
"Hacl.Spec.K256.Qinv.qsquare_times",
"Hacl.Spec.K256.Qinv.lemma_pow_mod_mul",
"Hacl.Spec.K256.Qinv.lemma_pow_mod_1"
] | [] | true | false | true | false | false | let qinv_lemma f =
| let x_1 = f in
let x_10 = qsquare_times f 1 in
qsquare_times_lemma f 1;
assert_norm (pow2 1 = 0x2);
assert (x_10 == M.pow f 0x2 % S.q);
let x_11 = S.qmul x_10 x_1 in
lemma_pow_mod_1 f;
lemma_pow_mod_mul f 0x2 0x1;
assert (x_11 == M.pow f 0x3 % S.q);
let x_101 = S.qmul x_10 x_11 in
lemma_pow_mod_mul f 0x2 0x3;
assert (x_101 == M.pow f 0x5 % S.q);
let x_111 = S.qmul x_10 x_101 in
lemma_pow_mod_mul f 0x2 0x5;
assert (x_111 == M.pow f 0x7 % S.q);
let x_1001 = S.qmul x_10 x_111 in
lemma_pow_mod_mul f 0x2 0x7;
assert (x_1001 == M.pow f 0x9 % S.q);
let x_1011 = S.qmul x_10 x_1001 in
lemma_pow_mod_mul f 0x2 0x9;
assert (x_1011 == M.pow f 0xb % S.q);
let x_1101 = S.qmul x_10 x_1011 in
lemma_pow_mod_mul f 0x2 0xb;
assert (x_1101 == M.pow f 0xd % S.q);
let x6 = S.qmul (qsquare_times x_1101 2) x_1011 in
qsquare_times_lemma x_1101 2;
assert_norm (pow2 2 = 0x4);
lemma_pow_pow_mod_mul f 0xd 0x4 0xb;
assert (x6 == M.pow f 0x3f % S.q);
let x8 = S.qmul (qsquare_times x6 2) x_11 in
qsquare_times_lemma x6 2;
lemma_pow_pow_mod_mul f 0x3f 0x4 0x3;
assert (x8 == M.pow f 0xff % S.q);
let x14 = S.qmul (qsquare_times x8 6) x6 in
qsquare_times_lemma x8 6;
assert_norm (pow2 6 = 0x40);
lemma_pow_pow_mod_mul f 0xff 0x40 0x3f;
assert (x14 == M.pow f 0x3fff % S.q);
let x28 = S.qmul (qsquare_times x14 14) x14 in
qsquare_times_lemma x14 14;
assert_norm (pow2 14 = 0x4000);
lemma_pow_pow_mod_mul f 0x3fff 0x4000 0x3fff;
assert (x28 == M.pow f 0xfffffff % S.q);
let x56 = S.qmul (qsquare_times x28 28) x28 in
qsquare_times_lemma x28 28;
assert_norm (pow2 28 = 0x10000000);
lemma_pow_pow_mod_mul f 0xfffffff 0x10000000 0xfffffff;
assert (x56 == M.pow f 0xffffffffffffff % S.q);
let r0 = S.qmul (qsquare_times x56 56) x56 in
qsquare_times_lemma x56 56;
assert_norm (pow2 56 = 0x100000000000000);
lemma_pow_pow_mod_mul f 0xffffffffffffff 0x100000000000000 0xffffffffffffff;
assert (r0 == M.pow f 0xffffffffffffffffffffffffffff % S.q);
let r1 = S.qmul (qsquare_times r0 14) x14 in
qsquare_times_lemma r0 14;
lemma_pow_pow_mod_mul f 0xffffffffffffffffffffffffffff 0x4000 0x3fff;
assert (r1 == M.pow f 0x3fffffffffffffffffffffffffffffff % S.q);
let r2 = S.qmul (qsquare_times r1 3) x_101 in
qsquare_times_lemma r1 3;
assert_norm (pow2 3 = 0x8);
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffff 0x8 0x5;
assert (r2 == M.pow f 0x1fffffffffffffffffffffffffffffffd % S.q);
let r3 = S.qmul (qsquare_times r2 4) x_111 in
qsquare_times_lemma r2 4;
assert_norm (pow2 4 = 0x10);
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd 0x10 0x7;
assert (r3 == M.pow f 0x1fffffffffffffffffffffffffffffffd7 % S.q);
let r4 = S.qmul (qsquare_times r3 4) x_101 in
qsquare_times_lemma r3 4;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd7 0x10 0x5;
assert (r4 == M.pow f 0x1fffffffffffffffffffffffffffffffd75 % S.q);
let r5 = S.qmul (qsquare_times r4 5) x_1011 in
qsquare_times_lemma r4 5;
assert_norm (pow2 5 = 0x20);
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd75 0x20 0xb;
assert (r5 == M.pow f 0x3fffffffffffffffffffffffffffffffaeab % S.q);
let r6 = S.qmul (qsquare_times r5 4) x_1011 in
qsquare_times_lemma r5 4;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeab 0x10 0xb;
assert (r6 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb % S.q);
let r7 = S.qmul (qsquare_times r6 4) x_111 in
qsquare_times_lemma r6 4;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb 0x10 0x7;
assert (r7 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb7 % S.q);
let r8 = S.qmul (qsquare_times r7 5) x_111 in
qsquare_times_lemma r7 5;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb7 0x20 0x7;
assert (r8 == M.pow f 0x7fffffffffffffffffffffffffffffff5d576e7 % S.q);
let r9 = S.qmul (qsquare_times r8 6) x_1101 in
qsquare_times_lemma r8 6;
lemma_pow_pow_mod_mul f 0x7fffffffffffffffffffffffffffffff5d576e7 0x40 0xd;
assert (r9 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd % S.q);
let r10 = S.qmul (qsquare_times r9 4) x_101 in
qsquare_times_lemma r9 4;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd 0x10 0x5;
assert (r10 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5 % S.q);
let r11 = S.qmul (qsquare_times r10 3) x_111 in
qsquare_times_lemma r10 3;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5 0x8 0x7;
assert (r11 == M.pow f 0xfffffffffffffffffffffffffffffffebaaedce6af % S.q);
let r12 = S.qmul (qsquare_times r11 5) x_1001 in
qsquare_times_lemma r11 5;
lemma_pow_pow_mod_mul f 0xfffffffffffffffffffffffffffffffebaaedce6af 0x20 0x9;
assert (r12 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9 % S.q);
let r13 = S.qmul (qsquare_times r12 6) x_101 in
qsquare_times_lemma r12 6;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9 0x40 0x5;
assert (r13 == M.pow f 0x7fffffffffffffffffffffffffffffff5d576e7357a45 % S.q);
let r14 = S.qmul (qsquare_times r13 10) x_111 in
qsquare_times_lemma r13 10;
assert_norm (pow2 10 = 0x400);
lemma_pow_pow_mod_mul f 0x7fffffffffffffffffffffffffffffff5d576e7357a45 0x400 0x7;
assert (r14 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e91407 % S.q);
let r15 = S.qmul (qsquare_times r14 4) x_111 in
qsquare_times_lemma r14 4;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e91407 0x10 0x7;
assert (r15 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e914077 % S.q);
let r16 = S.qmul (qsquare_times r15 9) x8 in
qsquare_times_lemma r15 9;
assert_norm (pow2 9 = 0x200);
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e914077 0x200 0xff;
assert (r16 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff % S.q);
let r17 = S.qmul (qsquare_times r16 5) x_1001 in
qsquare_times_lemma r16 5;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff 0x20 0x9;
assert (r17 == M.pow f 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe9 % S.q);
let r18 = S.qmul (qsquare_times r17 6) x_1011 in
qsquare_times_lemma r17 6;
lemma_pow_pow_mod_mul f 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe9 0x40 0xb;
assert (r18 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9140777fa4b % S.q);
let r19 = S.qmul (qsquare_times r18 4) x_1101 in
qsquare_times_lemma r18 4;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9140777fa4b 0x10 0xd;
assert (r19 == M.pow f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9140777fa4bd % S.q);
let r20 = S.qmul (qsquare_times r19 5) x_11 in
qsquare_times_lemma r19 5;
lemma_pow_pow_mod_mul f 0x1fffffffffffffffffffffffffffffffd755db9cd5e9140777fa4bd 0x20 0x3;
assert (r20 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3 % S.q);
let r21 = S.qmul (qsquare_times r20 6) x_1101 in
qsquare_times_lemma r20 6;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3 0x40 0xd;
assert (r21 == M.pow f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd % S.q);
let r22 = S.qmul (qsquare_times r21 10) x_1101 in
qsquare_times_lemma r21 10;
lemma_pow_pow_mod_mul f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd 0x400 0xd;
assert (r22 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3340d % S.q);
let r23 = S.qmul (qsquare_times r22 4) x_1001 in
qsquare_times_lemma r22 4;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3340d 0x10 0x9;
assert (r23 == M.pow f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3340d9 % S.q);
let r24 = S.qmul (qsquare_times r23 6) x_1 in
qsquare_times_lemma r23 6;
lemma_pow_pow_mod_mul f 0x3fffffffffffffffffffffffffffffffaeabb739abd2280eeff497a3340d9 0x40 0x1;
assert (r24 == M.pow f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd03641 % S.q);
let r25 = S.qmul (qsquare_times r24 8) x6 in
qsquare_times_lemma r24 8;
assert_norm (pow2 8 = 0x100);
lemma_pow_pow_mod_mul f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd03641 0x100 0x3f;
assert (r25 == M.pow f 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036413f % S.q) | false |
Sec2.HIFC.fst | Sec2.HIFC.test12_1 | val test12_1: Prims.unit
-> IFC unit
(union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, cw1); (union cr0 cr1, cw2)] | val test12_1: Prims.unit
-> IFC unit
(union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, cw1); (union cr0 cr1, cw2)] | let test12_1 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, cw1); (union cr0 cr1, cw2)]
= c0 (); (c1();c2()) | {
"file_name": "examples/layeredeffects/Sec2.HIFC.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 915,
"start_col": 0,
"start_line": 911
} | module Sec2.HIFC
open FStar.List.Tot
open FStar.Map
let loc = int
type store = m:Map.t loc int{forall l. contains m l}
let upd (s:store) (l:loc) (x:int) : store = Map.upd s l x
let sel (s:store) (l:loc) : int = Map.sel s l
open FStar.Set
(*** A basic Hoare state monad ***)
let pre = store -> Type0
let post a = store -> a -> store -> Type0
let hst a (p:pre) (q:post a) = s0:store{p s0} -> r:(a & store){q s0 (fst r) (snd r)}
let return_hst a (x:a) : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s
let bind_hst (a b:Type)
p q r s
(x:hst a p q)
(y: (x:a -> hst b (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= fun s0 -> let v, s1 = x s0 in y v s1
let subcomp_hst (a:Type) p q r s (x:hst a p q)
: Pure (hst a r s)
(requires (forall st. r st ==> p st) /\
(forall st0 res st1. q st0 res st1 ==> s st0 res st1))
(ensures fun _ -> True)
= x
let if_then_else_hst (a:Type) p q r s (x:hst a p q) (y:hst a r s) (b:bool) : Type =
hst a (fun st -> (b ==> p st) /\ ((~ b) ==> r st))
(fun st0 res st1 -> (b ==> q st0 res st1) /\ ((~ b) ==> s st0 res st1))
effect {
HST (a:Type) (p:pre) (q:post a)
with {
repr = hst;
return = return_hst;
bind = bind_hst;
subcomp = subcomp_hst;
if_then_else = if_then_else_hst
}
}
(*** Now, HIFC ***)
(* Some basic definitions of labels and sets *)
let label = Set.set loc
let label_inclusion (l0 l1:label) = Set.subset l0 l1
let bot : label = Set.empty
let single (l:loc) : label = Set.singleton l
let union (l0 l1:label) = Set.union l0 l1
let is_empty #a (s:Set.set a) = forall (x:a). ~ (Set.mem x s)
(* The write effect of a computation *)
let modifies (w:label) (s0 s1:store) = (forall l.{:pattern (sel s1 l)} ~(Set.mem l w) ==> sel s0 l == sel s1 l)
let writes #a #p #q (f:hst a p q) (writes:label) =
forall (s0:store{p s0}). let x1, s0' = f s0 in modifies writes s0 s0'
(* The read effect of a computation *)
let agree_on (reads:label) (s0 s1: store) = forall l. Set.mem l reads ==> sel s0 l == sel s1 l
let related_runs #a #p #q (f:hst a p q) (s0:store{p s0}) (s0':store{p s0'}) =
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\
(forall (l:loc). (sel s1 l == sel s1' l \/ (sel s1 l == sel s0 l /\ sel s1' l == sel s0' l))))
let reads #a #p #q (f:hst a p q) (reads:label) =
forall (s0:store{p s0}) (s0':store{p s0'}). agree_on reads s0 s0' ==> related_runs f s0 s0'
(* The respects flows refinement *)
let flow = label & label //from, to
let flows = list flow
let has_flow_1 (from to:loc) (f:flow) = from `Set.mem` fst f /\ to `Set.mem` snd f
let has_flow (from to:loc) (fs:flows) = (exists rs. rs `List.Tot.memP` fs /\ has_flow_1 from to rs)
let no_leakage_k #a #p #q (f:hst a p q) (from to:loc) (k:int) =
forall (s0:store{p s0}).{:pattern (upd s0 from k)}
p (upd s0 from k) ==>
sel (snd (f s0)) to == (sel (snd (f (upd s0 from k))) to)
let no_leakage #a #p #q (f:hst a p q) (from to:loc) = forall k. no_leakage_k f from to k
let respects #a #p #q (f:hst a p q) (fs:flows) =
(forall from to. {:pattern (no_leakage f from to)} ~(has_flow from to fs) /\ from<>to ==> no_leakage f from to)
(** Representation type for the HIFC effect
You should convince yourself that `reads`, `writes` and `respects`
really capture the intended semantics of read effects, write effects, and IFC **)
let hifc a (r:label) (w:label) (fs:flows) (p:pre) (q:post a) =
f:hst a p q {
reads f r /\
writes f w /\
respects f fs
}
(** returning a pure value `x` into hifc **)
let return (a:Type) (x:a) : hifc a bot bot [] (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) =
let f : hst a (fun _ -> True) (fun s0 r s1 -> s0 == s1 /\ r == x) = fun s -> x,s in
f
(** reading a loc `l` *)
let iread (l:loc) : hifc int (single l) bot [] (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) =
let f : hst int (fun _ -> True) (fun s0 x s1 -> s0 == s1 /\ x == sel s0 l) = fun s -> sel s l, s in
f
(** writing a loc `l` *)
let iwrite (l:loc) (x:int) : hifc unit bot (single l) [] (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) =
let f : hst unit (fun _ -> True) (fun s0 _ s1 -> s1 == upd s0 l x) = fun s -> (), upd s l x in
f
(** Now, a lot of what follows is proofs that allow us to define
return, bind, subsumption, and other combinators *)
let does_not_read_loc_v #a #p #q (f:hst a p q)
(reads:label)
(l:loc)
(s0:store{p s0})
v =
let s0' = upd s0 l v in
p s0' ==>
(let x1, s1 = f s0 in
let x1', s1' = f s0' in
x1 == x1' /\ //result does not depend on l
(forall l'. l' <> l ==> //for every location l' not equal to l
sel s1 l' == sel s1' l') /\ //its value in the two states is the same
(sel s1 l == sel s1' l \/ //and l is itself may be written, in which case its value is the same in both final states
//or its not, but then its values in the initial and final states are the same on both sides
(sel s1 l == sel s0 l /\
sel s1' l == sel s0' l)))
let does_not_read_loc #a #p #q (f:hst a p q) (reads:label) (l:loc) (s0:store{p s0}) =
forall v. does_not_read_loc_v f reads l s0 v
let reads_ok_preserves_equal_locs #a #p #q (f:hst a p q) (rds:label) (s0:store{p s0}) (s0':store{p s0'})
: Lemma (requires agree_on rds s0 s0' /\ reads f rds)
(ensures (let x1, s1 = f s0 in
let x1', s1' = f s0' in
agree_on rds s1 s1'))
= ()
let weaken_reads_ok #a #p #q (f:hst a p q) (rds rds1:label)
: Lemma (requires reads f rds /\
label_inclusion rds rds1)
(ensures reads f rds1)
= let aux s0 s0'
: Lemma (requires p s0 /\ p s0' /\ agree_on rds1 s0 s0')
(ensures agree_on rds s0 s0')
[SMTPat(agree_on rds1 s0 s0')]
= ()
in
()
let reads_ok_does_not_read_loc #a #p #q (f:hst a p q) (rds:label) (l:loc{~(Set.mem l rds)}) (s0:store{p s0})
: Lemma
(requires reads f rds)
(ensures does_not_read_loc f rds l s0)
= let aux (v:int)
: Lemma (requires p (upd s0 l v))
(ensures does_not_read_loc_v f rds l s0 v)
[SMTPat ((upd s0 l v))]
= assert (agree_on rds s0 (upd s0 l v));
()
in
()
let add_source (r:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> union r r0, w0) fs
let add_sink (w:label) (fs:flows) : flows = List.Tot.map (fun (r0, w0) -> r0, union w w0) fs
let flows_included_in (fs0 fs1:flows) =
forall f0. f0 `List.Tot.memP` fs0 ==>
(forall from to. has_flow_1 from to f0 /\ from <> to ==> (exists f1. f1 `List.Tot.memP` fs1 /\ has_flow_1 from to f1))
let flows_equiv (fs0 fs1:flows) = fs0 `flows_included_in` fs1 /\ fs1 `flows_included_in` fs0
let flows_equiv_refl fs
: Lemma (fs `flows_equiv` fs)
= ()
let flows_equiv_trans fs0 fs1 fs2
: Lemma (fs0 `flows_equiv` fs1 /\ fs1 `flows_equiv` fs2 ==> fs0 `flows_equiv` fs2)
= ()
let flows_included_in_union_distr_dest (a b c:label)
: Lemma (flows_equiv [a, union b c] [a, b; a, c])
= ()
let flows_included_in_union_distr_src (a b c:label)
: Lemma (flows_equiv [union a b, c] [a, c; b, c])
= ()
let flows_included_in_union (a b c:label)
: Lemma (flows_equiv ([a, union b c; union a b, c])
([a, b; union a b, c]))
= ()
(* The computation behavior of sequential composition of HIFC
is the same as HST.
This is a step towards defining bind_hifc ... we will
need several lemmas to give the result the hifc type we want *)
let bind_ifc' (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hst b (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= bind_hst _ _ _ _ _ _ x y
(* bind_ifc' reads the union of the read labels *)
let bind_ifc_reads_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (reads (bind_ifc' x y) (union r0 r1))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let reads = union r0 r1 in
let f_reads_ok (s0:store{p_f s0}) (s0':store{p_f s0'})
: Lemma
(requires agree_on reads s0 s0')
(ensures related_runs f s0 s0')
[SMTPat(agree_on reads s0 s0')]
= let y1, s1 = x s0 in
let y1', s1' = x s0' in
weaken_reads_ok x r0 reads;
assert (related_runs x s0 s0');
weaken_reads_ok (y y1) r1 reads;
reads_ok_preserves_equal_locs x reads s0 s0';
assert (forall l. l `Set.mem` r1 ==> sel s1 l == sel s1' l);
assert (agree_on r1 s1 s1');
assert (y1 == y1');
let res, s2 = y y1 s1 in
let res', s2' = y y1 s1' in
assert (res == res')
in
()
(* bind_ifc' writes the union of the write labels *)
let bind_ifc_writes_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (writes (bind_ifc' x y) (union w0 w1))
= ()
(* Now the harder part: Showing that bind_ifc respects a flows set
See bind_ifc_flows_ok several lines below *)
let rec memP_append_or (#a:Type) (x:a) (l0 l1:list a)
: Lemma (ensures (List.Tot.memP x (l0 @ l1) <==>
(List.Tot.memP x l0 \/ List.Tot.memP x l1)))
(decreases l0)
= match l0 with
| [] -> ()
| _::tl -> memP_append_or x tl l1
let has_flow_append (from to:loc) (fs fs':flows)
: Lemma (has_flow from to fs ==>
has_flow from to (fs @ fs') /\
has_flow from to (fs' @ fs))
= let aux (rs:_)
: Lemma (requires
List.Tot.memP rs fs)
(ensures
List.Tot.memP rs (fs @ fs') /\
List.Tot.memP rs (fs' @ fs))
[SMTPat (List.Tot.memP rs fs)]
= memP_append_or rs fs fs';
memP_append_or rs fs' fs
in
()
let elim_has_flow_seq (from to:loc)
(r0 r1 w1:label)
(fs0 fs1:flows)
: Lemma (requires (~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (~(has_flow from to fs0) /\
(~(Set.mem from r0) \/ ~(Set.mem to w1)) /\
~(has_flow from to (add_source r0 fs1))))
= assert (add_source r0 ((bot, w1)::fs1) ==
(Set.union r0 bot, w1)::add_source r0 fs1);
assert (Set.union r0 bot `Set.equal` r0);
has_flow_append from to fs0 ((r0, w1)::add_source r0 fs1);
assert (~(has_flow from to fs0));
has_flow_append from to ((r0, w1)::add_source r0 fs1) fs0;
assert (~(has_flow from to (((r0, w1)::add_source r0 fs1))));
assert ((r0, w1)::add_source r0 fs1 ==
[r0, w1] @ add_source r0 fs1);
has_flow_append from from [r0, w1] (add_source r0 fs1)
let rec add_source_monotonic (from to:loc) (r:label) (fs:flows)
: Lemma (has_flow from to fs ==> has_flow from to (add_source r fs))
= match fs with
| [] -> ()
| _::tl -> add_source_monotonic from to r tl
#push-options "--warn_error -271" // intentional empty SMTPat
let has_flow_soundness #a #r #w #fs #p #q (f:hifc a r w fs p q)
(from to:loc) (s:store{p s}) (k:int{p (upd s from k)})
: Lemma (requires
(let x, s1 = f s in
let _, s1' = f (upd s from k) in
from <> to /\
sel s1 to <> sel s1' to))
(ensures has_flow from to fs)
= let aux ()
: Lemma (requires (~(has_flow from to fs)))
(ensures False)
[SMTPat ()]
= assert (respects f fs);
assert (no_leakage f from to)
in
()
#pop-options
let bind_hst_no_leakage (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
(from to:loc)
(s0:store) (k:_)
: Lemma
(requires (
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let s0' = upd s0 from k in
p_f s0 /\
p_f s0' /\
from <> to /\
~(has_flow from to (fs0 @ add_source r0 ((bot, w1)::fs1)))))
(ensures (let f = bind_ifc' x y in
let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to))
= let f = bind_ifc' x y in
assert (reads x r0);
let s0' = upd s0 from k in
let _, s2f = f s0 in
let _, s2f' = f s0' in
let flows = (fs0 @ add_source r0 ((r1, w1)::fs1)) in
let v0, s1 = x s0 in
let v0', s1' = x s0' in
elim_has_flow_seq from to r0 r1 w1 fs0 fs1;
assert (~(has_flow from to fs0));
assert (respects x fs0);
assert (no_leakage x from to);
assert (sel s1 to == sel s1' to);
let _, s2 = y v0 s1 in
let _, s2' = y v0' s1' in
assert (s2 == s2f);
assert (s2' == s2f');
//Given: (from not-in r0 U r1) \/ (to not-in w1)
//suppose (from in r0) \/ (from in r1)
// them to not-in w1
//suppose (from not-in r0 U r1)
//then v0 = v0'
// s1' = upd from s1 k
// s2 to = s2' to
if Set.mem to w1
then begin
assert (~(Set.mem from r0));
assert (reads x r0);
reads_ok_does_not_read_loc x r0 from s0;
assert (does_not_read_loc x r0 from s0);
assert (does_not_read_loc_v x r0 from s0 k);
assert (v0 == v0');
assert (forall l. l <> from ==> sel s1 l == sel s1' l);
assert (Map.equal s1' (upd s1 from k) \/ Map.equal s1' s1);
if (sel s1 from = sel s1' from)
then begin
assert (Map.equal s1 s1')
end
else begin
assert (Map.equal s1' (upd s1 from k));
assert (reads (y v0) r1);
if (sel s2 to = sel s2' to)
then ()
else begin
assert (sel s2 to <> sel s1 to \/ sel s2' to <> sel s1' to);
has_flow_soundness (y v0) from to s1 k;
assert (has_flow from to fs1);
add_source_monotonic from to r0 fs1
//y reads from and writes to, so (from, to) should be in fs1
//so, we should get a contradiction
end
end
end
else //to is not in w1, so y does not write it
()
(* bind_ifc' has flows corresponding to the flows of
-- `x` the first computation, i.e., fs0
-- `y` the second computation ((bot, w1)::fs1)
augmented with the reads of `x`, since the
result of `x` is tainted by its reads *)
let bind_ifc_flows_ok (#a #b:Type)
(#w0 #r0 #w1 #r1:label)
(#fs0 #fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: Lemma (respects (bind_ifc' x y) (fs0 @ add_source r0 ((bot, w1)::fs1)))
= let f = bind_ifc' x y in
let p_f = (fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1)) in
let flows = (fs0 @ add_source r0 ((bot, w1)::fs1)) in
let respects_flows_lemma (from to:loc)
: Lemma (requires from <> to /\ ~(has_flow from to flows))
(ensures no_leakage f from to)
[SMTPat (has_flow from to flows)]
= let aux (s0:store{p_f s0}) (k:_{p_f (upd s0 from k)})
: Lemma (let s0' = upd s0 from k in
let _, s2 = f s0 in
let _, s2' = f s0' in
sel s2 to == sel s2' to)
[SMTPat (upd s0 from k)]
= bind_hst_no_leakage x y from to s0 k
in
()
in
()
(* Getting there: pre_bind is a valid bind for hifc.
But, we can do a bit better by integrating framing into it ... see below *)
let pre_bind (a b:Type)
(w0 r0 w1 r1:label) (fs0 fs1:flows)
#p #q #r #s
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. q s0 x s1 /\ s x s1 r s2))
= let f = bind_ifc' x y in
bind_ifc_reads_ok x y;
bind_ifc_writes_ok x y;
bind_ifc_flows_ok x y;
f
(* Incidentally, The IFC indexing structure is a monoid
under the label_equiv equivalence relation, making
hifc a graded Hoare monad
*)
let triple = label & label & flows
let unit_triple = bot, bot, []
let ifc_triple (w0, r0, fs0) (w1, r1, fs1) = (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1)))
let label_equiv (s0 s1:label) = Set.equal s0 s1
let triple_equiv (w0, r0, f0) (w1, r1, f1) = label_equiv w0 w1 /\ label_equiv r0 r1 /\ flows_equiv f0 f1
let triple_equiv_refl t0
: Lemma (triple_equiv t0 t0)
= ()
let rec add_source_bot (f:flows)
: Lemma (add_source bot f `flows_equiv` f)
= match f with
| [] -> ()
| _::tl -> add_source_bot tl
let flows_included_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_included_in f0 g0 /\
flows_included_in f1 g1)
(ensures flows_included_in (f0@f1) (g0@g1))
= let aux (f:_) (from to:_)
: Lemma (requires List.Tot.memP f (f0@f1) /\
from <> to /\
has_flow_1 from to f)
(ensures (exists g. g `List.Tot.memP` (g0@g1) /\ has_flow_1 from to g))
[SMTPat (has_flow_1 from to f)]
= memP_append_or f f0 f1;
assert (exists g. g `List.Tot.memP` g0 \/ g `List.Tot.memP` g1 /\ has_flow_1 from to g);
FStar.Classical.forall_intro (fun g -> memP_append_or g g0 g1)
in
()
let flows_equiv_append (f0 f1 g0 g1:flows)
: Lemma (requires flows_equiv f0 g0 /\ flows_equiv f1 g1)
(ensures flows_equiv (f0@f1) (g0@g1))
= flows_included_append f0 f1 g0 g1;
flows_included_append g0 g1 f0 f1
let rec append_nil_r #a (l:list a)
: Lemma (l @ [] == l)
= match l with
| [] -> ()
| _::tl -> append_nil_r tl
(* monoid laws for ifc_triple *)
let left_unit (w, r, f) =
assert (Set.equal (union bot bot) bot);
add_source_bot f;
assert (ifc_triple unit_triple (w, r, f) `triple_equiv` (w, r, f))
let right_unit (w, r, f) =
calc (==) {
ifc_triple (w, r, f) unit_triple;
(==) { }
(w `union` bot, r `union` bot, f @ add_source r ((bot, bot)::[]));
};
assert (flows_equiv (add_source r [(bot, bot)]) []);
flows_equiv_append f (add_source r [(bot, bot)]) f [];
append_nil_r f;
assert (ifc_triple (w, r, f) unit_triple `triple_equiv` (w, r, f))
open FStar.Calc
let assoc_hst (w0, r0, fs0) (w1, r1, fs1) (w2, r2, fs2) =
calc (==) {
ifc_triple (w0, r0, fs0) (ifc_triple (w1, r1, fs1) (w2, r2, fs2)) ;
(==) { }
ifc_triple (w0, r0, fs0) (union w1 w2, union r1 r2, (fs1 @ add_source r1 ((bot, w2)::fs2)));
(==) { }
(union w0 (union w1 w2), union r0 (union r1 r2), fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall w0 w1 w2. Set.equal (union w0 (union w1 w2)) (union (union w0 w1) w2)) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
fs0 @ (add_source r0 ((bot, union w1 w2) :: (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((union r0 bot, union w1 w2) :: add_source r0 (fs1 @ add_source r1 ((bot, w2)::fs2)))));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
(fs0 @ ((r0, union w1 w2) :: add_source r0 (fs1 @ (r1, w2) ::add_source r1 fs2))));
};
calc (==) {
ifc_triple (ifc_triple (w0, r0, fs0) (w1, r1, fs1)) (w2, r2, fs2);
(==) { }
ifc_triple (union w0 w1, union r0 r1, (fs0 @ add_source r0 ((bot, w1)::fs1))) (w2, r2, fs2);
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ add_source r0 ((bot, w1)::fs1)) @ (add_source (union r0 r1) ((bot, w2) :: fs2))));
(==) { }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((union r0 bot, w1)::add_source r0 fs1)) @ ((union (union r0 r1) bot, w2) :: add_source (union r0 r1) fs2)));
(==) { assert (forall s. Set.equal (union s bot) s) }
(union (union w0 w1) w2,
union (union r0 r1) r2,
((fs0 @ ((r0, w1)::add_source r0 fs1)) @ ((union r0 r1, w2) :: add_source (union r0 r1) fs2)));
}
(* Adding a frame combinator, refining the Hoare postcondition with
the interpretation of the write index *)
#push-options "--warn_error -271" // intentional empty SMTPat
let frame (a:Type) (r w:label) (fs:flows) #p #q (f:hifc a r w fs p q)
: hifc a r w fs p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1)
= let aux (s0:store{p s0}) (l:loc{~(Set.mem l w)})
: Lemma (let x, s1 = f s0 in
sel s0 l == sel s1 l)
[SMTPat()]
= ()
in
let g : hst a p (fun s0 x s1 -> q s0 x s1 /\ modifies w s0 s1) = fun s0 -> f s0 in
assert (reads f r);
let read_ok_lem (l:loc) (s:store{p s})
: Lemma (requires (~(Set.mem l r)))
(ensures (does_not_read_loc g r l s))
[SMTPat(does_not_read_loc g r l s)]
= reads_ok_does_not_read_loc g r l s;
assert (does_not_read_loc f r l s)
in
assert (reads g r);
assert (writes g w);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs) /\ from<>to)
(ensures (no_leakage g from to))
[SMTPat(has_flow from to fs)]
= assert (no_leakage f from to)
in
assert (respects g fs);
g
#pop-options
(*** And there we have our bind for hifc ***)
(* It is similar to pre_bind, but we integrate the use of `frame`
so that all HIFC computations are auto-framed *)
let bind (a b:Type)
(r0 w0:label)
(fs0:flows)
(p:pre) (q:post a)
(r1 w1:label) (fs1:flows)
(r:a -> pre) (s:a -> post b)
(x:hifc a r0 w0 fs0 p q)
(y: (x:a -> hifc b r1 w1 fs1 (r x) (s x)))
: hifc b (union r0 r1) (union w0 w1) (fs0 @ add_source r0 ((bot, w1)::fs1))
(fun s0 -> p s0 /\ (forall x s1. q s0 x s1 /\ modifies w0 s0 s1 ==> r x s1))
(fun s0 r s2 -> (exists x s1. (q s0 x s1 /\ modifies w0 s0 s1) /\ (s x s1 r s2 /\ modifies w1 s1 s2)))
= (pre_bind _ _ _ _ _ _ _ _ (frame _ _ _ _ x) (fun a -> frame _ _ _ _ (y a)))
(** This one lets you drop a flow `f` from the flow set
if the Hoare spec lets you prove that *)
let refine_flow_hifc #a #w #r #f #fs #p #q
(c: hifc a r w (f::fs) p q)
: Pure (hifc a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= c
(** Classic Hoare rule of consequence *)
let consequence (a:Type) (r0 w0:label) p q p' q' (fs0:flows) (f:hifc a r0 w0 fs0 p q)
: Pure (hst a p' q')
(requires (forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(fun _ -> True)
= let g : hst a p' q' = fun s -> f s in
g
(* A couple of utils *)
let norm_spec (p:Type)
: Lemma (requires (norm [delta;iota;zeta] p))
(ensures p)
= ()
let norm_spec_inv (p:Type)
: Lemma (requires p)
(ensures (norm [delta;iota;zeta] p))
= ()
(* A subsumption rule for hifc *)
let sub_hifc (a:Type) (r0 w0:label) (fs0:flows) p q (r1 w1:label) (fs1:flows) #p' #q'
(f:hifc a r0 w0 fs0 p q)
: Pure (hifc a r1 w1 fs1 p' q')
(requires
label_inclusion r0 r1 /\
label_inclusion w0 w1 /\
(norm [delta;iota;zeta] (fs0 `flows_included_in` fs1)) /\
(forall s. p' s ==> p s) /\
(forall s0 x s1. p' s0 /\ q s0 x s1 ==> q' s0 x s1))
(ensures fun _ -> True)
= let forig = f in
norm_spec (fs0 `flows_included_in` fs1);
assert ((fs0 `flows_included_in` fs1));
let f : hst a p' q' = consequence a r0 w0 p q p' q' fs0 f in
weaken_reads_ok f r0 r1;
assert (reads f r1);
assert (writes f w1);
let respects_flows_lemma (from to:_)
: Lemma
(requires ~(has_flow from to fs1) /\ from<>to)
(ensures (no_leakage f from to))
[SMTPat(no_leakage f from to)]
= assert (no_leakage forig from to)
in
assert (respects f fs1);
f
(* A conditional rule for hifc *)
let if_then_else (a:Type) r0 w0 f0 p0 q0
r1 w1 f1
p1 q1
(c_then:hifc a r0 w0 f0 p0 q0)
(c_else:hifc a r1 w1 f1 p1 q1)
(b:bool)
= hifc a (r0 `union` r1) (w0 `union` w1) (f0 @ f1)
(fun s -> if b then p0 s else p1 s)
(fun s0 x s1 -> if b then q0 s0 x s1 else q1 s0 x s1)
let rec append_memP #a (x:a) (l0 l1:list a)
: Lemma (List.Tot.memP x (l0 @ l1) <==> (List.Tot.memP x l0 \/ List.Tot.memP x l1))
= match l0 with
| [] -> ()
| hd::tl -> append_memP x tl l1
(* An auxiliary lemma needed to prove if_then_else sound *)
#push-options "--warn_error -271" // intentional empty SMTPat
let weaken_flows_append (fs fs':flows)
: Lemma (ensures (norm [delta;iota;zeta] (fs `flows_included_in` (fs @ fs')) /\
(norm [delta;iota;zeta] (fs' `flows_included_in` (fs @ fs')))))
[SMTPat ()]
= let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs)
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
let aux (f0:flow)
: Lemma
(requires f0 `List.Tot.memP` fs')
(ensures f0 `List.Tot.memP` (fs @ fs'))
[SMTPat(f0 `List.Tot.memP` (fs @ fs'))]
= append_memP f0 fs fs'
in
norm_spec_inv (fs `flows_included_in` (fs @ fs'));
norm_spec_inv (fs' `flows_included_in` (fs @ fs'))
#pop-options
(*** We now create our HIFC effect *)
total
reifiable
reflectable
effect {
HIFC (a:Type) (reads:label) (writes:label) (flows:flows) (p:pre) (q:post a)
with {
repr = hifc;
return;
bind;
subcomp = sub_hifc;
if_then_else = if_then_else
}
}
(* reflecting actions into it *)
let read (l:loc)
: HIFC int (single l) bot []
(requires fun _ -> True)
(ensures fun s0 x s1 -> x == sel s0 l)
= HIFC?.reflect (iread l)
let write (l:loc) (x:int)
: HIFC unit bot (single l) []
(requires fun _ -> True)
(ensures fun _ _ s1 -> sel s1 l == x)
= HIFC?.reflect (iwrite l x)
(* This is a technical bit to lift the F*'s WP-calculus of PURE
computations into the Hoare types of HIFC *)
let lift_PURE_HIFC (a:Type) (wp:pure_wp a) (f:unit -> PURE a wp)
: Pure (hifc a bot bot [] (fun _ -> True) (fun s0 _ s1 -> s0 == s1))
(requires wp (fun _ -> True))
(ensures fun _ -> True)
= FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall ();
return a (f ())
sub_effect PURE ~> HIFC = lift_PURE_HIFC
(* reflecting the flow refinement into the effect *)
let refine_flow #a #w #r #f #fs #p #q
($c: unit -> HIFC a r w (f::fs) p q)
: Pure (unit -> HIFC a r w fs p q)
(requires
(forall from to v.
has_flow_1 from to f /\
from <> to ==>
(forall s0 x x' s1 s1'.
p s0 /\
p (upd s0 from v) /\
q s0 x s1 /\
modifies w s0 s1 /\
q (upd s0 from v) x' s1' /\
modifies w (upd s0 from v) s1' ==>
sel s1 to == sel s1' to)))
(ensures fun _ -> True)
= (fun () -> HIFC?.reflect (refine_flow_hifc (reify (c()))))
(* Now for some examples *)
let ref (l:label) = r:loc {r `Set.mem` l}
assume val high : label
let low : label = Set.complement high
let lref = ref low
let href = ref high
let test (l:lref) (h:href)
: HIFC unit (union (single l) bot)
(union bot (single h))
(add_source (single l) [bot, single h])
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test2 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
let test3 (l:lref) (h:href)
: HIFC unit (single l)
(single h)
[single l, single h]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_lab (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 h == sel s0 l)
= write h (read l)
let test3_1 (l:lref) (h:href) (x:int)
: HIFC int (single l) (single h) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == 0 /\ r == sel s1 l)
= write h 0;
read l
let test4 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) [single h, bot]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test5 (l:lref) (h:href) (x:int)
: HIFC int (single h) (single l) []
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == x /\ r == sel s1 h)
= write l x;
read h
let test6 (l:lref) (h:href)
: HIFC unit low high [low, high]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 h == sel s0 l)
= let x = read l in
write h x
//This leaks the contents of the href
let test7 (l:lref) (h:href)
: HIFC unit (single h) (single l) [high, low]
(requires fun _ -> True)
(ensures fun s0 r s1 -> sel s1 l == sel s0 h)
= let x = read h in
write l x
//But, label-based IFC is inherently imprecise
//This one reports a leakage, even though it doesn't really leak h
let test8 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l) [(single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= let x0 = read h in
let x = read l in
write l (x + 1)
effect IFC (a:Type) (w r:label) (fs:flows) = HIFC a w r fs (fun _ -> True) (fun _ _ _ -> True)
let test_cond (l:lref) (h:href) (b:bool)
: IFC unit (union (single h) (single l)) (single l) [single h, single l]
= if b then write l (read h) else write l (read l + 1)
//But, using the Hoare refinements, we can recover precision and remove
//the spurious flow from h to l
let refine_test8 (l:lref) (h:href)
: unit -> HIFC unit (union (single h) (single l)) (single l) []
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l + 1)
= refine_flow (fun () -> test8 l h)
//But, label-based IFC is inherently imprecise
//This one still reports a leakage, even though it doesn't really leak h
let test9 (l:lref) (h:href)
: HIFC unit (union (single h) (single l)) (single l)
[(single l `union` single h, single l)]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l)
= let x= (let x0 = read h in
read l)
in
write l x
let refine_test9 (l:lref) (h:href)
: (unit -> HIFC unit (union (single h) (single l))
(single l)
[]
(requires fun _ -> True)
(ensures fun s0 _ s1 -> sel s1 l == sel s0 l))
= refine_flow (fun () -> test9 l h)
assume
val cw0 : label
assume
val cr0 : label
assume
val c0 (_:unit) : IFC unit cr0 cw0 []
assume
val cw1 : label
assume
val cr1 : label
assume
val c1 (_:unit) : IFC unit cr1 cw1 []
assume
val cw2 : label
assume
val cr2 : label
assume
val c2 (_:unit) : IFC unit cr2 cw2 []
let test10 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
(add_source cr0
((bot, union cw1 cw2)::
(add_source cr1 [bot, cw2])))
= c0 (); (c1();c2())
let test12 ()
: IFC unit (union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, union cw1 cw2);
(union cr0 cr1, cw2)]
= c0 (); (c1();c2()) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Monotonic.Pure.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Sec2.HIFC.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Set",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Map",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Sec2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> Sec2.HIFC.IFC Prims.unit | Sec2.HIFC.IFC | [] | [] | [
"Prims.unit",
"Sec2.HIFC.c2",
"Sec2.HIFC.c1",
"Sec2.HIFC.c0",
"Sec2.HIFC.union",
"Sec2.HIFC.cr0",
"Sec2.HIFC.cr1",
"Sec2.HIFC.cr2",
"Sec2.HIFC.cw0",
"Sec2.HIFC.cw1",
"Sec2.HIFC.cw2",
"Prims.Cons",
"Sec2.HIFC.flow",
"FStar.Pervasives.Native.Mktuple2",
"Sec2.HIFC.label",
"Prims.Nil"
] | [] | false | true | false | false | false | let test12_1 ()
: IFC unit
(union cr0 (union cr1 cr2))
(union cw0 (union cw1 cw2))
[(cr0, cw1); (union cr0 cr1, cw2)] =
| c0 ();
(c1 ();
c2 ()) | false |
Hacl.Impl.Frodo.KEM.Decaps.fst | Hacl.Impl.Frodo.KEM.Decaps.crypto_kem_dec_ss0 | val crypto_kem_dec_ss0:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> mask:uint16{v mask == 0 \/ v mask == v (ones U16 SEC)}
-> kp:lbytes (crypto_bytes a)
-> s:lbytes (crypto_bytes a)
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h ct /\ live h kp /\ live h s /\ live h ss /\
disjoint ss ct /\ disjoint ss kp /\ disjoint ss s /\
disjoint kp s)
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss0 a (as_seq h0 ct) mask (as_seq h0 kp) (as_seq h0 s)) | val crypto_kem_dec_ss0:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> mask:uint16{v mask == 0 \/ v mask == v (ones U16 SEC)}
-> kp:lbytes (crypto_bytes a)
-> s:lbytes (crypto_bytes a)
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h ct /\ live h kp /\ live h s /\ live h ss /\
disjoint ss ct /\ disjoint ss kp /\ disjoint ss s /\
disjoint kp s)
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss0 a (as_seq h0 ct) mask (as_seq h0 kp) (as_seq h0 s)) | let crypto_kem_dec_ss0 a ct mask kp s ss =
push_frame ();
let kp_s = create (crypto_bytes a) (u8 0) in
Lib.ByteBuffer.buf_mask_select kp s (to_u8 mask) kp_s;
let ss_init_len = crypto_ciphertextbytes a +! crypto_bytes a in
let ss_init = create ss_init_len (u8 0) in
concat2 (crypto_ciphertextbytes a) ct (crypto_bytes a) kp_s ss_init;
frodo_shake a ss_init_len ss_init (crypto_bytes a) ss;
clear_words_u8 ss_init;
clear_words_u8 kp_s;
pop_frame () | {
"file_name": "code/frodo/Hacl.Impl.Frodo.KEM.Decaps.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 224,
"start_col": 0,
"start_line": 213
} | module Hacl.Impl.Frodo.KEM.Decaps
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Hacl.Impl.Matrix
open Hacl.Impl.Frodo.Params
open Hacl.Impl.Frodo.KEM
open Hacl.Impl.Frodo.KEM.Encaps
open Hacl.Impl.Frodo.Encode
open Hacl.Impl.Frodo.Pack
open Hacl.Impl.Frodo.Sample
open Hacl.Frodo.Random
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module FP = Spec.Frodo.Params
module KG = Hacl.Impl.Frodo.KEM.KeyGen
module S = Spec.Frodo.KEM.Decaps
module M = Spec.Matrix
#set-options "--z3rlimit 100 --fuel 0 --ifuel 0"
inline_for_extraction noextract
val get_bp_c_matrices:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h ct /\ live h bp_matrix /\ live h c_matrix /\
disjoint bp_matrix ct /\ disjoint c_matrix ct /\ disjoint bp_matrix c_matrix)
(ensures fun h0 _ h1 -> modifies (loc bp_matrix |+| loc c_matrix) h0 h1 /\
(as_matrix h1 bp_matrix, as_matrix h1 c_matrix) == S.get_bp_c_matrices a (as_seq h0 ct))
let get_bp_c_matrices a ct bp_matrix c_matrix =
let c1 = sub ct 0ul (ct1bytes_len a) in
let c2 = sub ct (ct1bytes_len a) (ct2bytes_len a) in
frodo_unpack params_nbar (params_n a) (params_logq a) c1 bp_matrix;
frodo_unpack params_nbar params_nbar (params_logq a) c2 c_matrix
inline_for_extraction noextract
val frodo_mu_decode:
a:FP.frodo_alg
-> s_bytes:lbytes (secretmatrixbytes_len a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h s_bytes /\ live h bp_matrix /\
live h c_matrix /\ live h mu_decode /\
disjoint mu_decode s_bytes /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0))
(ensures fun h0 _ h1 -> modifies (loc mu_decode) h0 h1 /\
as_seq h1 mu_decode ==
S.frodo_mu_decode a (as_seq h0 s_bytes) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let frodo_mu_decode a s_bytes bp_matrix c_matrix mu_decode =
push_frame();
let s_matrix = matrix_create (params_n a) params_nbar in
let m_matrix = matrix_create params_nbar params_nbar in
matrix_from_lbytes s_bytes s_matrix;
matrix_mul_s bp_matrix s_matrix m_matrix;
matrix_sub c_matrix m_matrix;
frodo_key_decode (params_logq a) (params_extracted_bits a) params_nbar m_matrix mu_decode;
clear_matrix s_matrix;
clear_matrix m_matrix;
pop_frame()
inline_for_extraction noextract
val get_bpp_cp_matrices_:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> sp_matrix:matrix_t params_nbar (params_n a)
-> ep_matrix:matrix_t params_nbar (params_n a)
-> epp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
live h sp_matrix /\ live h ep_matrix /\ live h epp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc sk; loc bpp_matrix;
loc cp_matrix; loc sp_matrix; loc ep_matrix; loc epp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices_ a gen_a (as_seq h0 mu_decode) (as_seq h0 sk)
(as_matrix h0 sp_matrix) (as_matrix h0 ep_matrix) (as_matrix h0 epp_matrix))
let get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix =
FP.expand_crypto_secretkeybytes a;
FP.expand_crypto_publickeybytes a;
let pk = sub sk (crypto_bytes a) (crypto_publickeybytes a) in
let seed_a = sub pk 0ul bytes_seed_a in
let b = sub pk bytes_seed_a (crypto_publickeybytes a -! bytes_seed_a) in
frodo_mul_add_sa_plus_e a gen_a seed_a sp_matrix ep_matrix bpp_matrix;
frodo_mul_add_sb_plus_e_plus_mu a mu_decode b sp_matrix epp_matrix cp_matrix;
mod_pow2 (params_logq a) bpp_matrix;
mod_pow2 (params_logq a) cp_matrix
#push-options "--z3rlimit 150"
inline_for_extraction noextract
val get_bpp_cp_matrices:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> seed_se:lbytes (crypto_bytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h seed_se /\ live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc seed_se; loc sk; loc bpp_matrix; loc cp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices a gen_a (as_seq h0 mu_decode) (as_seq h0 seed_se) (as_seq h0 sk))
let get_bpp_cp_matrices a gen_a mu_decode seed_se sk bpp_matrix cp_matrix =
push_frame ();
let sp_matrix = matrix_create params_nbar (params_n a) in
let ep_matrix = matrix_create params_nbar (params_n a) in
let epp_matrix = matrix_create params_nbar params_nbar in
get_sp_ep_epp_matrices a seed_se sp_matrix ep_matrix epp_matrix;
get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix;
clear_matrix3 a sp_matrix ep_matrix epp_matrix;
pop_frame ()
#pop-options
inline_for_extraction noextract
val crypto_kem_dec_kp_s_cond:
a:FP.frodo_alg
-> bp_matrix:matrix_t params_nbar (params_n a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h bp_matrix /\ live h bpp_matrix /\
live h c_matrix /\ live h cp_matrix)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s_cond a (as_matrix h0 bp_matrix) (as_matrix h0 bpp_matrix)
(as_matrix h0 c_matrix) (as_matrix h0 cp_matrix))
let crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix =
let b1 = matrix_eq bp_matrix bpp_matrix in
let b2 = matrix_eq c_matrix cp_matrix in
b1 &. b2
inline_for_extraction noextract
val crypto_kem_dec_kp_s:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> seed_se:lbytes (crypto_bytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h mu_decode /\ live h bp_matrix /\ live h seed_se /\
live h c_matrix /\ live h sk /\
loc_pairwise_disjoint [loc mu_decode; loc seed_se; loc sk])
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s a gen_a (as_seq h0 mu_decode) (as_seq h0 seed_se)
(as_seq h0 sk) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let crypto_kem_dec_kp_s a gen_a mu_decode seed_se sk bp_matrix c_matrix =
push_frame ();
let bpp_matrix = matrix_create params_nbar (params_n a) in
let cp_matrix = matrix_create params_nbar params_nbar in
get_bpp_cp_matrices a gen_a mu_decode seed_se sk bpp_matrix cp_matrix;
let mask = crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix in
pop_frame ();
mask
inline_for_extraction noextract
val crypto_kem_dec_ss0:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> mask:uint16{v mask == 0 \/ v mask == v (ones U16 SEC)}
-> kp:lbytes (crypto_bytes a)
-> s:lbytes (crypto_bytes a)
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h ct /\ live h kp /\ live h s /\ live h ss /\
disjoint ss ct /\ disjoint ss kp /\ disjoint ss s /\
disjoint kp s)
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss0 a (as_seq h0 ct) mask (as_seq h0 kp) (as_seq h0 s)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Params.fst.checked",
"Spec.Frodo.KEM.Decaps.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Matrix.fst.checked",
"Hacl.Impl.Frodo.Sample.fst.checked",
"Hacl.Impl.Frodo.Params.fst.checked",
"Hacl.Impl.Frodo.Pack.fst.checked",
"Hacl.Impl.Frodo.KEM.KeyGen.fst.checked",
"Hacl.Impl.Frodo.KEM.Encaps.fst.checked",
"Hacl.Impl.Frodo.KEM.fst.checked",
"Hacl.Impl.Frodo.Encode.fst.checked",
"Hacl.Frodo.Random.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Frodo.KEM.Decaps.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.KEM.Decaps",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Frodo.KEM.KeyGen",
"short_module": "KG"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.Params",
"short_module": "FP"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Frodo.Random",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Sample",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Pack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Encode",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM.Encaps",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Params",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Matrix",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Spec.Frodo.Params.frodo_alg ->
ct: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_ciphertextbytes a) ->
mask:
Lib.IntTypes.uint16
{ Lib.IntTypes.v mask == 0 \/
Lib.IntTypes.v mask ==
Lib.IntTypes.v (Lib.IntTypes.ones Lib.IntTypes.U16 Lib.IntTypes.SEC) } ->
kp: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_bytes a) ->
s: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_bytes a) ->
ss: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_bytes a)
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Frodo.Params.frodo_alg",
"Hacl.Impl.Matrix.lbytes",
"Hacl.Impl.Frodo.Params.crypto_ciphertextbytes",
"Lib.IntTypes.uint16",
"Prims.l_or",
"Prims.eq2",
"Prims.int",
"Lib.IntTypes.v",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Lib.IntTypes.range_t",
"Lib.IntTypes.ones",
"Hacl.Impl.Frodo.Params.crypto_bytes",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Hacl.Impl.Frodo.KEM.clear_words_u8",
"Hacl.Impl.Frodo.Params.frodo_shake",
"Lib.Buffer.concat2",
"Lib.Buffer.MUT",
"Lib.IntTypes.uint8",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.Buffer.create",
"Lib.IntTypes.u8",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.op_Plus_Bang",
"Lib.ByteBuffer.buf_mask_select",
"Lib.IntTypes.to_u8",
"FStar.HyperStack.ST.push_frame"
] | [] | false | true | false | false | false | let crypto_kem_dec_ss0 a ct mask kp s ss =
| push_frame ();
let kp_s = create (crypto_bytes a) (u8 0) in
Lib.ByteBuffer.buf_mask_select kp s (to_u8 mask) kp_s;
let ss_init_len = crypto_ciphertextbytes a +! crypto_bytes a in
let ss_init = create ss_init_len (u8 0) in
concat2 (crypto_ciphertextbytes a) ct (crypto_bytes a) kp_s ss_init;
frodo_shake a ss_init_len ss_init (crypto_bytes a) ss;
clear_words_u8 ss_init;
clear_words_u8 kp_s;
pop_frame () | false |
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.ci_ex1_ | val ci_ex1_ (x: nat) : unit | val ci_ex1_ (x: nat) : unit | let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
() | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 4,
"end_line": 139,
"start_col": 0,
"start_line": 136
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.nat -> Prims.unit | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.unit",
"Prims._assert",
"Prims.eq2"
] | [] | false | false | false | true | false | let ci_ex1_ (x: nat) : unit =
| let y = x in
assert (x == y);
() | false |
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.invariant1 | val invariant1 : h: FStar.Monotonic.HyperStack.mem ->
r1: LowStar.Buffer.buffer Prims.int ->
r2: LowStar.Buffer.buffer Prims.int ->
r3: LowStar.Buffer.buffer Prims.int
-> Prims.GTot Prims.logical | let invariant1 (h : HS.mem) (r1 r2 r3 : B.buffer int) =
let l1 = B.as_seq h r1 in
let l2 = B.as_seq h r2 in
let l3 = B.as_seq h r3 in
invariant1_s l1 l2 l3 /\
B.live h r1 /\ B.live h r2 /\ B.live h r3 /\
B.disjoint r1 r2 /\ B.disjoint r2 r3 /\ B.disjoint r1 r3 | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 58,
"end_line": 289,
"start_col": 0,
"start_line": 283
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those,
/// put the pointer close enough to the beginning of the function and type `C-c C-e C-g`
let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z
/// Those commands also work on effectful terms. They may need to
/// introduce variables in the context. For instance, when dealing with pres/posts
/// of stateful terms, it will look for state variables with which to instantiate
/// those pre/posts, but might not be able to find suitable variables.
/// In this case, it introduces fresh variables (easy to recognize because they are
/// preceded by "__") and abstracts them away, to indicate the user that he needs
/// to provide those variables.
///
/// It leads to assertions of the form:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) __x0 __x1)
///
/// As (C-c C-e C-e) performs simple normalization (to remove abstractions,
/// for instance) on the terms it manipulates, you can manually rewrite this
/// assert to:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) x y)
///
/// then apply (C-c C-e C-e) on the above assertion to get:
/// [> assert(pred x y)
///
/// Try this on the stateful calls in the below function:
let ci_ex3 (r1 r2 : B.buffer int) :
ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
(**) let h0 = ST.get () in
let n1 = sf1 r1 in (* <- Try C-c C-e C-e here *)
(* [> assert(
[> (fun __h1_0 ->
[> LowStar.Monotonic.Buffer.live h0 r1 /\
[> LowStar.Monotonic.Buffer.as_seq h0 r1 == LowStar.Monotonic.Buffer.as_seq __h1_0 r1 /\
[> n1 =
[> FStar.List.Tot.Base.fold_left (fun x y -> x + y)
[> 0
[> (FStar.Seq.Properties.seq_to_list (LowStar.Monotonic.Buffer.as_seq h0 r1))) __h1_0); *)
(**) let h1 = ST.get () in
let n2 = sf1 r2 in
(**) let h2 = ST.get () in
n1 + n2
/// It may happen that the command needs to introduce assertions using variables
/// which are shadowed at that point. In this case, it "abstracts" them, like what
/// is done in the previous example. This way, the user still has an assertion
/// he can investigate, and where the problematic variables are clearly indicated
/// if he wants to work with it.
let ci_ex4 (x : int{x % 2 = 0}) :
Pure int
(requires True)
(ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) =
let x = x + 4 in (* We shadow the original ``x`` here *)
x (* <- Try C-c C-e C-e here *)
(**** Split conjunctions *)
/// Proof obligations are often written in the form of big conjunctions, and
/// F* may not always be precise enough to indicate which part of the conjunction
/// fails. The user then often has to "split" the conjunctions by hand, by
/// introducing one assertion per conjunct.
/// The fem-split-assert-assume-conjuncts command (C-c C-e C-s) automates the process.
/// Move the pointer anywhere inside the below assert and use C-c C-e C-s.
let split_ex1 (x y z : nat) : unit =
assert( (* <- Try C-c C-e C-s anywhere inside the assert *)
pred1 x y z /\
pred2 x y z /\
pred3 x y z /\
pred4 x y z /\
pred5 x y z /\
pred6 x y z)
/// Note that you can call the above command in any of the following terms:
/// - ``assert``
/// - ``assert_norm``
/// - ``assume``
(**** Terms unfoldign *)
/// It sometimes happens that we need to unfold a term in an assertion, for example
/// in order to check why some equality is not satisfied.
/// fem-unfold-in-assert-assume (C-c C-s C-u) addresses this issue.
let ut_ex1 (x y : nat) : unit =
let z1 = f3 (x + y) in
(* Unfold definitions: *)
assert(z1 = f3 (x + y)); (* <- Move the pointer EXACTLY over ``f3`` and use C-c C-e C-u *)
(* Unfold arbitrary identifiers:
* In case the term to unfold is not a top-level definition but a local
* variable, the command will look for a pure let-binding and will even
* explore post-conditions to look for an equality to find a term by
* which to replace the variable. *)
assert(z1 = 2 * (x + y)); (* <- Try the command on ``z1`` *)
(* Note that if the assertion is an equality, the command will only
* operate on one side of the equality at a time. *)
assert(z1 = z1);
(*
* It is even possible to apply the command on arbitrary term, in which
* case the command will explore post-conditions to find an equality to
* use for rewriting.
*)
assert(f3 (f3 (x + y)) = 4 * (x + y));
assert(2 * z1 = z1 + z1);
assert(f3 (f3 (x + y)) = 2 * z1) (* <- SELECT an operand then call C-c C-e C-u *)
/// Of course, it works with effectful functions too, and searches the context
/// for state variables to use:
let ut_ex2 () : ST.ST unit (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
let l : list int = [1; 2; 3; 4; 5; 6] in
let h0 = ST.get () in
let r = sf2 l in (* This dummy function introduces some equalities in the context *)
let h1 = ST.get () in
assert(B.as_seq h1 r == B.as_seq h1 r); (* <- Try here *)
()
(**** Combining commands *)
/// Those commands prove really useful when you combine them. The below example
/// is inspired by a function found in HACL*.
/// Invariants are sometimes divided in several pieces, for example a
/// functional part, to which we later add information about aliasing.
/// Moreover they are often written in the form of big conjunctions:
let invariant1_s (l1 l2 l3 : Seq.seq int) =
lpred1 l1 l2 /\
lpred2 l2 l3 /\
lpred3 l3 l1 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h: FStar.Monotonic.HyperStack.mem ->
r1: LowStar.Buffer.buffer Prims.int ->
r2: LowStar.Buffer.buffer Prims.int ->
r3: LowStar.Buffer.buffer Prims.int
-> Prims.GTot Prims.logical | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Buffer.buffer",
"Prims.int",
"Prims.l_and",
"FStar.InteractiveHelpers.Tutorial.invariant1_s",
"LowStar.Monotonic.Buffer.live",
"LowStar.Buffer.trivial_preorder",
"LowStar.Monotonic.Buffer.disjoint",
"FStar.Seq.Base.seq",
"LowStar.Monotonic.Buffer.as_seq",
"Prims.logical"
] | [] | false | false | false | false | true | let invariant1 (h: HS.mem) (r1 r2 r3: B.buffer int) =
| let l1 = B.as_seq h r1 in
let l2 = B.as_seq h r2 in
let l3 = B.as_seq h r3 in
invariant1_s l1 l2 l3 /\ B.live h r1 /\ B.live h r2 /\ B.live h r3 /\ B.disjoint r1 r2 /\
B.disjoint r2 r3 /\ B.disjoint r1 r3 | false |
|
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.invariant1_s | val invariant1_s : l1: FStar.Seq.Base.seq Prims.int ->
l2: FStar.Seq.Base.seq Prims.int ->
l3: FStar.Seq.Base.seq Prims.int
-> Prims.logical | let invariant1_s (l1 l2 l3 : Seq.seq int) =
lpred1 l1 l2 /\
lpred2 l2 l3 /\
lpred3 l3 l1 | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 14,
"end_line": 281,
"start_col": 0,
"start_line": 278
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those,
/// put the pointer close enough to the beginning of the function and type `C-c C-e C-g`
let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z
/// Those commands also work on effectful terms. They may need to
/// introduce variables in the context. For instance, when dealing with pres/posts
/// of stateful terms, it will look for state variables with which to instantiate
/// those pre/posts, but might not be able to find suitable variables.
/// In this case, it introduces fresh variables (easy to recognize because they are
/// preceded by "__") and abstracts them away, to indicate the user that he needs
/// to provide those variables.
///
/// It leads to assertions of the form:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) __x0 __x1)
///
/// As (C-c C-e C-e) performs simple normalization (to remove abstractions,
/// for instance) on the terms it manipulates, you can manually rewrite this
/// assert to:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) x y)
///
/// then apply (C-c C-e C-e) on the above assertion to get:
/// [> assert(pred x y)
///
/// Try this on the stateful calls in the below function:
let ci_ex3 (r1 r2 : B.buffer int) :
ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
(**) let h0 = ST.get () in
let n1 = sf1 r1 in (* <- Try C-c C-e C-e here *)
(* [> assert(
[> (fun __h1_0 ->
[> LowStar.Monotonic.Buffer.live h0 r1 /\
[> LowStar.Monotonic.Buffer.as_seq h0 r1 == LowStar.Monotonic.Buffer.as_seq __h1_0 r1 /\
[> n1 =
[> FStar.List.Tot.Base.fold_left (fun x y -> x + y)
[> 0
[> (FStar.Seq.Properties.seq_to_list (LowStar.Monotonic.Buffer.as_seq h0 r1))) __h1_0); *)
(**) let h1 = ST.get () in
let n2 = sf1 r2 in
(**) let h2 = ST.get () in
n1 + n2
/// It may happen that the command needs to introduce assertions using variables
/// which are shadowed at that point. In this case, it "abstracts" them, like what
/// is done in the previous example. This way, the user still has an assertion
/// he can investigate, and where the problematic variables are clearly indicated
/// if he wants to work with it.
let ci_ex4 (x : int{x % 2 = 0}) :
Pure int
(requires True)
(ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) =
let x = x + 4 in (* We shadow the original ``x`` here *)
x (* <- Try C-c C-e C-e here *)
(**** Split conjunctions *)
/// Proof obligations are often written in the form of big conjunctions, and
/// F* may not always be precise enough to indicate which part of the conjunction
/// fails. The user then often has to "split" the conjunctions by hand, by
/// introducing one assertion per conjunct.
/// The fem-split-assert-assume-conjuncts command (C-c C-e C-s) automates the process.
/// Move the pointer anywhere inside the below assert and use C-c C-e C-s.
let split_ex1 (x y z : nat) : unit =
assert( (* <- Try C-c C-e C-s anywhere inside the assert *)
pred1 x y z /\
pred2 x y z /\
pred3 x y z /\
pred4 x y z /\
pred5 x y z /\
pred6 x y z)
/// Note that you can call the above command in any of the following terms:
/// - ``assert``
/// - ``assert_norm``
/// - ``assume``
(**** Terms unfoldign *)
/// It sometimes happens that we need to unfold a term in an assertion, for example
/// in order to check why some equality is not satisfied.
/// fem-unfold-in-assert-assume (C-c C-s C-u) addresses this issue.
let ut_ex1 (x y : nat) : unit =
let z1 = f3 (x + y) in
(* Unfold definitions: *)
assert(z1 = f3 (x + y)); (* <- Move the pointer EXACTLY over ``f3`` and use C-c C-e C-u *)
(* Unfold arbitrary identifiers:
* In case the term to unfold is not a top-level definition but a local
* variable, the command will look for a pure let-binding and will even
* explore post-conditions to look for an equality to find a term by
* which to replace the variable. *)
assert(z1 = 2 * (x + y)); (* <- Try the command on ``z1`` *)
(* Note that if the assertion is an equality, the command will only
* operate on one side of the equality at a time. *)
assert(z1 = z1);
(*
* It is even possible to apply the command on arbitrary term, in which
* case the command will explore post-conditions to find an equality to
* use for rewriting.
*)
assert(f3 (f3 (x + y)) = 4 * (x + y));
assert(2 * z1 = z1 + z1);
assert(f3 (f3 (x + y)) = 2 * z1) (* <- SELECT an operand then call C-c C-e C-u *)
/// Of course, it works with effectful functions too, and searches the context
/// for state variables to use:
let ut_ex2 () : ST.ST unit (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
let l : list int = [1; 2; 3; 4; 5; 6] in
let h0 = ST.get () in
let r = sf2 l in (* This dummy function introduces some equalities in the context *)
let h1 = ST.get () in
assert(B.as_seq h1 r == B.as_seq h1 r); (* <- Try here *)
()
(**** Combining commands *)
/// Those commands prove really useful when you combine them. The below example
/// is inspired by a function found in HACL*.
/// Invariants are sometimes divided in several pieces, for example a
/// functional part, to which we later add information about aliasing. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
l1: FStar.Seq.Base.seq Prims.int ->
l2: FStar.Seq.Base.seq Prims.int ->
l3: FStar.Seq.Base.seq Prims.int
-> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"FStar.Seq.Base.seq",
"Prims.int",
"Prims.l_and",
"FStar.InteractiveHelpers.Tutorial.Definitions.lpred1",
"FStar.InteractiveHelpers.Tutorial.Definitions.lpred2",
"FStar.InteractiveHelpers.Tutorial.Definitions.lpred3",
"Prims.logical"
] | [] | false | false | false | true | true | let invariant1_s (l1 l2 l3: Seq.seq int) =
| lpred1 l1 l2 /\ lpred2 l2 l3 /\ lpred3 l3 l1 | false |
|
Hacl.EC.K256.fst | Hacl.EC.K256.mk_point_at_inf | val mk_point_at_inf: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ S.to_aff_point (P.point_eval h1 p) == S.aff_point_at_inf) | val mk_point_at_inf: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ S.to_aff_point (P.point_eval h1 p) == S.aff_point_at_inf) | let mk_point_at_inf p =
P.make_point_at_inf p | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 23,
"end_line": 234,
"start_col": 0,
"start_line": 233
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b))
let felem_mul a b out =
F.fmul out a b
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a))
let felem_sqr a out =
F.fsqr out a
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a))
let felem_inv a out =
FI.finv out a
[@@ Comment "Load a bid-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint"]
val felem_load: b:lbuffer uint8 32ul -> out:F.felem -> Stack unit
(requires fun h -> live h b /\ live h out /\ disjoint b out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == BSeq.nat_from_bytes_be (as_seq h0 b) % S.prime)
let felem_load b out =
F.load_felem out b
[@@ Comment "Serialize a field element into big-endian memory.
The argument `a` points to a field element of 5 limbs in size, i.e., uint64_t[5].
The outparam `out` points to 32 bytes of valid memory, i.e., uint8_t[32].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_store: a:F.felem -> out:lbuffer uint8 32ul -> Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == BSeq.nat_to_bytes_be 32 (F.feval h0 a))
let felem_store a out =
push_frame ();
let tmp = F.create_felem () in
let h0 = ST.get () in
BL.normalize5_lemma (1,1,1,1,2) (F.as_felem5 h0 a);
F.fnormalize tmp a;
F.store_felem out tmp;
pop_frame ()
[@@ CPrologue
"/*******************************************************************************
Verified group operations for the secp256k1 curve of the form y^2 = x^3 + 7.
This is a 64-bit optimized version, where a group element in projective coordinates
is represented as an array of 15 unsigned 64-bit integers, i.e., uint64_t[15].
*******************************************************************************/\n";
Comment "Write the point at infinity (additive identity) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_point_at_inf: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ S.to_aff_point (P.point_eval h1 p) == S.aff_point_at_inf) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Hacl.Impl.K256.Point.point -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.K256.Point.point",
"Hacl.Impl.K256.Point.make_point_at_inf",
"Prims.unit"
] | [] | false | true | false | false | false | let mk_point_at_inf p =
| P.make_point_at_inf p | false |
Hacl.EC.K256.fst | Hacl.EC.K256.mk_base_point | val mk_base_point: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ P.point_eval h1 p == S.g) | val mk_base_point: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ P.point_eval h1 p == S.g) | let mk_base_point p =
P.make_g p | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 12,
"end_line": 246,
"start_col": 0,
"start_line": 245
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b))
let felem_mul a b out =
F.fmul out a b
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a))
let felem_sqr a out =
F.fsqr out a
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a))
let felem_inv a out =
FI.finv out a
[@@ Comment "Load a bid-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint"]
val felem_load: b:lbuffer uint8 32ul -> out:F.felem -> Stack unit
(requires fun h -> live h b /\ live h out /\ disjoint b out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == BSeq.nat_from_bytes_be (as_seq h0 b) % S.prime)
let felem_load b out =
F.load_felem out b
[@@ Comment "Serialize a field element into big-endian memory.
The argument `a` points to a field element of 5 limbs in size, i.e., uint64_t[5].
The outparam `out` points to 32 bytes of valid memory, i.e., uint8_t[32].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_store: a:F.felem -> out:lbuffer uint8 32ul -> Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == BSeq.nat_to_bytes_be 32 (F.feval h0 a))
let felem_store a out =
push_frame ();
let tmp = F.create_felem () in
let h0 = ST.get () in
BL.normalize5_lemma (1,1,1,1,2) (F.as_felem5 h0 a);
F.fnormalize tmp a;
F.store_felem out tmp;
pop_frame ()
[@@ CPrologue
"/*******************************************************************************
Verified group operations for the secp256k1 curve of the form y^2 = x^3 + 7.
This is a 64-bit optimized version, where a group element in projective coordinates
is represented as an array of 15 unsigned 64-bit integers, i.e., uint64_t[15].
*******************************************************************************/\n";
Comment "Write the point at infinity (additive identity) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_point_at_inf: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ S.to_aff_point (P.point_eval h1 p) == S.aff_point_at_inf)
let mk_point_at_inf p =
P.make_point_at_inf p
[@@ Comment "Write the base point (generator) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_base_point: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ P.point_eval h1 p == S.g) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Hacl.Impl.K256.Point.point -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.K256.Point.point",
"Hacl.Impl.K256.Point.make_g",
"Prims.unit"
] | [] | false | true | false | false | false | let mk_base_point p =
| P.make_g p | false |
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.dbg_ex1 | val dbg_ex1: Prims.unit -> Tot nat | val dbg_ex1: Prims.unit -> Tot nat | let dbg_ex1 () : Tot nat =
let x = 4 in
let y = 2 in
f1 x y | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 8,
"end_line": 369,
"start_col": 0,
"start_line": 366
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those,
/// put the pointer close enough to the beginning of the function and type `C-c C-e C-g`
let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z
/// Those commands also work on effectful terms. They may need to
/// introduce variables in the context. For instance, when dealing with pres/posts
/// of stateful terms, it will look for state variables with which to instantiate
/// those pre/posts, but might not be able to find suitable variables.
/// In this case, it introduces fresh variables (easy to recognize because they are
/// preceded by "__") and abstracts them away, to indicate the user that he needs
/// to provide those variables.
///
/// It leads to assertions of the form:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) __x0 __x1)
///
/// As (C-c C-e C-e) performs simple normalization (to remove abstractions,
/// for instance) on the terms it manipulates, you can manually rewrite this
/// assert to:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) x y)
///
/// then apply (C-c C-e C-e) on the above assertion to get:
/// [> assert(pred x y)
///
/// Try this on the stateful calls in the below function:
let ci_ex3 (r1 r2 : B.buffer int) :
ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
(**) let h0 = ST.get () in
let n1 = sf1 r1 in (* <- Try C-c C-e C-e here *)
(* [> assert(
[> (fun __h1_0 ->
[> LowStar.Monotonic.Buffer.live h0 r1 /\
[> LowStar.Monotonic.Buffer.as_seq h0 r1 == LowStar.Monotonic.Buffer.as_seq __h1_0 r1 /\
[> n1 =
[> FStar.List.Tot.Base.fold_left (fun x y -> x + y)
[> 0
[> (FStar.Seq.Properties.seq_to_list (LowStar.Monotonic.Buffer.as_seq h0 r1))) __h1_0); *)
(**) let h1 = ST.get () in
let n2 = sf1 r2 in
(**) let h2 = ST.get () in
n1 + n2
/// It may happen that the command needs to introduce assertions using variables
/// which are shadowed at that point. In this case, it "abstracts" them, like what
/// is done in the previous example. This way, the user still has an assertion
/// he can investigate, and where the problematic variables are clearly indicated
/// if he wants to work with it.
let ci_ex4 (x : int{x % 2 = 0}) :
Pure int
(requires True)
(ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) =
let x = x + 4 in (* We shadow the original ``x`` here *)
x (* <- Try C-c C-e C-e here *)
(**** Split conjunctions *)
/// Proof obligations are often written in the form of big conjunctions, and
/// F* may not always be precise enough to indicate which part of the conjunction
/// fails. The user then often has to "split" the conjunctions by hand, by
/// introducing one assertion per conjunct.
/// The fem-split-assert-assume-conjuncts command (C-c C-e C-s) automates the process.
/// Move the pointer anywhere inside the below assert and use C-c C-e C-s.
let split_ex1 (x y z : nat) : unit =
assert( (* <- Try C-c C-e C-s anywhere inside the assert *)
pred1 x y z /\
pred2 x y z /\
pred3 x y z /\
pred4 x y z /\
pred5 x y z /\
pred6 x y z)
/// Note that you can call the above command in any of the following terms:
/// - ``assert``
/// - ``assert_norm``
/// - ``assume``
(**** Terms unfoldign *)
/// It sometimes happens that we need to unfold a term in an assertion, for example
/// in order to check why some equality is not satisfied.
/// fem-unfold-in-assert-assume (C-c C-s C-u) addresses this issue.
let ut_ex1 (x y : nat) : unit =
let z1 = f3 (x + y) in
(* Unfold definitions: *)
assert(z1 = f3 (x + y)); (* <- Move the pointer EXACTLY over ``f3`` and use C-c C-e C-u *)
(* Unfold arbitrary identifiers:
* In case the term to unfold is not a top-level definition but a local
* variable, the command will look for a pure let-binding and will even
* explore post-conditions to look for an equality to find a term by
* which to replace the variable. *)
assert(z1 = 2 * (x + y)); (* <- Try the command on ``z1`` *)
(* Note that if the assertion is an equality, the command will only
* operate on one side of the equality at a time. *)
assert(z1 = z1);
(*
* It is even possible to apply the command on arbitrary term, in which
* case the command will explore post-conditions to find an equality to
* use for rewriting.
*)
assert(f3 (f3 (x + y)) = 4 * (x + y));
assert(2 * z1 = z1 + z1);
assert(f3 (f3 (x + y)) = 2 * z1) (* <- SELECT an operand then call C-c C-e C-u *)
/// Of course, it works with effectful functions too, and searches the context
/// for state variables to use:
let ut_ex2 () : ST.ST unit (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
let l : list int = [1; 2; 3; 4; 5; 6] in
let h0 = ST.get () in
let r = sf2 l in (* This dummy function introduces some equalities in the context *)
let h1 = ST.get () in
assert(B.as_seq h1 r == B.as_seq h1 r); (* <- Try here *)
()
(**** Combining commands *)
/// Those commands prove really useful when you combine them. The below example
/// is inspired by a function found in HACL*.
/// Invariants are sometimes divided in several pieces, for example a
/// functional part, to which we later add information about aliasing.
/// Moreover they are often written in the form of big conjunctions:
let invariant1_s (l1 l2 l3 : Seq.seq int) =
lpred1 l1 l2 /\
lpred2 l2 l3 /\
lpred3 l3 l1
let invariant1 (h : HS.mem) (r1 r2 r3 : B.buffer int) =
let l1 = B.as_seq h r1 in
let l2 = B.as_seq h r2 in
let l3 = B.as_seq h r3 in
invariant1_s l1 l2 l3 /\
B.live h r1 /\ B.live h r2 /\ B.live h r3 /\
B.disjoint r1 r2 /\ B.disjoint r2 r3 /\ B.disjoint r1 r3
/// The following function has to maintain the invariant. Now let's imagine
/// that once the function is written, you fail to prove that the postcondition
/// is satisfied and, worse but you don't know it yet, the problem comes from
/// one of the conjuncts inside ``invariant_s``. With the commands introduced
/// so far, it is pretty easy to debug that thing!
let cc_ex1 (r1 r2 r3 : B.buffer int) :
ST.Stack nat
(requires (fun h0 -> invariant1 h0 r1 r2 r3))
(ensures (fun h0 x h1 -> invariant1 h1 r1 r2 r3 /\ x >= 3)) =
(**) let h0 = ST.get () in
let x = 3 in
(*
* ...
* In practice there would be some (a lot of) code here
* ...
*)
(**) let h1 = ST.get () in
x (* <- Try studying the proof obligations here *)
(**** For advanced users and hackers only *)
(***** Two-steps execution - TODO: broken *)
/// The commands implemented in the F* extended mode work by updating the function
/// defined by the user, by inserting some annotations and instructions for F*
/// (like post-processing instructions), query F* on the result and retrieve the
/// data output by the meta-F* functions to insert appropriate assertions in the
/// code.
/// Updating the user code requires some parsing, but it may happen that the commands
/// can't fill the holes in a function the user is writing, and which thus has many
/// missing parts. In this case, we need to provide a bit of help. For instance,
/// this can happen when trying to call the commands on a subterm inside the branch
/// of a match (or an 'if ... then .. else ...').
/// With the fem-insert-pos-markers (C-c C-s C-i), the user can do a differed command
/// execution, by first indicating the subterm which he wants to analyze, then by
/// indicating to F* how to parse the whole function.
/// Let's try on the below example:
let ts_ex1 (x : int) =
let y : nat =
if x >= 0 then
begin
let z1 = x + 4 in
let z2 : int = f1 z1 x in (* <- Say you want to use C-c C-e here: first use C-c C-s C-i *)
z2
end
else -x
in
let z = f2 y 3 in
z (* <- Then use C-c C-e C-e here to indicate where the end of the function is *)
/// You probably noticed that C-c C-s C-i introduces a marker in the code. You
/// can remove by calling C-c C-s C-i again (this will look for markers before
/// the pointer and remove them).
/// In the future, we intend to instrument Merlin to parse partially written
/// expressions, so that the user won't have to do two-steps execution for
/// examples like the above one.
(**** Debugging *)
/// If F* fails on the queries it receives, the interactive commands ask the user
/// if he wants to see the query which was sent, in which case emacs switches
/// between buffers.
/// By doing that, we provide a convenient way for debugging: you may have
/// to modify a bit the functions on which you call the command, or move the
/// pointers before executing it.
/// In the worst case, you can copy paste the query to the current F* buffer, modify
/// it and parse it yourself from there: the results of the analysis by the Meta-F*
/// functions will be output in the *Messages* buffer, from where you can easily
/// copy-paste them, which is still a lot faster than deriving such results by hand.
/// For information: the commands mostly work by inserting appropriate annotations
/// and terms inside the user code, before sending the command to F*. For instance,
/// using C-c C-e in the first function below is equivalent to parsing the second
/// one (modulo the fact that the generated assertions won't be inserted into the
/// user code): | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> Prims.nat | Prims.Tot | [
"total"
] | [] | [
"Prims.unit",
"FStar.InteractiveHelpers.Tutorial.Definitions.f1",
"Prims.int",
"Prims.nat"
] | [] | false | false | false | true | false | let dbg_ex1 () : Tot nat =
| let x = 4 in
let y = 2 in
f1 x y | false |
Hacl.Spec.P256.Qinv.fst | Hacl.Spec.P256.Qinv.qinv_lemma | val qinv_lemma: f:S.qelem -> Lemma (qinv f == M.pow f (S.order - 2) % S.order) | val qinv_lemma: f:S.qelem -> Lemma (qinv f == M.pow f (S.order - 2) % S.order) | let qinv_lemma f =
let x_10 = qsquare_times f 1 in
qsquare_times_lemma f 1;
assert_norm (pow2 1 = 0x2);
assert (x_10 == M.pow f 0x2 % S.order);
let x_11 = S.qmul x_10 f in
lemma_pow_mod_1 f;
lemma_pow_mod_mul f 0x2 0x1;
assert (x_11 == M.pow f 0x3 % S.order);
let x_101 = S.qmul x_10 x_11 in
lemma_pow_mod_mul f 0x2 0x3;
assert (x_101 == M.pow f 0x5 % S.order);
let x_111 = S.qmul x_10 x_101 in
lemma_pow_mod_mul f 0x2 0x5;
assert (x_111 == M.pow f 0x7 % S.order);
let x_1010 = qsquare_times x_101 1 in
qsquare_times_lemma x_101 1;
assert_norm (pow2 1 = 2);
lemma_pow_pow_mod f 0x5 0x2;
assert (x_1010 == M.pow f 0xa % S.order);
let x_1111 = S.qmul x_101 x_1010 in
lemma_pow_mod_mul f 0x5 0xa;
assert (x_1111 == M.pow f 0xf % S.order);
let x_10101 = S.qmul (qsquare_times x_1010 1) f in
qsquare_times_lemma x_1010 1;
lemma_pow_pow_mod_mul f 0xa 0x2 0x1;
assert (x_10101 == M.pow f 0x15 % S.order);
let x_101010 = qsquare_times x_10101 1 in
qsquare_times_lemma x_10101 1;
lemma_pow_pow_mod f 0x15 0x2;
assert (x_101010 == M.pow f 0x2a % S.order);
let x_101111 = S.qmul x_101 x_101010 in
lemma_pow_mod_mul f 0x5 0x2a;
assert (x_101111 == M.pow f 0x2f % S.order);
let x6 = S.qmul x_10101 x_101010 in
lemma_pow_mod_mul f 0x15 0x2a;
assert (x6 == M.pow f 0x3f % S.order);
let x8 = S.qmul (qsquare_times x6 2) x_11 in
qsquare_times_lemma x6 2;
assert_norm (pow2 2 = 0x4);
lemma_pow_pow_mod_mul f 0x3f 0x4 0x3;
assert (x8 == M.pow f 0xff % S.order);
let x16 = S.qmul (qsquare_times x8 8) x8 in
qsquare_times_lemma x8 8;
assert_norm (pow2 8 = 0x100);
lemma_pow_pow_mod_mul f 0xff 0x100 0xff;
assert (x16 == M.pow f 0xffff % S.order);
let x32 = S.qmul (qsquare_times x16 16) x16 in
qsquare_times_lemma x16 16;
assert_norm (pow2 16 = 0x10000);
lemma_pow_pow_mod_mul f 0xffff 0x10000 0xffff;
assert (x32 == M.pow f 0xffffffff % S.order);
let x96 = S.qmul (qsquare_times x32 64) x32 in
qsquare_times_lemma x32 64;
assert_norm (pow2 64 = 0x10000000000000000);
lemma_pow_pow_mod_mul f 0xffffffff 0x10000000000000000 0xffffffff;
assert (x96 == M.pow f 0xffffffff00000000ffffffff % S.order);
let x128 = S.qmul (qsquare_times x96 32) x32 in
qsquare_times_lemma x96 32;
assert_norm (pow2 32 = 0x100000000);
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffff 0x100000000 0xffffffff;
assert (x128 == M.pow f 0xffffffff00000000ffffffffffffffff % S.order);
let x134 = S.qmul (qsquare_times x128 6) x_101111 in
qsquare_times_lemma x128 6;
assert_norm (pow2 6 = 0x40);
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffff 0x40 0x2f;
assert (x134 == M.pow f 0x3fffffffc00000003fffffffffffffffef % S.order);
let x139 = S.qmul (qsquare_times x134 5) x_111 in
qsquare_times_lemma x134 5;
assert_norm (pow2 5 = 0x20);
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef 0x20 0x7;
assert (x139 == M.pow f 0x7fffffff800000007fffffffffffffffde7 % S.order);
let x143 = S.qmul (qsquare_times x139 4) x_11 in
qsquare_times_lemma x139 4;
assert_norm (pow2 4 = 0x10);
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde7 0x10 0x3;
assert (x143 == M.pow f 0x7fffffff800000007fffffffffffffffde73 % S.order);
let x148 = S.qmul (qsquare_times x143 5) x_1111 in
qsquare_times_lemma x143 5;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde73 0x20 0xf;
assert (x148 == M.pow f 0xffffffff00000000ffffffffffffffffbce6f % S.order);
let x153 = S.qmul (qsquare_times x148 5) x_10101 in
qsquare_times_lemma x148 5;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6f 0x20 0x15;
assert (x153 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf5 % S.order);
let x157 = S.qmul (qsquare_times x153 4) x_101 in
qsquare_times_lemma x153 4;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf5 0x10 0x5;
assert (x157 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55 % S.order);
let x160 = S.qmul (qsquare_times x157 3) x_101 in
qsquare_times_lemma x157 3;
assert_norm (pow2 3 = 0x8);
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55 0x8 0x5;
assert (x160 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faad % S.order);
let x163 = S.qmul (qsquare_times x160 3) x_101 in
qsquare_times_lemma x160 3;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faad 0x8 0x5;
assert (x163 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d % S.order);
let x168 = S.qmul (qsquare_times x163 5) x_111 in
qsquare_times_lemma x163 5;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d 0x20 0x7;
assert (x168 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7 % S.order);
let x177 = S.qmul (qsquare_times x168 9) x_101111 in
qsquare_times_lemma x168 9;
assert_norm (pow2 9 = 0x200);
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faada7 0x200 0x2f;
assert (x177 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f % S.order);
let x183 = S.qmul (qsquare_times x177 6) x_1111 in
qsquare_times_lemma x177 6;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f 0x40 0xf;
assert (x183 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d38bcf % S.order);
let x185 = S.qmul (qsquare_times x183 2) f in
qsquare_times_lemma x183 2;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d38bcf 0x4 0x1;
assert (x185 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d % S.order);
let x190 = S.qmul (qsquare_times x185 5) f in
qsquare_times_lemma x185 5;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d 0x20 0x1;
assert (x190 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a1 % S.order);
let x196 = S.qmul (qsquare_times x190 6) x_1111 in
qsquare_times_lemma x190 6;
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a1 0x40 0xf;
assert (x196 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f % S.order);
let x201 = S.qmul (qsquare_times x196 5) x_111 in
qsquare_times_lemma x196 5;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f 0x20 0x7;
assert (x201 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d09e7 % S.order);
let x205 = S.qmul (qsquare_times x201 4) x_111 in
qsquare_times_lemma x201 4;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d09e7 0x10 0x7;
assert (x205 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d09e77 % S.order);
let x210 = S.qmul (qsquare_times x205 5) x_111 in
qsquare_times_lemma x205 5;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d09e77 0x20 0x7;
assert (x210 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee7 % S.order);
let x215 = S.qmul (qsquare_times x210 5) x_101 in
qsquare_times_lemma x210 5;
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee7 0x20 0x5;
assert (x215 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5 % S.order);
let x218 = S.qmul (qsquare_times x215 3) x_11 in
qsquare_times_lemma x215 3;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5 0x8 0x3;
assert (x218 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b % S.order);
let x228 = S.qmul (qsquare_times x218 10) x_101111 in
qsquare_times_lemma x218 10;
assert_norm (pow2 10 = 0x400);
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b 0x400 0x2f;
assert (x228 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2f % S.order);
let x230 = S.qmul (qsquare_times x228 2) x_11 in
qsquare_times_lemma x228 2;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2f 0x4 0x3;
assert (x230 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b0bf % S.order);
let x235 = S.qmul (qsquare_times x230 5) x_11 in
qsquare_times_lemma x230 5;
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b0bf 0x20 0x3;
assert (x235 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5617e3 % S.order);
let x240 = S.qmul (qsquare_times x235 5) x_11 in
qsquare_times_lemma x235 5;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5617e3 0x20 0x3;
assert (x240 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63 % S.order);
let x243 = S.qmul (qsquare_times x240 3) f in
qsquare_times_lemma x240 3;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63 0x8 0x1;
assert (x243 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5617e319 % S.order);
let x250 = S.qmul (qsquare_times x243 7) x_10101 in
qsquare_times_lemma x243 7;
assert_norm (pow2 7 = 0x80);
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5617e319 0x80 0x15;
assert (x250 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b0bf18c95 % S.order);
let x256 = S.qmul (qsquare_times x250 6) x_1111 in
qsquare_times_lemma x250 6;
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b0bf18c95 0x40 0xf;
assert (x256 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63254f % S.order);
assert_norm (S.order - 2 = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63254f) | {
"file_name": "code/ecdsap256/Hacl.Spec.P256.Qinv.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 96,
"end_line": 398,
"start_col": 0,
"start_line": 184
} | module Hacl.Spec.P256.Qinv
open FStar.Mul
module SE = Spec.Exponentiation
module LE = Lib.Exponentiation
module M = Lib.NatMod
module S = Spec.P256
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
let nat_mod_comm_monoid = M.mk_nat_mod_comm_monoid S.order
let mk_to_nat_mod_comm_monoid : SE.to_comm_monoid S.qelem = {
SE.a_spec = S.qelem;
SE.comm_monoid = nat_mod_comm_monoid;
SE.refl = (fun (x:S.qelem) -> x);
}
val one_mod : SE.one_st S.qelem mk_to_nat_mod_comm_monoid
let one_mod _ = 1
val mul_mod : SE.mul_st S.qelem mk_to_nat_mod_comm_monoid
let mul_mod x y = S.qmul x y
val sqr_mod : SE.sqr_st S.qelem mk_to_nat_mod_comm_monoid
let sqr_mod x = S.qmul x x
let mk_nat_mod_concrete_ops : SE.concrete_ops S.qelem = {
SE.to = mk_to_nat_mod_comm_monoid;
SE.one = one_mod;
SE.mul = mul_mod;
SE.sqr = sqr_mod;
}
let qsquare_times (a:S.qelem) (b:nat) : S.qelem =
SE.exp_pow2 mk_nat_mod_concrete_ops a b
val qsquare_times_lemma: a:S.qelem -> b:nat ->
Lemma (qsquare_times a b == M.pow a (pow2 b) % S.order)
let qsquare_times_lemma a b =
SE.exp_pow2_lemma mk_nat_mod_concrete_ops a b;
LE.exp_pow2_lemma nat_mod_comm_monoid a b;
assert (qsquare_times a b == LE.pow nat_mod_comm_monoid a (pow2 b));
M.lemma_pow_nat_mod_is_pow #S.order a (pow2 b)
let qinv_x8_x128 (x6 x_11: S.qelem) : S.qelem =
let x8 = S.qmul (qsquare_times x6 2) x_11 in
let x16 = S.qmul (qsquare_times x8 8) x8 in
let x32 = S.qmul (qsquare_times x16 16) x16 in
let x96 = S.qmul (qsquare_times x32 64) x32 in
let x128 = S.qmul (qsquare_times x96 32) x32 in
x128
let qinv_x134_x153 (x128 x_11 x_111 x_1111 x_10101 x_101111: S.qelem) : S.qelem =
let x134 = S.qmul (qsquare_times x128 6) x_101111 in
let x139 = S.qmul (qsquare_times x134 5) x_111 in
let x143 = S.qmul (qsquare_times x139 4) x_11 in
let x148 = S.qmul (qsquare_times x143 5) x_1111 in
let x153 = S.qmul (qsquare_times x148 5) x_10101 in
x153
let qinv_x153_x177 (x153 x_101 x_111 x_101111: S.qelem) : S.qelem =
let x157 = S.qmul (qsquare_times x153 4) x_101 in
let x160 = S.qmul (qsquare_times x157 3) x_101 in
let x163 = S.qmul (qsquare_times x160 3) x_101 in
let x168 = S.qmul (qsquare_times x163 5) x_111 in
let x177 = S.qmul (qsquare_times x168 9) x_101111 in
x177
let qinv_x177_x210 (f x177 x_111 x_1111: S.qelem) : S.qelem =
let x183 = S.qmul (qsquare_times x177 6) x_1111 in
let x185 = S.qmul (qsquare_times x183 2) f in
let x190 = S.qmul (qsquare_times x185 5) f in
let x196 = S.qmul (qsquare_times x190 6) x_1111 in
let x201 = S.qmul (qsquare_times x196 5) x_111 in
let x205 = S.qmul (qsquare_times x201 4) x_111 in
let x210 = S.qmul (qsquare_times x205 5) x_111 in
x210
let qinv_x210_x240 (x210 x_11 x_101 x_101111: S.qelem) : S.qelem =
let x215 = S.qmul (qsquare_times x210 5) x_101 in
let x218 = S.qmul (qsquare_times x215 3) x_11 in
let x228 = S.qmul (qsquare_times x218 10) x_101111 in
let x230 = S.qmul (qsquare_times x228 2) x_11 in
let x235 = S.qmul (qsquare_times x230 5) x_11 in
let x240 = S.qmul (qsquare_times x235 5) x_11 in
x240
let qinv_x240_x256 (f x240 x_1111 x_10101: S.qelem) : S.qelem =
let x243 = S.qmul (qsquare_times x240 3) f in
let x250 = S.qmul (qsquare_times x243 7) x_10101 in
let x256 = S.qmul (qsquare_times x250 6) x_1111 in
x256
let qinv_x8_x256 (f x6 x_11 x_101 x_111 x_1111 x_10101 x_101111 : S.qelem) : S.qelem =
let x128 = qinv_x8_x128 x6 x_11 in
let x153 = qinv_x134_x153 x128 x_11 x_111 x_1111 x_10101 x_101111 in
let x177 = qinv_x153_x177 x153 x_101 x_111 x_101111 in
let x210 = qinv_x177_x210 f x177 x_111 x_1111 in
let x240 = qinv_x210_x240 x210 x_11 x_101 x_101111 in
let x256 = qinv_x240_x256 f x240 x_1111 x_10101 in
x256
(**
The algorithm is taken from
https://briansmith.org/ecc-inversion-addition-chains-01
*)
val qinv: f:S.qelem -> S.qelem
let qinv f =
let x_10 = qsquare_times f 1 in // x_10 is used 3x
let x_11 = S.qmul x_10 f in
let x_101 = S.qmul x_10 x_11 in
let x_111 = S.qmul x_10 x_101 in
let x_1010 = qsquare_times x_101 1 in // x_1010 is used 2x
let x_1111 = S.qmul x_101 x_1010 in
let x_10101 = S.qmul (qsquare_times x_1010 1) f in
let x_101010 = qsquare_times x_10101 1 in // x_101010 is used 2x
let x_101111 = S.qmul x_101 x_101010 in
let x6 = S.qmul x_10101 x_101010 in
qinv_x8_x256 f x6 x_11 x_101 x_111 x_1111 x_10101 x_101111
// TODO: mv to lib/
val lemma_pow_mod_1: f:S.qelem -> Lemma (f == M.pow f 1 % S.order)
let lemma_pow_mod_1 f =
M.lemma_pow1 f;
Math.Lemmas.small_mod f S.order;
assert_norm (pow2 0 = 1);
assert (f == M.pow f 1 % S.order)
val lemma_pow_mod_mul: f:S.qelem -> a:nat -> b:nat ->
Lemma (S.qmul (M.pow f a % S.order) (M.pow f b % S.order) == M.pow f (a + b) % S.order)
let lemma_pow_mod_mul f a b =
calc (==) {
S.qmul (M.pow f a % S.order) (M.pow f b % S.order);
(==) {
Math.Lemmas.lemma_mod_mul_distr_l (M.pow f a) (M.pow f b % S.order) S.order;
Math.Lemmas.lemma_mod_mul_distr_r (M.pow f a) (M.pow f b) S.order }
M.pow f a * M.pow f b % S.order;
(==) { M.lemma_pow_add f a b }
M.pow f (a + b) % S.order;
}
val lemma_pow_pow_mod: f:S.qelem -> a:nat -> b:nat ->
Lemma (M.pow (M.pow f a % S.order) b % S.order == M.pow f (a * b) % S.order)
let lemma_pow_pow_mod f a b =
calc (==) {
M.pow (M.pow f a % S.order) b % S.order;
(==) { M.lemma_pow_mod_base (M.pow f a) b S.order }
M.pow (M.pow f a) b % S.order;
(==) { M.lemma_pow_mul f a b }
M.pow f (a * b) % S.order;
}
val lemma_pow_pow_mod_mul: f:S.qelem -> a:nat -> b:nat -> c:nat ->
Lemma (S.qmul (M.pow (M.pow f a % S.order) b % S.order) (M.pow f c % S.order) == M.pow f (a * b + c) % S.order)
let lemma_pow_pow_mod_mul f a b c =
calc (==) {
S.qmul (M.pow (M.pow f a % S.order) b % S.order) (M.pow f c % S.order);
(==) { lemma_pow_pow_mod f a b }
S.qmul (M.pow f (a * b) % S.order) (M.pow f c % S.order);
(==) { lemma_pow_mod_mul f (a * b) c }
M.pow f (a * b + c) % S.order;
}
//////////////////////////////
// S.order - 2 = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63254f | {
"checked_file": "/",
"dependencies": [
"Spec.P256.fst.checked",
"Spec.Exponentiation.fsti.checked",
"prims.fst.checked",
"Lib.NatMod.fsti.checked",
"Lib.Exponentiation.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.P256.Qinv.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.P256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.NatMod",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.Exponentiation",
"short_module": "LE"
},
{
"abbrev": true,
"full_module": "Spec.Exponentiation",
"short_module": "SE"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: Spec.P256.PointOps.qelem
-> FStar.Pervasives.Lemma
(ensures
Hacl.Spec.P256.Qinv.qinv f ==
Lib.NatMod.pow f (Spec.P256.PointOps.order - 2) % Spec.P256.PointOps.order) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Spec.P256.PointOps.qelem",
"FStar.Pervasives.assert_norm",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Subtraction",
"Spec.P256.PointOps.order",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Prims.op_Modulus",
"Lib.NatMod.pow",
"Hacl.Spec.P256.Qinv.lemma_pow_pow_mod_mul",
"Hacl.Spec.P256.Qinv.qsquare_times_lemma",
"Spec.P256.PointOps.qmul",
"Hacl.Spec.P256.Qinv.qsquare_times",
"Prims.pow2",
"Hacl.Spec.P256.Qinv.lemma_pow_mod_mul",
"Hacl.Spec.P256.Qinv.lemma_pow_pow_mod",
"Hacl.Spec.P256.Qinv.lemma_pow_mod_1"
] | [] | true | false | true | false | false | let qinv_lemma f =
| let x_10 = qsquare_times f 1 in
qsquare_times_lemma f 1;
assert_norm (pow2 1 = 0x2);
assert (x_10 == M.pow f 0x2 % S.order);
let x_11 = S.qmul x_10 f in
lemma_pow_mod_1 f;
lemma_pow_mod_mul f 0x2 0x1;
assert (x_11 == M.pow f 0x3 % S.order);
let x_101 = S.qmul x_10 x_11 in
lemma_pow_mod_mul f 0x2 0x3;
assert (x_101 == M.pow f 0x5 % S.order);
let x_111 = S.qmul x_10 x_101 in
lemma_pow_mod_mul f 0x2 0x5;
assert (x_111 == M.pow f 0x7 % S.order);
let x_1010 = qsquare_times x_101 1 in
qsquare_times_lemma x_101 1;
assert_norm (pow2 1 = 2);
lemma_pow_pow_mod f 0x5 0x2;
assert (x_1010 == M.pow f 0xa % S.order);
let x_1111 = S.qmul x_101 x_1010 in
lemma_pow_mod_mul f 0x5 0xa;
assert (x_1111 == M.pow f 0xf % S.order);
let x_10101 = S.qmul (qsquare_times x_1010 1) f in
qsquare_times_lemma x_1010 1;
lemma_pow_pow_mod_mul f 0xa 0x2 0x1;
assert (x_10101 == M.pow f 0x15 % S.order);
let x_101010 = qsquare_times x_10101 1 in
qsquare_times_lemma x_10101 1;
lemma_pow_pow_mod f 0x15 0x2;
assert (x_101010 == M.pow f 0x2a % S.order);
let x_101111 = S.qmul x_101 x_101010 in
lemma_pow_mod_mul f 0x5 0x2a;
assert (x_101111 == M.pow f 0x2f % S.order);
let x6 = S.qmul x_10101 x_101010 in
lemma_pow_mod_mul f 0x15 0x2a;
assert (x6 == M.pow f 0x3f % S.order);
let x8 = S.qmul (qsquare_times x6 2) x_11 in
qsquare_times_lemma x6 2;
assert_norm (pow2 2 = 0x4);
lemma_pow_pow_mod_mul f 0x3f 0x4 0x3;
assert (x8 == M.pow f 0xff % S.order);
let x16 = S.qmul (qsquare_times x8 8) x8 in
qsquare_times_lemma x8 8;
assert_norm (pow2 8 = 0x100);
lemma_pow_pow_mod_mul f 0xff 0x100 0xff;
assert (x16 == M.pow f 0xffff % S.order);
let x32 = S.qmul (qsquare_times x16 16) x16 in
qsquare_times_lemma x16 16;
assert_norm (pow2 16 = 0x10000);
lemma_pow_pow_mod_mul f 0xffff 0x10000 0xffff;
assert (x32 == M.pow f 0xffffffff % S.order);
let x96 = S.qmul (qsquare_times x32 64) x32 in
qsquare_times_lemma x32 64;
assert_norm (pow2 64 = 0x10000000000000000);
lemma_pow_pow_mod_mul f 0xffffffff 0x10000000000000000 0xffffffff;
assert (x96 == M.pow f 0xffffffff00000000ffffffff % S.order);
let x128 = S.qmul (qsquare_times x96 32) x32 in
qsquare_times_lemma x96 32;
assert_norm (pow2 32 = 0x100000000);
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffff 0x100000000 0xffffffff;
assert (x128 == M.pow f 0xffffffff00000000ffffffffffffffff % S.order);
let x134 = S.qmul (qsquare_times x128 6) x_101111 in
qsquare_times_lemma x128 6;
assert_norm (pow2 6 = 0x40);
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffff 0x40 0x2f;
assert (x134 == M.pow f 0x3fffffffc00000003fffffffffffffffef % S.order);
let x139 = S.qmul (qsquare_times x134 5) x_111 in
qsquare_times_lemma x134 5;
assert_norm (pow2 5 = 0x20);
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef 0x20 0x7;
assert (x139 == M.pow f 0x7fffffff800000007fffffffffffffffde7 % S.order);
let x143 = S.qmul (qsquare_times x139 4) x_11 in
qsquare_times_lemma x139 4;
assert_norm (pow2 4 = 0x10);
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde7 0x10 0x3;
assert (x143 == M.pow f 0x7fffffff800000007fffffffffffffffde73 % S.order);
let x148 = S.qmul (qsquare_times x143 5) x_1111 in
qsquare_times_lemma x143 5;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde73 0x20 0xf;
assert (x148 == M.pow f 0xffffffff00000000ffffffffffffffffbce6f % S.order);
let x153 = S.qmul (qsquare_times x148 5) x_10101 in
qsquare_times_lemma x148 5;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6f 0x20 0x15;
assert (x153 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf5 % S.order);
let x157 = S.qmul (qsquare_times x153 4) x_101 in
qsquare_times_lemma x153 4;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf5 0x10 0x5;
assert (x157 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55 % S.order);
let x160 = S.qmul (qsquare_times x157 3) x_101 in
qsquare_times_lemma x157 3;
assert_norm (pow2 3 = 0x8);
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55 0x8 0x5;
assert (x160 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faad % S.order);
let x163 = S.qmul (qsquare_times x160 3) x_101 in
qsquare_times_lemma x160 3;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faad 0x8 0x5;
assert (x163 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d % S.order);
let x168 = S.qmul (qsquare_times x163 5) x_111 in
qsquare_times_lemma x163 5;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d 0x20 0x7;
assert (x168 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7 % S.order);
let x177 = S.qmul (qsquare_times x168 9) x_101111 in
qsquare_times_lemma x168 9;
assert_norm (pow2 9 = 0x200);
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faada7 0x200 0x2f;
assert (x177 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f % S.order);
let x183 = S.qmul (qsquare_times x177 6) x_1111 in
qsquare_times_lemma x177 6;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f 0x40 0xf;
assert (x183 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d38bcf % S.order);
let x185 = S.qmul (qsquare_times x183 2) f in
qsquare_times_lemma x183 2;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d38bcf 0x4 0x1;
assert (x185 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d % S.order);
let x190 = S.qmul (qsquare_times x185 5) f in
qsquare_times_lemma x185 5;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d 0x20 0x1;
assert (x190 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a1 % S.order);
let x196 = S.qmul (qsquare_times x190 6) x_1111 in
qsquare_times_lemma x190 6;
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a1 0x40 0xf;
assert (x196 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f % S.order);
let x201 = S.qmul (qsquare_times x196 5) x_111 in
qsquare_times_lemma x196 5;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f 0x20 0x7;
assert (x201 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d09e7 % S.order);
let x205 = S.qmul (qsquare_times x201 4) x_111 in
qsquare_times_lemma x201 4;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d09e7 0x10 0x7;
assert (x205 == M.pow f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d09e77 % S.order);
let x210 = S.qmul (qsquare_times x205 5) x_111 in
qsquare_times_lemma x205 5;
lemma_pow_pow_mod_mul f 0x1fffffffe00000001ffffffffffffffff79cdf55b4e2f3d09e77 0x20 0x7;
assert (x210 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee7 % S.order);
let x215 = S.qmul (qsquare_times x210 5) x_101 in
qsquare_times_lemma x210 5;
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee7 0x20 0x5;
assert (x215 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5 % S.order);
let x218 = S.qmul (qsquare_times x215 3) x_11 in
qsquare_times_lemma x215 3;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5 0x8 0x3;
assert (x218 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b % S.order);
let x228 = S.qmul (qsquare_times x218 10) x_101111 in
qsquare_times_lemma x218 10;
assert_norm (pow2 10 = 0x400);
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b 0x400 0x2f;
assert (x228 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2f % S.order);
let x230 = S.qmul (qsquare_times x228 2) x_11 in
qsquare_times_lemma x228 2;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2f 0x4 0x3;
assert (x230 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b0bf % S.order);
let x235 = S.qmul (qsquare_times x230 5) x_11 in
qsquare_times_lemma x230 5;
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b0bf 0x20 0x3;
assert (x235 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5617e3 % S.order);
let x240 = S.qmul (qsquare_times x235 5) x_11 in
qsquare_times_lemma x235 5;
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5617e3 0x20 0x3;
assert (x240 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63 % S.order);
let x243 = S.qmul (qsquare_times x240 3) f in
qsquare_times_lemma x240 3;
lemma_pow_pow_mod_mul f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63 0x8 0x1;
assert (x243 == M.pow f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5617e319 % S.order);
let x250 = S.qmul (qsquare_times x243 7) x_10101 in
qsquare_times_lemma x243 7;
assert_norm (pow2 7 = 0x80);
lemma_pow_pow_mod_mul f 0x7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5617e319 0x80 0x15;
assert (x250 == M.pow f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b0bf18c95 % S.order);
let x256 = S.qmul (qsquare_times x250 6) x_1111 in
qsquare_times_lemma x250 6;
lemma_pow_pow_mod_mul f 0x3fffffffc00000003fffffffffffffffef39beab69c5e7a13cee72b0bf18c95 0x40 0xf;
assert (x256 == M.pow f 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63254f % S.order
);
assert_norm (S.order - 2 = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63254f) | false |
Hacl.Impl.Frodo.KEM.Decaps.fst | Hacl.Impl.Frodo.KEM.Decaps.crypto_kem_dec_seed_se_k | val crypto_kem_dec_seed_se_k:
a:FP.frodo_alg
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> seed_se_k:lbytes (2ul *! crypto_bytes a)
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\ live h seed_se_k /\
disjoint mu_decode seed_se_k /\ disjoint sk seed_se_k)
(ensures fun h0 _ h1 -> modifies (loc seed_se_k) h0 h1 /\
as_seq h1 seed_se_k == S.crypto_kem_dec_seed_se_k a (as_seq h0 mu_decode) (as_seq h0 sk)) | val crypto_kem_dec_seed_se_k:
a:FP.frodo_alg
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> seed_se_k:lbytes (2ul *! crypto_bytes a)
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\ live h seed_se_k /\
disjoint mu_decode seed_se_k /\ disjoint sk seed_se_k)
(ensures fun h0 _ h1 -> modifies (loc seed_se_k) h0 h1 /\
as_seq h1 seed_se_k == S.crypto_kem_dec_seed_se_k a (as_seq h0 mu_decode) (as_seq h0 sk)) | let crypto_kem_dec_seed_se_k a mu_decode sk seed_se_k =
push_frame ();
let pkh_mu_decode_len = bytes_pkhash a +! bytes_mu a in
let pkh_mu_decode = create pkh_mu_decode_len (u8 0) in
let pkh = sub sk (crypto_secretkeybytes a -! bytes_pkhash a) (bytes_pkhash a) in
concat2 (bytes_pkhash a) pkh (bytes_mu a) mu_decode pkh_mu_decode;
frodo_shake a pkh_mu_decode_len pkh_mu_decode (2ul *! crypto_bytes a) seed_se_k;
pop_frame () | {
"file_name": "code/frodo/Hacl.Impl.Frodo.KEM.Decaps.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 248,
"start_col": 0,
"start_line": 240
} | module Hacl.Impl.Frodo.KEM.Decaps
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Hacl.Impl.Matrix
open Hacl.Impl.Frodo.Params
open Hacl.Impl.Frodo.KEM
open Hacl.Impl.Frodo.KEM.Encaps
open Hacl.Impl.Frodo.Encode
open Hacl.Impl.Frodo.Pack
open Hacl.Impl.Frodo.Sample
open Hacl.Frodo.Random
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module FP = Spec.Frodo.Params
module KG = Hacl.Impl.Frodo.KEM.KeyGen
module S = Spec.Frodo.KEM.Decaps
module M = Spec.Matrix
#set-options "--z3rlimit 100 --fuel 0 --ifuel 0"
inline_for_extraction noextract
val get_bp_c_matrices:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h ct /\ live h bp_matrix /\ live h c_matrix /\
disjoint bp_matrix ct /\ disjoint c_matrix ct /\ disjoint bp_matrix c_matrix)
(ensures fun h0 _ h1 -> modifies (loc bp_matrix |+| loc c_matrix) h0 h1 /\
(as_matrix h1 bp_matrix, as_matrix h1 c_matrix) == S.get_bp_c_matrices a (as_seq h0 ct))
let get_bp_c_matrices a ct bp_matrix c_matrix =
let c1 = sub ct 0ul (ct1bytes_len a) in
let c2 = sub ct (ct1bytes_len a) (ct2bytes_len a) in
frodo_unpack params_nbar (params_n a) (params_logq a) c1 bp_matrix;
frodo_unpack params_nbar params_nbar (params_logq a) c2 c_matrix
inline_for_extraction noextract
val frodo_mu_decode:
a:FP.frodo_alg
-> s_bytes:lbytes (secretmatrixbytes_len a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h s_bytes /\ live h bp_matrix /\
live h c_matrix /\ live h mu_decode /\
disjoint mu_decode s_bytes /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0))
(ensures fun h0 _ h1 -> modifies (loc mu_decode) h0 h1 /\
as_seq h1 mu_decode ==
S.frodo_mu_decode a (as_seq h0 s_bytes) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let frodo_mu_decode a s_bytes bp_matrix c_matrix mu_decode =
push_frame();
let s_matrix = matrix_create (params_n a) params_nbar in
let m_matrix = matrix_create params_nbar params_nbar in
matrix_from_lbytes s_bytes s_matrix;
matrix_mul_s bp_matrix s_matrix m_matrix;
matrix_sub c_matrix m_matrix;
frodo_key_decode (params_logq a) (params_extracted_bits a) params_nbar m_matrix mu_decode;
clear_matrix s_matrix;
clear_matrix m_matrix;
pop_frame()
inline_for_extraction noextract
val get_bpp_cp_matrices_:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> sp_matrix:matrix_t params_nbar (params_n a)
-> ep_matrix:matrix_t params_nbar (params_n a)
-> epp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
live h sp_matrix /\ live h ep_matrix /\ live h epp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc sk; loc bpp_matrix;
loc cp_matrix; loc sp_matrix; loc ep_matrix; loc epp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices_ a gen_a (as_seq h0 mu_decode) (as_seq h0 sk)
(as_matrix h0 sp_matrix) (as_matrix h0 ep_matrix) (as_matrix h0 epp_matrix))
let get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix =
FP.expand_crypto_secretkeybytes a;
FP.expand_crypto_publickeybytes a;
let pk = sub sk (crypto_bytes a) (crypto_publickeybytes a) in
let seed_a = sub pk 0ul bytes_seed_a in
let b = sub pk bytes_seed_a (crypto_publickeybytes a -! bytes_seed_a) in
frodo_mul_add_sa_plus_e a gen_a seed_a sp_matrix ep_matrix bpp_matrix;
frodo_mul_add_sb_plus_e_plus_mu a mu_decode b sp_matrix epp_matrix cp_matrix;
mod_pow2 (params_logq a) bpp_matrix;
mod_pow2 (params_logq a) cp_matrix
#push-options "--z3rlimit 150"
inline_for_extraction noextract
val get_bpp_cp_matrices:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> seed_se:lbytes (crypto_bytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h seed_se /\ live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc seed_se; loc sk; loc bpp_matrix; loc cp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices a gen_a (as_seq h0 mu_decode) (as_seq h0 seed_se) (as_seq h0 sk))
let get_bpp_cp_matrices a gen_a mu_decode seed_se sk bpp_matrix cp_matrix =
push_frame ();
let sp_matrix = matrix_create params_nbar (params_n a) in
let ep_matrix = matrix_create params_nbar (params_n a) in
let epp_matrix = matrix_create params_nbar params_nbar in
get_sp_ep_epp_matrices a seed_se sp_matrix ep_matrix epp_matrix;
get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix;
clear_matrix3 a sp_matrix ep_matrix epp_matrix;
pop_frame ()
#pop-options
inline_for_extraction noextract
val crypto_kem_dec_kp_s_cond:
a:FP.frodo_alg
-> bp_matrix:matrix_t params_nbar (params_n a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h bp_matrix /\ live h bpp_matrix /\
live h c_matrix /\ live h cp_matrix)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s_cond a (as_matrix h0 bp_matrix) (as_matrix h0 bpp_matrix)
(as_matrix h0 c_matrix) (as_matrix h0 cp_matrix))
let crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix =
let b1 = matrix_eq bp_matrix bpp_matrix in
let b2 = matrix_eq c_matrix cp_matrix in
b1 &. b2
inline_for_extraction noextract
val crypto_kem_dec_kp_s:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> seed_se:lbytes (crypto_bytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h mu_decode /\ live h bp_matrix /\ live h seed_se /\
live h c_matrix /\ live h sk /\
loc_pairwise_disjoint [loc mu_decode; loc seed_se; loc sk])
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s a gen_a (as_seq h0 mu_decode) (as_seq h0 seed_se)
(as_seq h0 sk) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let crypto_kem_dec_kp_s a gen_a mu_decode seed_se sk bp_matrix c_matrix =
push_frame ();
let bpp_matrix = matrix_create params_nbar (params_n a) in
let cp_matrix = matrix_create params_nbar params_nbar in
get_bpp_cp_matrices a gen_a mu_decode seed_se sk bpp_matrix cp_matrix;
let mask = crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix in
pop_frame ();
mask
inline_for_extraction noextract
val crypto_kem_dec_ss0:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> mask:uint16{v mask == 0 \/ v mask == v (ones U16 SEC)}
-> kp:lbytes (crypto_bytes a)
-> s:lbytes (crypto_bytes a)
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h ct /\ live h kp /\ live h s /\ live h ss /\
disjoint ss ct /\ disjoint ss kp /\ disjoint ss s /\
disjoint kp s)
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss0 a (as_seq h0 ct) mask (as_seq h0 kp) (as_seq h0 s))
let crypto_kem_dec_ss0 a ct mask kp s ss =
push_frame ();
let kp_s = create (crypto_bytes a) (u8 0) in
Lib.ByteBuffer.buf_mask_select kp s (to_u8 mask) kp_s;
let ss_init_len = crypto_ciphertextbytes a +! crypto_bytes a in
let ss_init = create ss_init_len (u8 0) in
concat2 (crypto_ciphertextbytes a) ct (crypto_bytes a) kp_s ss_init;
frodo_shake a ss_init_len ss_init (crypto_bytes a) ss;
clear_words_u8 ss_init;
clear_words_u8 kp_s;
pop_frame ()
inline_for_extraction noextract
val crypto_kem_dec_seed_se_k:
a:FP.frodo_alg
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> seed_se_k:lbytes (2ul *! crypto_bytes a)
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\ live h seed_se_k /\
disjoint mu_decode seed_se_k /\ disjoint sk seed_se_k)
(ensures fun h0 _ h1 -> modifies (loc seed_se_k) h0 h1 /\
as_seq h1 seed_se_k == S.crypto_kem_dec_seed_se_k a (as_seq h0 mu_decode) (as_seq h0 sk)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Params.fst.checked",
"Spec.Frodo.KEM.Decaps.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Matrix.fst.checked",
"Hacl.Impl.Frodo.Sample.fst.checked",
"Hacl.Impl.Frodo.Params.fst.checked",
"Hacl.Impl.Frodo.Pack.fst.checked",
"Hacl.Impl.Frodo.KEM.KeyGen.fst.checked",
"Hacl.Impl.Frodo.KEM.Encaps.fst.checked",
"Hacl.Impl.Frodo.KEM.fst.checked",
"Hacl.Impl.Frodo.Encode.fst.checked",
"Hacl.Frodo.Random.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Frodo.KEM.Decaps.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.KEM.Decaps",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Frodo.KEM.KeyGen",
"short_module": "KG"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.Params",
"short_module": "FP"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Frodo.Random",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Sample",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Pack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Encode",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM.Encaps",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Params",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Matrix",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Spec.Frodo.Params.frodo_alg ->
mu_decode: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.bytes_mu a) ->
sk: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_secretkeybytes a) ->
seed_se_k: Hacl.Impl.Matrix.lbytes (2ul *! Hacl.Impl.Frodo.Params.crypto_bytes a)
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Frodo.Params.frodo_alg",
"Hacl.Impl.Matrix.lbytes",
"Hacl.Impl.Frodo.Params.bytes_mu",
"Hacl.Impl.Frodo.Params.crypto_secretkeybytes",
"Lib.IntTypes.op_Star_Bang",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"FStar.UInt32.__uint_to_t",
"Hacl.Impl.Frodo.Params.crypto_bytes",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Hacl.Impl.Frodo.Params.frodo_shake",
"Lib.Buffer.concat2",
"Lib.Buffer.MUT",
"Lib.IntTypes.uint8",
"Hacl.Impl.Frodo.Params.bytes_pkhash",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"Lib.Buffer.sub",
"Lib.IntTypes.op_Subtraction_Bang",
"Lib.Buffer.create",
"Lib.IntTypes.u8",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.op_Plus_Bang",
"FStar.HyperStack.ST.push_frame"
] | [] | false | true | false | false | false | let crypto_kem_dec_seed_se_k a mu_decode sk seed_se_k =
| push_frame ();
let pkh_mu_decode_len = bytes_pkhash a +! bytes_mu a in
let pkh_mu_decode = create pkh_mu_decode_len (u8 0) in
let pkh = sub sk (crypto_secretkeybytes a -! bytes_pkhash a) (bytes_pkhash a) in
concat2 (bytes_pkhash a) pkh (bytes_mu a) mu_decode pkh_mu_decode;
frodo_shake a pkh_mu_decode_len pkh_mu_decode (2ul *! crypto_bytes a) seed_se_k;
pop_frame () | false |
Hacl.EC.K256.fst | Hacl.EC.K256.mk_felem_zero | val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero) | val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero) | let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 46,
"start_col": 0,
"start_line": 44
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: Hacl.K256.Field.felem -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.K256.Field.felem",
"Hacl.K256.Field.set_zero",
"Prims.unit",
"FStar.Math.Lemmas.small_mod",
"Spec.K256.PointOps.zero",
"Spec.K256.PointOps.prime"
] | [] | false | true | false | false | false | let mk_felem_zero f =
| Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f | false |
EtM.Ideal.fsti | EtM.Ideal.ind_cpa_rest_adv | val ind_cpa_rest_adv : b: Prims.bool{EtM.Ideal.ind_cpa ==> b} | let ind_cpa_rest_adv = uf_cma | {
"file_name": "examples/crypto/EtM.Ideal.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 23,
"start_col": 0,
"start_line": 23
} | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module EtM.Ideal
val ind_cpa : bool
val uf_cma : b:bool{ ind_cpa ==> b } | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "EtM.Ideal.fsti"
} | [
{
"abbrev": false,
"full_module": "EtM",
"short_module": null
},
{
"abbrev": false,
"full_module": "EtM",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: Prims.bool{EtM.Ideal.ind_cpa ==> b} | Prims.Tot | [
"total"
] | [] | [
"EtM.Ideal.uf_cma"
] | [] | false | false | false | false | false | let ind_cpa_rest_adv =
| uf_cma | false |
|
Hacl.EC.K256.fst | Hacl.EC.K256.mk_felem_one | val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one) | val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one) | let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 13,
"end_line": 59,
"start_col": 0,
"start_line": 57
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: Hacl.K256.Field.felem -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.K256.Field.felem",
"Hacl.K256.Field.set_one",
"Prims.unit",
"FStar.Math.Lemmas.small_mod",
"Spec.K256.PointOps.one",
"Spec.K256.PointOps.prime"
] | [] | false | true | false | false | false | let mk_felem_one f =
| Math.Lemmas.small_mod S.one S.prime;
F.set_one f | false |
Hacl.EC.K256.fst | Hacl.EC.K256.felem_add | val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b)) | val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b)) | let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 27,
"end_line": 85,
"start_col": 0,
"start_line": 78
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.K256.Field.felem -> b: Hacl.K256.Field.felem -> out: Hacl.K256.Field.felem
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.K256.Field.felem",
"Hacl.K256.Field.fnormalize_weak",
"Prims.unit",
"Hacl.Spec.K256.Field52.Lemmas.normalize_weak5_lemma",
"FStar.Pervasives.Native.Mktuple5",
"Prims.nat",
"Hacl.K256.Field.as_felem5",
"Prims._assert",
"Hacl.Spec.K256.Field52.Definitions.felem_fits5",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.K256.Field.fadd",
"Hacl.Spec.K256.Field52.Lemmas.fadd5_lemma"
] | [] | false | true | false | false | false | let felem_add a b out =
| let h0 = ST.get () in
BL.fadd5_lemma (1, 1, 1, 1, 2) (1, 1, 1, 1, 2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2, 2, 2, 2, 4));
BL.normalize_weak5_lemma (2, 2, 2, 2, 4) (F.as_felem5 h1 out);
F.fnormalize_weak out out | false |
Hacl.Impl.Frodo.KEM.Decaps.fst | Hacl.Impl.Frodo.KEM.Decaps.crypto_kem_dec0 | val crypto_kem_dec0:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> ss:lbytes (crypto_bytes a)
-> ct:lbytes (crypto_ciphertextbytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h ss /\ live h ct /\ live h sk /\ live h mu_decode /\
live h bp_matrix /\ live h c_matrix /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0) /\
loc_pairwise_disjoint [loc ss; loc ct; loc sk;
loc mu_decode; loc bp_matrix; loc c_matrix])
(ensures fun h0 _ h1 -> modifies (loc ss |+| loc mu_decode) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ a gen_a (as_seq h0 ct)
(as_seq h0 sk) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix)) | val crypto_kem_dec0:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> ss:lbytes (crypto_bytes a)
-> ct:lbytes (crypto_ciphertextbytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h ss /\ live h ct /\ live h sk /\ live h mu_decode /\
live h bp_matrix /\ live h c_matrix /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0) /\
loc_pairwise_disjoint [loc ss; loc ct; loc sk;
loc mu_decode; loc bp_matrix; loc c_matrix])
(ensures fun h0 _ h1 -> modifies (loc ss |+| loc mu_decode) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ a gen_a (as_seq h0 ct)
(as_seq h0 sk) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix)) | let crypto_kem_dec0 a gen_a ss ct sk bp_matrix c_matrix mu_decode =
crypto_kem_dec_mu a sk bp_matrix c_matrix mu_decode;
crypto_kem_dec_ss2 a gen_a ct sk mu_decode bp_matrix c_matrix ss | {
"file_name": "code/frodo/Hacl.Impl.Frodo.KEM.Decaps.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 66,
"end_line": 356,
"start_col": 0,
"start_line": 354
} | module Hacl.Impl.Frodo.KEM.Decaps
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Hacl.Impl.Matrix
open Hacl.Impl.Frodo.Params
open Hacl.Impl.Frodo.KEM
open Hacl.Impl.Frodo.KEM.Encaps
open Hacl.Impl.Frodo.Encode
open Hacl.Impl.Frodo.Pack
open Hacl.Impl.Frodo.Sample
open Hacl.Frodo.Random
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module FP = Spec.Frodo.Params
module KG = Hacl.Impl.Frodo.KEM.KeyGen
module S = Spec.Frodo.KEM.Decaps
module M = Spec.Matrix
#set-options "--z3rlimit 100 --fuel 0 --ifuel 0"
inline_for_extraction noextract
val get_bp_c_matrices:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h ct /\ live h bp_matrix /\ live h c_matrix /\
disjoint bp_matrix ct /\ disjoint c_matrix ct /\ disjoint bp_matrix c_matrix)
(ensures fun h0 _ h1 -> modifies (loc bp_matrix |+| loc c_matrix) h0 h1 /\
(as_matrix h1 bp_matrix, as_matrix h1 c_matrix) == S.get_bp_c_matrices a (as_seq h0 ct))
let get_bp_c_matrices a ct bp_matrix c_matrix =
let c1 = sub ct 0ul (ct1bytes_len a) in
let c2 = sub ct (ct1bytes_len a) (ct2bytes_len a) in
frodo_unpack params_nbar (params_n a) (params_logq a) c1 bp_matrix;
frodo_unpack params_nbar params_nbar (params_logq a) c2 c_matrix
inline_for_extraction noextract
val frodo_mu_decode:
a:FP.frodo_alg
-> s_bytes:lbytes (secretmatrixbytes_len a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h s_bytes /\ live h bp_matrix /\
live h c_matrix /\ live h mu_decode /\
disjoint mu_decode s_bytes /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0))
(ensures fun h0 _ h1 -> modifies (loc mu_decode) h0 h1 /\
as_seq h1 mu_decode ==
S.frodo_mu_decode a (as_seq h0 s_bytes) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let frodo_mu_decode a s_bytes bp_matrix c_matrix mu_decode =
push_frame();
let s_matrix = matrix_create (params_n a) params_nbar in
let m_matrix = matrix_create params_nbar params_nbar in
matrix_from_lbytes s_bytes s_matrix;
matrix_mul_s bp_matrix s_matrix m_matrix;
matrix_sub c_matrix m_matrix;
frodo_key_decode (params_logq a) (params_extracted_bits a) params_nbar m_matrix mu_decode;
clear_matrix s_matrix;
clear_matrix m_matrix;
pop_frame()
inline_for_extraction noextract
val get_bpp_cp_matrices_:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> sp_matrix:matrix_t params_nbar (params_n a)
-> ep_matrix:matrix_t params_nbar (params_n a)
-> epp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
live h sp_matrix /\ live h ep_matrix /\ live h epp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc sk; loc bpp_matrix;
loc cp_matrix; loc sp_matrix; loc ep_matrix; loc epp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices_ a gen_a (as_seq h0 mu_decode) (as_seq h0 sk)
(as_matrix h0 sp_matrix) (as_matrix h0 ep_matrix) (as_matrix h0 epp_matrix))
let get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix =
FP.expand_crypto_secretkeybytes a;
FP.expand_crypto_publickeybytes a;
let pk = sub sk (crypto_bytes a) (crypto_publickeybytes a) in
let seed_a = sub pk 0ul bytes_seed_a in
let b = sub pk bytes_seed_a (crypto_publickeybytes a -! bytes_seed_a) in
frodo_mul_add_sa_plus_e a gen_a seed_a sp_matrix ep_matrix bpp_matrix;
frodo_mul_add_sb_plus_e_plus_mu a mu_decode b sp_matrix epp_matrix cp_matrix;
mod_pow2 (params_logq a) bpp_matrix;
mod_pow2 (params_logq a) cp_matrix
#push-options "--z3rlimit 150"
inline_for_extraction noextract
val get_bpp_cp_matrices:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> seed_se:lbytes (crypto_bytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack unit
(requires fun h ->
live h seed_se /\ live h mu_decode /\ live h sk /\
live h bpp_matrix /\ live h cp_matrix /\
loc_pairwise_disjoint [loc mu_decode; loc seed_se; loc sk; loc bpp_matrix; loc cp_matrix])
(ensures fun h0 _ h1 -> modifies (loc bpp_matrix |+| loc cp_matrix) h0 h1 /\
(as_matrix h1 bpp_matrix, as_matrix h1 cp_matrix) ==
S.get_bpp_cp_matrices a gen_a (as_seq h0 mu_decode) (as_seq h0 seed_se) (as_seq h0 sk))
let get_bpp_cp_matrices a gen_a mu_decode seed_se sk bpp_matrix cp_matrix =
push_frame ();
let sp_matrix = matrix_create params_nbar (params_n a) in
let ep_matrix = matrix_create params_nbar (params_n a) in
let epp_matrix = matrix_create params_nbar params_nbar in
get_sp_ep_epp_matrices a seed_se sp_matrix ep_matrix epp_matrix;
get_bpp_cp_matrices_ a gen_a mu_decode sk bpp_matrix cp_matrix sp_matrix ep_matrix epp_matrix;
clear_matrix3 a sp_matrix ep_matrix epp_matrix;
pop_frame ()
#pop-options
inline_for_extraction noextract
val crypto_kem_dec_kp_s_cond:
a:FP.frodo_alg
-> bp_matrix:matrix_t params_nbar (params_n a)
-> bpp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> cp_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h bp_matrix /\ live h bpp_matrix /\
live h c_matrix /\ live h cp_matrix)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s_cond a (as_matrix h0 bp_matrix) (as_matrix h0 bpp_matrix)
(as_matrix h0 c_matrix) (as_matrix h0 cp_matrix))
let crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix =
let b1 = matrix_eq bp_matrix bpp_matrix in
let b2 = matrix_eq c_matrix cp_matrix in
b1 &. b2
inline_for_extraction noextract
val crypto_kem_dec_kp_s:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> mu_decode:lbytes (bytes_mu a)
-> seed_se:lbytes (crypto_bytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> Stack uint16
(requires fun h ->
live h mu_decode /\ live h bp_matrix /\ live h seed_se /\
live h c_matrix /\ live h sk /\
loc_pairwise_disjoint [loc mu_decode; loc seed_se; loc sk])
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == S.crypto_kem_dec_kp_s a gen_a (as_seq h0 mu_decode) (as_seq h0 seed_se)
(as_seq h0 sk) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let crypto_kem_dec_kp_s a gen_a mu_decode seed_se sk bp_matrix c_matrix =
push_frame ();
let bpp_matrix = matrix_create params_nbar (params_n a) in
let cp_matrix = matrix_create params_nbar params_nbar in
get_bpp_cp_matrices a gen_a mu_decode seed_se sk bpp_matrix cp_matrix;
let mask = crypto_kem_dec_kp_s_cond a bp_matrix bpp_matrix c_matrix cp_matrix in
pop_frame ();
mask
inline_for_extraction noextract
val crypto_kem_dec_ss0:
a:FP.frodo_alg
-> ct:lbytes (crypto_ciphertextbytes a)
-> mask:uint16{v mask == 0 \/ v mask == v (ones U16 SEC)}
-> kp:lbytes (crypto_bytes a)
-> s:lbytes (crypto_bytes a)
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h ct /\ live h kp /\ live h s /\ live h ss /\
disjoint ss ct /\ disjoint ss kp /\ disjoint ss s /\
disjoint kp s)
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss0 a (as_seq h0 ct) mask (as_seq h0 kp) (as_seq h0 s))
let crypto_kem_dec_ss0 a ct mask kp s ss =
push_frame ();
let kp_s = create (crypto_bytes a) (u8 0) in
Lib.ByteBuffer.buf_mask_select kp s (to_u8 mask) kp_s;
let ss_init_len = crypto_ciphertextbytes a +! crypto_bytes a in
let ss_init = create ss_init_len (u8 0) in
concat2 (crypto_ciphertextbytes a) ct (crypto_bytes a) kp_s ss_init;
frodo_shake a ss_init_len ss_init (crypto_bytes a) ss;
clear_words_u8 ss_init;
clear_words_u8 kp_s;
pop_frame ()
inline_for_extraction noextract
val crypto_kem_dec_seed_se_k:
a:FP.frodo_alg
-> mu_decode:lbytes (bytes_mu a)
-> sk:lbytes (crypto_secretkeybytes a)
-> seed_se_k:lbytes (2ul *! crypto_bytes a)
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h sk /\ live h seed_se_k /\
disjoint mu_decode seed_se_k /\ disjoint sk seed_se_k)
(ensures fun h0 _ h1 -> modifies (loc seed_se_k) h0 h1 /\
as_seq h1 seed_se_k == S.crypto_kem_dec_seed_se_k a (as_seq h0 mu_decode) (as_seq h0 sk))
let crypto_kem_dec_seed_se_k a mu_decode sk seed_se_k =
push_frame ();
let pkh_mu_decode_len = bytes_pkhash a +! bytes_mu a in
let pkh_mu_decode = create pkh_mu_decode_len (u8 0) in
let pkh = sub sk (crypto_secretkeybytes a -! bytes_pkhash a) (bytes_pkhash a) in
concat2 (bytes_pkhash a) pkh (bytes_mu a) mu_decode pkh_mu_decode;
frodo_shake a pkh_mu_decode_len pkh_mu_decode (2ul *! crypto_bytes a) seed_se_k;
pop_frame ()
inline_for_extraction noextract
val crypto_kem_dec_ss1:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> ct:lbytes (crypto_ciphertextbytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> mu_decode:lbytes (bytes_mu a)
-> seed_se_k:lbytes (2ul *! crypto_bytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h bp_matrix /\ live h seed_se_k /\
live h c_matrix /\ live h sk /\ live h ct /\ live h ss /\
loc_pairwise_disjoint [loc ct; loc sk; loc seed_se_k;
loc mu_decode; loc bp_matrix; loc c_matrix; loc ss])
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss1 a gen_a (as_seq h0 ct) (as_seq h0 sk)
(as_seq h0 mu_decode) (as_seq h0 seed_se_k) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let crypto_kem_dec_ss1 a gen_a ct sk mu_decode seed_se_k bp_matrix c_matrix ss =
let seed_se = sub seed_se_k 0ul (crypto_bytes a) in
let kp = sub seed_se_k (crypto_bytes a) (crypto_bytes a) in
let s = sub sk 0ul (crypto_bytes a) in
let mask = crypto_kem_dec_kp_s a gen_a mu_decode seed_se sk bp_matrix c_matrix in
crypto_kem_dec_ss0 a ct mask kp s ss
inline_for_extraction noextract
val crypto_kem_dec_ss2:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> ct:lbytes (crypto_ciphertextbytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> mu_decode:lbytes (bytes_mu a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> ss:lbytes (crypto_bytes a)
-> Stack unit
(requires fun h ->
live h mu_decode /\ live h bp_matrix /\
live h c_matrix /\ live h sk /\ live h ct /\ live h ss /\
loc_pairwise_disjoint [loc ct; loc sk;
loc mu_decode; loc bp_matrix; loc c_matrix; loc ss])
(ensures fun h0 _ h1 -> modifies (loc ss) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ss2 a gen_a (as_seq h0 ct) (as_seq h0 sk)
(as_seq h0 mu_decode) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let crypto_kem_dec_ss2 a gen_a ct sk mu_decode bp_matrix c_matrix ss =
push_frame ();
let seed_se_k = create (2ul *! crypto_bytes a) (u8 0) in
crypto_kem_dec_seed_se_k a mu_decode sk seed_se_k;
crypto_kem_dec_ss1 a gen_a ct sk mu_decode seed_se_k bp_matrix c_matrix ss;
clear_words_u8 seed_se_k;
pop_frame ()
inline_for_extraction noextract
val crypto_kem_dec_mu:
a:FP.frodo_alg
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h sk /\ live h mu_decode /\
live h bp_matrix /\ live h c_matrix /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0) /\
loc_pairwise_disjoint [loc sk; loc mu_decode; loc bp_matrix; loc c_matrix])
(ensures fun h0 _ h1 -> modifies (loc mu_decode) h0 h1 /\
as_seq h1 mu_decode == S.crypto_kem_dec_mu a (as_seq h0 sk)
(as_matrix h0 bp_matrix) (as_matrix h0 c_matrix))
let crypto_kem_dec_mu a sk bp_matrix c_matrix mu_decode =
FP.expand_crypto_secretkeybytes a;
let s_bytes = sub sk (crypto_bytes a +! crypto_publickeybytes a) (secretmatrixbytes_len a) in
frodo_mu_decode a s_bytes bp_matrix c_matrix mu_decode
inline_for_extraction noextract
val crypto_kem_dec0:
a:FP.frodo_alg
-> gen_a:FP.frodo_gen_a{is_supported gen_a}
-> ss:lbytes (crypto_bytes a)
-> ct:lbytes (crypto_ciphertextbytes a)
-> sk:lbytes (crypto_secretkeybytes a)
-> bp_matrix:matrix_t params_nbar (params_n a)
-> c_matrix:matrix_t params_nbar params_nbar
-> mu_decode:lbytes (bytes_mu a)
-> Stack unit
(requires fun h ->
live h ss /\ live h ct /\ live h sk /\ live h mu_decode /\
live h bp_matrix /\ live h c_matrix /\
as_seq h (mu_decode) == Seq.create (v (bytes_mu a)) (u8 0) /\
loc_pairwise_disjoint [loc ss; loc ct; loc sk;
loc mu_decode; loc bp_matrix; loc c_matrix])
(ensures fun h0 _ h1 -> modifies (loc ss |+| loc mu_decode) h0 h1 /\
as_seq h1 ss == S.crypto_kem_dec_ a gen_a (as_seq h0 ct)
(as_seq h0 sk) (as_matrix h0 bp_matrix) (as_matrix h0 c_matrix)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Params.fst.checked",
"Spec.Frodo.KEM.Decaps.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Matrix.fst.checked",
"Hacl.Impl.Frodo.Sample.fst.checked",
"Hacl.Impl.Frodo.Params.fst.checked",
"Hacl.Impl.Frodo.Pack.fst.checked",
"Hacl.Impl.Frodo.KEM.KeyGen.fst.checked",
"Hacl.Impl.Frodo.KEM.Encaps.fst.checked",
"Hacl.Impl.Frodo.KEM.fst.checked",
"Hacl.Impl.Frodo.Encode.fst.checked",
"Hacl.Frodo.Random.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Frodo.KEM.Decaps.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.KEM.Decaps",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Frodo.KEM.KeyGen",
"short_module": "KG"
},
{
"abbrev": true,
"full_module": "Spec.Frodo.Params",
"short_module": "FP"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Frodo.Random",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Sample",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Pack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Encode",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM.Encaps",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.Params",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Matrix",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Frodo.KEM",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Spec.Frodo.Params.frodo_alg ->
gen_a: Spec.Frodo.Params.frodo_gen_a{Hacl.Impl.Frodo.Params.is_supported gen_a} ->
ss: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_bytes a) ->
ct: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_ciphertextbytes a) ->
sk: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.crypto_secretkeybytes a) ->
bp_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
(Hacl.Impl.Frodo.Params.params_n a) ->
c_matrix:
Hacl.Impl.Matrix.matrix_t Hacl.Impl.Frodo.Params.params_nbar
Hacl.Impl.Frodo.Params.params_nbar ->
mu_decode: Hacl.Impl.Matrix.lbytes (Hacl.Impl.Frodo.Params.bytes_mu a)
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Frodo.Params.frodo_alg",
"Spec.Frodo.Params.frodo_gen_a",
"Prims.b2t",
"Hacl.Impl.Frodo.Params.is_supported",
"Hacl.Impl.Matrix.lbytes",
"Hacl.Impl.Frodo.Params.crypto_bytes",
"Hacl.Impl.Frodo.Params.crypto_ciphertextbytes",
"Hacl.Impl.Frodo.Params.crypto_secretkeybytes",
"Hacl.Impl.Matrix.matrix_t",
"Hacl.Impl.Frodo.Params.params_nbar",
"Hacl.Impl.Frodo.Params.params_n",
"Hacl.Impl.Frodo.Params.bytes_mu",
"Hacl.Impl.Frodo.KEM.Decaps.crypto_kem_dec_ss2",
"Prims.unit",
"Hacl.Impl.Frodo.KEM.Decaps.crypto_kem_dec_mu"
] | [] | false | true | false | false | false | let crypto_kem_dec0 a gen_a ss ct sk bp_matrix c_matrix mu_decode =
| crypto_kem_dec_mu a sk bp_matrix c_matrix mu_decode;
crypto_kem_dec_ss2 a gen_a ct sk mu_decode bp_matrix c_matrix ss | false |
EtM.Ideal.fsti | EtM.Ideal.conf | val conf : Prims.bool | let conf = ind_cpa && uf_cma | {
"file_name": "examples/crypto/EtM.Ideal.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 26,
"start_col": 0,
"start_line": 26
} | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module EtM.Ideal
val ind_cpa : bool
val uf_cma : b:bool{ ind_cpa ==> b }
let ind_cpa_rest_adv = uf_cma
(* well typed adversaries only submit ciphertext obtained using encrypt *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "EtM.Ideal.fsti"
} | [
{
"abbrev": false,
"full_module": "EtM",
"short_module": null
},
{
"abbrev": false,
"full_module": "EtM",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.op_AmpAmp",
"EtM.Ideal.ind_cpa",
"EtM.Ideal.uf_cma"
] | [] | false | false | false | true | false | let conf =
| ind_cpa && uf_cma | false |
|
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.ci_ex2 | val ci_ex2 (x: nat{x % 2 = 0}) (y: int{y % 3 = 0 /\ x + y % 5 = 0})
: Pure int (requires (x + y >= 0)) (ensures (fun z -> z >= 0)) | val ci_ex2 (x: nat{x % 2 = 0}) (y: int{y % 3 = 0 /\ x + y % 5 = 0})
: Pure int (requires (x + y >= 0)) (ensures (fun z -> z >= 0)) | let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 154,
"start_col": 0,
"start_line": 144
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those, | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.nat{x % 2 = 0} -> y: Prims.int{y % 3 = 0 /\ x + y % 5 = 0} -> Prims.Pure Prims.int | Prims.Pure | [] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Prims.l_and",
"Prims.op_Addition",
"Prims.op_GreaterThanOrEqual"
] | [] | false | false | false | false | false | let ci_ex2 (x: nat{x % 2 = 0}) (y: int{y % 3 = 0 /\ x + y % 5 = 0})
: Pure int (requires (x + y >= 0)) (ensures (fun z -> z >= 0)) =
| let z = x + y in
z | false |
EtM.Ideal.fsti | EtM.Ideal.auth | val auth : b: Prims.bool{EtM.Ideal.ind_cpa ==> b} | let auth = uf_cma | {
"file_name": "examples/crypto/EtM.Ideal.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 18,
"end_line": 28,
"start_col": 0,
"start_line": 28
} | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module EtM.Ideal
val ind_cpa : bool
val uf_cma : b:bool{ ind_cpa ==> b }
let ind_cpa_rest_adv = uf_cma
(* well typed adversaries only submit ciphertext obtained using encrypt *)
let conf = ind_cpa && uf_cma | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "EtM.Ideal.fsti"
} | [
{
"abbrev": false,
"full_module": "EtM",
"short_module": null
},
{
"abbrev": false,
"full_module": "EtM",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: Prims.bool{EtM.Ideal.ind_cpa ==> b} | Prims.Tot | [
"total"
] | [] | [
"EtM.Ideal.uf_cma"
] | [] | false | false | false | false | false | let auth =
| uf_cma | false |
|
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.ci_ex1 | val ci_ex1 (x y: nat) : z: int{z % 3 = 0} | val ci_ex1 (x y: nat) : z: int{z % 3 = 0} | let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 20,
"end_line": 128,
"start_col": 0,
"start_line": 96
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.nat -> y: Prims.nat -> z: Prims.int{z % 3 = 0} | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"FStar.Mul.op_Star",
"Prims.op_Addition",
"Prims.int",
"Prims.b2t",
"Prims.op_Equality",
"Prims.op_Modulus",
"FStar.InteractiveHelpers.Tutorial.Definitions.f4",
"Prims.op_GreaterThanOrEqual",
"FStar.InteractiveHelpers.Tutorial.Definitions.f2",
"FStar.InteractiveHelpers.Tutorial.Definitions.f1"
] | [] | false | false | false | false | false | let ci_ex1 (x y: nat) : z: int{z % 3 = 0} =
| let x1 = f1 (x + 4) y in
let x2 = f2 x1 y in
let x3 = f4 x2 in
3 * (x1 + x2 + x3) | false |
Hacl.EC.K256.fst | Hacl.EC.K256.felem_inv | val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a)) | val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a)) | let felem_inv a out =
FI.finv out a | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 15,
"end_line": 171,
"start_col": 0,
"start_line": 170
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b))
let felem_mul a b out =
F.fmul out a b
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a))
let felem_sqr a out =
F.fsqr out a
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.K256.Field.felem -> out: Hacl.K256.Field.felem -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.K256.Field.felem",
"Hacl.Impl.K256.Finv.finv",
"Prims.unit"
] | [] | false | true | false | false | false | let felem_inv a out =
| FI.finv out a | false |
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.simpl_ex1 | val simpl_ex1 : x: Prims.nat -> Prims.int | let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w' | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 4,
"end_line": 54,
"start_col": 0,
"start_line": 34
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r). | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.nat -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.int",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"Prims.unit",
"Prims._assert",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.op_GreaterThan",
"Prims.bool"
] | [] | false | false | false | true | false | let simpl_ex1 (x: nat) =
| let y = 4 in
let z = 3 in
let w:w: nat{w >= 10} =
if x > 3
then
(assert (y + x > 7);
let x' = x + 1 in
assert (y + x' > 8);
let x'' = 2 * (y + x') in
assert (x'' > 16);
assert (x'' >= 10);
2 * x')
else 12
in
assert (x >= 0 /\ y >= 0);
let w' = 2 * w + z in
w' | false |
|
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.ci_ex3 | val ci_ex3 (r1 r2: B.buffer int)
: ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) | val ci_ex3 (r1 r2: B.buffer int)
: ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) | let ci_ex3 (r1 r2 : B.buffer int) :
ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
(**) let h0 = ST.get () in
let n1 = sf1 r1 in (* <- Try C-c C-e C-e here *)
(* [> assert(
[> (fun __h1_0 ->
[> LowStar.Monotonic.Buffer.live h0 r1 /\
[> LowStar.Monotonic.Buffer.as_seq h0 r1 == LowStar.Monotonic.Buffer.as_seq __h1_0 r1 /\
[> n1 =
[> FStar.List.Tot.Base.fold_left (fun x y -> x + y)
[> 0
[> (FStar.Seq.Properties.seq_to_list (LowStar.Monotonic.Buffer.as_seq h0 r1))) __h1_0); *)
(**) let h1 = ST.get () in
let n2 = sf1 r2 in
(**) let h2 = ST.get () in
n1 + n2 | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 9,
"end_line": 192,
"start_col": 0,
"start_line": 177
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those,
/// put the pointer close enough to the beginning of the function and type `C-c C-e C-g`
let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z
/// Those commands also work on effectful terms. They may need to
/// introduce variables in the context. For instance, when dealing with pres/posts
/// of stateful terms, it will look for state variables with which to instantiate
/// those pre/posts, but might not be able to find suitable variables.
/// In this case, it introduces fresh variables (easy to recognize because they are
/// preceded by "__") and abstracts them away, to indicate the user that he needs
/// to provide those variables.
///
/// It leads to assertions of the form:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) __x0 __x1)
///
/// As (C-c C-e C-e) performs simple normalization (to remove abstractions,
/// for instance) on the terms it manipulates, you can manually rewrite this
/// assert to:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) x y)
///
/// then apply (C-c C-e C-e) on the above assertion to get:
/// [> assert(pred x y)
///
/// Try this on the stateful calls in the below function: | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r1: LowStar.Buffer.buffer Prims.int -> r2: LowStar.Buffer.buffer Prims.int
-> FStar.HyperStack.ST.Stack Prims.int | FStar.HyperStack.ST.Stack | [] | [] | [
"LowStar.Buffer.buffer",
"Prims.int",
"Prims.op_Addition",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"FStar.InteractiveHelpers.Tutorial.Definitions.sf1",
"Prims.l_True"
] | [] | false | true | false | false | false | let ci_ex3 (r1 r2: B.buffer int)
: ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
| let h0 = ST.get () in
let n1 = sf1 r1 in
let h1 = ST.get () in
let n2 = sf1 r2 in
let h2 = ST.get () in
n1 + n2 | false |
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.simpl_ex2 | val simpl_ex2 : x: Prims.nat -> Prims.int | let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3 | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 4,
"end_line": 82,
"start_col": 0,
"start_line": 69
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.nat -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.unit",
"Prims._assert",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"FStar.Mul.op_Star",
"Prims.op_Addition",
"Prims.logical",
"Prims.l_True",
"FStar.Pervasives.assert_norm",
"Prims.op_LessThan"
] | [] | false | false | false | true | false | let simpl_ex2 (x: nat) =
| let x1 = x + 1 in
assert (x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm (237 * 486 = 115182);
assert (x2 = 3 * (x + 2));
assert (x2 % 3 = 0);
assert_norm (8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert (x3 = 6 * (x + 2) + 6);
let assertion = True in
assert (x3 = 6 * (x + 3));
assert (x3 % 3 = 0);
x3 | false |
|
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.ci_ex4 | val ci_ex4 (x: int{x % 2 = 0})
: Pure int (requires True) (ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) | val ci_ex4 (x: int{x % 2 = 0})
: Pure int (requires True) (ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) | let ci_ex4 (x : int{x % 2 = 0}) :
Pure int
(requires True)
(ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) =
let x = x + 4 in (* We shadow the original ``x`` here *)
x | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 204,
"start_col": 0,
"start_line": 199
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those,
/// put the pointer close enough to the beginning of the function and type `C-c C-e C-g`
let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z
/// Those commands also work on effectful terms. They may need to
/// introduce variables in the context. For instance, when dealing with pres/posts
/// of stateful terms, it will look for state variables with which to instantiate
/// those pre/posts, but might not be able to find suitable variables.
/// In this case, it introduces fresh variables (easy to recognize because they are
/// preceded by "__") and abstracts them away, to indicate the user that he needs
/// to provide those variables.
///
/// It leads to assertions of the form:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) __x0 __x1)
///
/// As (C-c C-e C-e) performs simple normalization (to remove abstractions,
/// for instance) on the terms it manipulates, you can manually rewrite this
/// assert to:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) x y)
///
/// then apply (C-c C-e C-e) on the above assertion to get:
/// [> assert(pred x y)
///
/// Try this on the stateful calls in the below function:
let ci_ex3 (r1 r2 : B.buffer int) :
ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
(**) let h0 = ST.get () in
let n1 = sf1 r1 in (* <- Try C-c C-e C-e here *)
(* [> assert(
[> (fun __h1_0 ->
[> LowStar.Monotonic.Buffer.live h0 r1 /\
[> LowStar.Monotonic.Buffer.as_seq h0 r1 == LowStar.Monotonic.Buffer.as_seq __h1_0 r1 /\
[> n1 =
[> FStar.List.Tot.Base.fold_left (fun x y -> x + y)
[> 0
[> (FStar.Seq.Properties.seq_to_list (LowStar.Monotonic.Buffer.as_seq h0 r1))) __h1_0); *)
(**) let h1 = ST.get () in
let n2 = sf1 r2 in
(**) let h2 = ST.get () in
n1 + n2
/// It may happen that the command needs to introduce assertions using variables
/// which are shadowed at that point. In this case, it "abstracts" them, like what
/// is done in the previous example. This way, the user still has an assertion
/// he can investigate, and where the problematic variables are clearly indicated | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.int{x % 2 = 0} -> Prims.Pure Prims.int | Prims.Pure | [] | [] | [
"Prims.int",
"Prims.b2t",
"Prims.op_Equality",
"Prims.op_Modulus",
"Prims.op_Addition",
"Prims.l_True",
"Prims.l_and",
"Prims.op_GreaterThanOrEqual"
] | [] | false | false | false | false | false | let ci_ex4 (x: int{x % 2 = 0})
: Pure int (requires True) (ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) =
| let x = x + 4 in
x | false |
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.ts_ex1 | val ts_ex1 : x: Prims.int -> z: Prims.nat{z >= 8} | let ts_ex1 (x : int) =
let y : nat =
if x >= 0 then
begin
let z1 = x + 4 in
let z2 : int = f1 z1 x in (* <- Say you want to use C-c C-e here: first use C-c C-s C-i *)
z2
end
else -x
in
let z = f2 y 3 in
z | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 338,
"start_col": 0,
"start_line": 327
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those,
/// put the pointer close enough to the beginning of the function and type `C-c C-e C-g`
let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z
/// Those commands also work on effectful terms. They may need to
/// introduce variables in the context. For instance, when dealing with pres/posts
/// of stateful terms, it will look for state variables with which to instantiate
/// those pre/posts, but might not be able to find suitable variables.
/// In this case, it introduces fresh variables (easy to recognize because they are
/// preceded by "__") and abstracts them away, to indicate the user that he needs
/// to provide those variables.
///
/// It leads to assertions of the form:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) __x0 __x1)
///
/// As (C-c C-e C-e) performs simple normalization (to remove abstractions,
/// for instance) on the terms it manipulates, you can manually rewrite this
/// assert to:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) x y)
///
/// then apply (C-c C-e C-e) on the above assertion to get:
/// [> assert(pred x y)
///
/// Try this on the stateful calls in the below function:
let ci_ex3 (r1 r2 : B.buffer int) :
ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
(**) let h0 = ST.get () in
let n1 = sf1 r1 in (* <- Try C-c C-e C-e here *)
(* [> assert(
[> (fun __h1_0 ->
[> LowStar.Monotonic.Buffer.live h0 r1 /\
[> LowStar.Monotonic.Buffer.as_seq h0 r1 == LowStar.Monotonic.Buffer.as_seq __h1_0 r1 /\
[> n1 =
[> FStar.List.Tot.Base.fold_left (fun x y -> x + y)
[> 0
[> (FStar.Seq.Properties.seq_to_list (LowStar.Monotonic.Buffer.as_seq h0 r1))) __h1_0); *)
(**) let h1 = ST.get () in
let n2 = sf1 r2 in
(**) let h2 = ST.get () in
n1 + n2
/// It may happen that the command needs to introduce assertions using variables
/// which are shadowed at that point. In this case, it "abstracts" them, like what
/// is done in the previous example. This way, the user still has an assertion
/// he can investigate, and where the problematic variables are clearly indicated
/// if he wants to work with it.
let ci_ex4 (x : int{x % 2 = 0}) :
Pure int
(requires True)
(ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) =
let x = x + 4 in (* We shadow the original ``x`` here *)
x (* <- Try C-c C-e C-e here *)
(**** Split conjunctions *)
/// Proof obligations are often written in the form of big conjunctions, and
/// F* may not always be precise enough to indicate which part of the conjunction
/// fails. The user then often has to "split" the conjunctions by hand, by
/// introducing one assertion per conjunct.
/// The fem-split-assert-assume-conjuncts command (C-c C-e C-s) automates the process.
/// Move the pointer anywhere inside the below assert and use C-c C-e C-s.
let split_ex1 (x y z : nat) : unit =
assert( (* <- Try C-c C-e C-s anywhere inside the assert *)
pred1 x y z /\
pred2 x y z /\
pred3 x y z /\
pred4 x y z /\
pred5 x y z /\
pred6 x y z)
/// Note that you can call the above command in any of the following terms:
/// - ``assert``
/// - ``assert_norm``
/// - ``assume``
(**** Terms unfoldign *)
/// It sometimes happens that we need to unfold a term in an assertion, for example
/// in order to check why some equality is not satisfied.
/// fem-unfold-in-assert-assume (C-c C-s C-u) addresses this issue.
let ut_ex1 (x y : nat) : unit =
let z1 = f3 (x + y) in
(* Unfold definitions: *)
assert(z1 = f3 (x + y)); (* <- Move the pointer EXACTLY over ``f3`` and use C-c C-e C-u *)
(* Unfold arbitrary identifiers:
* In case the term to unfold is not a top-level definition but a local
* variable, the command will look for a pure let-binding and will even
* explore post-conditions to look for an equality to find a term by
* which to replace the variable. *)
assert(z1 = 2 * (x + y)); (* <- Try the command on ``z1`` *)
(* Note that if the assertion is an equality, the command will only
* operate on one side of the equality at a time. *)
assert(z1 = z1);
(*
* It is even possible to apply the command on arbitrary term, in which
* case the command will explore post-conditions to find an equality to
* use for rewriting.
*)
assert(f3 (f3 (x + y)) = 4 * (x + y));
assert(2 * z1 = z1 + z1);
assert(f3 (f3 (x + y)) = 2 * z1) (* <- SELECT an operand then call C-c C-e C-u *)
/// Of course, it works with effectful functions too, and searches the context
/// for state variables to use:
let ut_ex2 () : ST.ST unit (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
let l : list int = [1; 2; 3; 4; 5; 6] in
let h0 = ST.get () in
let r = sf2 l in (* This dummy function introduces some equalities in the context *)
let h1 = ST.get () in
assert(B.as_seq h1 r == B.as_seq h1 r); (* <- Try here *)
()
(**** Combining commands *)
/// Those commands prove really useful when you combine them. The below example
/// is inspired by a function found in HACL*.
/// Invariants are sometimes divided in several pieces, for example a
/// functional part, to which we later add information about aliasing.
/// Moreover they are often written in the form of big conjunctions:
let invariant1_s (l1 l2 l3 : Seq.seq int) =
lpred1 l1 l2 /\
lpred2 l2 l3 /\
lpred3 l3 l1
let invariant1 (h : HS.mem) (r1 r2 r3 : B.buffer int) =
let l1 = B.as_seq h r1 in
let l2 = B.as_seq h r2 in
let l3 = B.as_seq h r3 in
invariant1_s l1 l2 l3 /\
B.live h r1 /\ B.live h r2 /\ B.live h r3 /\
B.disjoint r1 r2 /\ B.disjoint r2 r3 /\ B.disjoint r1 r3
/// The following function has to maintain the invariant. Now let's imagine
/// that once the function is written, you fail to prove that the postcondition
/// is satisfied and, worse but you don't know it yet, the problem comes from
/// one of the conjuncts inside ``invariant_s``. With the commands introduced
/// so far, it is pretty easy to debug that thing!
let cc_ex1 (r1 r2 r3 : B.buffer int) :
ST.Stack nat
(requires (fun h0 -> invariant1 h0 r1 r2 r3))
(ensures (fun h0 x h1 -> invariant1 h1 r1 r2 r3 /\ x >= 3)) =
(**) let h0 = ST.get () in
let x = 3 in
(*
* ...
* In practice there would be some (a lot of) code here
* ...
*)
(**) let h1 = ST.get () in
x (* <- Try studying the proof obligations here *)
(**** For advanced users and hackers only *)
(***** Two-steps execution - TODO: broken *)
/// The commands implemented in the F* extended mode work by updating the function
/// defined by the user, by inserting some annotations and instructions for F*
/// (like post-processing instructions), query F* on the result and retrieve the
/// data output by the meta-F* functions to insert appropriate assertions in the
/// code.
/// Updating the user code requires some parsing, but it may happen that the commands
/// can't fill the holes in a function the user is writing, and which thus has many
/// missing parts. In this case, we need to provide a bit of help. For instance,
/// this can happen when trying to call the commands on a subterm inside the branch
/// of a match (or an 'if ... then .. else ...').
/// With the fem-insert-pos-markers (C-c C-s C-i), the user can do a differed command
/// execution, by first indicating the subterm which he wants to analyze, then by
/// indicating to F* how to parse the whole function. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.int -> z: Prims.nat{z >= 8} | Prims.Tot | [
"total"
] | [] | [
"Prims.int",
"Prims.nat",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.InteractiveHelpers.Tutorial.Definitions.f2",
"FStar.InteractiveHelpers.Tutorial.Definitions.f1",
"Prims.op_Addition",
"Prims.bool",
"Prims.op_Minus"
] | [] | false | false | false | false | false | let ts_ex1 (x: int) =
| let y:nat =
if x >= 0
then
let z1 = x + 4 in
let z2:int = f1 z1 x in
z2
else - x
in
let z = f2 y 3 in
z | false |
|
Hacl.EC.K256.fst | Hacl.EC.K256.point_negate | val point_negate (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint out p /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.point_negate (P.point_eval h0 p)) | val point_negate (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint out p /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.point_negate (P.point_eval h0 p)) | let point_negate p out =
P.point_negate out p | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 22,
"end_line": 265,
"start_col": 0,
"start_line": 264
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b))
let felem_mul a b out =
F.fmul out a b
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a))
let felem_sqr a out =
F.fsqr out a
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a))
let felem_inv a out =
FI.finv out a
[@@ Comment "Load a bid-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint"]
val felem_load: b:lbuffer uint8 32ul -> out:F.felem -> Stack unit
(requires fun h -> live h b /\ live h out /\ disjoint b out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == BSeq.nat_from_bytes_be (as_seq h0 b) % S.prime)
let felem_load b out =
F.load_felem out b
[@@ Comment "Serialize a field element into big-endian memory.
The argument `a` points to a field element of 5 limbs in size, i.e., uint64_t[5].
The outparam `out` points to 32 bytes of valid memory, i.e., uint8_t[32].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_store: a:F.felem -> out:lbuffer uint8 32ul -> Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == BSeq.nat_to_bytes_be 32 (F.feval h0 a))
let felem_store a out =
push_frame ();
let tmp = F.create_felem () in
let h0 = ST.get () in
BL.normalize5_lemma (1,1,1,1,2) (F.as_felem5 h0 a);
F.fnormalize tmp a;
F.store_felem out tmp;
pop_frame ()
[@@ CPrologue
"/*******************************************************************************
Verified group operations for the secp256k1 curve of the form y^2 = x^3 + 7.
This is a 64-bit optimized version, where a group element in projective coordinates
is represented as an array of 15 unsigned 64-bit integers, i.e., uint64_t[15].
*******************************************************************************/\n";
Comment "Write the point at infinity (additive identity) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_point_at_inf: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ S.to_aff_point (P.point_eval h1 p) == S.aff_point_at_inf)
let mk_point_at_inf p =
P.make_point_at_inf p
[@@ Comment "Write the base point (generator) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_base_point: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ P.point_eval h1 p == S.g)
let mk_base_point p =
P.make_g p
[@@ Comment "Write `-p` in `out` (point negation).
The argument `p` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p` and `out` are either disjoint or equal"]
val point_negate (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint out p /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.point_negate (P.point_eval h0 p)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Hacl.Impl.K256.Point.point -> out: Hacl.Impl.K256.Point.point
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.K256.Point.point",
"Hacl.Impl.K256.Point.point_negate",
"Prims.unit"
] | [] | false | true | false | false | false | let point_negate p out =
| P.point_negate out p | false |
Hacl.EC.K256.fst | Hacl.EC.K256.point_double | val point_double (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint p out /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\ P.point_eval h1 out == S.point_double (P.point_eval h0 p)) | val point_double (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint p out /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\ P.point_eval h1 out == S.point_double (P.point_eval h0 p)) | let point_double p out =
PD.point_double out p | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 23,
"end_line": 303,
"start_col": 0,
"start_line": 302
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b))
let felem_mul a b out =
F.fmul out a b
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a))
let felem_sqr a out =
F.fsqr out a
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a))
let felem_inv a out =
FI.finv out a
[@@ Comment "Load a bid-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint"]
val felem_load: b:lbuffer uint8 32ul -> out:F.felem -> Stack unit
(requires fun h -> live h b /\ live h out /\ disjoint b out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == BSeq.nat_from_bytes_be (as_seq h0 b) % S.prime)
let felem_load b out =
F.load_felem out b
[@@ Comment "Serialize a field element into big-endian memory.
The argument `a` points to a field element of 5 limbs in size, i.e., uint64_t[5].
The outparam `out` points to 32 bytes of valid memory, i.e., uint8_t[32].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_store: a:F.felem -> out:lbuffer uint8 32ul -> Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == BSeq.nat_to_bytes_be 32 (F.feval h0 a))
let felem_store a out =
push_frame ();
let tmp = F.create_felem () in
let h0 = ST.get () in
BL.normalize5_lemma (1,1,1,1,2) (F.as_felem5 h0 a);
F.fnormalize tmp a;
F.store_felem out tmp;
pop_frame ()
[@@ CPrologue
"/*******************************************************************************
Verified group operations for the secp256k1 curve of the form y^2 = x^3 + 7.
This is a 64-bit optimized version, where a group element in projective coordinates
is represented as an array of 15 unsigned 64-bit integers, i.e., uint64_t[15].
*******************************************************************************/\n";
Comment "Write the point at infinity (additive identity) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_point_at_inf: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ S.to_aff_point (P.point_eval h1 p) == S.aff_point_at_inf)
let mk_point_at_inf p =
P.make_point_at_inf p
[@@ Comment "Write the base point (generator) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_base_point: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ P.point_eval h1 p == S.g)
let mk_base_point p =
P.make_g p
[@@ Comment "Write `-p` in `out` (point negation).
The argument `p` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p` and `out` are either disjoint or equal"]
val point_negate (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint out p /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.point_negate (P.point_eval h0 p))
let point_negate p out =
P.point_negate out p
[@@ Comment "Write `p + q` in `out` (point addition).
The arguments `p`, `q` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p`, `q`, and `out` are either pairwise disjoint or equal"]
val point_add (p q out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ live h q /\
eq_or_disjoint p out /\ eq_or_disjoint q out /\ eq_or_disjoint p q /\
P.point_inv h p /\ P.point_inv h q)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.point_add (P.point_eval h0 p) (P.point_eval h0 q))
let point_add p q out =
PA.point_add out p q
[@@ Comment "Write `p + p` in `out` (point doubling).
The argument `p` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p` and `out` are either disjoint or equal"]
val point_double (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint p out /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\ P.point_eval h1 out == S.point_double (P.point_eval h0 p)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Hacl.Impl.K256.Point.point -> out: Hacl.Impl.K256.Point.point
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.K256.Point.point",
"Hacl.Impl.K256.PointDouble.point_double",
"Prims.unit"
] | [] | false | true | false | false | false | let point_double p out =
| PD.point_double out p | false |
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.ut_ex1 | val ut_ex1 (x y: nat) : unit | val ut_ex1 (x y: nat) : unit | let ut_ex1 (x y : nat) : unit =
let z1 = f3 (x + y) in
(* Unfold definitions: *)
assert(z1 = f3 (x + y)); (* <- Move the pointer EXACTLY over ``f3`` and use C-c C-e C-u *)
(* Unfold arbitrary identifiers:
* In case the term to unfold is not a top-level definition but a local
* variable, the command will look for a pure let-binding and will even
* explore post-conditions to look for an equality to find a term by
* which to replace the variable. *)
assert(z1 = 2 * (x + y)); (* <- Try the command on ``z1`` *)
(* Note that if the assertion is an equality, the command will only
* operate on one side of the equality at a time. *)
assert(z1 = z1);
(*
* It is even possible to apply the command on arbitrary term, in which
* case the command will explore post-conditions to find an equality to
* use for rewriting.
*)
assert(f3 (f3 (x + y)) = 4 * (x + y));
assert(2 * z1 = z1 + z1);
assert(f3 (f3 (x + y)) = 2 * z1) | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 258,
"start_col": 0,
"start_line": 234
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those,
/// put the pointer close enough to the beginning of the function and type `C-c C-e C-g`
let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z
/// Those commands also work on effectful terms. They may need to
/// introduce variables in the context. For instance, when dealing with pres/posts
/// of stateful terms, it will look for state variables with which to instantiate
/// those pre/posts, but might not be able to find suitable variables.
/// In this case, it introduces fresh variables (easy to recognize because they are
/// preceded by "__") and abstracts them away, to indicate the user that he needs
/// to provide those variables.
///
/// It leads to assertions of the form:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) __x0 __x1)
///
/// As (C-c C-e C-e) performs simple normalization (to remove abstractions,
/// for instance) on the terms it manipulates, you can manually rewrite this
/// assert to:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) x y)
///
/// then apply (C-c C-e C-e) on the above assertion to get:
/// [> assert(pred x y)
///
/// Try this on the stateful calls in the below function:
let ci_ex3 (r1 r2 : B.buffer int) :
ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
(**) let h0 = ST.get () in
let n1 = sf1 r1 in (* <- Try C-c C-e C-e here *)
(* [> assert(
[> (fun __h1_0 ->
[> LowStar.Monotonic.Buffer.live h0 r1 /\
[> LowStar.Monotonic.Buffer.as_seq h0 r1 == LowStar.Monotonic.Buffer.as_seq __h1_0 r1 /\
[> n1 =
[> FStar.List.Tot.Base.fold_left (fun x y -> x + y)
[> 0
[> (FStar.Seq.Properties.seq_to_list (LowStar.Monotonic.Buffer.as_seq h0 r1))) __h1_0); *)
(**) let h1 = ST.get () in
let n2 = sf1 r2 in
(**) let h2 = ST.get () in
n1 + n2
/// It may happen that the command needs to introduce assertions using variables
/// which are shadowed at that point. In this case, it "abstracts" them, like what
/// is done in the previous example. This way, the user still has an assertion
/// he can investigate, and where the problematic variables are clearly indicated
/// if he wants to work with it.
let ci_ex4 (x : int{x % 2 = 0}) :
Pure int
(requires True)
(ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) =
let x = x + 4 in (* We shadow the original ``x`` here *)
x (* <- Try C-c C-e C-e here *)
(**** Split conjunctions *)
/// Proof obligations are often written in the form of big conjunctions, and
/// F* may not always be precise enough to indicate which part of the conjunction
/// fails. The user then often has to "split" the conjunctions by hand, by
/// introducing one assertion per conjunct.
/// The fem-split-assert-assume-conjuncts command (C-c C-e C-s) automates the process.
/// Move the pointer anywhere inside the below assert and use C-c C-e C-s.
let split_ex1 (x y z : nat) : unit =
assert( (* <- Try C-c C-e C-s anywhere inside the assert *)
pred1 x y z /\
pred2 x y z /\
pred3 x y z /\
pred4 x y z /\
pred5 x y z /\
pred6 x y z)
/// Note that you can call the above command in any of the following terms:
/// - ``assert``
/// - ``assert_norm``
/// - ``assume``
(**** Terms unfoldign *)
/// It sometimes happens that we need to unfold a term in an assertion, for example
/// in order to check why some equality is not satisfied.
/// fem-unfold-in-assert-assume (C-c C-s C-u) addresses this issue. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.nat -> y: Prims.nat -> Prims.unit | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims._assert",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.InteractiveHelpers.Tutorial.Definitions.f3",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"Prims.unit"
] | [] | false | false | false | true | false | let ut_ex1 (x y: nat) : unit =
| let z1 = f3 (x + y) in
assert (z1 = f3 (x + y));
assert (z1 = 2 * (x + y));
assert (z1 = z1);
assert (f3 (f3 (x + y)) = 4 * (x + y));
assert (2 * z1 = z1 + z1);
assert (f3 (f3 (x + y)) = 2 * z1) | false |
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.cc_ex1 | val cc_ex1 (r1 r2 r3: B.buffer int)
: ST.Stack nat
(requires (fun h0 -> invariant1 h0 r1 r2 r3))
(ensures (fun h0 x h1 -> invariant1 h1 r1 r2 r3 /\ x >= 3)) | val cc_ex1 (r1 r2 r3: B.buffer int)
: ST.Stack nat
(requires (fun h0 -> invariant1 h0 r1 r2 r3))
(ensures (fun h0 x h1 -> invariant1 h1 r1 r2 r3 /\ x >= 3)) | let cc_ex1 (r1 r2 r3 : B.buffer int) :
ST.Stack nat
(requires (fun h0 -> invariant1 h0 r1 r2 r3))
(ensures (fun h0 x h1 -> invariant1 h1 r1 r2 r3 /\ x >= 3)) =
(**) let h0 = ST.get () in
let x = 3 in
(*
* ...
* In practice there would be some (a lot of) code here
* ...
*)
(**) let h1 = ST.get () in
x | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 308,
"start_col": 0,
"start_line": 296
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those,
/// put the pointer close enough to the beginning of the function and type `C-c C-e C-g`
let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z
/// Those commands also work on effectful terms. They may need to
/// introduce variables in the context. For instance, when dealing with pres/posts
/// of stateful terms, it will look for state variables with which to instantiate
/// those pre/posts, but might not be able to find suitable variables.
/// In this case, it introduces fresh variables (easy to recognize because they are
/// preceded by "__") and abstracts them away, to indicate the user that he needs
/// to provide those variables.
///
/// It leads to assertions of the form:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) __x0 __x1)
///
/// As (C-c C-e C-e) performs simple normalization (to remove abstractions,
/// for instance) on the terms it manipulates, you can manually rewrite this
/// assert to:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) x y)
///
/// then apply (C-c C-e C-e) on the above assertion to get:
/// [> assert(pred x y)
///
/// Try this on the stateful calls in the below function:
let ci_ex3 (r1 r2 : B.buffer int) :
ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
(**) let h0 = ST.get () in
let n1 = sf1 r1 in (* <- Try C-c C-e C-e here *)
(* [> assert(
[> (fun __h1_0 ->
[> LowStar.Monotonic.Buffer.live h0 r1 /\
[> LowStar.Monotonic.Buffer.as_seq h0 r1 == LowStar.Monotonic.Buffer.as_seq __h1_0 r1 /\
[> n1 =
[> FStar.List.Tot.Base.fold_left (fun x y -> x + y)
[> 0
[> (FStar.Seq.Properties.seq_to_list (LowStar.Monotonic.Buffer.as_seq h0 r1))) __h1_0); *)
(**) let h1 = ST.get () in
let n2 = sf1 r2 in
(**) let h2 = ST.get () in
n1 + n2
/// It may happen that the command needs to introduce assertions using variables
/// which are shadowed at that point. In this case, it "abstracts" them, like what
/// is done in the previous example. This way, the user still has an assertion
/// he can investigate, and where the problematic variables are clearly indicated
/// if he wants to work with it.
let ci_ex4 (x : int{x % 2 = 0}) :
Pure int
(requires True)
(ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) =
let x = x + 4 in (* We shadow the original ``x`` here *)
x (* <- Try C-c C-e C-e here *)
(**** Split conjunctions *)
/// Proof obligations are often written in the form of big conjunctions, and
/// F* may not always be precise enough to indicate which part of the conjunction
/// fails. The user then often has to "split" the conjunctions by hand, by
/// introducing one assertion per conjunct.
/// The fem-split-assert-assume-conjuncts command (C-c C-e C-s) automates the process.
/// Move the pointer anywhere inside the below assert and use C-c C-e C-s.
let split_ex1 (x y z : nat) : unit =
assert( (* <- Try C-c C-e C-s anywhere inside the assert *)
pred1 x y z /\
pred2 x y z /\
pred3 x y z /\
pred4 x y z /\
pred5 x y z /\
pred6 x y z)
/// Note that you can call the above command in any of the following terms:
/// - ``assert``
/// - ``assert_norm``
/// - ``assume``
(**** Terms unfoldign *)
/// It sometimes happens that we need to unfold a term in an assertion, for example
/// in order to check why some equality is not satisfied.
/// fem-unfold-in-assert-assume (C-c C-s C-u) addresses this issue.
let ut_ex1 (x y : nat) : unit =
let z1 = f3 (x + y) in
(* Unfold definitions: *)
assert(z1 = f3 (x + y)); (* <- Move the pointer EXACTLY over ``f3`` and use C-c C-e C-u *)
(* Unfold arbitrary identifiers:
* In case the term to unfold is not a top-level definition but a local
* variable, the command will look for a pure let-binding and will even
* explore post-conditions to look for an equality to find a term by
* which to replace the variable. *)
assert(z1 = 2 * (x + y)); (* <- Try the command on ``z1`` *)
(* Note that if the assertion is an equality, the command will only
* operate on one side of the equality at a time. *)
assert(z1 = z1);
(*
* It is even possible to apply the command on arbitrary term, in which
* case the command will explore post-conditions to find an equality to
* use for rewriting.
*)
assert(f3 (f3 (x + y)) = 4 * (x + y));
assert(2 * z1 = z1 + z1);
assert(f3 (f3 (x + y)) = 2 * z1) (* <- SELECT an operand then call C-c C-e C-u *)
/// Of course, it works with effectful functions too, and searches the context
/// for state variables to use:
let ut_ex2 () : ST.ST unit (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
let l : list int = [1; 2; 3; 4; 5; 6] in
let h0 = ST.get () in
let r = sf2 l in (* This dummy function introduces some equalities in the context *)
let h1 = ST.get () in
assert(B.as_seq h1 r == B.as_seq h1 r); (* <- Try here *)
()
(**** Combining commands *)
/// Those commands prove really useful when you combine them. The below example
/// is inspired by a function found in HACL*.
/// Invariants are sometimes divided in several pieces, for example a
/// functional part, to which we later add information about aliasing.
/// Moreover they are often written in the form of big conjunctions:
let invariant1_s (l1 l2 l3 : Seq.seq int) =
lpred1 l1 l2 /\
lpred2 l2 l3 /\
lpred3 l3 l1
let invariant1 (h : HS.mem) (r1 r2 r3 : B.buffer int) =
let l1 = B.as_seq h r1 in
let l2 = B.as_seq h r2 in
let l3 = B.as_seq h r3 in
invariant1_s l1 l2 l3 /\
B.live h r1 /\ B.live h r2 /\ B.live h r3 /\
B.disjoint r1 r2 /\ B.disjoint r2 r3 /\ B.disjoint r1 r3
/// The following function has to maintain the invariant. Now let's imagine
/// that once the function is written, you fail to prove that the postcondition
/// is satisfied and, worse but you don't know it yet, the problem comes from
/// one of the conjuncts inside ``invariant_s``. With the commands introduced | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r1: LowStar.Buffer.buffer Prims.int ->
r2: LowStar.Buffer.buffer Prims.int ->
r3: LowStar.Buffer.buffer Prims.int
-> FStar.HyperStack.ST.Stack Prims.nat | FStar.HyperStack.ST.Stack | [] | [] | [
"LowStar.Buffer.buffer",
"Prims.int",
"Prims.nat",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"FStar.InteractiveHelpers.Tutorial.invariant1",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual"
] | [] | false | true | false | false | false | let cc_ex1 (r1 r2 r3: B.buffer int)
: ST.Stack nat
(requires (fun h0 -> invariant1 h0 r1 r2 r3))
(ensures (fun h0 x h1 -> invariant1 h1 r1 r2 r3 /\ x >= 3)) =
| let h0 = ST.get () in
let x = 3 in
let h1 = ST.get () in
x | false |
FStar.InteractiveHelpers.Tutorial.fst | FStar.InteractiveHelpers.Tutorial.ut_ex2 | val ut_ex2: Prims.unit -> ST.ST unit (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) | val ut_ex2: Prims.unit -> ST.ST unit (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) | let ut_ex2 () : ST.ST unit (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
let l : list int = [1; 2; 3; 4; 5; 6] in
let h0 = ST.get () in
let r = sf2 l in (* This dummy function introduces some equalities in the context *)
let h1 = ST.get () in
assert(B.as_seq h1 r == B.as_seq h1 r); (* <- Try here *)
() | {
"file_name": "examples/interactive/FStar.InteractiveHelpers.Tutorial.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 4,
"end_line": 268,
"start_col": 0,
"start_line": 262
} | module FStar.InteractiveHelpers.Tutorial
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
open FStar.List
open FStar.Tactics.V2
open FStar.Mul
open FStar.InteractiveHelpers.Tutorial.Definitions
/// WARNING: if a command fails, it is very likely because of the below issue
/// The extended mode requires the FStar.InteractiveHelpers module to run meta-processing
/// functions on the code the user is working on. F* needs to know which modules
/// to load from the very start and won't load additional modules on-demand (you
/// will need to restart the F* mode), which means that if you intend to use the
/// extended mode while working on a file, you have to make sure F* will load
/// FStar.InteractiveHelpers:
module FI = FStar.InteractiveHelpers
/// alternatively if you're not afraid of shadowing:
/// open FStar.InteractiveHelpers
#push-options "--z3rlimit 50 --fuel 0 --ifuel 0"
(*** Basic commands *)
(**** Rolling admit (C-S-r) *)
/// A very common technique used when incrementally working on a proof is the
/// "rolling-admit" technique, which consists in inserting an admit in the
/// a function which doesn't typecheck and moving it around until we
/// identify the exact piece of code which makes verification fail.
/// This technique is made simpler by the [fem-roll-admit command] (C-S-r).
/// Try typing C-S-r anywhere below to insert/move an admit:
let simpl_ex1 (x : nat) =
let y = 4 in
let z = 3 in
let w : w:nat{w >= 10} =
if x > 3 then
begin
assert(y + x > 7);
let x' = x + 1 in
assert(y + x' > 8);
let x'' = 2 * (y + x') in
assert(x'' > 16);
assert(x'' >= 10);
2 * x'
end
else 12
in
assert(
x >= 0 /\
y >= 0);
let w' = 2 * w + z in
w'
(**** Switch between asserts/assumes (C-S-s) *)
/// People often write functions and proofs incrementally, by keeping an admit
/// at the very end and adding function calls or assertions one at a time,
/// type-checking with F* at every code change to make sure that it is legal.
/// However, when such functions or proofs become long, whenever we query F*,
/// it takes a lot of time to recheck all the already known-to-succeed proof
/// obligations, before getting to the new (interesting) ones. A common way of
/// mitigating this problem is to convert the assertions to assumptions once we
/// know they succeed.
/// Try calling fem-switch-assert-assume (C-S-s) in the below function.
/// Note that it operates either on the assertion under the pointer, or on the
/// current selection.
let simpl_ex2 (x : nat) =
let x1 = x + 1 in
assert(x1 = x + 1);
let x2 = 3 * (x1 + 1) in
assert_norm(237 * 486 = 115182);
assert(x2 = 3 * (x + 2));
assert(x2 % 3 = 0);
assert_norm(8 * 4 < 40);
let x3 = 2 * x2 + 6 in
assert(x3 = 6 * (x + 2) + 6);
let assertion = True in
assert(x3 = 6 * (x + 3));
assert(x3 % 3 = 0);
x3
(*** Advanced commands *)
(**** Insert context/proof obligations information *)
(**** C-c C-e C-e *)
/// If often happens that we want to know what the precondition which fails at
/// some specific place is exactly, or if, once instantiated, what the postcondition
/// of some lemma is indeed what we believe it is, because it fails to prove some
/// obligation for example, etc. In other words: we sometimes feel blind when
/// working in F*, and the workarounds (mostly copy-pasting and instantiating by hand
/// the relevant pre/postconditions) are often painful.
/// The effectful term analysis command (C-c C-e C-e) addresses this issue.
/// Try testing the fem-insert-pre-post command on the let-bindings and the return result
let ci_ex1 (x y : nat) : z:int{z % 3 = 0} =
(* Preconditions:
* Type C-c C-e C-e below to insert:
* [> assert(x + 4 > 3); *)
let x1 = f1 (x + 4) y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Postconditions: *)
let x2 = f2 x1 y in (* <- Put your pointer after the 'in' and type C-c C-e C-e *)
(* Type C-c C-e C-e above to insert:
* [> assert(has_type x2 nat);
* [> assert(x2 >= 8);
* [> assert(x2 % 2 = 0); *)
(* Typing obligations:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (x2 <: Prims.nat) Prims.int);
* [> assert(x2 % 2 = 0);
* Note that the assertion gives indications about the parameter
* known type, the target type and the (potential) refinement.
* Also note that the type obligations are not introduced when
* they are trivial (if the original type and the target type are
* exactly the same, syntactically).
* WARNING: `has_type` is very low level and shouldn't be used in user proofs.
* In the future, we intend to remove the assertions containing `has_type`,
* and only introduce assertions for the refinements.
*)
let x3 = f4 x2 in (* <- Put your pointer on the left and type C-c C-e C-e *)
(* Current goal:
* Type C-c C-e C-e below to insert:
* [> assert(Prims.has_type (3 * (x1 + x2 + x3)) Prims.int);
* [> assert(3 * (x1 + x2 + x3) % 3 = 0); *)
3 * (x1 + x2 + x3) (* <- Put your pointer on the left and type C-c C-e C-e *)
/// Note that you can use the "--print_implicits" option to adjust the output.
/// Some proof obligations indeed sometimes fail while the user is certain to
/// have the appropriate hypothesis in his context, because F* did not infer the
/// same implicits for the proof obligation and the proposition, a problem the user
/// often doesn't see. Debugging such issues can be a nightmare.
#push-options "--print_implicits"
let ci_ex1_ (x : nat) : unit =
let y = x in
assert(x == y); (* <- Use C-c C-e C-e here *)
()
#pop-options
/// You may need to know the "global" assumptions. In order to get those,
/// put the pointer close enough to the beginning of the function and type `C-c C-e C-g`
let ci_ex2 (x : nat{x % 2 = 0}) (y : int{y % 3 = 0 /\ x + y % 5 = 0}) :
Pure int
(requires (x + y >= 0))
(ensures (fun z -> z >= 0)) =
(* [> assert(Prims.has_type x Prims.nat); *)
(* [> assert(x % 2 = 0); *)
(* [> assert(Prims.has_type y Prims.int); *)
(* [> assert(y % 3 = 0 /\ x + y % 5 = 0); *)
(* [> assert(x + y >= 0); *)
let z = x + y in
z
/// Those commands also work on effectful terms. They may need to
/// introduce variables in the context. For instance, when dealing with pres/posts
/// of stateful terms, it will look for state variables with which to instantiate
/// those pre/posts, but might not be able to find suitable variables.
/// In this case, it introduces fresh variables (easy to recognize because they are
/// preceded by "__") and abstracts them away, to indicate the user that he needs
/// to provide those variables.
///
/// It leads to assertions of the form:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) __x0 __x1)
///
/// As (C-c C-e C-e) performs simple normalization (to remove abstractions,
/// for instance) on the terms it manipulates, you can manually rewrite this
/// assert to:
/// [> assert((fun __x0 __x1 -> pred __x0 __x1) x y)
///
/// then apply (C-c C-e C-e) on the above assertion to get:
/// [> assert(pred x y)
///
/// Try this on the stateful calls in the below function:
let ci_ex3 (r1 r2 : B.buffer int) :
ST.Stack int (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
(**) let h0 = ST.get () in
let n1 = sf1 r1 in (* <- Try C-c C-e C-e here *)
(* [> assert(
[> (fun __h1_0 ->
[> LowStar.Monotonic.Buffer.live h0 r1 /\
[> LowStar.Monotonic.Buffer.as_seq h0 r1 == LowStar.Monotonic.Buffer.as_seq __h1_0 r1 /\
[> n1 =
[> FStar.List.Tot.Base.fold_left (fun x y -> x + y)
[> 0
[> (FStar.Seq.Properties.seq_to_list (LowStar.Monotonic.Buffer.as_seq h0 r1))) __h1_0); *)
(**) let h1 = ST.get () in
let n2 = sf1 r2 in
(**) let h2 = ST.get () in
n1 + n2
/// It may happen that the command needs to introduce assertions using variables
/// which are shadowed at that point. In this case, it "abstracts" them, like what
/// is done in the previous example. This way, the user still has an assertion
/// he can investigate, and where the problematic variables are clearly indicated
/// if he wants to work with it.
let ci_ex4 (x : int{x % 2 = 0}) :
Pure int
(requires True)
(ensures (fun x' -> x' % 2 = 0 /\ x' >= x)) =
let x = x + 4 in (* We shadow the original ``x`` here *)
x (* <- Try C-c C-e C-e here *)
(**** Split conjunctions *)
/// Proof obligations are often written in the form of big conjunctions, and
/// F* may not always be precise enough to indicate which part of the conjunction
/// fails. The user then often has to "split" the conjunctions by hand, by
/// introducing one assertion per conjunct.
/// The fem-split-assert-assume-conjuncts command (C-c C-e C-s) automates the process.
/// Move the pointer anywhere inside the below assert and use C-c C-e C-s.
let split_ex1 (x y z : nat) : unit =
assert( (* <- Try C-c C-e C-s anywhere inside the assert *)
pred1 x y z /\
pred2 x y z /\
pred3 x y z /\
pred4 x y z /\
pred5 x y z /\
pred6 x y z)
/// Note that you can call the above command in any of the following terms:
/// - ``assert``
/// - ``assert_norm``
/// - ``assume``
(**** Terms unfoldign *)
/// It sometimes happens that we need to unfold a term in an assertion, for example
/// in order to check why some equality is not satisfied.
/// fem-unfold-in-assert-assume (C-c C-s C-u) addresses this issue.
let ut_ex1 (x y : nat) : unit =
let z1 = f3 (x + y) in
(* Unfold definitions: *)
assert(z1 = f3 (x + y)); (* <- Move the pointer EXACTLY over ``f3`` and use C-c C-e C-u *)
(* Unfold arbitrary identifiers:
* In case the term to unfold is not a top-level definition but a local
* variable, the command will look for a pure let-binding and will even
* explore post-conditions to look for an equality to find a term by
* which to replace the variable. *)
assert(z1 = 2 * (x + y)); (* <- Try the command on ``z1`` *)
(* Note that if the assertion is an equality, the command will only
* operate on one side of the equality at a time. *)
assert(z1 = z1);
(*
* It is even possible to apply the command on arbitrary term, in which
* case the command will explore post-conditions to find an equality to
* use for rewriting.
*)
assert(f3 (f3 (x + y)) = 4 * (x + y));
assert(2 * z1 = z1 + z1);
assert(f3 (f3 (x + y)) = 2 * z1) (* <- SELECT an operand then call C-c C-e C-u *)
/// Of course, it works with effectful functions too, and searches the context | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.fst.checked",
"FStar.InteractiveHelpers.Tutorial.Definitions.fst.checked",
"FStar.InteractiveHelpers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "FStar.InteractiveHelpers.Tutorial.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.InteractiveHelpers",
"short_module": "FI"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers.Tutorial.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.InteractiveHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> FStar.HyperStack.ST.ST Prims.unit | FStar.HyperStack.ST.ST | [] | [] | [
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"FStar.Seq.Base.seq",
"Prims.int",
"LowStar.Monotonic.Buffer.as_seq",
"LowStar.Buffer.trivial_preorder",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"LowStar.Buffer.buffer",
"FStar.InteractiveHelpers.Tutorial.Definitions.sf2",
"Prims.list",
"Prims.Cons",
"Prims.Nil",
"Prims.l_True"
] | [] | false | true | false | false | false | let ut_ex2 () : ST.ST unit (requires (fun _ -> True)) (ensures (fun _ _ _ -> True)) =
| let l:list int = [1; 2; 3; 4; 5; 6] in
let h0 = ST.get () in
let r = sf2 l in
let h1 = ST.get () in
assert (B.as_seq h1 r == B.as_seq h1 r);
() | false |
Hacl.EC.K256.fst | Hacl.EC.K256.felem_load | val felem_load: b:lbuffer uint8 32ul -> out:F.felem -> Stack unit
(requires fun h -> live h b /\ live h out /\ disjoint b out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == BSeq.nat_from_bytes_be (as_seq h0 b) % S.prime) | val felem_load: b:lbuffer uint8 32ul -> out:F.felem -> Stack unit
(requires fun h -> live h b /\ live h out /\ disjoint b out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == BSeq.nat_from_bytes_be (as_seq h0 b) % S.prime) | let felem_load b out =
F.load_felem out b | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 20,
"end_line": 189,
"start_col": 0,
"start_line": 188
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b))
let felem_mul a b out =
F.fmul out a b
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a))
let felem_sqr a out =
F.fsqr out a
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a))
let felem_inv a out =
FI.finv out a
[@@ Comment "Load a bid-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint"]
val felem_load: b:lbuffer uint8 32ul -> out:F.felem -> Stack unit
(requires fun h -> live h b /\ live h out /\ disjoint b out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == BSeq.nat_from_bytes_be (as_seq h0 b) % S.prime) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: Lib.Buffer.lbuffer Lib.IntTypes.uint8 32ul -> out: Hacl.K256.Field.felem
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"FStar.UInt32.__uint_to_t",
"Hacl.K256.Field.felem",
"Hacl.K256.Field.load_felem",
"Prims.unit"
] | [] | false | true | false | false | false | let felem_load b out =
| F.load_felem out b | false |
Hacl.EC.K256.fst | Hacl.EC.K256.point_store | val point_store: p:P.point -> out:lbuffer uint8 64ul -> Stack unit
(requires fun h ->
live h out /\ live h p /\ disjoint p out /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == S.point_store (P.point_eval h0 p)) | val point_store: p:P.point -> out:lbuffer uint8 64ul -> Stack unit
(requires fun h ->
live h out /\ live h p /\ disjoint p out /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == S.point_store (P.point_eval h0 p)) | let point_store p out =
P.point_store out p | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 21,
"end_line": 355,
"start_col": 0,
"start_line": 354
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b))
let felem_mul a b out =
F.fmul out a b
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a))
let felem_sqr a out =
F.fsqr out a
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a))
let felem_inv a out =
FI.finv out a
[@@ Comment "Load a bid-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint"]
val felem_load: b:lbuffer uint8 32ul -> out:F.felem -> Stack unit
(requires fun h -> live h b /\ live h out /\ disjoint b out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == BSeq.nat_from_bytes_be (as_seq h0 b) % S.prime)
let felem_load b out =
F.load_felem out b
[@@ Comment "Serialize a field element into big-endian memory.
The argument `a` points to a field element of 5 limbs in size, i.e., uint64_t[5].
The outparam `out` points to 32 bytes of valid memory, i.e., uint8_t[32].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_store: a:F.felem -> out:lbuffer uint8 32ul -> Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == BSeq.nat_to_bytes_be 32 (F.feval h0 a))
let felem_store a out =
push_frame ();
let tmp = F.create_felem () in
let h0 = ST.get () in
BL.normalize5_lemma (1,1,1,1,2) (F.as_felem5 h0 a);
F.fnormalize tmp a;
F.store_felem out tmp;
pop_frame ()
[@@ CPrologue
"/*******************************************************************************
Verified group operations for the secp256k1 curve of the form y^2 = x^3 + 7.
This is a 64-bit optimized version, where a group element in projective coordinates
is represented as an array of 15 unsigned 64-bit integers, i.e., uint64_t[15].
*******************************************************************************/\n";
Comment "Write the point at infinity (additive identity) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_point_at_inf: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ S.to_aff_point (P.point_eval h1 p) == S.aff_point_at_inf)
let mk_point_at_inf p =
P.make_point_at_inf p
[@@ Comment "Write the base point (generator) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_base_point: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ P.point_eval h1 p == S.g)
let mk_base_point p =
P.make_g p
[@@ Comment "Write `-p` in `out` (point negation).
The argument `p` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p` and `out` are either disjoint or equal"]
val point_negate (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint out p /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.point_negate (P.point_eval h0 p))
let point_negate p out =
P.point_negate out p
[@@ Comment "Write `p + q` in `out` (point addition).
The arguments `p`, `q` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p`, `q`, and `out` are either pairwise disjoint or equal"]
val point_add (p q out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ live h q /\
eq_or_disjoint p out /\ eq_or_disjoint q out /\ eq_or_disjoint p q /\
P.point_inv h p /\ P.point_inv h q)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.point_add (P.point_eval h0 p) (P.point_eval h0 q))
let point_add p q out =
PA.point_add out p q
[@@ Comment "Write `p + p` in `out` (point doubling).
The argument `p` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p` and `out` are either disjoint or equal"]
val point_double (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint p out /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\ P.point_eval h1 out == S.point_double (P.point_eval h0 p))
let point_double p out =
PD.point_double out p
[@@ Comment "Write `[scalar]p` in `out` (point multiplication or scalar multiplication).
The argument `p` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
The argument `scalar` is meant to be 32 bytes in size, i.e., uint8_t[32].
The function first loads a bid-endian scalar element from `scalar` and
then computes a point multiplication.
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `scalar`, `p`, and `out` are pairwise disjoint"]
val point_mul: scalar:lbuffer uint8 32ul -> p:P.point -> out:P.point -> Stack unit
(requires fun h ->
live h scalar /\ live h p /\ live h out /\
disjoint out p /\ disjoint out scalar /\ disjoint p scalar /\
P.point_inv h p /\
BSeq.nat_from_bytes_be (as_seq h scalar) < S.q) // it's still safe to invoke this function with scalar >= S.q
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
S.to_aff_point (P.point_eval h1 out) ==
S.to_aff_point (S.point_mul (BSeq.nat_from_bytes_be (as_seq h0 scalar)) (P.point_eval h0 p)))
let point_mul scalar p out =
push_frame ();
let scalar_q = Q.create_qelem () in
Q.load_qelem scalar_q scalar;
PM.point_mul out scalar_q p;
pop_frame ()
[@@ Comment "Convert a point from projective coordinates to its raw form.
The argument `p` points to a point of 15 limbs in size, i.e., uint64_t[15].
The outparam `out` points to 64 bytes of valid memory, i.e., uint8_t[64].
The function first converts a given point `p` from projective to affine coordinates
and then writes [ `x`; `y` ] in `out`.
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p` and `out` are disjoint."]
val point_store: p:P.point -> out:lbuffer uint8 64ul -> Stack unit
(requires fun h ->
live h out /\ live h p /\ disjoint p out /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == S.point_store (P.point_eval h0 p)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Hacl.Impl.K256.Point.point -> out: Lib.Buffer.lbuffer Lib.IntTypes.uint8 64ul
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.K256.Point.point",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"FStar.UInt32.__uint_to_t",
"Hacl.Impl.K256.Point.point_store",
"Prims.unit"
] | [] | false | true | false | false | false | let point_store p out =
| P.point_store out p | false |
Hacl.EC.K256.fst | Hacl.EC.K256.felem_mul | val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b)) | val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b)) | let felem_mul a b out =
F.fmul out a b | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 16,
"end_line": 131,
"start_col": 0,
"start_line": 130
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.K256.Field.felem -> b: Hacl.K256.Field.felem -> out: Hacl.K256.Field.felem
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.K256.Field.felem",
"Hacl.K256.Field.fmul",
"Prims.unit"
] | [] | false | true | false | false | false | let felem_mul a b out =
| F.fmul out a b | false |
Hacl.EC.K256.fst | Hacl.EC.K256.point_load | val point_load: b:lbuffer uint8 64ul -> out:P.point -> Stack unit
(requires fun h ->
live h out /\ live h b /\ disjoint b out /\
S.point_inv_bytes (as_seq h b))
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.load_point_nocheck (as_seq h0 b)) | val point_load: b:lbuffer uint8 64ul -> out:P.point -> Stack unit
(requires fun h ->
live h out /\ live h b /\ disjoint b out /\
S.point_inv_bytes (as_seq h b))
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.load_point_nocheck (as_seq h0 b)) | let point_load b out =
P.load_point_nocheck out b | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 28,
"end_line": 376,
"start_col": 0,
"start_line": 375
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b))
let felem_mul a b out =
F.fmul out a b
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a))
let felem_sqr a out =
F.fsqr out a
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.finv (F.feval h0 a))
let felem_inv a out =
FI.finv out a
[@@ Comment "Load a bid-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint"]
val felem_load: b:lbuffer uint8 32ul -> out:F.felem -> Stack unit
(requires fun h -> live h b /\ live h out /\ disjoint b out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == BSeq.nat_from_bytes_be (as_seq h0 b) % S.prime)
let felem_load b out =
F.load_felem out b
[@@ Comment "Serialize a field element into big-endian memory.
The argument `a` points to a field element of 5 limbs in size, i.e., uint64_t[5].
The outparam `out` points to 32 bytes of valid memory, i.e., uint8_t[32].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_store: a:F.felem -> out:lbuffer uint8 32ul -> Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == BSeq.nat_to_bytes_be 32 (F.feval h0 a))
let felem_store a out =
push_frame ();
let tmp = F.create_felem () in
let h0 = ST.get () in
BL.normalize5_lemma (1,1,1,1,2) (F.as_felem5 h0 a);
F.fnormalize tmp a;
F.store_felem out tmp;
pop_frame ()
[@@ CPrologue
"/*******************************************************************************
Verified group operations for the secp256k1 curve of the form y^2 = x^3 + 7.
This is a 64-bit optimized version, where a group element in projective coordinates
is represented as an array of 15 unsigned 64-bit integers, i.e., uint64_t[15].
*******************************************************************************/\n";
Comment "Write the point at infinity (additive identity) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_point_at_inf: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ S.to_aff_point (P.point_eval h1 p) == S.aff_point_at_inf)
let mk_point_at_inf p =
P.make_point_at_inf p
[@@ Comment "Write the base point (generator) in `p`.
The outparam `p` is meant to be 15 limbs in size, i.e., uint64_t[15]."]
val mk_base_point: p:P.point -> Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
P.point_inv h1 p /\ P.point_eval h1 p == S.g)
let mk_base_point p =
P.make_g p
[@@ Comment "Write `-p` in `out` (point negation).
The argument `p` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p` and `out` are either disjoint or equal"]
val point_negate (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint out p /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.point_negate (P.point_eval h0 p))
let point_negate p out =
P.point_negate out p
[@@ Comment "Write `p + q` in `out` (point addition).
The arguments `p`, `q` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p`, `q`, and `out` are either pairwise disjoint or equal"]
val point_add (p q out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ live h q /\
eq_or_disjoint p out /\ eq_or_disjoint q out /\ eq_or_disjoint p q /\
P.point_inv h p /\ P.point_inv h q)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.point_add (P.point_eval h0 p) (P.point_eval h0 q))
let point_add p q out =
PA.point_add out p q
[@@ Comment "Write `p + p` in `out` (point doubling).
The argument `p` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p` and `out` are either disjoint or equal"]
val point_double (p out:P.point) : Stack unit
(requires fun h ->
live h out /\ live h p /\ eq_or_disjoint p out /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\ P.point_eval h1 out == S.point_double (P.point_eval h0 p))
let point_double p out =
PD.point_double out p
[@@ Comment "Write `[scalar]p` in `out` (point multiplication or scalar multiplication).
The argument `p` and the outparam `out` are meant to be 15 limbs in size, i.e., uint64_t[15].
The argument `scalar` is meant to be 32 bytes in size, i.e., uint8_t[32].
The function first loads a bid-endian scalar element from `scalar` and
then computes a point multiplication.
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `scalar`, `p`, and `out` are pairwise disjoint"]
val point_mul: scalar:lbuffer uint8 32ul -> p:P.point -> out:P.point -> Stack unit
(requires fun h ->
live h scalar /\ live h p /\ live h out /\
disjoint out p /\ disjoint out scalar /\ disjoint p scalar /\
P.point_inv h p /\
BSeq.nat_from_bytes_be (as_seq h scalar) < S.q) // it's still safe to invoke this function with scalar >= S.q
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
S.to_aff_point (P.point_eval h1 out) ==
S.to_aff_point (S.point_mul (BSeq.nat_from_bytes_be (as_seq h0 scalar)) (P.point_eval h0 p)))
let point_mul scalar p out =
push_frame ();
let scalar_q = Q.create_qelem () in
Q.load_qelem scalar_q scalar;
PM.point_mul out scalar_q p;
pop_frame ()
[@@ Comment "Convert a point from projective coordinates to its raw form.
The argument `p` points to a point of 15 limbs in size, i.e., uint64_t[15].
The outparam `out` points to 64 bytes of valid memory, i.e., uint8_t[64].
The function first converts a given point `p` from projective to affine coordinates
and then writes [ `x`; `y` ] in `out`.
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `p` and `out` are disjoint."]
val point_store: p:P.point -> out:lbuffer uint8 64ul -> Stack unit
(requires fun h ->
live h out /\ live h p /\ disjoint p out /\
P.point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == S.point_store (P.point_eval h0 p))
let point_store p out =
P.point_store out p
[@@ Comment "Convert a point to projective coordinates from its raw form.
The argument `b` points to 64 bytes of valid memory, i.e., uint8_t[64].
The outparam `out` points to a point of 15 limbs in size, i.e., uint64_t[15].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` is valid point, i.e., x < prime and y < prime and (x, y) is on the curve
• `b` and `out` are disjoint."]
val point_load: b:lbuffer uint8 64ul -> out:P.point -> Stack unit
(requires fun h ->
live h out /\ live h b /\ disjoint b out /\
S.point_inv_bytes (as_seq h b))
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
P.point_inv h1 out /\
P.point_eval h1 out == S.load_point_nocheck (as_seq h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: Lib.Buffer.lbuffer Lib.IntTypes.uint8 64ul -> out: Hacl.Impl.K256.Point.point
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"FStar.UInt32.__uint_to_t",
"Hacl.Impl.K256.Point.point",
"Hacl.Impl.K256.Point.load_point_nocheck",
"Prims.unit"
] | [] | false | true | false | false | false | let point_load b out =
| P.load_point_nocheck out b | false |
Hacl.EC.K256.fst | Hacl.EC.K256.felem_sub | val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b)) | val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b)) | let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 27,
"end_line": 111,
"start_col": 0,
"start_line": 104
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.K256.Field.felem -> b: Hacl.K256.Field.felem -> out: Hacl.K256.Field.felem
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.K256.Field.felem",
"Hacl.K256.Field.fnormalize_weak",
"Prims.unit",
"Hacl.Spec.K256.Field52.Lemmas.normalize_weak5_lemma",
"FStar.Pervasives.Native.Mktuple5",
"Prims.nat",
"Hacl.K256.Field.as_felem5",
"Prims._assert",
"Hacl.Spec.K256.Field52.Definitions.felem_fits5",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.K256.Field.fsub",
"Lib.IntTypes.u64",
"Hacl.Spec.K256.Field52.Lemmas.fsub5_lemma"
] | [] | false | true | false | false | false | let felem_sub a b out =
| let h0 = ST.get () in
BL.fsub5_lemma (1, 1, 1, 1, 2) (1, 1, 1, 1, 2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5, 5, 5, 5, 6));
BL.normalize_weak5_lemma (5, 5, 5, 5, 6) (F.as_felem5 h1 out);
F.fnormalize_weak out out | false |
Hacl.EC.K256.fst | Hacl.EC.K256.felem_sqr | val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a)) | val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a)) | let felem_sqr a out =
F.fsqr out a | {
"file_name": "code/k256/Hacl.EC.K256.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 150,
"start_col": 0,
"start_line": 149
} | module Hacl.EC.K256
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module BSeq = Lib.ByteSequence
module S = Spec.K256
module F = Hacl.K256.Field
module Q = Hacl.K256.Scalar
module FI = Hacl.Impl.K256.Finv
module P = Hacl.Impl.K256.Point
module PA = Hacl.Impl.K256.PointAdd
module PD = Hacl.Impl.K256.PointDouble
module PM = Hacl.Impl.K256.PointMul
module BL = Hacl.Spec.K256.Field52.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^256 - 0x1000003D1.
This is a 64-bit optimized version, where a field element in radix-2^{52} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.zero)
let mk_felem_zero f =
Math.Lemmas.small_mod S.zero S.prime;
F.set_zero f
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: f:F.felem -> Stack unit
(requires fun h -> live h f)
(ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\
F.inv_lazy_reduced2 h1 f /\ F.feval h1 f == S.one)
let mk_felem_one f =
Math.Lemmas.small_mod S.one S.prime;
F.set_one f
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fadd (F.feval h0 a) (F.feval h0 b))
let felem_add a b out =
let h0 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b);
F.fadd out a b;
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (2,2,2,2,4));
BL.normalize_weak5_lemma (2,2,2,2,4) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fsub (F.feval h0 a) (F.feval h0 b))
let felem_sub a b out =
let h0 = ST.get () in
BL.fsub5_lemma (1,1,1,1,2) (1,1,1,1,2) (F.as_felem5 h0 a) (F.as_felem5 h0 b) (u64 2);
F.fsub out a b (u64 2);
let h1 = ST.get () in
assert (F.felem_fits5 (F.as_felem5 h1 out) (5,5,5,5,6));
BL.normalize_weak5_lemma (5,5,5,5,6) (F.as_felem5 h1 out);
F.fnormalize_weak out out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul (a b out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
eq_or_disjoint out a /\ eq_or_disjoint out b /\ eq_or_disjoint a b /\
F.inv_lazy_reduced2 h a /\ F.inv_lazy_reduced2 h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 b))
let felem_mul a b out =
F.fmul out a b
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr (a out:F.felem) : Stack unit
(requires fun h ->
live h a /\ live h out /\ eq_or_disjoint out a /\
F.inv_lazy_reduced2 h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F.inv_lazy_reduced2 h1 out /\
F.feval h1 out == S.fmul (F.feval h0 a) (F.feval h0 a)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Scalar.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.PointMul.fsti.checked",
"Hacl.Impl.K256.PointDouble.fst.checked",
"Hacl.Impl.K256.PointAdd.fst.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.Finv.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.K256.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointMul",
"short_module": "PM"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointDouble",
"short_module": "PD"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.PointAdd",
"short_module": "PA"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Point",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.K256.Finv",
"short_module": "FI"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Scalar",
"short_module": "Q"
},
{
"abbrev": true,
"full_module": "Hacl.K256.Field",
"short_module": "F"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.K256.Field.felem -> out: Hacl.K256.Field.felem -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.K256.Field.felem",
"Hacl.K256.Field.fsqr",
"Prims.unit"
] | [] | false | true | false | false | false | let felem_sqr a out =
| F.fsqr out a | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.