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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.strict_suffix_of_or_eq_nil | val strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma (ensures (strict_suffix_of [] l \/ l == [])) | val strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma (ensures (strict_suffix_of [] l \/ l == [])) | let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 38,
"end_line": 1041,
"start_col": 0,
"start_line": 1036
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 FStar.List.Tot.Base.strict_suffix_of [] l \/ l == []) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.strict_suffix_of_nil",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_or",
"FStar.List.Tot.Base.strict_suffix_of",
"Prims.Nil",
"Prims.eq2",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma (ensures (strict_suffix_of [] l \/ l == [])) =
| match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.fold_left_map | val fold_left_map
(#a #b #c: Type)
(f_aba: (a -> b -> Tot a))
(f_bc: (b -> Tot c))
(f_aca: (a -> c -> Tot a))
(l: list b)
: Lemma (requires forall (x: a) (y: b). f_aba x y == f_aca x (f_bc y))
(ensures forall (x: a). fold_left f_aba x l == fold_left f_aca x (map f_bc l)) | val fold_left_map
(#a #b #c: Type)
(f_aba: (a -> b -> Tot a))
(f_bc: (b -> Tot c))
(f_aca: (a -> c -> Tot a))
(l: list b)
: Lemma (requires forall (x: a) (y: b). f_aba x y == f_aca x (f_bc y))
(ensures forall (x: a). fold_left f_aba x l == fold_left f_aca x (map f_bc l)) | let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 46,
"end_line": 940,
"start_col": 0,
"start_line": 928
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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_aba: (_: a -> _: b -> a) -> f_bc: (_: b -> c) -> f_aca: (_: a -> _: c -> a) -> l: Prims.list b
-> FStar.Pervasives.Lemma (requires forall (x: a) (y: b). f_aba x y == f_aca x (f_bc y))
(ensures
forall (x: a).
FStar.List.Tot.Base.fold_left f_aba x l ==
FStar.List.Tot.Base.fold_left f_aca x (FStar.List.Tot.Base.map f_bc l)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.fold_left_map",
"Prims.unit",
"Prims.l_Forall",
"Prims.eq2",
"Prims.squash",
"FStar.List.Tot.Base.fold_left",
"FStar.List.Tot.Base.map",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec fold_left_map
(#a #b #c: Type)
(f_aba: (a -> b -> Tot a))
(f_bc: (b -> Tot c))
(f_aca: (a -> c -> Tot a))
(l: list b)
: Lemma (requires forall (x: a) (y: b). f_aba x y == f_aca x (f_bc y))
(ensures forall (x: a). fold_left f_aba x l == fold_left f_aca x (map f_bc l)) =
| match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.strict_suffix_of_correct | val strict_suffix_of_correct (#a: _) (l1 l2: list a)
: Lemma (requires True) (ensures (strict_suffix_of l1 l2 ==> l1 << l2)) (decreases l2) | val strict_suffix_of_correct (#a: _) (l1 l2: list a)
: Lemma (requires True) (ensures (strict_suffix_of l1 l2 ==> l1 << l2)) (decreases l2) | let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 1066,
"start_col": 0,
"start_line": 1058
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.strict_suffix_of l1 l2 ==> l1 << l2)
(decreases l2) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.strict_suffix_of_correct",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"FStar.List.Tot.Base.strict_suffix_of",
"Prims.precedes",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec strict_suffix_of_correct #a (l1: list a) (l2: list a)
: Lemma (requires True) (ensures (strict_suffix_of l1 l2 ==> l1 << l2)) (decreases l2) =
| match l2 with
| [] -> ()
| _ :: q -> strict_suffix_of_correct l1 q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.mem_strict_suffix_of | val mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma (requires True) (ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2)) | val mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma (requires True) (ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2)) | let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 1085,
"start_col": 0,
"start_line": 1078
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 | l1: Prims.list a -> m: a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.mem m l1 /\ FStar.List.Tot.Base.strict_suffix_of l1 l2 ==>
FStar.List.Tot.Base.mem m l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.mem_strict_suffix_of",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"Prims.l_and",
"Prims.b2t",
"FStar.List.Tot.Base.mem",
"FStar.List.Tot.Base.strict_suffix_of",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma (requires True) (ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2)) =
| match l2 with
| [] -> ()
| a :: q -> mem_strict_suffix_of l1 m q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.strict_suffix_of_or_eq_exists_append | val strict_suffix_of_or_eq_exists_append (#a: Type) (l1 l2: list a)
: Lemma (ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3. l2 == append l3 l1))) | val strict_suffix_of_or_eq_exists_append (#a: Type) (l1 l2: list a)
: Lemma (ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3. l2 == append l3 l1))) | let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] ) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 14,
"end_line": 1125,
"start_col": 0,
"start_line": 1111
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.strict_suffix_of l1 l2 \/ l1 == l2 ==>
(exists (l3: Prims.list a). l2 == l3 @ l1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.Classical.or_elim",
"FStar.List.Tot.Base.strict_suffix_of",
"Prims.eq2",
"Prims.squash",
"Prims.l_or",
"Prims.l_Exists",
"FStar.List.Tot.Base.append",
"FStar.List.Tot.Properties.strict_suffix_of_exists_append",
"Prims.unit",
"FStar.Classical.exists_intro",
"Prims.Nil",
"Prims.l_True",
"Prims.l_imp",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let strict_suffix_of_or_eq_exists_append (#a: Type) (l1 l2: list a)
: Lemma (ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3. l2 == append l3 l1))) =
| FStar.Classical.or_elim #(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3. l2 == append l3 l1)
(fun _ -> strict_suffix_of_exists_append l1 l2)
(fun _ -> FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) []) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.fold_left_append | val fold_left_append (#a #b: Type) (f: (a -> b -> Tot a)) (l1 l2: list b)
: Lemma (ensures forall x. fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2) | val fold_left_append (#a #b: Type) (f: (a -> b -> Tot a)) (l1 l2: list b)
: Lemma (ensures forall x. fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2) | let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 962,
"start_col": 0,
"start_line": 954
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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: (_: a -> _: b -> a) -> l1: Prims.list b -> l2: Prims.list b
-> FStar.Pervasives.Lemma
(ensures
forall (x: a).
FStar.List.Tot.Base.fold_left f x (l1 @ l2) ==
FStar.List.Tot.Base.fold_left f (FStar.List.Tot.Base.fold_left f x l1) l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.fold_left_append",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_Forall",
"Prims.eq2",
"FStar.List.Tot.Base.fold_left",
"FStar.List.Tot.Base.op_At",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec fold_left_append (#a #b: Type) (f: (a -> b -> Tot a)) (l1 l2: list b)
: Lemma (ensures forall x. fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2) =
| match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.strict_suffix_of_trans | val strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma (requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)] | val strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma (requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)] | let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 44,
"end_line": 1056,
"start_col": 0,
"start_line": 1048
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 | l1: Prims.list a -> l2: Prims.list a -> l3: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.strict_suffix_of l1 l2 /\ FStar.List.Tot.Base.strict_suffix_of l2 l3 ==>
FStar.List.Tot.Base.strict_suffix_of l1 l3)
(decreases l3)
[
SMTPat (FStar.List.Tot.Base.strict_suffix_of l1 l2);
SMTPat (FStar.List.Tot.Base.strict_suffix_of l2 l3)
] | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.strict_suffix_of_trans",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"Prims.l_and",
"FStar.List.Tot.Base.strict_suffix_of",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [
"recursion"
] | false | false | true | false | false | let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma (requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)] =
| match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.map_strict_suffix_of | val map_strict_suffix_of (#a #b: Type) (f: (a -> Tot b)) (l1 l2: list a)
: Lemma (requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2) | val map_strict_suffix_of (#a #b: Type) (f: (a -> Tot b)) (l1 l2: list a)
: Lemma (requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2) | let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 1076,
"start_col": 0,
"start_line": 1068
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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: (_: a -> b) -> l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.strict_suffix_of l1 l2 ==>
FStar.List.Tot.Base.strict_suffix_of (FStar.List.Tot.Base.map f l1)
(FStar.List.Tot.Base.map f l2)) (decreases l2) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.map_strict_suffix_of",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"FStar.List.Tot.Base.strict_suffix_of",
"FStar.List.Tot.Base.map",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec map_strict_suffix_of (#a #b: Type) (f: (a -> Tot b)) (l1 l2: list a)
: Lemma (requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2) =
| match l2 with
| [] -> ()
| a :: q -> map_strict_suffix_of f l1 q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.index_extensionality_aux | val index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit{length l1 == length l2}))
(l_index: (i: (i: nat{i < length l1}) -> Tot (l_index: unit{index l1 i == index l2 i})))
: Lemma (ensures (l1 == l2)) | val index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit{length l1 == length l2}))
(l_index: (i: (i: nat{i < length l1}) -> Tot (l_index: unit{index l1 i == index l2 i})))
: Lemma (ensures (l1 == l2)) | let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> () | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 11,
"end_line": 1013,
"start_col": 8,
"start_line": 998
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 |
l1: Prims.list a ->
l2: Prims.list a ->
l_len: Prims.unit{FStar.List.Tot.Base.length l1 == FStar.List.Tot.Base.length l2} ->
l_index:
(i: Prims.nat{i < FStar.List.Tot.Base.length l1}
-> l_index: Prims.unit{FStar.List.Tot.Base.index l1 i == FStar.List.Tot.Base.index l2 i})
-> FStar.Pervasives.Lemma (ensures l1 == l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Prims.unit",
"Prims.eq2",
"Prims.nat",
"FStar.List.Tot.Base.length",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.List.Tot.Base.index",
"FStar.Pervasives.Native.Mktuple2",
"FStar.List.Tot.Properties.index_extensionality_aux",
"Prims.op_Addition",
"FStar.Pervasives.Native.tuple2",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit{length l1 == length l2}))
(l_index: (i: (i: nat{i < length l1}) -> Tot (l_index: unit{index l1 i == index l2 i})))
: Lemma (ensures (l1 == l2)) =
| match (l1, l2) with
| a1 :: q1, a2 :: q2 ->
let a_eq:(a_eq: unit{a1 == a2}) = l_index 0 in
let q_len:(q_len: unit{length q1 == length q2}) = () in
let q_index (i: (i: nat{i < length q1})) : Tot (q_index: unit{index q1 i == index q2 i}) =
l_index (i + 1)
in
let q_eq:(q_eq: unit{l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> () | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.output_len | val output_len : a: Hacl.Streaming.Blake2.Common.alg -> FStar.UInt32.t | let output_len (a : alg) = U32.uint_to_t (output_size a) | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 56,
"end_line": 241,
"start_col": 0,
"start_line": 241
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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.Streaming.Blake2.Common.alg -> FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"FStar.UInt32.uint_to_t",
"Hacl.Streaming.Blake2.Common.output_size",
"FStar.UInt32.t"
] | [] | false | false | false | true | false | let output_len (a: alg) =
| U32.uint_to_t (output_size a) | false |
|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.stateful_key | val stateful_key (a: alg) (kk: key_size a) : I.stateful unit | val stateful_key (a: alg) (kk: key_size a) : I.stateful unit | let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ()) | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 206,
"start_col": 0,
"start_line": 168
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed). | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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.Streaming.Blake2.Common.alg -> kk: Hacl.Streaming.Blake2.Common.key_size a
-> Hacl.Streaming.Interface.stateful Prims.unit | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.key_size",
"Hacl.Streaming.Interface.Stateful",
"Prims.unit",
"Hacl.Streaming.Blake2.Common.stateful_key_t",
"FStar.Monotonic.HyperStack.mem",
"Prims.op_Equality",
"Prims.int",
"LowStar.Monotonic.Buffer.loc_none",
"Prims.bool",
"LowStar.Monotonic.Buffer.loc_addr_of_buffer",
"Hacl.Streaming.Blake2.Common.uint8",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.buffer",
"LowStar.Monotonic.Buffer.loc",
"Prims.l_True",
"LowStar.Monotonic.Buffer.freeable",
"LowStar.Monotonic.Buffer.live",
"FStar.Seq.Base.seq",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"FStar.Seq.Base.empty",
"LowStar.Monotonic.Buffer.as_seq",
"Prims.op_GreaterThan",
"Hacl.Streaming.Blake2.Common.buffer_to_stateful_key_t",
"LowStar.Monotonic.Buffer.length",
"LowStar.Buffer.alloca",
"Lib.IntTypes.u8",
"FStar.UInt32.uint_to_t",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.l_and",
"FStar.UInt32.v",
"Prims.b2t",
"Prims.op_Negation",
"LowStar.Monotonic.Buffer.g_is_null",
"Hacl.Streaming.Blake2.Common.unit_to_stateful_key_t",
"FStar.Monotonic.HyperHeap.rid",
"LowStar.Buffer.malloc",
"LowStar.Monotonic.Buffer.frameOf",
"FStar.Ghost.erased",
"LowStar.Monotonic.Buffer.free",
"LowStar.Monotonic.Buffer.blit",
"FStar.UInt32.__uint_to_t",
"Hacl.Streaming.Interface.stateful"
] | [] | false | false | false | false | false | let stateful_key (a: alg) (kk: key_size a) : I.stateful unit =
| I.Stateful (fun _ -> stateful_key_t a kk)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(fun #_ h s -> if kk = 0 then True else B.live h (s <: B.buffer uint8))
(fun _ -> s: S.seq uint8 {S.length s == kk})
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ())
(fun #_ l s h0 h1 -> ())
(fun #_ l s h0 h1 -> ())
(fun () ->
if kk > 0
then buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(fun () r ->
if kk > 0
then buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8))
(fun _ s_src s_dst ->
if kk > 0
then B.blit (s_src <: B.buffer uint8) 0ul (s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)) | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.empty_key | val empty_key : a: Hacl.Streaming.Blake2.Common.alg -> Type0 | let empty_key a = I.optional_key () I.Erased (stateful_key a 0) | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 63,
"end_line": 617,
"start_col": 0,
"start_line": 617
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128)
inline_for_extraction noextract
let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x
/// Specs
/// -----
noextract
let init_s (a : alg) (kk : size_nat{kk <= max_key a}) :
Tot (t a) =
Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a)
noextract
let update_multi_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 }) :
Tot (t a)
=
let nb = S.length input / U32.v (block_len a) in
Lib.LoopCombinators.repeati nb (Spec.blake2_update1 a prevlen input) acc
noextract
let update_last_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ S.length input + prevlen <= max_input_length a /\
S.length input <= Spec.size_block a }) :
Tot (t a) =
Spec.blake2_update_last a prevlen (S.length input) input acc
noextract
let finish_s (#a : alg) (acc : t a) :
output : S.seq uint8 { S.length output = U32.v (output_len a) } =
Spec.blake2_finish a acc (U32.v (output_len a))
noextract
let spec_s (a : alg)
(kk : size_nat{kk <= max_key a})
(key : lbytes kk)
(input : S.seq uint8{if kk = 0 then S.length input <= max_input_length a else S.length input + Spec.size_block a <= max_input_length a}) =
Spec.blake2 a input (Spec.blake2_default_params a) kk key (output_size a)
/// Interlude for spec proofs
/// -------------------------
val update_multi_zero:
#a : alg ->
acc:t a ->
prevlen:nat{prevlen % Spec.size_block a = 0} ->
Lemma
(requires (prevlen <= max_input_length a))
(ensures (update_multi_s #a acc prevlen S.empty == acc))
let update_multi_zero #a acc prevlen =
Lib.LoopCombinators.eq_repeati0 (0 / U32.v (block_len a)) (Spec.blake2_update1 a prevlen S.empty) acc
#push-options "--z3cliopt smt.arith.nl=false"
val update_multi_associative:
#a : alg ->
acc: t a ->
prevlen1:nat ->
prevlen2:nat ->
input1:S.seq uint8 ->
input2:S.seq uint8 ->
Lemma
(requires (
(**) Math.Lemmas.pos_times_pos_is_pos Spec.size_block_w (Spec.size_word a);
prevlen1 % Spec.size_block a = 0 /\
S.length input1 % Spec.size_block a = 0 /\
S.length input2 % Spec.size_block a = 0 /\
prevlen1 + S.length input1 + S.length input2 <= max_input_length a /\
prevlen2 = prevlen1 + S.length input1))
(ensures (
let input = S.append input1 input2 in
S.length input % Spec.size_block a = 0 /\
prevlen2 % Spec.size_block a = 0 /\
update_multi_s (update_multi_s acc prevlen1 input1) prevlen2 input2 ==
update_multi_s acc prevlen1 input))
#pop-options
#push-options "--z3rlimit 400"
let update_multi_associative #a acc prevlen1 prevlen2 input1 input2 =
let input = S.append input1 input2 in
let nb = S.length input / U32.v (block_len a) in
let nb1 = S.length input1 / U32.v (block_len a) in
let nb2 = S.length input2 / U32.v (block_len a) in
let f = Spec.blake2_update1 a prevlen1 input in
let f1 = Spec.blake2_update1 a prevlen1 input1 in
let f2 = Spec.blake2_update1 a prevlen2 input2 in
let aux1 (i:nat{i < nb1}) (acc:t a) : Lemma (f i acc == f1 i acc)
= assert (Spec.get_blocki a input i `Seq.equal` Spec.get_blocki a input1 i)
in
let aux2 (i:nat{i < nb2}) (acc:t a) : Lemma (f2 i acc == f (i + nb1) acc)
= assert (Spec.get_blocki a input2 i `Seq.equal` Spec.get_blocki a input (i + nb1))
in
let open Lib.LoopCombinators in
let open Lib.Sequence.Lemmas in
calc (==) {
update_multi_s (update_multi_s acc prevlen1 input1) prevlen2 input2;
(==) { }
repeati nb2 f2 (repeati nb1 f1 acc);
(==) {
Classical.forall_intro_2 aux1;
repeati_extensionality nb1 f1 f acc
}
repeati nb2 f2 (repeati nb1 f acc);
(==) {
repeati_def nb1 f acc;
repeati_def nb2 f2 (repeat_right 0 nb1 (fixed_a (t a)) f acc)
}
repeat_right 0 nb2 (fixed_a (t a)) f2 (repeat_right 0 nb1 (fixed_a (t a)) f acc);
(==) {
Classical.forall_intro_2 aux2;
repeat_gen_right_extensionality nb2 nb1 (fixed_a (t a)) (fixed_a (t a)) f2 f (repeat_right 0 nb1 (fixed_a (t a)) f acc)
}
repeat_right nb1 (nb1 + nb2) (fixed_a (t a)) f (repeat_right 0 nb1 (fixed_a (t a)) f acc);
(==) { repeat_right_plus 0 nb1 nb (fixed_a (t a)) f acc; repeati_def nb f acc }
repeati nb f acc;
(==) { }
update_multi_s acc prevlen1 input;
}
#pop-options
/// A helper function: the hash incremental function defined with the functions
/// locally defined (with a signature adapted to the functor).
noextract
val blake2_hash_incremental_s :
a : alg ->
kk: size_nat{kk <= max_key a} ->
k: lbytes kk ->
input:S.seq uint8 { if kk = 0 then S.length input <= max_input_length a else S.length input + (Spec.size_block a) <= max_input_length a } ->
output:S.seq uint8 { S.length output = output_size a }
#push-options "--z3cliopt smt.arith.nl=false"
let blake2_hash_incremental_s a kk k input0 =
let key_block = if kk > 0 then Spec.blake2_key_block a kk k else S.empty in
let key_block_len = S.length key_block in
let input = Seq.append key_block input0 in
assert (key_block_len = (if kk = 0 then 0 else Spec.size_block a));
(**) Math.Lemmas.modulo_lemma 0 (U32.v (block_len a));
let bs, l = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
let acc1 = init_s a kk in
let acc2 = update_multi_s #a acc1 0 bs in
let acc3 = update_last_s #a acc2 (S.length bs) l in
let acc4 = finish_s #a acc3 in
acc4
#pop-options
#push-options "--z3cliopt smt.arith.nl=false"
val repeati_split_at_eq :
a : alg ->
s : t a ->
input:S.seq uint8 { S.length input <= max_input_length a } ->
Lemma(
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
n_blocks = Lib.Sequence.length blocks / Spec.size_block a /\ // This is necessary for type-checking
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 input) s ==
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 blocks) s)
#pop-options
#push-options "--z3cliopt smt.arith.nl=false"
let repeati_split_at_eq a s input =
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
let f = Spec.blake2_update1 a 0 input in
let g = Spec.blake2_update1 a 0 blocks in
let s1 = Lib.LoopCombinators.repeati n_blocks f s in
assert (Lib.Sequence.length blocks = n_blocks * Spec.size_block a);
Math.Lemmas.cancel_mul_div n_blocks (Spec.size_block a);
assert (n_blocks = Lib.Sequence.length blocks / Spec.size_block a);
assert (Lib.Sequence.length blocks <= max_input_length a);
let s2 = Lib.LoopCombinators.repeati n_blocks g s in
assert (input `Seq.equal` Seq.append blocks last);
assert (S.length input = S.length blocks + S.length last);
introduce forall (i:nat{i < n_blocks}). (Spec.get_blocki a input i) `S.equal` (Spec.get_blocki a blocks i)
with
begin
let b0 = Spec.get_blocki a input i in
let b1 = Spec.get_blocki a blocks i in
assert (S.length blocks = n_blocks * Spec.size_block a);
Math.Lemmas.lemma_mult_le_right (Spec.size_block a) (i + 1) n_blocks;
assert ((i + 1) * Spec.size_block a <= S.length blocks);
Math.Lemmas.lemma_mult_le_right (Spec.size_block a) i n_blocks;
assert (i * Spec.size_block a <= S.length blocks);
Math.Lemmas.distributivity_add_left i 1 (Spec.size_block a);
assert ((i + 1) * Spec.size_block a = i * Spec.size_block a + Spec.size_block a);
introduce forall (j: nat{j < Spec.size_block a}). S.index b0 j == S.index b1 j
with
begin
assert (i * Spec.size_block a + j < i * Spec.size_block a + Spec.size_block a);
Math.Lemmas.nat_times_nat_is_nat i (Spec.size_block a);
S.lemma_index_slice input (i * Spec.size_block a) ((i + 1) * Spec.size_block a) j;
assert (S.index b0 j == S.index input (j + (i * Spec.size_block a)))
end
end;
assert (forall (i:nat{i < n_blocks}) acc. f i acc == g i acc);
Lib.Sequence.Lemmas.repeati_extensionality n_blocks f g s
#pop-options
val spec_is_incremental :
a : alg ->
kk: size_nat{kk <= max_key a} ->
k: lbytes kk ->
input:S.seq uint8 { if kk = 0 then S.length input <= max_input_length a else S.length input + (Spec.size_block a) <= max_input_length a } ->
Lemma(
blake2_hash_incremental_s a kk k input ==
Spec.blake2 a input (Spec.blake2_default_params a) kk k (output_size a))
#restart-solver
#push-options "--z3cliopt smt.arith.nl=false"
let spec_is_incremental a kk k input0 =
let key_block = if kk > 0 then Spec.blake2_key_block a kk k else S.empty in
let key_block_len = S.length key_block in
let input = Seq.append key_block input0 in
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
let s = init_s a kk in
repeati_split_at_eq a s input;
let f = Spec.blake2_update1 a 0 input in
let g = Spec.blake2_update1 a 0 blocks in
let s1 = Lib.LoopCombinators.repeati n_blocks f s in
let s2 = Lib.LoopCombinators.repeati n_blocks g s in
assert (s1 == s2);
S.lemma_eq_intro (S.slice input (S.length input - l_last) (S.length input)) last;
S.lemma_eq_intro (S.slice last (S.length last - l_last) (S.length last)) last;
Spec.Blake2.Alternative.lemma_spec_equivalence_update a kk k input0 s;
assert (U32.v (output_len a) = output_size a)
#pop-options
inline_for_extraction noextract
val init_key_block (a : alg) (kk : key_size a) (k : stateful_key_t a kk)
(buf_: B.buffer uint8 { B.length buf_ = Spec.size_block a }) :
ST.Stack unit
(requires fun h0 ->
let key = stateful_key a kk in
key.invariant h0 k /\
B.live h0 buf_ /\
B.(loc_disjoint (loc_buffer buf_) (key.footprint h0 k)))
(ensures fun h0 _ h1 ->
B.(modifies (loc_buffer buf_) h0 h1) /\
begin
let k = (stateful_key a kk).v () h0 k in
let input_length = if kk > 0 then Spec.size_block a else 0 in
let input = if kk > 0 then Spec.blake2_key_block a kk k else S.empty in
S.equal (S.slice (B.as_seq h1 buf_) 0 input_length) input
end)
let init_key_block a kk k buf_ =
if kk = 0 then ()
else
begin
(**) let h0 = ST.get () in
(* Set the end of the buffer to 0 *)
[@inline_let] let sub_b_len = U32.(block_len a -^ U32.uint_to_t kk) in
let sub_b = B.sub buf_ (U32.uint_to_t kk) sub_b_len in
B.fill sub_b (Lib.IntTypes.u8 0) sub_b_len;
(**) let h1 = ST.get () in
(**) assert(S.slice (B.as_seq h1 buf_) kk (Spec.size_block a) `S.equal` B.as_seq h1 sub_b);
(* Copy the key at the beginning of the buffer *)
Lib.Buffer.update_sub #Lib.Buffer.MUT #uint8 #(U32.uint_to_t (Spec.size_block a))
buf_ 0ul (U32.uint_to_t kk) (stateful_key_to_buffer k);
(**) let h2 = ST.get () in
(**) begin
(**) let k : LS.lseq uint8 kk = (stateful_key a kk).v () h0 k in
(**) let buf_v1 : LS.lseq uint8 (Spec.size_block a) = B.as_seq h1 buf_ in
(**) let buf_v2 : LS.lseq uint8 (Spec.size_block a) = B.as_seq h2 buf_ in
(* Prove that [buf_] is equal to [key @ create ... 0] *)
(**) assert(buf_v2 `S.equal` LS.update_sub buf_v1 0 kk k);
(**) let zeroed : LS.lseq uint8 (Spec.size_block a - kk) = S.create (Spec.size_block a - kk) (Lib.IntTypes.u8 0) in
(**) assert(B.as_seq h1 sub_b `S.equal` zeroed);
(**) let key_and_zeroed : LS.lseq uint8 (Spec.size_block a) = Seq.append k zeroed in
(**) assert(S.equal (S.slice key_and_zeroed 0 kk) k);
(**) assert(S.equal (S.slice key_and_zeroed kk (Spec.size_block a)) zeroed);
(**) LS.lemma_update_sub #uint8 #(Spec.size_block a) buf_v1 0 kk k key_and_zeroed;
(**) assert(buf_v2 `S.equal` key_and_zeroed);
(* Prove that the initial input is equal to [key @ create ... 0] *)
(**) let input = Spec.blake2_key_block a kk k in
(**) let key_block0: LS.lseq uint8 (Spec.size_block a) = S.create (Spec.size_block a) (Lib.IntTypes.u8 0) in
(**) assert(input `S.equal` LS.update_sub key_block0 0 kk k);
(**) assert(Seq.equal (LS.sub key_and_zeroed 0 kk) k);
(**) assert(Seq.equal (LS.sub key_and_zeroed kk (Spec.size_block a - kk))
(LS.sub key_block0 kk (Spec.size_block a - kk)));
(**) LS.lemma_update_sub #uint8 #(Spec.size_block a) key_block0 0 kk k key_and_zeroed;
(**) assert(input `S.equal` key_and_zeroed)
(**) end
end
/// Runtime
/// -------
#push-options "--ifuel 1"// --z3cliopt smt.arith.nl=false"
inline_for_extraction noextract
let blake2 (a : alg)
(m : valid_m_spec a)
(kk : key_size a)
(init : blake2_init_st a m)
(update_multi : blake2_update_multi_st a m)
(update_last : blake2_update_last_st a m)
(finish : blake2_finish_st a m) :
I.block unit =
I.Block
I.Erased (* key management *)
(stateful_blake2 a m) (* state *)
(stateful_key a kk) (* key *)
unit (* output_length_t *)
(fun () -> max_input_len a) (* max_input_length *)
(fun () () -> output_size a) (* output_len *)
(fun () -> block_len a) (* block_len *)
(fun () -> block_len a) (* blocks_state_len *)
(fun () -> if kk > 0 then block_len a else 0ul) (* init_input_len *)
(fun () k -> if kk > 0 then Spec.blake2_key_block a kk k else S.empty)
(fun () _k -> init_s a kk) (* init_s *)
(fun () acc prevlen input -> update_multi_s acc prevlen input) (* update_multi_s *)
(fun () acc prevlen input -> update_last_s acc prevlen input) (* update_last_s *)
(fun () _k acc _ -> finish_s #a acc) (* finish_s *)
(fun () k input l -> spec_s a kk k input) (* spec_s *)
(* update_multi_zero *)
(fun () acc prevlen -> update_multi_zero #a acc prevlen)
(* update_multi_associative *)
(fun () acc prevlen1 prevlen2 input1 input2 ->
update_multi_associative acc prevlen1 prevlen2 input1 input2)
(fun () k input _ ->
spec_is_incremental a kk k input) (* spec_is_incremental *)
(fun _ acc -> ()) (* index_of_state *)
(* init *)
(fun _ key buf_ acc ->
[@inline_let] let wv = get_wv acc in
[@inline_let] let h = get_state_p acc in
init_key_block a kk key buf_;
init h (Lib.IntTypes.size kk) (output_len a))
(* update_multi *)
(fun _ acc prevlen blocks len ->
let wv, hash = acc in
let nb = len `U32.div` Core.size_block a in
update_multi #len wv hash (blake2_prevlen a prevlen) blocks nb)
(* update_last *)
(fun _ acc prevlen last last_len ->
let wv, hash = acc in
update_last #last_len wv hash (blake2_prevlen a prevlen) last_len last)
(* finish *)
(fun _ k acc dst _ ->
[@inline_let] let wv = get_wv acc in
[@inline_let] let h = get_state_p acc in
finish (output_len a) dst h)
#pop-options | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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.Streaming.Blake2.Common.alg -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Interface.optional_key",
"Prims.unit",
"Hacl.Streaming.Interface.Erased",
"Hacl.Streaming.Blake2.Common.stateful_key"
] | [] | false | false | false | true | true | let empty_key a =
| I.optional_key () I.Erased (stateful_key a 0) | false |
|
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.strict_suffix_of_exists_append | val strict_suffix_of_exists_append (#a: Type) (l1 l2: list a)
: Lemma (ensures (strict_suffix_of l1 l2 ==> (exists l3. l2 == append l3 l1))) | val strict_suffix_of_exists_append (#a: Type) (l1 l2: list a)
: Lemma (ensures (strict_suffix_of l1 l2 ==> (exists l3. l2 == append l3 l1))) | let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
)) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 15,
"end_line": 1109,
"start_col": 0,
"start_line": 1087
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.strict_suffix_of l1 l2 ==> (exists (l3: Prims.list a). l2 == l3 @ l1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.Classical.or_elim",
"Prims.eq2",
"FStar.List.Tot.Base.strict_suffix_of",
"Prims.squash",
"Prims.l_or",
"Prims.l_Exists",
"FStar.List.Tot.Base.append",
"FStar.Classical.exists_intro",
"Prims.Cons",
"Prims.Nil",
"Prims.unit",
"FStar.Classical.exists_elim",
"FStar.List.Tot.Properties.strict_suffix_of_exists_append",
"Prims.l_True",
"Prims.l_imp",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec strict_suffix_of_exists_append (#a: Type) (l1 l2: list a)
: Lemma (ensures (strict_suffix_of l1 l2 ==> (exists l3. l2 == append l3 l1))) =
| match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim #(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3. l2 == append l3 l1)
(fun _ -> FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) ([a]))
(fun _ ->
FStar.Classical.exists_elim (exists l3. l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 -> FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3))) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.precedes_append_cons_prod_r | val precedes_append_cons_prod_r (#a #b: Type) (l1: list (a * b)) (x: a) (y: b) (l2: list (a * b))
: Lemma (ensures x << (append l1 ((x, y) :: l2)) /\ y << (append l1 ((x, y) :: l2))) | val precedes_append_cons_prod_r (#a #b: Type) (l1: list (a * b)) (x: a) (y: b) (l2: list (a * b))
: Lemma (ensures x << (append l1 ((x, y) :: l2)) /\ y << (append l1 ((x, y) :: l2))) | let precedes_append_cons_prod_r
(#a #b: Type)
(l1: list (a * b))
(x: a)
(y: b)
(l2: list (a * b))
: Lemma
(ensures
x << (append l1 ((x, y) :: l2)) /\
y << (append l1 ((x, y) :: l2)))
= precedes_append_cons_r l1 (x, y) l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 1158,
"start_col": 0,
"start_line": 1148
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
))
let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] )
(** Properties of << with lists *)
let precedes_tl
(#a: Type)
(l: list a {Cons? l})
: Lemma (ensures (tl l << l))
= ()
let rec precedes_append_cons_r
(#a: Type)
(l1: list a)
(x: a)
(l2: list a)
: Lemma
(requires True)
(ensures (x << append l1 (x :: l2)))
[SMTPat (x << append l1 (x :: l2))]
= match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 | l1: Prims.list (a * b) -> x: a -> y: b -> l2: Prims.list (a * b)
-> FStar.Pervasives.Lemma (ensures x << l1 @ (x, y) :: l2 /\ y << l1 @ (x, y) :: l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Properties.precedes_append_cons_r",
"FStar.Pervasives.Native.Mktuple2",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_and",
"Prims.precedes",
"FStar.List.Tot.Base.append",
"Prims.Cons",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let precedes_append_cons_prod_r (#a #b: Type) (l1: list (a * b)) (x: a) (y: b) (l2: list (a * b))
: Lemma (ensures x << (append l1 ((x, y) :: l2)) /\ y << (append l1 ((x, y) :: l2))) =
| precedes_append_cons_r l1 (x, y) l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.assoc_precedes | val assoc_precedes (#a: eqtype) (#b: Type) (x: a) (l: list (a * b)) (y: b)
: Lemma (requires (assoc x l == Some y)) (ensures (x << l /\ y << l)) | val assoc_precedes (#a: eqtype) (#b: Type) (x: a) (l: list (a * b)) (y: b)
: Lemma (requires (assoc x l == Some y)) (ensures (x << l /\ y << l)) | let assoc_precedes
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
(y: b)
: Lemma
(requires (assoc x l == Some y))
(ensures (x << l /\ y << l))
= assoc_memP_some x y l;
memP_precedes (x, y) l | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 24,
"end_line": 1188,
"start_col": 0,
"start_line": 1178
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
))
let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] )
(** Properties of << with lists *)
let precedes_tl
(#a: Type)
(l: list a {Cons? l})
: Lemma (ensures (tl l << l))
= ()
let rec precedes_append_cons_r
(#a: Type)
(l1: list a)
(x: a)
(l2: list a)
: Lemma
(requires True)
(ensures (x << append l1 (x :: l2)))
[SMTPat (x << append l1 (x :: l2))]
= match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2
let precedes_append_cons_prod_r
(#a #b: Type)
(l1: list (a * b))
(x: a)
(y: b)
(l2: list (a * b))
: Lemma
(ensures
x << (append l1 ((x, y) :: l2)) /\
y << (append l1 ((x, y) :: l2)))
= precedes_append_cons_r l1 (x, y) l2
let rec memP_precedes
(#a: Type)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> x << l))
(decreases l)
= match l with
| [] -> ()
| y :: q ->
FStar.Classical.or_elim
#(x == y)
#(memP x q)
#(fun _ -> x << l)
(fun _ -> ())
(fun _ -> memP_precedes x q) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 -> l: Prims.list (a * b) -> y: b
-> FStar.Pervasives.Lemma
(requires FStar.List.Tot.Base.assoc x l == FStar.Pervasives.Native.Some y)
(ensures x << l /\ y << l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Properties.memP_precedes",
"FStar.Pervasives.Native.Mktuple2",
"Prims.unit",
"FStar.List.Tot.Properties.assoc_memP_some",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.List.Tot.Base.assoc",
"FStar.Pervasives.Native.Some",
"Prims.squash",
"Prims.l_and",
"Prims.precedes",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let assoc_precedes (#a: eqtype) (#b: Type) (x: a) (l: list (a * b)) (y: b)
: Lemma (requires (assoc x l == Some y)) (ensures (x << l /\ y << l)) =
| assoc_memP_some x y l;
memP_precedes (x, y) l | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.precedes_append_cons_r | val precedes_append_cons_r (#a: Type) (l1: list a) (x: a) (l2: list a)
: Lemma (requires True) (ensures (x << append l1 (x :: l2))) [SMTPat (x << append l1 (x :: l2))] | val precedes_append_cons_r (#a: Type) (l1: list a) (x: a) (l2: list a)
: Lemma (requires True) (ensures (x << append l1 (x :: l2))) [SMTPat (x << append l1 (x :: l2))] | let rec precedes_append_cons_r
(#a: Type)
(l1: list a)
(x: a)
(l2: list a)
: Lemma
(requires True)
(ensures (x << append l1 (x :: l2)))
[SMTPat (x << append l1 (x :: l2))]
= match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 43,
"end_line": 1146,
"start_col": 0,
"start_line": 1135
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
))
let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] )
(** Properties of << with lists *)
let precedes_tl
(#a: Type)
(l: list a {Cons? l})
: Lemma (ensures (tl l << l))
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 | l1: Prims.list a -> x: a -> l2: Prims.list a
-> FStar.Pervasives.Lemma (ensures x << l1 @ x :: l2) [SMTPat (x << l1 @ x :: l2)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.precedes_append_cons_r",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.precedes",
"FStar.List.Tot.Base.append",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [
"recursion"
] | false | false | true | false | false | let rec precedes_append_cons_r (#a: Type) (l1: list a) (x: a) (l2: list a)
: Lemma (requires True) (ensures (x << append l1 (x :: l2))) [SMTPat (x << append l1 (x :: l2))] =
| match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.find_none | val find_none (#a: Type) (f: (a -> Tot bool)) (l: list a) (x: a)
: Lemma (requires (find f l == None /\ memP x l)) (ensures (f x == false)) | val find_none (#a: Type) (f: (a -> Tot bool)) (l: list a) (x: a)
: Lemma (requires (find f l == None /\ memP x l)) (ensures (f x == false)) | let rec find_none
(#a: Type)
(f: (a -> Tot bool))
(l: list a)
(x: a)
: Lemma
(requires (find f l == None /\ memP x l))
(ensures (f x == false))
= let (x' :: l') = l in
Classical.or_elim
#(x == x')
#(~ (x == x'))
#(fun _ -> f x == false)
(fun h -> ())
(fun h -> find_none f l' x) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 1206,
"start_col": 0,
"start_line": 1192
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
))
let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] )
(** Properties of << with lists *)
let precedes_tl
(#a: Type)
(l: list a {Cons? l})
: Lemma (ensures (tl l << l))
= ()
let rec precedes_append_cons_r
(#a: Type)
(l1: list a)
(x: a)
(l2: list a)
: Lemma
(requires True)
(ensures (x << append l1 (x :: l2)))
[SMTPat (x << append l1 (x :: l2))]
= match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2
let precedes_append_cons_prod_r
(#a #b: Type)
(l1: list (a * b))
(x: a)
(y: b)
(l2: list (a * b))
: Lemma
(ensures
x << (append l1 ((x, y) :: l2)) /\
y << (append l1 ((x, y) :: l2)))
= precedes_append_cons_r l1 (x, y) l2
let rec memP_precedes
(#a: Type)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> x << l))
(decreases l)
= match l with
| [] -> ()
| y :: q ->
FStar.Classical.or_elim
#(x == y)
#(memP x q)
#(fun _ -> x << l)
(fun _ -> ())
(fun _ -> memP_precedes x q)
let assoc_precedes
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
(y: b)
: Lemma
(requires (assoc x l == Some y))
(ensures (x << l /\ y << l))
= assoc_memP_some x y l;
memP_precedes (x, y) l
(** Properties about find *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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: (_: a -> Prims.bool) -> l: Prims.list a -> x: a
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.find f l == FStar.Pervasives.Native.None /\ FStar.List.Tot.Base.memP x l
) (ensures f x == false) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.bool",
"Prims.list",
"FStar.Classical.or_elim",
"Prims.eq2",
"Prims.l_not",
"Prims.squash",
"Prims.l_or",
"Prims.unit",
"FStar.List.Tot.Properties.find_none",
"Prims.l_and",
"FStar.Pervasives.Native.option",
"Prims.b2t",
"FStar.List.Tot.Base.find",
"FStar.Pervasives.Native.None",
"FStar.List.Tot.Base.memP",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec find_none (#a: Type) (f: (a -> Tot bool)) (l: list a) (x: a)
: Lemma (requires (find f l == None /\ memP x l)) (ensures (f x == false)) =
| let x' :: l' = l in
Classical.or_elim #(x == x')
#(~(x == x'))
#(fun _ -> f x == false)
(fun h -> ())
(fun h -> find_none f l' x) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.init_last_def | val init_last_def (#a: Type) (l: list a) (x: a)
: Lemma
(let l' = append l [x] in
init l' == l /\ last l' == x) | val init_last_def (#a: Type) (l: list a) (x: a)
: Lemma
(let l' = append l [x] in
init l' == l /\ last l' == x) | let rec init_last_def (#a: Type) (l: list a) (x: a) : Lemma
(let l' = append l [x] in
init l' == l /\ last l' == x)
= match l with
| [] -> ()
| y :: q -> init_last_def q x | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 1225,
"start_col": 0,
"start_line": 1220
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
))
let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] )
(** Properties of << with lists *)
let precedes_tl
(#a: Type)
(l: list a {Cons? l})
: Lemma (ensures (tl l << l))
= ()
let rec precedes_append_cons_r
(#a: Type)
(l1: list a)
(x: a)
(l2: list a)
: Lemma
(requires True)
(ensures (x << append l1 (x :: l2)))
[SMTPat (x << append l1 (x :: l2))]
= match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2
let precedes_append_cons_prod_r
(#a #b: Type)
(l1: list (a * b))
(x: a)
(y: b)
(l2: list (a * b))
: Lemma
(ensures
x << (append l1 ((x, y) :: l2)) /\
y << (append l1 ((x, y) :: l2)))
= precedes_append_cons_r l1 (x, y) l2
let rec memP_precedes
(#a: Type)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> x << l))
(decreases l)
= match l with
| [] -> ()
| y :: q ->
FStar.Classical.or_elim
#(x == y)
#(memP x q)
#(fun _ -> x << l)
(fun _ -> ())
(fun _ -> memP_precedes x q)
let assoc_precedes
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
(y: b)
: Lemma
(requires (assoc x l == Some y))
(ensures (x << l /\ y << l))
= assoc_memP_some x y l;
memP_precedes (x, y) l
(** Properties about find *)
let rec find_none
(#a: Type)
(f: (a -> Tot bool))
(l: list a)
(x: a)
: Lemma
(requires (find f l == None /\ memP x l))
(ensures (f x == false))
= let (x' :: l') = l in
Classical.or_elim
#(x == x')
#(~ (x == x'))
#(fun _ -> f x == false)
(fun h -> ())
(fun h -> find_none f l' x)
(** Properties of init and last *)
let rec append_init_last (#a: Type) (l: list a { Cons? l }) : Lemma
(l == append (init l) [last l])
= match l with
| a :: q ->
if Cons? q
then
append_init_last q
else
() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 -> x: a
-> FStar.Pervasives.Lemma
(ensures
(let l' = l @ [x] in
FStar.List.Tot.Base.init l' == l /\ FStar.List.Tot.Base.last l' == x)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.init_last_def",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_and",
"Prims.eq2",
"FStar.List.Tot.Base.init",
"FStar.List.Tot.Base.last",
"FStar.List.Tot.Base.append",
"Prims.Cons",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec init_last_def (#a: Type) (l: list a) (x: a)
: Lemma
(let l' = append l [x] in
init l' == l /\ last l' == x) =
| match l with
| [] -> ()
| y :: q -> init_last_def q x | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.init_last_inj | val init_last_inj (#a: Type) (l1: list a {Cons? l1}) (l2: list a {Cons? l2})
: Lemma (requires (init l1 == init l2 /\ last l1 == last l2)) (ensures (l1 == l2)) | val init_last_inj (#a: Type) (l1: list a {Cons? l1}) (l2: list a {Cons? l2})
: Lemma (requires (init l1 == init l2 /\ last l1 == last l2)) (ensures (l1 == l2)) | let init_last_inj (#a: Type) (l1: list a { Cons? l1 } ) (l2: list a { Cons? l2 } ) : Lemma
(requires (init l1 == init l2 /\ last l1 == last l2))
(ensures (l1 == l2))
= append_init_last l1;
append_init_last l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 21,
"end_line": 1231,
"start_col": 0,
"start_line": 1227
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
))
let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] )
(** Properties of << with lists *)
let precedes_tl
(#a: Type)
(l: list a {Cons? l})
: Lemma (ensures (tl l << l))
= ()
let rec precedes_append_cons_r
(#a: Type)
(l1: list a)
(x: a)
(l2: list a)
: Lemma
(requires True)
(ensures (x << append l1 (x :: l2)))
[SMTPat (x << append l1 (x :: l2))]
= match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2
let precedes_append_cons_prod_r
(#a #b: Type)
(l1: list (a * b))
(x: a)
(y: b)
(l2: list (a * b))
: Lemma
(ensures
x << (append l1 ((x, y) :: l2)) /\
y << (append l1 ((x, y) :: l2)))
= precedes_append_cons_r l1 (x, y) l2
let rec memP_precedes
(#a: Type)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> x << l))
(decreases l)
= match l with
| [] -> ()
| y :: q ->
FStar.Classical.or_elim
#(x == y)
#(memP x q)
#(fun _ -> x << l)
(fun _ -> ())
(fun _ -> memP_precedes x q)
let assoc_precedes
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
(y: b)
: Lemma
(requires (assoc x l == Some y))
(ensures (x << l /\ y << l))
= assoc_memP_some x y l;
memP_precedes (x, y) l
(** Properties about find *)
let rec find_none
(#a: Type)
(f: (a -> Tot bool))
(l: list a)
(x: a)
: Lemma
(requires (find f l == None /\ memP x l))
(ensures (f x == false))
= let (x' :: l') = l in
Classical.or_elim
#(x == x')
#(~ (x == x'))
#(fun _ -> f x == false)
(fun h -> ())
(fun h -> find_none f l' x)
(** Properties of init and last *)
let rec append_init_last (#a: Type) (l: list a { Cons? l }) : Lemma
(l == append (init l) [last l])
= match l with
| a :: q ->
if Cons? q
then
append_init_last q
else
()
let rec init_last_def (#a: Type) (l: list a) (x: a) : Lemma
(let l' = append l [x] in
init l' == l /\ last l' == x)
= match l with
| [] -> ()
| y :: q -> init_last_def q x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 | l1: Prims.list a {Cons? l1} -> l2: Prims.list a {Cons? l2}
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.init l1 == FStar.List.Tot.Base.init l2 /\
FStar.List.Tot.Base.last l1 == FStar.List.Tot.Base.last l2) (ensures l1 == l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Prims.b2t",
"Prims.uu___is_Cons",
"FStar.List.Tot.Properties.append_init_last",
"Prims.unit",
"Prims.l_and",
"Prims.eq2",
"FStar.List.Tot.Base.init",
"FStar.List.Tot.Base.last",
"Prims.squash",
"Prims.l_or",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let init_last_inj (#a: Type) (l1: list a {Cons? l1}) (l2: list a {Cons? l2})
: Lemma (requires (init l1 == init l2 /\ last l1 == last l2)) (ensures (l1 == l2)) =
| append_init_last l1;
append_init_last l2 | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.finish_s | val finish_s (#a: alg) (acc: t a) : output: S.seq uint8 {S.length output = U32.v (output_len a)} | val finish_s (#a: alg) (acc: t a) : output: S.seq uint8 {S.length output = U32.v (output_len a)} | let finish_s (#a : alg) (acc : t a) :
output : S.seq uint8 { S.length output = U32.v (output_len a) } =
Spec.blake2_finish a acc (U32.v (output_len a)) | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 49,
"end_line": 286,
"start_col": 0,
"start_line": 284
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128)
inline_for_extraction noextract
let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x
/// Specs
/// -----
noextract
let init_s (a : alg) (kk : size_nat{kk <= max_key a}) :
Tot (t a) =
Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a)
noextract
let update_multi_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 }) :
Tot (t a)
=
let nb = S.length input / U32.v (block_len a) in
Lib.LoopCombinators.repeati nb (Spec.blake2_update1 a prevlen input) acc
noextract
let update_last_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ S.length input + prevlen <= max_input_length a /\
S.length input <= Spec.size_block a }) :
Tot (t a) =
Spec.blake2_update_last a prevlen (S.length input) input acc | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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 | acc: Hacl.Streaming.Blake2.Common.t a
-> output:
FStar.Seq.Base.seq Hacl.Streaming.Blake2.Common.uint8
{FStar.Seq.Base.length output = FStar.UInt32.v (Hacl.Streaming.Blake2.Common.output_len a)} | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.t",
"Spec.Blake2.blake2_finish",
"FStar.UInt32.v",
"Hacl.Streaming.Blake2.Common.output_len",
"FStar.Seq.Base.seq",
"Hacl.Streaming.Blake2.Common.uint8",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.l_or",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt.size",
"FStar.UInt32.n",
"FStar.Seq.Base.length"
] | [] | false | false | false | false | false | let finish_s (#a: alg) (acc: t a) : output: S.seq uint8 {S.length output = U32.v (output_len a)} =
| Spec.blake2_finish a acc (U32.v (output_len a)) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_init_last | val append_init_last (#a: Type) (l: list a {Cons? l}) : Lemma (l == append (init l) [last l]) | val append_init_last (#a: Type) (l: list a {Cons? l}) : Lemma (l == append (init l) [last l]) | let rec append_init_last (#a: Type) (l: list a { Cons? l }) : Lemma
(l == append (init l) [last l])
= match l with
| a :: q ->
if Cons? q
then
append_init_last q
else
() | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 8,
"end_line": 1218,
"start_col": 0,
"start_line": 1210
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
))
let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] )
(** Properties of << with lists *)
let precedes_tl
(#a: Type)
(l: list a {Cons? l})
: Lemma (ensures (tl l << l))
= ()
let rec precedes_append_cons_r
(#a: Type)
(l1: list a)
(x: a)
(l2: list a)
: Lemma
(requires True)
(ensures (x << append l1 (x :: l2)))
[SMTPat (x << append l1 (x :: l2))]
= match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2
let precedes_append_cons_prod_r
(#a #b: Type)
(l1: list (a * b))
(x: a)
(y: b)
(l2: list (a * b))
: Lemma
(ensures
x << (append l1 ((x, y) :: l2)) /\
y << (append l1 ((x, y) :: l2)))
= precedes_append_cons_r l1 (x, y) l2
let rec memP_precedes
(#a: Type)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> x << l))
(decreases l)
= match l with
| [] -> ()
| y :: q ->
FStar.Classical.or_elim
#(x == y)
#(memP x q)
#(fun _ -> x << l)
(fun _ -> ())
(fun _ -> memP_precedes x q)
let assoc_precedes
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
(y: b)
: Lemma
(requires (assoc x l == Some y))
(ensures (x << l /\ y << l))
= assoc_memP_some x y l;
memP_precedes (x, y) l
(** Properties about find *)
let rec find_none
(#a: Type)
(f: (a -> Tot bool))
(l: list a)
(x: a)
: Lemma
(requires (find f l == None /\ memP x l))
(ensures (f x == false))
= let (x' :: l') = l in
Classical.or_elim
#(x == x')
#(~ (x == x'))
#(fun _ -> f x == false)
(fun h -> ())
(fun h -> find_none f l' x)
(** Properties of init and last *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 {Cons? l}
-> FStar.Pervasives.Lemma (ensures l == FStar.List.Tot.Base.init l @ [FStar.List.Tot.Base.last l]) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Prims.b2t",
"Prims.uu___is_Cons",
"FStar.List.Tot.Properties.append_init_last",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.List.Tot.Base.append",
"FStar.List.Tot.Base.init",
"Prims.Cons",
"FStar.List.Tot.Base.last",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec append_init_last (#a: Type) (l: list a {Cons? l}) : Lemma (l == append (init l) [last l]) =
| match l with | a :: q -> if Cons? q then append_init_last q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.for_all_append | val for_all_append (#a: _) (f: (a -> Tot bool)) (s1 s2: list a)
: Lemma (ensures for_all f (s1 @ s2) <==> for_all f s1 && for_all f s2) | val for_all_append (#a: _) (f: (a -> Tot bool)) (s1 s2: list a)
: Lemma (ensures for_all f (s1 @ s2) <==> for_all f s1 && for_all f s2) | let rec for_all_append #a (f: a -> Tot bool) (s1 s2: list a): Lemma
(ensures for_all f (s1 @ s2) <==> for_all f s1 && for_all f s2)
=
let _ = allow_inversion (list a) in
match s1 with
| [] -> ()
| hd1 :: tl1 -> for_all_append f tl1 s2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 1242,
"start_col": 0,
"start_line": 1236
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
))
let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] )
(** Properties of << with lists *)
let precedes_tl
(#a: Type)
(l: list a {Cons? l})
: Lemma (ensures (tl l << l))
= ()
let rec precedes_append_cons_r
(#a: Type)
(l1: list a)
(x: a)
(l2: list a)
: Lemma
(requires True)
(ensures (x << append l1 (x :: l2)))
[SMTPat (x << append l1 (x :: l2))]
= match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2
let precedes_append_cons_prod_r
(#a #b: Type)
(l1: list (a * b))
(x: a)
(y: b)
(l2: list (a * b))
: Lemma
(ensures
x << (append l1 ((x, y) :: l2)) /\
y << (append l1 ((x, y) :: l2)))
= precedes_append_cons_r l1 (x, y) l2
let rec memP_precedes
(#a: Type)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> x << l))
(decreases l)
= match l with
| [] -> ()
| y :: q ->
FStar.Classical.or_elim
#(x == y)
#(memP x q)
#(fun _ -> x << l)
(fun _ -> ())
(fun _ -> memP_precedes x q)
let assoc_precedes
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
(y: b)
: Lemma
(requires (assoc x l == Some y))
(ensures (x << l /\ y << l))
= assoc_memP_some x y l;
memP_precedes (x, y) l
(** Properties about find *)
let rec find_none
(#a: Type)
(f: (a -> Tot bool))
(l: list a)
(x: a)
: Lemma
(requires (find f l == None /\ memP x l))
(ensures (f x == false))
= let (x' :: l') = l in
Classical.or_elim
#(x == x')
#(~ (x == x'))
#(fun _ -> f x == false)
(fun h -> ())
(fun h -> find_none f l' x)
(** Properties of init and last *)
let rec append_init_last (#a: Type) (l: list a { Cons? l }) : Lemma
(l == append (init l) [last l])
= match l with
| a :: q ->
if Cons? q
then
append_init_last q
else
()
let rec init_last_def (#a: Type) (l: list a) (x: a) : Lemma
(let l' = append l [x] in
init l' == l /\ last l' == x)
= match l with
| [] -> ()
| y :: q -> init_last_def q x
let init_last_inj (#a: Type) (l1: list a { Cons? l1 } ) (l2: list a { Cons? l2 } ) : Lemma
(requires (init l1 == init l2 /\ last l1 == last l2))
(ensures (l1 == l2))
= append_init_last l1;
append_init_last l2
(* Properties of for_all *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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": 1,
"initial_ifuel": 1,
"max_fuel": 1,
"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: (_: a -> Prims.bool) -> s1: Prims.list a -> s2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.for_all f (s1 @ s2) <==>
FStar.List.Tot.Base.for_all f s1 && FStar.List.Tot.Base.for_all f s2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.bool",
"Prims.list",
"FStar.List.Tot.Properties.for_all_append",
"Prims.unit",
"FStar.Pervasives.allow_inversion",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"Prims.b2t",
"FStar.List.Tot.Base.for_all",
"FStar.List.Tot.Base.op_At",
"Prims.op_AmpAmp",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec for_all_append #a (f: (a -> Tot bool)) (s1: list a) (s2: list a)
: Lemma (ensures for_all f (s1 @ s2) <==> for_all f s1 && for_all f s2) =
| let _ = allow_inversion (list a) in
match s1 with
| [] -> ()
| hd1 :: tl1 -> for_all_append f tl1 s2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.memP_precedes | val memP_precedes (#a: Type) (x: a) (l: list a)
: Lemma (requires True) (ensures (memP x l ==> x << l)) (decreases l) | val memP_precedes (#a: Type) (x: a) (l: list a)
: Lemma (requires True) (ensures (memP x l ==> x << l)) (decreases l) | let rec memP_precedes
(#a: Type)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> x << l))
(decreases l)
= match l with
| [] -> ()
| y :: q ->
FStar.Classical.or_elim
#(x == y)
#(memP x q)
#(fun _ -> x << l)
(fun _ -> ())
(fun _ -> memP_precedes x q) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 1176,
"start_col": 0,
"start_line": 1160
} | (*
Copyright 2008-2014 Nikhil Swamy and 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.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *)
let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q
let strict_suffix_of_or_eq_nil (#a: Type) (l: list a)
: Lemma
(ensures (strict_suffix_of [] l \/ l == []))
= match l with
| [] -> ()
| a :: q -> strict_suffix_of_nil a q
let strict_suffix_of_cons (#a: Type) (x: a) (l: list a) :
Lemma
(ensures (strict_suffix_of l (x::l)))
= ()
let rec strict_suffix_of_trans (#a: Type) (l1 l2 l3: list a)
: Lemma
(requires True)
(ensures ((strict_suffix_of l1 l2 /\ strict_suffix_of l2 l3) ==> strict_suffix_of l1 l3))
(decreases l3)
[SMTPat (strict_suffix_of l1 l2); SMTPat (strict_suffix_of l2 l3)]
= match l3 with
| [] -> ()
| _ :: q -> strict_suffix_of_trans l1 l2 q
let rec strict_suffix_of_correct (#a) (l1 l2: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> l1 << l2))
(decreases l2)
= match l2 with
| [] -> ()
| _ :: q ->
strict_suffix_of_correct l1 q
let rec map_strict_suffix_of (#a #b: Type) (f: a -> Tot b) (l1: list a) (l2: list a) :
Lemma
(requires True)
(ensures (strict_suffix_of l1 l2 ==> strict_suffix_of (map f l1) (map f l2)))
(decreases l2)
= match l2 with
| [] -> ()
| a::q ->
map_strict_suffix_of f l1 q
let rec mem_strict_suffix_of (#a: eqtype) (l1: list a) (m: a) (l2: list a)
: Lemma
(requires True)
(ensures ((mem m l1 /\ strict_suffix_of l1 l2) ==> mem m l2))
= match l2 with
| [] -> ()
| a :: q ->
mem_strict_suffix_of l1 m q
let rec strict_suffix_of_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures (strict_suffix_of l1 l2 ==> (exists l3 . l2 == append l3 l1)))
= match l2 with
| [] -> ()
| a :: q ->
FStar.Classical.or_elim
#(l1 == q)
#(strict_suffix_of l1 q)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: []))
(fun _ ->
FStar.Classical.exists_elim
(exists l3 . l2 == append l3 l1)
#_
#(fun l3 -> q == append l3 l1)
(strict_suffix_of_exists_append l1 q)
(fun l3 ->
FStar.Classical.exists_intro (fun l3 -> l2 == append l3 l1) (a :: l3)
))
let strict_suffix_of_or_eq_exists_append
(#a: Type)
(l1 l2: list a)
: Lemma
(ensures ((strict_suffix_of l1 l2 \/ l1 == l2) ==> (exists l3 . l2 == append l3 l1)))
= FStar.Classical.or_elim
#(strict_suffix_of l1 l2)
#(l1 == l2)
#(fun _ -> exists l3 . l2 == append l3 l1)
(fun _ ->
strict_suffix_of_exists_append l1 l2)
(fun _ ->
FStar.Classical.exists_intro
(fun l3 -> l2 == append l3 l1)
[] )
(** Properties of << with lists *)
let precedes_tl
(#a: Type)
(l: list a {Cons? l})
: Lemma (ensures (tl l << l))
= ()
let rec precedes_append_cons_r
(#a: Type)
(l1: list a)
(x: a)
(l2: list a)
: Lemma
(requires True)
(ensures (x << append l1 (x :: l2)))
[SMTPat (x << append l1 (x :: l2))]
= match l1 with
| [] -> ()
| _ :: q -> precedes_append_cons_r q x l2
let precedes_append_cons_prod_r
(#a #b: Type)
(l1: list (a * b))
(x: a)
(y: b)
(l2: list (a * b))
: Lemma
(ensures
x << (append l1 ((x, y) :: l2)) /\
y << (append l1 ((x, y) :: l2)))
= precedes_append_cons_r l1 (x, y) l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"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 -> l: Prims.list a
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.memP x l ==> x << l) (decreases l) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.Classical.or_elim",
"Prims.eq2",
"FStar.List.Tot.Base.memP",
"Prims.squash",
"Prims.l_or",
"Prims.precedes",
"Prims.unit",
"FStar.List.Tot.Properties.memP_precedes",
"Prims.l_True",
"Prims.l_imp",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec memP_precedes (#a: Type) (x: a) (l: list a)
: Lemma (requires True) (ensures (memP x l ==> x << l)) (decreases l) =
| match l with
| [] -> ()
| y :: q ->
FStar.Classical.or_elim #(x == y)
#(memP x q)
#(fun _ -> x << l)
(fun _ -> ())
(fun _ -> memP_precedes x q) | false |
Hacl.Bignum.MontExponentiation.fst | Hacl.Bignum.MontExponentiation.bn_exp_mont_bm_consttime | val bn_exp_mont_bm_consttime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len | val bn_exp_mont_bm_consttime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len | let bn_exp_mont_bm_consttime #t k n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
[@inline_let] let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_mont_ladder_swap_consttime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) ctx aM bLen bBits b resM;
LE.exp_mont_ladder_swap_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
LE.exp_mont_ladder_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame () | {
"file_name": "code/bignum/Hacl.Bignum.MontExponentiation.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 234,
"start_col": 0,
"start_line": 222
} | module Hacl.Bignum.MontExponentiation
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BD = Hacl.Spec.Bignum.Definitions
module SN = Hacl.Spec.Bignum
module SM = Hacl.Spec.Bignum.Montgomery
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module LE = Lib.Exponentiation
module BE = Hacl.Impl.Exponentiation
module E = Hacl.Spec.Exponentiation.Lemmas
module S = Hacl.Spec.Bignum.MontExponentiation
#reset-options "--z3rlimit 50 --fuel 0 --ifuel 0"
// All operations are performed in the Montgomery domain!
inline_for_extraction noextract
let a_spec (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len{0 < BD.bn_v n}) =
Lib.NatMod.nat_mod (BD.bn_v n)
inline_for_extraction noextract
let linv (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len) : Type0 =
BD.bn_v a < BD.bn_v n
inline_for_extraction noextract
let refl (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len{linv n a}) : a_spec n =
BD.bn_v a
inline_for_extraction noextract
let linv_ctx (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (ctx:BD.lbignum t (len + len)) : Type0 =
let ctx_n = LSeq.sub ctx 0 len in
let ctx_r2 = LSeq.sub ctx len len in
ctx_n == n /\
0 < BD.bn_v n /\ BD.bn_v ctx_r2 = pow2 (2 * bits t * len) % BD.bn_v n
inline_for_extraction noextract
let mk_to_nat_mont_ll_comm_monoid
(t:limb_t)
(len:BN.meta_len t)
(n:BD.lbignum t (v len))
(mu:limb t{SM.bn_mont_pre n mu})
: BE.to_comm_monoid t len (len +! len) =
{
BE.a_spec = a_spec n;
BE.comm_monoid = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (BD.bn_v n) (v mu);
BE.linv_ctx = linv_ctx n;
BE.linv = linv n;
BE.refl = refl n;
}
inline_for_extraction noextract
val bn_mont_one:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lone_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_one #t k n mu ctx oneM =
[@inline_let] let len = k.BM.bn.BN.len in
let ctx_n = sub ctx 0ul len in
let ctx_r2 = sub ctx len len in
let h0 = ST.get () in
SM.bn_mont_one_lemma n mu (as_seq h0 ctx_r2);
BM.bn_mont_one len k.BM.from ctx_n mu ctx_r2 oneM
inline_for_extraction noextract
val bn_mont_mul:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lmul_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_mul #t k n mu ctx aM bM resM =
let h0 = ST.get () in
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 bM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.mul ctx_n mu aM bM resM
inline_for_extraction noextract
val bn_mont_sqr:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lsqr_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_sqr #t k n mu ctx aM resM =
let h0 = ST.get () in
SM.bn_mont_sqr_lemma n mu (as_seq h0 aM);
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 aM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.sqr ctx_n mu aM resM
inline_for_extraction noextract
let mk_bn_mont_concrete_ops
(t:limb_t)
(k:BM.mont t)
(n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len)))
(mu:limb t{SM.bn_mont_pre n mu}) :
BE.concrete_ops t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len) =
{
BE.to = mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu;
BE.lone = bn_mont_one k n mu;
BE.lmul = bn_mont_mul k n mu;
BE.lsqr = bn_mont_sqr k n mu;
}
///////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
val mk_ctx:
#t:limb_t
-> len:BN.meta_len t
-> n:lbignum t len
-> r2:lbignum t len
-> ctx:lbignum t (len +! len) ->
Stack unit
(requires fun h ->
live h n /\ live h r2 /\ live h ctx /\
disjoint n ctx /\ disjoint r2 ctx /\
0 < bn_v h n /\ bn_v h r2 == pow2 (2 * bits t * v len) % bn_v h n)
(ensures fun h0 _ h1 -> modifies (loc ctx) h0 h1 /\
linv_ctx (as_seq h0 n) (as_seq h1 ctx))
let mk_ctx #t len n r2 ctx =
let h0 = ST.get () in
update_sub ctx 0ul len n;
let h1 = ST.get () in
assert (LSeq.sub (as_seq h1 ctx) 0 (v len) == as_seq h0 n);
update_sub ctx len len r2;
let h2 = ST.get () in
LSeq.eq_intro
(LSeq.sub (as_seq h2 ctx) 0 (v len))
(LSeq.sub (as_seq h1 ctx) 0 (v len));
assert (LSeq.sub (as_seq h2 ctx) 0 (v len) == as_seq h0 n);
assert (LSeq.sub (as_seq h2 ctx) (v len) (v len) == as_seq h0 r2)
noextract
let bn_exp_mont_pre
(#t:limb_t)
(#len:SN.bn_len t)
(n:BD.lbignum t len)
(mu:limb t)
(r2:BD.lbignum t len)
(aM:BD.lbignum t len)
(bBits:size_nat)
(b:BD.lbignum t (BD.blocks0 bBits (bits t)))
=
SM.bn_mont_pre n mu /\
BD.bn_v r2 == pow2 (2 * bits t * len) % BD.bn_v n /\
BD.bn_v b < pow2 bBits /\
BD.bn_v aM < BD.bn_v n
inline_for_extraction noextract
let bn_exp_mont_st (t:limb_t) (len:BN.meta_len t) =
n:lbignum t len
-> mu:limb t
-> r2:lbignum t len
-> aM:lbignum t len
-> bBits:size_t
-> b:lbignum t (blocks0 bBits (size (bits t)))
-> resM:lbignum t len ->
Stack unit
(requires fun h ->
live h n /\ live h aM /\ live h b /\ live h resM /\ live h r2 /\
disjoint resM aM /\ disjoint resM b /\ disjoint resM n /\ disjoint n aM /\
disjoint resM r2 /\ disjoint aM r2 /\ disjoint n r2 /\ disjoint aM b /\
bn_exp_mont_pre (as_seq h n) mu (as_seq h r2) (as_seq h aM) (v bBits) (as_seq h b))
(ensures fun h0 _ h1 -> modifies (loc aM |+| loc resM) h0 h1 /\
(let k1 = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu) in
bn_v h1 resM == LE.pow k1 (bn_v h0 aM) (bn_v h0 b)))
// This function is *NOT* constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_bm_vartime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_bm_vartime #t k n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
[@inline_let] let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_rl_vartime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) ctx aM bLen bBits b resM;
LE.exp_rl_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame ()
// This function is constant-time on the exponent b.
inline_for_extraction noextract | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.NatMod.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Exponentiation.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Exponentiation.Lemmas.fst.checked",
"Hacl.Spec.Bignum.Montgomery.fsti.checked",
"Hacl.Spec.Bignum.MontExponentiation.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Spec.Bignum.fsti.checked",
"Hacl.Impl.Exponentiation.fsti.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Bignum.MontExponentiation.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontExponentiation",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Exponentiation.Lemmas",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Lib.Exponentiation",
"short_module": "LE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum",
"short_module": "SN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"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": "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.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"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 | k: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontExponentiation.bn_exp_mont_st t (Mkbn?.len (Mkmont?.bn k)) | Prims.Tot | [
"total"
] | [] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Hacl.Bignum.Definitions.limb",
"Lib.IntTypes.size_t",
"Hacl.Bignum.Definitions.blocks0",
"Lib.IntTypes.size",
"Lib.IntTypes.bits",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Lib.Exponentiation.exp_mont_ladder_lemma",
"Lib.NatMod.nat_mod",
"Hacl.Bignum.Definitions.bn_v",
"FStar.Ghost.reveal",
"Lib.Exponentiation.Definition.comm_monoid",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.Exponentiation.exp_mont_ladder_swap_lemma",
"Hacl.Impl.Exponentiation.lexp_mont_ladder_swap_consttime",
"Lib.IntTypes.op_Plus_Bang",
"Hacl.Bignum.MontExponentiation.mk_bn_mont_concrete_ops",
"FStar.Ghost.hide",
"Hacl.Spec.Bignum.Definitions.lbignum",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Bignum.MontExponentiation.mk_ctx",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.add",
"Lib.Buffer.create",
"Lib.IntTypes.uint",
"Lib.IntTypes.SEC",
"Lib.Buffer.lbuffer",
"FStar.Ghost.erased",
"Hacl.Spec.Exponentiation.Lemmas.mk_nat_mont_ll_comm_monoid",
"Lib.IntTypes.int_t",
"Prims.eq2",
"Prims.int",
"Prims.l_or",
"Lib.IntTypes.range",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThan",
"Prims.op_LessThanOrEqual",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_Multiply",
"Lib.IntTypes.mk_int",
"Hacl.Spec.Bignum.Definitions.blocks0",
"Hacl.Bignum.meta_len",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"FStar.HyperStack.ST.push_frame"
] | [] | false | false | false | false | false | let bn_exp_mont_bm_consttime #t k n mu r2 aM bBits b resM =
| push_frame ();
let h0 = ST.get () in
[@@ inline_let ]let len = k.BM.bn.BN.len in
[@@ inline_let ]let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_mont_ladder_swap_consttime len
(len +! len)
(mk_bn_mont_concrete_ops t k (as_seq h0 n) mu)
ctx
aM
bLen
bBits
b
resM;
LE.exp_mont_ladder_swap_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
LE.exp_mont_ladder_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame () | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.array_pred | val array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 | val array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 | let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 17,
"end_line": 18,
"start_col": 0,
"start_line": 17
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.nat -> s: Prims.list t -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.nat",
"Prims.list",
"Prims.eq2",
"FStar.List.Tot.Base.length"
] | [] | false | false | false | false | true | let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
| L.length s == n | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.init_s | val init_s (a: alg) (kk: size_nat{kk <= max_key a}) : Tot (t a) | val init_s (a: alg) (kk: size_nat{kk <= max_key a}) : Tot (t a) | let init_s (a : alg) (kk : size_nat{kk <= max_key a}) :
Tot (t a) =
Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a) | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 75,
"end_line": 263,
"start_col": 0,
"start_line": 261
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128)
inline_for_extraction noextract
let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x
/// Specs
/// ----- | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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.Streaming.Blake2.Common.alg ->
kk: Hacl.Streaming.Blake2.Common.size_nat{kk <= Hacl.Streaming.Blake2.Common.max_key a}
-> Hacl.Streaming.Blake2.Common.t a | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.size_nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Hacl.Streaming.Blake2.Common.max_key",
"Spec.Blake2.blake2_init_hash",
"Spec.Blake2.Definitions.blake2_default_params",
"Hacl.Streaming.Blake2.Common.output_size",
"Hacl.Streaming.Blake2.Common.t"
] | [] | false | false | false | false | false | let init_s (a: alg) (kk: size_nat{kk <= max_key a}) : Tot (t a) =
| Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a) | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.spec_s | val spec_s : a: Hacl.Streaming.Blake2.Common.alg ->
kk: Hacl.Streaming.Blake2.Common.size_nat{kk <= Hacl.Streaming.Blake2.Common.max_key a} ->
key: Hacl.Streaming.Blake2.Common.lbytes kk ->
input:
FStar.Seq.Base.seq Hacl.Streaming.Blake2.Common.uint8
{ (match kk = 0 with
| true -> FStar.Seq.Base.length input <= Hacl.Streaming.Blake2.Common.max_input_length a
| _ ->
FStar.Seq.Base.length input + Spec.Blake2.Definitions.size_block a <=
Hacl.Streaming.Blake2.Common.max_input_length a)
<:
Type0 }
-> Lib.ByteSequence.lbytes (Hacl.Streaming.Blake2.Common.output_size a) | let spec_s (a : alg)
(kk : size_nat{kk <= max_key a})
(key : lbytes kk)
(input : S.seq uint8{if kk = 0 then S.length input <= max_input_length a else S.length input + Spec.size_block a <= max_input_length a}) =
Spec.blake2 a input (Spec.blake2_default_params a) kk key (output_size a) | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 75,
"end_line": 293,
"start_col": 0,
"start_line": 289
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128)
inline_for_extraction noextract
let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x
/// Specs
/// -----
noextract
let init_s (a : alg) (kk : size_nat{kk <= max_key a}) :
Tot (t a) =
Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a)
noextract
let update_multi_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 }) :
Tot (t a)
=
let nb = S.length input / U32.v (block_len a) in
Lib.LoopCombinators.repeati nb (Spec.blake2_update1 a prevlen input) acc
noextract
let update_last_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ S.length input + prevlen <= max_input_length a /\
S.length input <= Spec.size_block a }) :
Tot (t a) =
Spec.blake2_update_last a prevlen (S.length input) input acc
noextract
let finish_s (#a : alg) (acc : t a) :
output : S.seq uint8 { S.length output = U32.v (output_len a) } =
Spec.blake2_finish a acc (U32.v (output_len a)) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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.Streaming.Blake2.Common.alg ->
kk: Hacl.Streaming.Blake2.Common.size_nat{kk <= Hacl.Streaming.Blake2.Common.max_key a} ->
key: Hacl.Streaming.Blake2.Common.lbytes kk ->
input:
FStar.Seq.Base.seq Hacl.Streaming.Blake2.Common.uint8
{ (match kk = 0 with
| true -> FStar.Seq.Base.length input <= Hacl.Streaming.Blake2.Common.max_input_length a
| _ ->
FStar.Seq.Base.length input + Spec.Blake2.Definitions.size_block a <=
Hacl.Streaming.Blake2.Common.max_input_length a)
<:
Type0 }
-> Lib.ByteSequence.lbytes (Hacl.Streaming.Blake2.Common.output_size a) | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.size_nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Hacl.Streaming.Blake2.Common.max_key",
"Hacl.Streaming.Blake2.Common.lbytes",
"FStar.Seq.Base.seq",
"Hacl.Streaming.Blake2.Common.uint8",
"Prims.op_Equality",
"Prims.int",
"FStar.Seq.Base.length",
"Hacl.Streaming.Blake2.Common.max_input_length",
"Prims.bool",
"Prims.op_Addition",
"Spec.Blake2.Definitions.size_block",
"Spec.Blake2.blake2",
"Spec.Blake2.Definitions.blake2_default_params",
"Hacl.Streaming.Blake2.Common.output_size",
"Lib.ByteSequence.lbytes"
] | [] | false | false | false | false | false | let spec_s
(a: alg)
(kk: size_nat{kk <= max_key a})
(key: lbytes kk)
(input:
S.seq uint8
{ if kk = 0
then S.length input <= max_input_length a
else S.length input + Spec.size_block a <= max_input_length a })
=
| Spec.blake2 a input (Spec.blake2_default_params a) kk key (output_size a) | false |
|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.stateful_blake2 | val stateful_blake2 (a: alg) (m: m_spec) : I.stateful unit | val stateful_blake2 (a: alg) (m: m_spec) : I.stateful unit | let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m)) | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 43,
"end_line": 134,
"start_col": 0,
"start_line": 89
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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.Streaming.Blake2.Common.alg -> m: Hacl.Streaming.Blake2.Common.m_spec
-> Hacl.Streaming.Interface.stateful Prims.unit | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.m_spec",
"Hacl.Streaming.Interface.Stateful",
"Prims.unit",
"Hacl.Streaming.Blake2.Common.s",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Blake2.Core.state_p",
"LowStar.Monotonic.Buffer.loc_union",
"LowStar.Monotonic.Buffer.loc_addr_of_buffer",
"Hacl.Impl.Blake2.Core.element_t",
"LowStar.Buffer.trivial_preorder",
"Hacl.Streaming.Blake2.Common.state_to_lbuffer",
"LowStar.Monotonic.Buffer.loc",
"Prims.l_and",
"LowStar.Monotonic.Buffer.freeable",
"LowStar.Monotonic.Buffer.live",
"LowStar.Monotonic.Buffer.disjoint",
"Hacl.Streaming.Blake2.Common.t",
"Hacl.Streaming.Blake2.Common.s_v",
"FStar.Pervasives.Native.Mktuple2",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.mul_mod",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Hacl.Impl.Blake2.Core.row_len",
"Hacl.Impl.Blake2.Core.alloc_state",
"FStar.Monotonic.HyperHeap.rid",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.eq2",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"FStar.UInt32.v",
"FStar.UInt32.mul",
"Prims.b2t",
"Prims.op_Negation",
"LowStar.Monotonic.Buffer.g_is_null",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Buffer.malloc",
"Hacl.Impl.Blake2.Core.zero_element",
"FStar.UInt32.op_Star_Hat",
"FStar.UInt32.__uint_to_t",
"FStar.Ghost.erased",
"LowStar.Monotonic.Buffer.free",
"LowStar.Monotonic.Buffer.blit",
"Hacl.Streaming.Interface.stateful"
] | [] | false | false | false | true | false | let stateful_blake2 (a: alg) (m: m_spec) : I.stateful unit =
| I.Stateful (fun () -> s a m)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union (B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\ B.freeable (state_to_lbuffer b))
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\ B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a)
(fun () h acc -> s_v h acc)
(fun #_ h acc ->
let wv, b = acc in
())
(fun #_ l acc h0 h1 ->
let wv, b = acc in
())
(fun #_ _ _ _ _ -> ())
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(fun _ acc ->
match acc with
| wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(fun _ src dst ->
match src with
| src_wv, src_b ->
match dst with
| src_wv, dst_b ->
B.blit (state_to_lbuffer src_b)
0ul
(state_to_lbuffer dst_b)
0ul
U32.(4ul *^ Core.row_len a m)) | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.array | val array : t: Type -> n: Prims.nat -> Type | let array (t: Type) (n: nat) = (l: list t { array_pred n l } ) | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 62,
"end_line": 53,
"start_col": 0,
"start_line": 53
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: Type -> n: Prims.nat -> Type | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.list",
"LowParse.Spec.Array.array_pred"
] | [] | false | false | false | true | true | let array (t: Type) (n: nat) =
| (l: list t {array_pred n l}) | false |
|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.blake2_prevlen | val blake2_prevlen (a: alg) (prevlen: U64.t{U64.v prevlen <= max_input_length a})
: x: Spec.limb_t a {Lib.IntTypes.uint_v x = U64.v prevlen} | val blake2_prevlen (a: alg) (prevlen: U64.t{U64.v prevlen <= max_input_length a})
: x: Spec.limb_t a {Lib.IntTypes.uint_v x = U64.v prevlen} | let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 32,
"end_line": 255,
"start_col": 0,
"start_line": 246
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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.Streaming.Blake2.Common.alg ->
prevlen:
FStar.UInt64.t{FStar.UInt64.v prevlen <= Hacl.Streaming.Blake2.Common.max_input_length a}
-> x: Spec.Blake2.Definitions.limb_t a {Lib.IntTypes.uint_v x = FStar.UInt64.v prevlen} | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"FStar.UInt64.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt64.v",
"Hacl.Streaming.Blake2.Common.max_input_length",
"Lib.IntTypes.to_u64",
"Lib.IntTypes.U64",
"Lib.IntTypes.PUB",
"Lib.IntTypes.cast",
"Lib.IntTypes.SEC",
"Lib.IntTypes.U128",
"Lib.IntTypes.int_t",
"Spec.Blake2.Definitions.limb_t",
"Prims.op_Equality",
"Prims.int",
"Prims.l_or",
"Lib.IntTypes.range",
"Spec.Blake2.Definitions.limb_inttype",
"FStar.UInt.size",
"FStar.UInt64.n",
"Lib.IntTypes.uint_v"
] | [] | false | false | false | false | false | let blake2_prevlen (a: alg) (prevlen: U64.t{U64.v prevlen <= max_input_length a})
: x: Spec.limb_t a {Lib.IntTypes.uint_v x = U64.v prevlen} =
| let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@@ inline_let ]let x:uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.update_last_s | val update_last_s
(#a: alg)
(acc: t a)
(prevlen: nat{prevlen % Spec.size_block a = 0})
(input:
Seq.seq uint8
{S.length input + prevlen <= max_input_length a /\ S.length input <= Spec.size_block a})
: Tot (t a) | val update_last_s
(#a: alg)
(acc: t a)
(prevlen: nat{prevlen % Spec.size_block a = 0})
(input:
Seq.seq uint8
{S.length input + prevlen <= max_input_length a /\ S.length input <= Spec.size_block a})
: Tot (t a) | let update_last_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ S.length input + prevlen <= max_input_length a /\
S.length input <= Spec.size_block a }) :
Tot (t a) =
Spec.blake2_update_last a prevlen (S.length input) input acc | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 62,
"end_line": 281,
"start_col": 0,
"start_line": 276
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128)
inline_for_extraction noextract
let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x
/// Specs
/// -----
noextract
let init_s (a : alg) (kk : size_nat{kk <= max_key a}) :
Tot (t a) =
Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a)
noextract
let update_multi_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 }) :
Tot (t a)
=
let nb = S.length input / U32.v (block_len a) in
Lib.LoopCombinators.repeati nb (Spec.blake2_update1 a prevlen input) acc | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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 |
acc: Hacl.Streaming.Blake2.Common.t a ->
prevlen: Prims.nat{prevlen % Spec.Blake2.Definitions.size_block a = 0} ->
input:
FStar.Seq.Base.seq Hacl.Streaming.Blake2.Common.uint8
{ FStar.Seq.Base.length input + prevlen <= Hacl.Streaming.Blake2.Common.max_input_length a /\
FStar.Seq.Base.length input <= Spec.Blake2.Definitions.size_block a }
-> Hacl.Streaming.Blake2.Common.t a | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.t",
"Prims.nat",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Spec.Blake2.Definitions.size_block",
"FStar.Seq.Base.seq",
"Hacl.Streaming.Blake2.Common.uint8",
"Prims.l_and",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Seq.Base.length",
"Hacl.Streaming.Blake2.Common.max_input_length",
"Spec.Blake2.blake2_update_last"
] | [] | false | false | false | false | false | let update_last_s
(#a: alg)
(acc: t a)
(prevlen: nat{prevlen % Spec.size_block a = 0})
(input:
Seq.seq uint8
{S.length input + prevlen <= max_input_length a /\ S.length input <= Spec.size_block a})
: Tot (t a) =
| Spec.blake2_update_last a prevlen (S.length input) input acc | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.vlarray | val vlarray : t: Type -> min: Prims.nat -> max: Prims.nat -> Type | let vlarray (t: Type) (min max: nat) =
(l: list t { min <= L.length l /\ L.length l <= max } ) | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 57,
"end_line": 371,
"start_col": 0,
"start_line": 370
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low
let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max
let vldata_to_vlarray_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
(ensures (
vlarray_pred elem_count_min elem_count_max x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: Type -> min: Prims.nat -> max: Prims.nat -> Type | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.list",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.List.Tot.Base.length"
] | [] | false | false | false | true | true | let vlarray (t: Type) (min max: nat) =
| (l: list t {min <= L.length l /\ L.length l <= max}) | false |
|
LowParse.Spec.Array.fst | LowParse.Spec.Array.vlarray_pred | val vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 | val vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 | let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 24,
"end_line": 302,
"start_col": 0,
"start_line": 300
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | min: Prims.nat -> max: Prims.nat -> s: Prims.list t -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.nat",
"Prims.list",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.List.Tot.Base.length"
] | [] | false | false | false | false | true | let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
| let l = L.length s in
min <= l /\ l <= max | false |
Vale.Interop.Base.fst | Vale.Interop.Base.default_bq | val default_bq : Vale.Interop.Base.buffer_qualifiers | let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
} | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 1,
"end_line": 61,
"start_col": 0,
"start_line": 57
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
} | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Vale.Interop.Base.buffer_qualifiers | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.Base.Mkbuffer_qualifiers",
"Vale.Arch.HeapTypes_s.Secret"
] | [] | false | false | false | true | false | let default_bq =
| { modified = true; taint = Secret; strict_disjointness = false } | false |
|
Vale.Interop.Base.fst | Vale.Interop.Base.mut_to_b8 | val mut_to_b8 (src: base_typ) (b: B.buffer (base_typ_as_type src)) : GTot b8 | val mut_to_b8 (src: base_typ) (b: B.buffer (base_typ_as_type src)) : GTot b8 | let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 15,
"end_line": 43,
"start_col": 0,
"start_line": 42
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
src: Vale.Arch.HeapTypes_s.base_typ ->
b: LowStar.Buffer.buffer (Vale.Interop.Types.base_typ_as_type src)
-> Prims.GTot Vale.Interop.Types.b8 | Prims.GTot | [
"sometrivial"
] | [] | [
"Vale.Arch.HeapTypes_s.base_typ",
"LowStar.Buffer.buffer",
"Vale.Interop.Types.base_typ_as_type",
"Vale.Interop.Types.Buffer",
"Vale.Interop.Types.b8"
] | [] | false | false | false | false | false | let mut_to_b8 (src: base_typ) (b: B.buffer (base_typ_as_type src)) : GTot b8 =
| Buffer true b | false |
Vale.Interop.Base.fst | Vale.Interop.Base.valid_base_type | val valid_base_type : Type0 | let valid_base_type = x:base_typ{x <> TUInt128} | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 47,
"end_line": 70,
"start_col": 0,
"start_line": 70
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
} | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Vale.Arch.HeapTypes_s.base_typ",
"Prims.b2t",
"Prims.op_disEquality",
"Vale.Arch.HeapTypes_s.TUInt128"
] | [] | false | false | false | true | true | let valid_base_type =
| x: base_typ{x <> TUInt128} | false |
|
Vale.Interop.Base.fst | Vale.Interop.Base.imm_to_b8 | val imm_to_b8 (src: base_typ) (b: IB.ibuffer (base_typ_as_type src)) : GTot b8 | val imm_to_b8 (src: base_typ) (b: IB.ibuffer (base_typ_as_type src)) : GTot b8 | let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 16,
"end_line": 40,
"start_col": 0,
"start_line": 39
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0 | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
src: Vale.Arch.HeapTypes_s.base_typ ->
b: LowStar.ImmutableBuffer.ibuffer (Vale.Interop.Types.base_typ_as_type src)
-> Prims.GTot Vale.Interop.Types.b8 | Prims.GTot | [
"sometrivial"
] | [] | [
"Vale.Arch.HeapTypes_s.base_typ",
"LowStar.ImmutableBuffer.ibuffer",
"Vale.Interop.Types.base_typ_as_type",
"Vale.Interop.Types.Buffer",
"Vale.Interop.Types.b8"
] | [] | false | false | false | false | false | let imm_to_b8 (src: base_typ) (b: IB.ibuffer (base_typ_as_type src)) : GTot b8 =
| Buffer false b | false |
Vale.Interop.Base.fst | Vale.Interop.Base.stack_bq | val stack_bq : Vale.Interop.Base.buffer_qualifiers | let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
} | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 1,
"end_line": 68,
"start_col": 0,
"start_line": 64
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
} | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Vale.Interop.Base.buffer_qualifiers | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.Base.Mkbuffer_qualifiers",
"Vale.Arch.HeapTypes_s.Public"
] | [] | false | false | false | true | false | let stack_bq =
| { modified = true; taint = Public; strict_disjointness = true } | false |
|
Vale.Interop.Base.fst | Vale.Interop.Base.normal | val normal (#a: Type) (x: a) : a | val normal (#a: Type) (x: a) : a | let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 6,
"end_line": 105,
"start_col": 0,
"start_line": 80
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> a | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.norm",
"Prims.Cons",
"FStar.Pervasives.norm_step",
"FStar.Pervasives.iota",
"FStar.Pervasives.zeta",
"FStar.Pervasives.delta_attr",
"Prims.string",
"Prims.Nil",
"FStar.Pervasives.delta_only",
"FStar.Pervasives.primops",
"FStar.Pervasives.simplify"
] | [] | false | false | false | true | false | let normal (#a: Type) (x: a) : a =
| FStar.Pervasives.norm [
iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [
`%TD_Buffer?; `%BS.Mkmachine_state?.ms_ok; `%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags; `%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack; `%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace; `%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on; `%List.Tot.fold_right_gtot; `%List.Tot.map_gtot;
`%List.Tot.length; `%fst; `%snd; `%Mktuple2?._1; `%Mktuple2?._2
];
primops;
simplify
]
x | false |
Vale.Interop.Base.fst | Vale.Interop.Base.arg | val arg : Type0 | let arg = t:td & td_as_type t | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 29,
"end_line": 117,
"start_col": 0,
"start_line": 117
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.dtuple2",
"Vale.Interop.Base.td",
"Vale.Interop.Base.td_as_type"
] | [] | false | false | false | true | true | let arg =
| t: td & td_as_type t | false |
|
Vale.Interop.Base.fst | Vale.Interop.Base.arr | val arr : dom: Type -> codom: Type -> Type | let arr (dom:Type) (codom:Type) = dom -> codom | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 46,
"end_line": 130,
"start_col": 0,
"start_line": 130
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | dom: Type -> codom: Type -> Type | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | true | let arr (dom codom: Type) =
| dom -> codom | false |
|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.repeati_split_at_eq | val repeati_split_at_eq :
a : alg ->
s : t a ->
input:S.seq uint8 { S.length input <= max_input_length a } ->
Lemma(
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
n_blocks = Lib.Sequence.length blocks / Spec.size_block a /\ // This is necessary for type-checking
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 input) s ==
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 blocks) s) | val repeati_split_at_eq :
a : alg ->
s : t a ->
input:S.seq uint8 { S.length input <= max_input_length a } ->
Lemma(
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
n_blocks = Lib.Sequence.length blocks / Spec.size_block a /\ // This is necessary for type-checking
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 input) s ==
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 blocks) s) | let repeati_split_at_eq a s input =
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
let f = Spec.blake2_update1 a 0 input in
let g = Spec.blake2_update1 a 0 blocks in
let s1 = Lib.LoopCombinators.repeati n_blocks f s in
assert (Lib.Sequence.length blocks = n_blocks * Spec.size_block a);
Math.Lemmas.cancel_mul_div n_blocks (Spec.size_block a);
assert (n_blocks = Lib.Sequence.length blocks / Spec.size_block a);
assert (Lib.Sequence.length blocks <= max_input_length a);
let s2 = Lib.LoopCombinators.repeati n_blocks g s in
assert (input `Seq.equal` Seq.append blocks last);
assert (S.length input = S.length blocks + S.length last);
introduce forall (i:nat{i < n_blocks}). (Spec.get_blocki a input i) `S.equal` (Spec.get_blocki a blocks i)
with
begin
let b0 = Spec.get_blocki a input i in
let b1 = Spec.get_blocki a blocks i in
assert (S.length blocks = n_blocks * Spec.size_block a);
Math.Lemmas.lemma_mult_le_right (Spec.size_block a) (i + 1) n_blocks;
assert ((i + 1) * Spec.size_block a <= S.length blocks);
Math.Lemmas.lemma_mult_le_right (Spec.size_block a) i n_blocks;
assert (i * Spec.size_block a <= S.length blocks);
Math.Lemmas.distributivity_add_left i 1 (Spec.size_block a);
assert ((i + 1) * Spec.size_block a = i * Spec.size_block a + Spec.size_block a);
introduce forall (j: nat{j < Spec.size_block a}). S.index b0 j == S.index b1 j
with
begin
assert (i * Spec.size_block a + j < i * Spec.size_block a + Spec.size_block a);
Math.Lemmas.nat_times_nat_is_nat i (Spec.size_block a);
S.lemma_index_slice input (i * Spec.size_block a) ((i + 1) * Spec.size_block a) j;
assert (S.index b0 j == S.index input (j + (i * Spec.size_block a)))
end
end;
assert (forall (i:nat{i < n_blocks}) acc. f i acc == g i acc);
Lib.Sequence.Lemmas.repeati_extensionality n_blocks f g s | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 59,
"end_line": 450,
"start_col": 0,
"start_line": 415
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128)
inline_for_extraction noextract
let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x
/// Specs
/// -----
noextract
let init_s (a : alg) (kk : size_nat{kk <= max_key a}) :
Tot (t a) =
Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a)
noextract
let update_multi_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 }) :
Tot (t a)
=
let nb = S.length input / U32.v (block_len a) in
Lib.LoopCombinators.repeati nb (Spec.blake2_update1 a prevlen input) acc
noextract
let update_last_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ S.length input + prevlen <= max_input_length a /\
S.length input <= Spec.size_block a }) :
Tot (t a) =
Spec.blake2_update_last a prevlen (S.length input) input acc
noextract
let finish_s (#a : alg) (acc : t a) :
output : S.seq uint8 { S.length output = U32.v (output_len a) } =
Spec.blake2_finish a acc (U32.v (output_len a))
noextract
let spec_s (a : alg)
(kk : size_nat{kk <= max_key a})
(key : lbytes kk)
(input : S.seq uint8{if kk = 0 then S.length input <= max_input_length a else S.length input + Spec.size_block a <= max_input_length a}) =
Spec.blake2 a input (Spec.blake2_default_params a) kk key (output_size a)
/// Interlude for spec proofs
/// -------------------------
val update_multi_zero:
#a : alg ->
acc:t a ->
prevlen:nat{prevlen % Spec.size_block a = 0} ->
Lemma
(requires (prevlen <= max_input_length a))
(ensures (update_multi_s #a acc prevlen S.empty == acc))
let update_multi_zero #a acc prevlen =
Lib.LoopCombinators.eq_repeati0 (0 / U32.v (block_len a)) (Spec.blake2_update1 a prevlen S.empty) acc
#push-options "--z3cliopt smt.arith.nl=false"
val update_multi_associative:
#a : alg ->
acc: t a ->
prevlen1:nat ->
prevlen2:nat ->
input1:S.seq uint8 ->
input2:S.seq uint8 ->
Lemma
(requires (
(**) Math.Lemmas.pos_times_pos_is_pos Spec.size_block_w (Spec.size_word a);
prevlen1 % Spec.size_block a = 0 /\
S.length input1 % Spec.size_block a = 0 /\
S.length input2 % Spec.size_block a = 0 /\
prevlen1 + S.length input1 + S.length input2 <= max_input_length a /\
prevlen2 = prevlen1 + S.length input1))
(ensures (
let input = S.append input1 input2 in
S.length input % Spec.size_block a = 0 /\
prevlen2 % Spec.size_block a = 0 /\
update_multi_s (update_multi_s acc prevlen1 input1) prevlen2 input2 ==
update_multi_s acc prevlen1 input))
#pop-options
#push-options "--z3rlimit 400"
let update_multi_associative #a acc prevlen1 prevlen2 input1 input2 =
let input = S.append input1 input2 in
let nb = S.length input / U32.v (block_len a) in
let nb1 = S.length input1 / U32.v (block_len a) in
let nb2 = S.length input2 / U32.v (block_len a) in
let f = Spec.blake2_update1 a prevlen1 input in
let f1 = Spec.blake2_update1 a prevlen1 input1 in
let f2 = Spec.blake2_update1 a prevlen2 input2 in
let aux1 (i:nat{i < nb1}) (acc:t a) : Lemma (f i acc == f1 i acc)
= assert (Spec.get_blocki a input i `Seq.equal` Spec.get_blocki a input1 i)
in
let aux2 (i:nat{i < nb2}) (acc:t a) : Lemma (f2 i acc == f (i + nb1) acc)
= assert (Spec.get_blocki a input2 i `Seq.equal` Spec.get_blocki a input (i + nb1))
in
let open Lib.LoopCombinators in
let open Lib.Sequence.Lemmas in
calc (==) {
update_multi_s (update_multi_s acc prevlen1 input1) prevlen2 input2;
(==) { }
repeati nb2 f2 (repeati nb1 f1 acc);
(==) {
Classical.forall_intro_2 aux1;
repeati_extensionality nb1 f1 f acc
}
repeati nb2 f2 (repeati nb1 f acc);
(==) {
repeati_def nb1 f acc;
repeati_def nb2 f2 (repeat_right 0 nb1 (fixed_a (t a)) f acc)
}
repeat_right 0 nb2 (fixed_a (t a)) f2 (repeat_right 0 nb1 (fixed_a (t a)) f acc);
(==) {
Classical.forall_intro_2 aux2;
repeat_gen_right_extensionality nb2 nb1 (fixed_a (t a)) (fixed_a (t a)) f2 f (repeat_right 0 nb1 (fixed_a (t a)) f acc)
}
repeat_right nb1 (nb1 + nb2) (fixed_a (t a)) f (repeat_right 0 nb1 (fixed_a (t a)) f acc);
(==) { repeat_right_plus 0 nb1 nb (fixed_a (t a)) f acc; repeati_def nb f acc }
repeati nb f acc;
(==) { }
update_multi_s acc prevlen1 input;
}
#pop-options
/// A helper function: the hash incremental function defined with the functions
/// locally defined (with a signature adapted to the functor).
noextract
val blake2_hash_incremental_s :
a : alg ->
kk: size_nat{kk <= max_key a} ->
k: lbytes kk ->
input:S.seq uint8 { if kk = 0 then S.length input <= max_input_length a else S.length input + (Spec.size_block a) <= max_input_length a } ->
output:S.seq uint8 { S.length output = output_size a }
#push-options "--z3cliopt smt.arith.nl=false"
let blake2_hash_incremental_s a kk k input0 =
let key_block = if kk > 0 then Spec.blake2_key_block a kk k else S.empty in
let key_block_len = S.length key_block in
let input = Seq.append key_block input0 in
assert (key_block_len = (if kk = 0 then 0 else Spec.size_block a));
(**) Math.Lemmas.modulo_lemma 0 (U32.v (block_len a));
let bs, l = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
let acc1 = init_s a kk in
let acc2 = update_multi_s #a acc1 0 bs in
let acc3 = update_last_s #a acc2 (S.length bs) l in
let acc4 = finish_s #a acc3 in
acc4
#pop-options
#push-options "--z3cliopt smt.arith.nl=false"
val repeati_split_at_eq :
a : alg ->
s : t a ->
input:S.seq uint8 { S.length input <= max_input_length a } ->
Lemma(
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
n_blocks = Lib.Sequence.length blocks / Spec.size_block a /\ // This is necessary for type-checking
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 input) s ==
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 blocks) s)
#pop-options | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Hacl.Streaming.Blake2.Common.alg ->
s: Hacl.Streaming.Blake2.Common.t a ->
input:
FStar.Seq.Base.seq Hacl.Streaming.Blake2.Common.uint8
{FStar.Seq.Base.length input <= Hacl.Streaming.Blake2.Common.max_input_length a}
-> FStar.Pervasives.Lemma
(ensures
(let _ = Spec.Blake2.split a (FStar.Seq.Base.length input) in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ n_blocks _ = _ in
let _ =
Lib.UpdateMulti.split_at_last_lazy (FStar.UInt32.v (Hacl.Streaming.Blake2.Common.block_len
a))
input
in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ blocks _ = _ in
n_blocks = Lib.Sequence.length blocks / Spec.Blake2.Definitions.size_block a /\
Lib.LoopCombinators.repeati n_blocks (Spec.Blake2.blake2_update1 a 0 input) s ==
Lib.LoopCombinators.repeati n_blocks (Spec.Blake2.blake2_update1 a 0 blocks) s)
<:
Type0)
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.t",
"FStar.Seq.Base.seq",
"Hacl.Streaming.Blake2.Common.uint8",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Seq.Base.length",
"Hacl.Streaming.Blake2.Common.max_input_length",
"Prims.nat",
"Lib.UpdateMulti.uint8",
"Lib.Sequence.Lemmas.repeati_extensionality",
"Spec.Blake2.Definitions.state",
"Prims.unit",
"Prims._assert",
"Prims.l_Forall",
"Prims.op_LessThan",
"Prims.eq2",
"FStar.Classical.Sugar.forall_intro",
"FStar.Seq.Base.equal",
"Lib.IntTypes.uint8",
"Spec.Blake2.get_blocki",
"Spec.Blake2.Definitions.size_block",
"FStar.Seq.Base.index",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"FStar.Seq.Base.lemma_index_slice",
"FStar.Math.Lemmas.nat_times_nat_is_nat",
"Prims.squash",
"Prims.op_Equality",
"Prims.int",
"FStar.Math.Lemmas.distributivity_add_left",
"FStar.Math.Lemmas.lemma_mult_le_right",
"Spec.Blake2.Definitions.block_s",
"FStar.Seq.Base.append",
"Lib.Sequence.lseq",
"Lib.IntTypes.int_t",
"Spec.Blake2.Definitions.wt",
"Lib.IntTypes.SEC",
"Lib.LoopCombinators.repeati",
"Lib.Sequence.length",
"Prims.op_Division",
"FStar.Math.Lemmas.cancel_mul_div",
"Prims.l_and",
"Lib.IntTypes.U8",
"Spec.Blake2.Definitions.max_limb",
"Spec.Blake2.blake2_update1",
"FStar.Pervasives.Native.tuple2",
"Lib.UpdateMulti.split_at_last_lazy",
"FStar.UInt32.v",
"Hacl.Streaming.Blake2.Common.block_len",
"Prims.op_Multiply",
"Spec.Blake2.split"
] | [] | false | false | true | false | false | let repeati_split_at_eq a s input =
| let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
let f = Spec.blake2_update1 a 0 input in
let g = Spec.blake2_update1 a 0 blocks in
let s1 = Lib.LoopCombinators.repeati n_blocks f s in
assert (Lib.Sequence.length blocks = n_blocks * Spec.size_block a);
Math.Lemmas.cancel_mul_div n_blocks (Spec.size_block a);
assert (n_blocks = Lib.Sequence.length blocks / Spec.size_block a);
assert (Lib.Sequence.length blocks <= max_input_length a);
let s2 = Lib.LoopCombinators.repeati n_blocks g s in
assert (input `Seq.equal` (Seq.append blocks last));
assert (S.length input = S.length blocks + S.length last);
introduce forall (i: nat{i < n_blocks}) . (Spec.get_blocki a input i)
`S.equal`
(Spec.get_blocki a blocks i)
with let b0 = Spec.get_blocki a input i in
let b1 = Spec.get_blocki a blocks i in
assert (S.length blocks = n_blocks * Spec.size_block a);
Math.Lemmas.lemma_mult_le_right (Spec.size_block a) (i + 1) n_blocks;
assert ((i + 1) * Spec.size_block a <= S.length blocks);
Math.Lemmas.lemma_mult_le_right (Spec.size_block a) i n_blocks;
assert (i * Spec.size_block a <= S.length blocks);
Math.Lemmas.distributivity_add_left i 1 (Spec.size_block a);
assert ((i + 1) * Spec.size_block a = i * Spec.size_block a + Spec.size_block a);
introduce forall (j: nat{j < Spec.size_block a}) . S.index b0 j == S.index b1 j
with (assert (i * Spec.size_block a + j < i * Spec.size_block a + Spec.size_block a);
Math.Lemmas.nat_times_nat_is_nat i (Spec.size_block a);
S.lemma_index_slice input (i * Spec.size_block a) ((i + 1) * Spec.size_block a) j;
assert (S.index b0 j == S.index input (j + (i * Spec.size_block a))));
assert (forall (i: nat{i < n_blocks}) acc. f i acc == g i acc);
Lib.Sequence.Lemmas.repeati_extensionality n_blocks f g s | false |
Vale.Interop.Base.fst | Vale.Interop.Base.intro_n_arrow_nil | val intro_n_arrow_nil (a: Type) (x: a) : n_arrow [] a | val intro_n_arrow_nil (a: Type) (x: a) : n_arrow [] a | let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 149,
"start_col": 0,
"start_line": 147
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Type -> x: a -> Vale.Interop.Base.n_arrow [] a | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.Base.n_arrow",
"Prims.Nil",
"Vale.Interop.Base.td"
] | [] | false | false | false | true | false | let intro_n_arrow_nil (a: Type) (x: a) : n_arrow [] a =
| x | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.update_multi_s | val update_multi_s
(#a: alg)
(acc: t a)
(prevlen: nat{prevlen % Spec.size_block a = 0})
(input:
Seq.seq uint8
{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 })
: Tot (t a) | val update_multi_s
(#a: alg)
(acc: t a)
(prevlen: nat{prevlen % Spec.size_block a = 0})
(input:
Seq.seq uint8
{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 })
: Tot (t a) | let update_multi_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 }) :
Tot (t a)
=
let nb = S.length input / U32.v (block_len a) in
Lib.LoopCombinators.repeati nb (Spec.blake2_update1 a prevlen input) acc | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 74,
"end_line": 273,
"start_col": 0,
"start_line": 266
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128)
inline_for_extraction noextract
let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x
/// Specs
/// -----
noextract
let init_s (a : alg) (kk : size_nat{kk <= max_key a}) :
Tot (t a) =
Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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 |
acc: Hacl.Streaming.Blake2.Common.t a ->
prevlen: Prims.nat{prevlen % Spec.Blake2.Definitions.size_block a = 0} ->
input:
FStar.Seq.Base.seq Hacl.Streaming.Blake2.Common.uint8
{ prevlen + FStar.Seq.Base.length input <= Hacl.Streaming.Blake2.Common.max_input_length a /\
FStar.Seq.Base.length input % Spec.Blake2.Definitions.size_block a = 0 }
-> Hacl.Streaming.Blake2.Common.t a | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.t",
"Prims.nat",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Spec.Blake2.Definitions.size_block",
"FStar.Seq.Base.seq",
"Hacl.Streaming.Blake2.Common.uint8",
"Prims.l_and",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Seq.Base.length",
"Hacl.Streaming.Blake2.Common.max_input_length",
"Lib.LoopCombinators.repeati",
"Spec.Blake2.Definitions.state",
"Spec.Blake2.blake2_update1",
"Prims.op_Division",
"FStar.UInt32.v",
"Hacl.Streaming.Blake2.Common.block_len"
] | [] | false | false | false | false | false | let update_multi_s
(#a: alg)
(acc: t a)
(prevlen: nat{prevlen % Spec.size_block a = 0})
(input:
Seq.seq uint8
{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 })
: Tot (t a) =
| let nb = S.length input / U32.v (block_len a) in
Lib.LoopCombinators.repeati nb (Spec.blake2_update1 a prevlen input) acc | false |
Vale.Interop.Base.fst | Vale.Interop.Base.intro_n_arrow_cons | val intro_n_arrow_cons (hd: td) (b: Type) (tl: list td) (x: (td_as_type hd -> n_arrow tl b))
: n_arrow (hd :: tl) b | val intro_n_arrow_cons (hd: td) (b: Type) (tl: list td) (x: (td_as_type hd -> n_arrow tl b))
: n_arrow (hd :: tl) b | let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 155,
"start_col": 0,
"start_line": 152
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
hd: Vale.Interop.Base.td ->
b: Type ->
tl: Prims.list Vale.Interop.Base.td ->
x: (_: Vale.Interop.Base.td_as_type hd -> Vale.Interop.Base.n_arrow tl b)
-> Vale.Interop.Base.n_arrow (hd :: tl) b | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.Base.td",
"Prims.list",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.n_arrow",
"Prims.Cons"
] | [] | false | false | false | false | false | let intro_n_arrow_cons (hd: td) (b: Type) (tl: list td) (x: (td_as_type hd -> n_arrow tl b))
: n_arrow (hd :: tl) b =
| x | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.fldata_array_precond | val fldata_array_precond (k: parser_kind) (array_byte_size elem_count: nat) : Tot bool | val fldata_array_precond (k: parser_kind) (array_byte_size elem_count: nat) : Tot bool | let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 50,
"end_line": 28,
"start_col": 0,
"start_line": 21
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | k: LowParse.Spec.Base.parser_kind -> array_byte_size: Prims.nat -> elem_count: Prims.nat
-> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"Prims.nat",
"Prims.op_AmpAmp",
"LowParse.Spec.List.serialize_list_precond",
"Prims.op_Equality",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.int",
"FStar.Mul.op_Star",
"Prims.bool"
] | [] | false | false | false | true | false | let fldata_array_precond (k: parser_kind) (array_byte_size elem_count: nat) : Tot bool =
| serialize_list_precond k && k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size | false |
Vale.Interop.Base.fst | Vale.Interop.Base.ibuf_t | val ibuf_t : src: Vale.Arch.HeapTypes_s.base_typ -> t: Vale.Arch.HeapTypes_s.base_typ -> Type0 | let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0} | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 110,
"end_line": 24,
"start_col": 0,
"start_line": 24
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0} | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | src: Vale.Arch.HeapTypes_s.base_typ -> t: Vale.Arch.HeapTypes_s.base_typ -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Vale.Arch.HeapTypes_s.base_typ",
"LowStar.ImmutableBuffer.ibuffer",
"Vale.Interop.Types.base_typ_as_type",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"FStar.Mul.op_Star",
"LowStar.Monotonic.Buffer.length",
"LowStar.ImmutableBuffer.immutable_preorder",
"Vale.Interop.Types.view_n_unfold"
] | [] | false | false | false | true | true | let ibuf_t src t =
| b: IB.ibuffer (base_typ_as_type src) {(B.length b * view_n_unfold src) % view_n_unfold t = 0} | false |
|
LowParse.Spec.Array.fst | LowParse.Spec.Array.parse_array' | val parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (fldata_array_precond k array_byte_size elem_count == true))
(ensures (fun _ -> True)) | val parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (fldata_array_precond k array_byte_size elem_count == true))
(ensures (fun _ -> True)) | let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u) | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 121,
"end_line": 111,
"start_col": 0,
"start_line": 97
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: LowParse.Spec.Base.serializer p -> array_byte_size: Prims.nat -> elem_count: Prims.nat
-> Prims.Pure
(LowParse.Spec.Base.parser (LowParse.Spec.Array.parse_array_kind' array_byte_size)
(LowParse.Spec.Array.array t elem_count)) | Prims.Pure | [] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"LowParse.Spec.Combinators.parse_synth",
"LowParse.Spec.FLData.parse_fldata_kind",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.FLData.parse_fldata_strong_t",
"Prims.list",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list",
"LowParse.Spec.Array.array",
"LowParse.Spec.FLData.parse_fldata_strong",
"LowParse.Spec.Array.fldata_to_array",
"Prims.unit",
"LowParse.Spec.Array.fldata_to_array_inj",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"LowParse.Spec.Array.parse_array_kind'",
"Prims.l_True"
] | [] | false | false | false | false | false | let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (fldata_array_precond k array_byte_size elem_count == true))
(ensures (fun _ -> True)) =
| let u:u: unit{fldata_array_precond k array_byte_size elem_count == true} = () in
fldata_to_array_inj s array_byte_size elem_count u;
(parse_fldata_strong (serialize_list _ s) array_byte_size)
`parse_synth`
(fldata_to_array s array_byte_size elem_count u) | false |
Vale.Interop.Base.fst | Vale.Interop.Base.intro_dep_arrow_nil | val intro_dep_arrow_nil (b: Type) (f: b) : n_dep_arrow [] b | val intro_dep_arrow_nil (b: Type) (f: b) : n_dep_arrow [] b | let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 171,
"start_col": 0,
"start_line": 168
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: Type -> f: b -> Vale.Interop.Base.n_dep_arrow [] b | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.Base.n_dep_arrow",
"Prims.Nil",
"Vale.Interop.Base.td"
] | [] | false | false | false | true | false | let intro_dep_arrow_nil (b: Type) (f: b) : n_dep_arrow [] b =
| f | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.parse_array_kind' | val parse_array_kind' (array_byte_size: nat) : Tot parser_kind | val parse_array_kind' (array_byte_size: nat) : Tot parser_kind | let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 51,
"end_line": 95,
"start_col": 0,
"start_line": 92
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | array_byte_size: Prims.nat -> LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"LowParse.Spec.FLData.parse_fldata_kind",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.Base.parser_kind"
] | [] | false | false | false | true | false | let parse_array_kind' (array_byte_size: nat) : Tot parser_kind =
| parse_fldata_kind array_byte_size parse_list_kind | false |
Vale.Interop.Base.fst | Vale.Interop.Base.buf_t | val buf_t : src: Vale.Arch.HeapTypes_s.base_typ -> t: Vale.Arch.HeapTypes_s.base_typ -> Type0 | let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0} | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 107,
"end_line": 21,
"start_col": 0,
"start_line": 21
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | src: Vale.Arch.HeapTypes_s.base_typ -> t: Vale.Arch.HeapTypes_s.base_typ -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Vale.Arch.HeapTypes_s.base_typ",
"LowStar.Buffer.buffer",
"Vale.Interop.Types.base_typ_as_type",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"FStar.Mul.op_Star",
"LowStar.Monotonic.Buffer.length",
"LowStar.Buffer.trivial_preorder",
"Vale.Interop.Types.view_n_unfold"
] | [] | false | false | false | true | true | let buf_t src t =
| b: B.buffer (base_typ_as_type src) {(B.length b * view_n_unfold src) % view_n_unfold t = 0} | false |
|
LowParse.Spec.Array.fst | LowParse.Spec.Array.parse_array_kind | val parse_array_kind (k: parser_kind) (array_byte_size elem_count: nat) : Tot parser_kind | val parse_array_kind (k: parser_kind) (array_byte_size elem_count: nat) : Tot parser_kind | let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 37,
"end_line": 149,
"start_col": 0,
"start_line": 138
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | k: LowParse.Spec.Base.parser_kind -> array_byte_size: Prims.nat -> elem_count: Prims.nat
-> LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"Prims.nat",
"Prims.op_AmpAmp",
"LowParse.Spec.Array.fldata_array_precond",
"Prims.op_Equality",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.parser_kind_metadata_some",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.ParserKindMetadataTotal",
"LowParse.Spec.Base.total_constant_size_parser_kind",
"Prims.bool",
"LowParse.Spec.Array.parse_array_kind'"
] | [] | false | false | false | true | false | let parse_array_kind (k: parser_kind) (array_byte_size elem_count: nat) : Tot parser_kind =
| if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then total_constant_size_parser_kind array_byte_size
else parse_array_kind' array_byte_size | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.vldata_vlarray_precond | val vldata_vlarray_precond
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min elem_count_max: nat)
: GTot bool | val vldata_vlarray_precond
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min elem_count_max: nat)
: GTot bool | let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 81,
"end_line": 328,
"start_col": 0,
"start_line": 304
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max: Prims.nat ->
p: LowParse.Spec.Base.parser k t ->
elem_count_min: Prims.nat ->
elem_count_max: Prims.nat
-> Prims.GTot Prims.bool | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"Prims.op_AmpAmp",
"LowParse.Spec.List.serialize_list_precond",
"Prims.op_Equality",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"FStar.Mul.op_Star",
"Prims.op_Addition",
"Prims.bool"
] | [] | false | false | false | false | false | let vldata_vlarray_precond
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min elem_count_max: nat)
: GTot bool =
| serialize_list_precond k && k.parser_kind_high = Some k.parser_kind_low &&
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low | false |
Vale.Interop.Base.fst | Vale.Interop.Base.coerce | val coerce (x: 'a{'a == 'b}) : 'b | val coerce (x: 'a{'a == 'b}) : 'b | let coerce (x:'a{'a == 'b}) : 'b = x | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 36,
"end_line": 46,
"start_col": 0,
"start_line": 46
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: 'a{'a == 'b} -> 'b | Prims.Tot | [
"total"
] | [] | [
"Prims.eq2"
] | [] | false | false | false | false | false | let coerce (x: 'a{'a == 'b}) : 'b =
| x | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.parse_array | val parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (fldata_array_precond k array_byte_size elem_count == true))
(ensures (fun _ -> True)) | val parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (fldata_array_precond k array_byte_size elem_count == true))
(ensures (fun _ -> True)) | let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count) | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 104,
"end_line": 185,
"start_col": 0,
"start_line": 172
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: LowParse.Spec.Base.serializer p -> array_byte_size: Prims.nat -> elem_count: Prims.nat
-> Prims.Pure
(LowParse.Spec.Base.parser (LowParse.Spec.Array.parse_array_kind k array_byte_size elem_count)
(LowParse.Spec.Array.array t elem_count)) | Prims.Pure | [] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"LowParse.Spec.Base.strengthen",
"LowParse.Spec.Array.parse_array_kind",
"LowParse.Spec.Array.array",
"LowParse.Spec.Array.parse_array'",
"Prims.unit",
"LowParse.Spec.Array.parse_array_kind_correct",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"Prims.l_True"
] | [] | false | false | false | false | false | let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (fldata_array_precond k array_byte_size elem_count == true))
(ensures (fun _ -> True)) =
| parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count)
(parse_array' s array_byte_size elem_count) | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.array_to_fldata_correct | val array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(x: list t)
: Lemma
(requires
(fldata_array_precond k array_byte_size elem_count == true /\ array_pred elem_count x))
(ensures (parse_fldata_strong_pred (serialize_list _ s) array_byte_size x)) | val array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(x: list t)
: Lemma
(requires
(fldata_array_precond k array_byte_size elem_count == true /\ array_pred elem_count x))
(ensures (parse_fldata_strong_pred (serialize_list _ s) array_byte_size x)) | let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 46,
"end_line": 204,
"start_col": 0,
"start_line": 187
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
s: LowParse.Spec.Base.serializer p ->
array_byte_size: Prims.nat ->
elem_count: Prims.nat ->
x: Prims.list t
-> FStar.Pervasives.Lemma
(requires
LowParse.Spec.Array.fldata_array_precond k array_byte_size elem_count == true /\
LowParse.Spec.Array.array_pred elem_count x)
(ensures
LowParse.Spec.FLData.parse_fldata_strong_pred (LowParse.Spec.List.serialize_list p s)
array_byte_size
x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"Prims.list",
"LowParse.Spec.List.list_length_constant_size_parser_correct",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list",
"Prims.unit",
"Prims.l_and",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"LowParse.Spec.Array.array_pred",
"Prims.squash",
"LowParse.Spec.FLData.parse_fldata_strong_pred",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(x: list t)
: Lemma
(requires
(fldata_array_precond k array_byte_size elem_count == true /\ array_pred elem_count x))
(ensures (parse_fldata_strong_pred (serialize_list _ s) array_byte_size x)) =
| let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.serialize_array | val serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
: Tot (serializer (parse_array s array_byte_size elem_count)) | val serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
: Tot (serializer (parse_array s array_byte_size elem_count)) | let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 72,
"end_line": 271,
"start_col": 0,
"start_line": 260
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
s: LowParse.Spec.Base.serializer p ->
array_byte_size: Prims.nat ->
elem_count: Prims.nat ->
u242:
u243:
Prims.unit{LowParse.Spec.Array.fldata_array_precond k array_byte_size elem_count == true}
-> LowParse.Spec.Base.serializer (LowParse.Spec.Array.parse_array s array_byte_size elem_count) | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"LowParse.Spec.Array.array",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.Array.parse_array_kind'",
"LowParse.Spec.Array.parse_array'",
"LowParse.Spec.Array.serialize_array'",
"LowParse.Bytes.bytes",
"LowParse.Spec.Array.parse_array_kind",
"LowParse.Spec.Array.parse_array"
] | [] | false | false | false | false | false | let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
: Tot (serializer (parse_array s array_byte_size elem_count)) =
| fun x -> serialize (serialize_array' s array_byte_size elem_count u) x | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.update_multi_zero | val update_multi_zero:
#a : alg ->
acc:t a ->
prevlen:nat{prevlen % Spec.size_block a = 0} ->
Lemma
(requires (prevlen <= max_input_length a))
(ensures (update_multi_s #a acc prevlen S.empty == acc)) | val update_multi_zero:
#a : alg ->
acc:t a ->
prevlen:nat{prevlen % Spec.size_block a = 0} ->
Lemma
(requires (prevlen <= max_input_length a))
(ensures (update_multi_s #a acc prevlen S.empty == acc)) | let update_multi_zero #a acc prevlen =
Lib.LoopCombinators.eq_repeati0 (0 / U32.v (block_len a)) (Spec.blake2_update1 a prevlen S.empty) acc | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 103,
"end_line": 307,
"start_col": 0,
"start_line": 306
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128)
inline_for_extraction noextract
let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x
/// Specs
/// -----
noextract
let init_s (a : alg) (kk : size_nat{kk <= max_key a}) :
Tot (t a) =
Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a)
noextract
let update_multi_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 }) :
Tot (t a)
=
let nb = S.length input / U32.v (block_len a) in
Lib.LoopCombinators.repeati nb (Spec.blake2_update1 a prevlen input) acc
noextract
let update_last_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ S.length input + prevlen <= max_input_length a /\
S.length input <= Spec.size_block a }) :
Tot (t a) =
Spec.blake2_update_last a prevlen (S.length input) input acc
noextract
let finish_s (#a : alg) (acc : t a) :
output : S.seq uint8 { S.length output = U32.v (output_len a) } =
Spec.blake2_finish a acc (U32.v (output_len a))
noextract
let spec_s (a : alg)
(kk : size_nat{kk <= max_key a})
(key : lbytes kk)
(input : S.seq uint8{if kk = 0 then S.length input <= max_input_length a else S.length input + Spec.size_block a <= max_input_length a}) =
Spec.blake2 a input (Spec.blake2_default_params a) kk key (output_size a)
/// Interlude for spec proofs
/// -------------------------
val update_multi_zero:
#a : alg ->
acc:t a ->
prevlen:nat{prevlen % Spec.size_block a = 0} ->
Lemma
(requires (prevlen <= max_input_length a))
(ensures (update_multi_s #a acc prevlen S.empty == acc)) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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 |
acc: Hacl.Streaming.Blake2.Common.t a ->
prevlen: Prims.nat{prevlen % Spec.Blake2.Definitions.size_block a = 0}
-> FStar.Pervasives.Lemma (requires prevlen <= Hacl.Streaming.Blake2.Common.max_input_length a)
(ensures Hacl.Streaming.Blake2.Common.update_multi_s acc prevlen FStar.Seq.Base.empty == acc) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.t",
"Prims.nat",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Spec.Blake2.Definitions.size_block",
"Lib.LoopCombinators.eq_repeati0",
"Spec.Blake2.Definitions.state",
"Prims.op_Division",
"FStar.UInt32.v",
"Hacl.Streaming.Blake2.Common.block_len",
"Spec.Blake2.blake2_update1",
"FStar.Seq.Base.empty",
"Lib.IntTypes.uint_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"Prims.unit"
] | [] | true | false | true | false | false | let update_multi_zero #a acc prevlen =
| Lib.LoopCombinators.eq_repeati0 (0 / U32.v (block_len a))
(Spec.blake2_update1 a prevlen S.empty)
acc | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.parse_vlarray | val parse_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
: Tot
(parser (parse_vlarray_kind array_byte_size_min array_byte_size_max)
(vlarray t elem_count_min elem_count_max)) | val parse_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
: Tot
(parser (parse_vlarray_kind array_byte_size_min array_byte_size_max)
(vlarray t elem_count_min elem_count_max)) | let parse_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Tot (parser (parse_vlarray_kind array_byte_size_min array_byte_size_max) (vlarray t elem_count_min elem_count_max))
= vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s)
`parse_synth`
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 93,
"end_line": 439,
"start_col": 0,
"start_line": 423
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low
let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max
let vldata_to_vlarray_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
(ensures (
vlarray_pred elem_count_min elem_count_max x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low
inline_for_extraction
let vlarray (t: Type) (min max: nat) =
(l: list t { min <= L.length l /\ L.length l <= max } )
inline_for_extraction
let vldata_to_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
: Tot (vlarray t elem_count_min elem_count_max)
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vldata_to_vlarray_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x'
let vldata_to_vlarray_inj
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Lemma
(forall (x1 x2: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)) .
{:pattern
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1);
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2)}
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1 ==
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_vlarray_kind
(array_byte_size_min: nat)
(array_byte_size_max: nat { array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\ array_byte_size_max < 4294967296 } )
: Tot parser_kind
= parse_bounded_vldata_strong_kind array_byte_size_min array_byte_size_max (log256' array_byte_size_max) parse_list_kind | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max: Prims.nat ->
s: LowParse.Spec.Base.serializer p ->
elem_count_min: Prims.nat ->
elem_count_max: Prims.nat ->
u413:
u414:
Prims.unit
{ LowParse.Spec.Array.vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true }
-> LowParse.Spec.Base.parser (LowParse.Spec.Array.parse_vlarray_kind array_byte_size_min
array_byte_size_max)
(LowParse.Spec.Array.vlarray t elem_count_min elem_count_max) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.vldata_vlarray_precond",
"LowParse.Spec.Combinators.parse_synth",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"Prims.list",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list",
"LowParse.Spec.Array.vlarray",
"LowParse.Spec.VLData.parse_bounded_vldata_strong",
"LowParse.Spec.Array.vldata_to_vlarray",
"LowParse.Spec.Array.vldata_to_vlarray_inj",
"LowParse.Spec.Array.parse_vlarray_kind"
] | [] | false | false | false | false | false | let parse_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
: Tot
(parser (parse_vlarray_kind array_byte_size_min array_byte_size_max)
(vlarray t elem_count_min elem_count_max)) =
| vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
(parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s))
`parse_synth`
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.fldata_to_array_correct | val fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(x: list t)
: Lemma
(requires
(fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x))
(ensures (array_pred elem_count x)) | val fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(x: list t)
: Lemma
(requires
(fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x))
(ensures (array_pred elem_count x)) | let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 55,
"end_line": 50,
"start_col": 0,
"start_line": 30
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
s: LowParse.Spec.Base.serializer p ->
array_byte_size: Prims.nat ->
elem_count: Prims.nat ->
x: Prims.list t
-> FStar.Pervasives.Lemma
(requires
LowParse.Spec.Array.fldata_array_precond k array_byte_size elem_count == true /\
LowParse.Spec.FLData.parse_fldata_strong_pred (LowParse.Spec.List.serialize_list p s)
array_byte_size
x) (ensures LowParse.Spec.Array.array_pred elem_count x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"Prims.list",
"LowParse.Math.mul_reg_r",
"FStar.List.Tot.Base.length",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.unit",
"LowParse.Spec.List.list_length_constant_size_parser_correct",
"Prims._assert",
"Prims.eq2",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"LowParse.Spec.List.parse_list",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.List.serialize_list",
"Prims.l_and",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"LowParse.Spec.FLData.parse_fldata_strong_pred",
"Prims.squash",
"LowParse.Spec.Array.array_pred",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(x: list t)
: Lemma
(requires
(fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x))
(ensures (array_pred elem_count x)) =
| let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low | false |
Vale.Interop.Base.fst | Vale.Interop.Base.__test | val __test:n_dep_arrow [TD_Base TUInt8] (fun (x: UInt8.t) -> y: UInt8.t{x == y}) | val __test:n_dep_arrow [TD_Base TUInt8] (fun (x: UInt8.t) -> y: UInt8.t{x == y}) | let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 62,
"end_line": 205,
"start_col": 0,
"start_line": 204
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Vale.Interop.Base.n_dep_arrow [Vale.Interop.Base.TD_Base Vale.Arch.HeapTypes_s.TUInt8]
(fun x -> y: FStar.UInt8.t{x == y}) | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt8.t",
"Vale.Interop.Base.intro_dep_arrow_nil",
"Prims.eq2",
"Vale.Interop.Base.n_dep_arrow",
"Prims.Nil",
"Vale.Interop.Base.td",
"Vale.Interop.Base.elim_1",
"Prims.Cons",
"Vale.Interop.Base.TD_Base",
"Vale.Arch.HeapTypes_s.TUInt8",
"Vale.Interop.Base.n_arrow"
] | [] | false | false | false | false | false | let __test:n_dep_arrow [TD_Base TUInt8] (fun (x: UInt8.t) -> y: UInt8.t{x == y}) =
| fun (x: UInt8.t) -> intro_dep_arrow_nil (y: UInt8.t{x == y}) x | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.array_to_fldata | val array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size) | val array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size) | let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x' | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 4,
"end_line": 223,
"start_col": 0,
"start_line": 207
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
s: LowParse.Spec.Base.serializer p ->
array_byte_size: Prims.nat ->
elem_count: Prims.nat ->
u188:
u190:
Prims.unit{LowParse.Spec.Array.fldata_array_precond k array_byte_size elem_count == true} ->
x: LowParse.Spec.Array.array t elem_count
-> LowParse.Spec.FLData.parse_fldata_strong_t (LowParse.Spec.List.serialize_list p s)
array_byte_size | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"LowParse.Spec.Array.array",
"LowParse.Spec.Array.array_to_fldata_correct",
"Prims.list",
"LowParse.Spec.FLData.parse_fldata_strong_t",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list"
] | [] | false | false | false | false | false | let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size) =
| [@@ inline_let ]let x':list t = x in
[@@ inline_let ]let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x' | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.parse_array_kind_correct | val parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
: Lemma (requires (fldata_array_precond k array_byte_size elem_count == true))
(ensures
(parser_kind_prop (parse_array_kind k array_byte_size elem_count)
(parse_array' s array_byte_size elem_count))) | val parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
: Lemma (requires (fldata_array_precond k array_byte_size elem_count == true))
(ensures
(parser_kind_prop (parse_array_kind k array_byte_size elem_count)
(parse_array' s array_byte_size elem_count))) | let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 170,
"start_col": 0,
"start_line": 151
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: LowParse.Spec.Base.serializer p -> array_byte_size: Prims.nat -> elem_count: Prims.nat
-> FStar.Pervasives.Lemma
(requires LowParse.Spec.Array.fldata_array_precond k array_byte_size elem_count == true)
(ensures
LowParse.Spec.Base.parser_kind_prop (LowParse.Spec.Array.parse_array_kind k
array_byte_size
elem_count)
(LowParse.Spec.Array.parse_array' s array_byte_size elem_count)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"Prims.op_Equality",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.parser_kind_metadata_some",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.ParserKindMetadataTotal",
"FStar.Classical.forall_intro",
"LowParse.Bytes.bytes",
"Prims.l_imp",
"Prims.l_and",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Array.array",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"LowParse.Spec.Array.parse_array'",
"FStar.Classical.move_requires",
"LowParse.Spec.Array.parse_array_total_constant_size",
"Prims.unit",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"LowParse.Spec.Array.parse_array_kind",
"LowParse.Spec.Array.parse_array_kind'",
"Prims.squash",
"LowParse.Spec.Base.parser_kind_prop",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
: Lemma (requires (fldata_array_precond k array_byte_size elem_count == true))
(ensures
(parser_kind_prop (parse_array_kind k array_byte_size elem_count)
(parse_array' s array_byte_size elem_count))) =
| if k.parser_kind_metadata = Some ParserKindMetadataTotal
then
(parser_kind_prop_equiv (parse_array_kind' array_byte_size)
(parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count)
(parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s
array_byte_size
elem_count))) | false |
Vale.Interop.Base.fst | Vale.Interop.Base.disjoint_or_eq_1 | val disjoint_or_eq_1 : a: Vale.Interop.Base.arg -> b: Vale.Interop.Base.arg -> Prims.logical | let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 15,
"end_line": 232,
"start_col": 0,
"start_line": 219
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Vale.Interop.Base.arg -> b: Vale.Interop.Base.arg -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.Base.arg",
"FStar.Pervasives.Native.Mktuple2",
"Prims.dtuple2",
"Vale.Interop.Base.td",
"Vale.Interop.Base.td_as_type",
"Vale.Arch.HeapTypes_s.base_typ",
"Prims.bool",
"Vale.Arch.HeapTypes_s.taint",
"Vale.Interop.Base.TD_Buffer",
"Vale.Interop.Base.Mkbuffer_qualifiers",
"Vale.Interop.Base.buffer_qualifiers",
"Vale.Interop.Base.disjoint_not_eq",
"LowStar.Buffer.trivial_preorder",
"Vale.Interop.Types.base_typ_as_type",
"Vale.Interop.Base.TD_ImmBuffer",
"LowStar.ImmutableBuffer.immutable_preorder",
"Prims.l_or",
"Prims.l_and",
"Prims.op_Equals_Equals_Equals",
"Prims.eq2",
"FStar.Pervasives.Native.tuple2",
"Prims.l_True",
"Prims.logical"
] | [] | false | false | false | true | true | let disjoint_or_eq_1 (a b: arg) =
| match a, b with
| (| TD_Buffer _ _ { strict_disjointness = true } , xb |), (| TD_Buffer _ _ _ , yb |)
| (| TD_ImmBuffer _ _ { strict_disjointness = true } , xb |), (| TD_ImmBuffer _ _ _ , yb |)
| (| TD_Buffer _ _ _ , xb |), (| TD_Buffer _ _ { strict_disjointness = true } , yb |)
| (| TD_ImmBuffer _ _ _ , xb |), (| TD_ImmBuffer _ _ { strict_disjointness = true } , yb |)
| (| TD_ImmBuffer _ _ _ , xb |), (| TD_Buffer _ _ _ , yb |)
| (| TD_Buffer _ _ _ , xb |), (| TD_ImmBuffer _ _ _ , yb |) -> disjoint_not_eq xb yb
| (| TD_Buffer srcx tx { taint = tntx } , xb |), (| TD_Buffer srcy ty { taint = tnty } , yb |)
| (| TD_ImmBuffer srcx tx { taint = tntx } , xb |), (| TD_ImmBuffer srcy ty { taint = tnty } , yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True | false |
|
LowParse.Spec.Array.fst | LowParse.Spec.Array.vldata_vlarray_precond_parser_kind_low | val vldata_vlarray_precond_parser_kind_low
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min elem_count_max: nat)
: Lemma
(requires
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[
SMTPat (k.parser_kind_low);
SMTPat
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max)
] | val vldata_vlarray_precond_parser_kind_low
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min elem_count_max: nat)
: Lemma
(requires
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[
SMTPat (k.parser_kind_low);
SMTPat
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max)
] | let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 58,
"end_line": 342,
"start_col": 0,
"start_line": 330
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max: Prims.nat ->
p: LowParse.Spec.Base.parser k t ->
elem_count_min: Prims.nat ->
elem_count_max: Prims.nat
-> FStar.Pervasives.Lemma
(requires
LowParse.Spec.Array.vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max)
(ensures Mkparser_kind'?.parser_kind_low k < 4294967296)
[
SMTPat (Mkparser_kind'?.parser_kind_low k);
SMTPat (LowParse.Spec.Array.vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max)
] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"FStar.Math.Lemmas.lemma_mult_le_right",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.unit",
"Prims.b2t",
"LowParse.Spec.Array.vldata_vlarray_precond",
"Prims.squash",
"Prims.op_LessThan",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.bool",
"Prims.Nil"
] | [] | true | false | true | false | false | let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min elem_count_max: nat)
: Lemma
(requires
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[
SMTPat (k.parser_kind_low);
SMTPat
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max)
] =
| M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.vldata_to_vlarray_correct | val vldata_to_vlarray_correct
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(x: list t)
: Lemma
(requires
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true /\
parse_bounded_vldata_strong_pred array_byte_size_min
array_byte_size_max
(serialize_list _ s)
x)) (ensures (vlarray_pred elem_count_min elem_count_max x)) | val vldata_to_vlarray_correct
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(x: list t)
: Lemma
(requires
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true /\
parse_bounded_vldata_strong_pred array_byte_size_min
array_byte_size_max
(serialize_list _ s)
x)) (ensures (vlarray_pred elem_count_min elem_count_max x)) | let vldata_to_vlarray_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
(ensures (
vlarray_pred elem_count_min elem_count_max x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 55,
"end_line": 367,
"start_col": 0,
"start_line": 344
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low
let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max: Prims.nat ->
s: LowParse.Spec.Base.serializer p ->
elem_count_min: Prims.nat ->
elem_count_max: Prims.nat ->
x: Prims.list t
-> FStar.Pervasives.Lemma
(requires
LowParse.Spec.Array.vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true /\
LowParse.Spec.VLData.parse_bounded_vldata_strong_pred array_byte_size_min
array_byte_size_max
(LowParse.Spec.List.serialize_list p s)
x) (ensures LowParse.Spec.Array.vlarray_pred elem_count_min elem_count_max x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.list",
"LowParse.Math.lt_mul_add_reg_r",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.unit",
"LowParse.Spec.List.list_length_constant_size_parser_correct",
"Prims._assert",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"LowParse.Spec.List.parse_list",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"FStar.List.Tot.Base.length",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.List.serialize_list",
"Prims.l_and",
"Prims.bool",
"LowParse.Spec.Array.vldata_vlarray_precond",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_pred",
"Prims.squash",
"LowParse.Spec.Array.vlarray_pred",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let vldata_to_vlarray_correct
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(x: list t)
: Lemma
(requires
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true /\
parse_bounded_vldata_strong_pred array_byte_size_min
array_byte_size_max
(serialize_list _ s)
x)) (ensures (vlarray_pred elem_count_min elem_count_max x)) =
| let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.parse_array_total_constant_size | val parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(x: bytes)
: Lemma
(requires
(fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\ Seq.length x >= array_byte_size)
) (ensures (Some? (parse (parse_array' s array_byte_size elem_count) x))) | val parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(x: bytes)
: Lemma
(requires
(fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\ Seq.length x >= array_byte_size)
) (ensures (Some? (parse (parse_array' s array_byte_size elem_count) x))) | let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p) | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 55,
"end_line": 135,
"start_col": 0,
"start_line": 113
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
s: LowParse.Spec.Base.serializer p ->
array_byte_size: Prims.nat ->
elem_count: Prims.nat ->
x: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(requires
LowParse.Spec.Array.fldata_array_precond k array_byte_size elem_count == true /\
Mkparser_kind'?.parser_kind_metadata k ==
FStar.Pervasives.Native.Some LowParse.Spec.Base.ParserKindMetadataTotal /\
FStar.Seq.Base.length x >= array_byte_size)
(ensures
Some? (LowParse.Spec.Base.parse (LowParse.Spec.Array.parse_array' s
array_byte_size
elem_count)
x)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"Prims.list",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.List.parse_list",
"Prims.unit",
"LowParse.Spec.List.parse_list_total_constant_size",
"FStar.Seq.Base.seq",
"LowParse.Bytes.byte",
"FStar.Seq.Base.slice",
"LowParse.Spec.Combinators.parse_synth_eq",
"LowParse.Spec.FLData.parse_fldata_kind",
"LowParse.Spec.FLData.parse_fldata_strong_t",
"LowParse.Spec.List.serialize_list",
"LowParse.Spec.Array.array",
"LowParse.Spec.FLData.parse_fldata_strong",
"LowParse.Spec.Array.fldata_to_array",
"LowParse.Spec.Array.fldata_to_array_inj",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"Prims.l_and",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.parser_kind_metadata_some",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.ParserKindMetadataTotal",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.Seq.Base.length",
"Prims.squash",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"LowParse.Spec.Array.parse_array'",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(x: bytes)
: Lemma
(requires
(fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\ Seq.length x >= array_byte_size)
) (ensures (Some? (parse (parse_array' s array_byte_size elem_count) x))) =
| let u:u: unit{fldata_array_precond k array_byte_size elem_count == true} = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size)
(fldata_to_array s array_byte_size elem_count u)
x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p) | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.serialize_array' | val serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
: Tot (serializer (parse_array' s array_byte_size elem_count)) | val serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
: Tot (serializer (parse_array' s array_byte_size elem_count)) | let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
() | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 6,
"end_line": 258,
"start_col": 0,
"start_line": 240
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
s: LowParse.Spec.Base.serializer p ->
array_byte_size: Prims.nat ->
elem_count: Prims.nat ->
u228:
u229:
Prims.unit{LowParse.Spec.Array.fldata_array_precond k array_byte_size elem_count == true}
-> LowParse.Spec.Base.serializer (LowParse.Spec.Array.parse_array' s array_byte_size elem_count) | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"LowParse.Spec.Combinators.serialize_synth",
"LowParse.Spec.FLData.parse_fldata_kind",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.FLData.parse_fldata_strong_t",
"Prims.list",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list",
"LowParse.Spec.Array.array",
"LowParse.Spec.FLData.parse_fldata_strong",
"LowParse.Spec.Array.fldata_to_array",
"LowParse.Spec.FLData.serialize_fldata_strong",
"LowParse.Spec.Array.array_to_fldata",
"LowParse.Spec.Array.array_to_fldata_to_array",
"LowParse.Spec.Array.fldata_to_array_inj",
"LowParse.Spec.Array.parse_array_kind'",
"LowParse.Spec.Array.parse_array'"
] | [] | false | false | false | false | false | let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
: Tot (serializer (parse_array' s array_byte_size elem_count)) =
| fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth _
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
() | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.fldata_to_array | val fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count) | val fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count) | let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x' | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 4,
"end_line": 72,
"start_col": 0,
"start_line": 56
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } ) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
s: LowParse.Spec.Base.serializer p ->
array_byte_size: Prims.nat ->
elem_count: Prims.nat ->
u59:
u61: Prims.unit{LowParse.Spec.Array.fldata_array_precond k array_byte_size elem_count == true} ->
x:
LowParse.Spec.FLData.parse_fldata_strong_t (LowParse.Spec.List.serialize_list p s)
array_byte_size
-> LowParse.Spec.Array.array t elem_count | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"LowParse.Spec.FLData.parse_fldata_strong_t",
"LowParse.Spec.List.parse_list_kind",
"Prims.list",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list",
"LowParse.Spec.Array.fldata_to_array_correct",
"LowParse.Spec.Array.array"
] | [] | false | false | false | false | false | let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count) =
| [@@ inline_let ]let x':list t = x in
[@@ inline_let ]let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x' | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.length_serialize_array | val length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) ==
(L.length x)
`FStar.Mul.op_Star`
k.parser_kind_low) | val length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) ==
(L.length x)
`FStar.Mul.op_Star`
k.parser_kind_low) | let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x) | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 79,
"end_line": 297,
"start_col": 0,
"start_line": 273
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
s: LowParse.Spec.Base.serializer p ->
array_byte_size: Prims.nat ->
elem_count: Prims.nat ->
u266:
u270:
Prims.unit{LowParse.Spec.Array.fldata_array_precond k array_byte_size elem_count == true} ->
x: LowParse.Spec.Array.array t elem_count
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.length (LowParse.Spec.Base.serialize (LowParse.Spec.Array.serialize_array s
array_byte_size
elem_count
u266)
x) ==
FStar.List.Tot.Base.length x * Mkparser_kind'?.parser_kind_low k) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.nat",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.fldata_array_precond",
"LowParse.Spec.Array.array",
"LowParse.Spec.List.list_length_constant_size_parser_correct",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.List.parse_list_kind",
"Prims.list",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list",
"LowParse.Spec.Combinators.serialize_synth_eq",
"LowParse.Spec.FLData.parse_fldata_kind",
"LowParse.Spec.FLData.parse_fldata_strong_t",
"LowParse.Spec.FLData.parse_fldata_strong",
"LowParse.Spec.Array.fldata_to_array",
"LowParse.Spec.FLData.serialize_fldata_strong",
"LowParse.Spec.Array.array_to_fldata",
"LowParse.Spec.Array.array_to_fldata_to_array",
"LowParse.Spec.Array.fldata_to_array_inj",
"Prims.l_True",
"Prims.squash",
"Prims.int",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"LowParse.Spec.Array.parse_array_kind",
"LowParse.Spec.Array.parse_array",
"LowParse.Spec.Array.serialize_array",
"FStar.Mul.op_Star",
"FStar.List.Tot.Base.length",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size elem_count: nat)
(u: unit{fldata_array_precond k array_byte_size elem_count == true})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) ==
(L.length x)
`FStar.Mul.op_Star`
k.parser_kind_low) =
| fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq _
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x) | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.parse_vlarray_kind | val parse_vlarray_kind
(array_byte_size_min: nat)
(array_byte_size_max:
nat
{ array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\
array_byte_size_max < 4294967296 })
: Tot parser_kind | val parse_vlarray_kind
(array_byte_size_min: nat)
(array_byte_size_max:
nat
{ array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\
array_byte_size_max < 4294967296 })
: Tot parser_kind | let parse_vlarray_kind
(array_byte_size_min: nat)
(array_byte_size_max: nat { array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\ array_byte_size_max < 4294967296 } )
: Tot parser_kind
= parse_bounded_vldata_strong_kind array_byte_size_min array_byte_size_max (log256' array_byte_size_max) parse_list_kind | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 120,
"end_line": 421,
"start_col": 0,
"start_line": 417
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low
let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max
let vldata_to_vlarray_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
(ensures (
vlarray_pred elem_count_min elem_count_max x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low
inline_for_extraction
let vlarray (t: Type) (min max: nat) =
(l: list t { min <= L.length l /\ L.length l <= max } )
inline_for_extraction
let vldata_to_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
: Tot (vlarray t elem_count_min elem_count_max)
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vldata_to_vlarray_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x'
let vldata_to_vlarray_inj
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Lemma
(forall (x1 x2: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)) .
{:pattern
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1);
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2)}
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1 ==
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2 ==>
x1 == x2)
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max:
Prims.nat
{ array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\
array_byte_size_max < 4294967296 }
-> LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.Base.parser_kind"
] | [] | false | false | false | false | false | let parse_vlarray_kind
(array_byte_size_min: nat)
(array_byte_size_max:
nat
{ array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\
array_byte_size_max < 4294967296 })
: Tot parser_kind =
| parse_bounded_vldata_strong_kind array_byte_size_min
array_byte_size_max
(log256' array_byte_size_max)
parse_list_kind | false |
Vale.Interop.Base.fst | Vale.Interop.Base.disjoint_or_eq | val disjoint_or_eq : l: Prims.list Vale.Interop.Base.arg -> Type0 | let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 42,
"end_line": 236,
"start_col": 0,
"start_line": 235
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Vale.Interop.Base.arg -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Vale.Interop.Base.arg",
"FStar.BigOps.pairwise_and'",
"Vale.Interop.Base.disjoint_or_eq_1"
] | [] | false | false | false | true | true | let disjoint_or_eq (l: list arg) =
| BigOps.pairwise_and' disjoint_or_eq_1 l | false |
|
Vale.Interop.Base.fst | Vale.Interop.Base.n_arrow | val n_arrow : dom: Prims.list Vale.Interop.Base.td -> codom: Type -> Type | let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 47,
"end_line": 127,
"start_col": 0,
"start_line": 124
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
//////////////////////////////////////////////////////////////////////////////// | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | dom: Prims.list Vale.Interop.Base.td -> codom: Type -> Type | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Vale.Interop.Base.td",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.n_arrow"
] | [
"recursion"
] | false | false | false | true | true | let rec n_arrow (dom: list td) (codom: Type) =
| match dom with
| [] -> codom
| hd :: tl -> td_as_type hd -> n_arrow tl codom | false |
|
LowParse.Spec.Array.fst | LowParse.Spec.Array.vlarray_to_vldata | val vlarray_to_vldata
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
(x: vlarray t elem_count_min elem_count_max)
: Tot
(parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)) | val vlarray_to_vldata
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
(x: vlarray t elem_count_min elem_count_max)
: Tot
(parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)) | let vlarray_to_vldata
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: vlarray t elem_count_min elem_count_max)
: Tot (parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vlarray_to_vldata_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x' | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 4,
"end_line": 526,
"start_col": 0,
"start_line": 508
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low
let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max
let vldata_to_vlarray_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
(ensures (
vlarray_pred elem_count_min elem_count_max x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low
inline_for_extraction
let vlarray (t: Type) (min max: nat) =
(l: list t { min <= L.length l /\ L.length l <= max } )
inline_for_extraction
let vldata_to_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
: Tot (vlarray t elem_count_min elem_count_max)
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vldata_to_vlarray_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x'
let vldata_to_vlarray_inj
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Lemma
(forall (x1 x2: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)) .
{:pattern
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1);
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2)}
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1 ==
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_vlarray_kind
(array_byte_size_min: nat)
(array_byte_size_max: nat { array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\ array_byte_size_max < 4294967296 } )
: Tot parser_kind
= parse_bounded_vldata_strong_kind array_byte_size_min array_byte_size_max (log256' array_byte_size_max) parse_list_kind
let parse_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Tot (parser (parse_vlarray_kind array_byte_size_min array_byte_size_max) (vlarray t elem_count_min elem_count_max))
= vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s)
`parse_synth`
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u
let parse_vlarray_eq_some
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(input: bytes)
: Lemma
(requires (
Some? (parse (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) input)
))
(ensures (
let sz = log256' array_byte_size_max in
let pi = parse (parse_bounded_integer sz) input in
Some? pi /\ (
let Some (len, c_len) = pi in
c_len == sz /\
array_byte_size_min <= U32.v len /\ U32.v len <= array_byte_size_max /\ (
let input1 = Seq.slice input c_len (Seq.length input) in
U32.v len <= Seq.length input1 /\ (
let input2 = Seq.slice input1 0 (U32.v len) in
let pl = parse (parse_list p) input2 in
Some? pl /\ (
let Some (l, c_l) = pl in
c_l == U32.v len /\
vlarray_pred elem_count_min elem_count_max l /\
parse (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) input == Some (l, c_len + c_l)
))))))
=
parser_kind_prop_equiv k p;
vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max ();
parse_synth_eq (parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s)) (vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max ()) input;
parse_vldata_gen_eq (log256' array_byte_size_max) (in_bounds array_byte_size_min array_byte_size_max) (parse_list p) input;
parser_kind_prop_equiv parse_list_kind (parse_list p)
let vlarray_to_vldata_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
vlarray_pred elem_count_min elem_count_max x
))
(ensures (
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lemma_mult_le_right k.parser_kind_low elem_count_min l;
M.lemma_mult_le_right k.parser_kind_low l elem_count_max | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max: Prims.nat ->
s: LowParse.Spec.Base.serializer p ->
elem_count_min: Prims.nat ->
elem_count_max: Prims.nat ->
u490:
u492:
Prims.unit
{ LowParse.Spec.Array.vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true } ->
x: LowParse.Spec.Array.vlarray t elem_count_min elem_count_max
-> LowParse.Spec.VLData.parse_bounded_vldata_strong_t array_byte_size_min
array_byte_size_max
(LowParse.Spec.List.serialize_list p s) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.vldata_vlarray_precond",
"LowParse.Spec.Array.vlarray",
"LowParse.Spec.Array.vlarray_to_vldata_correct",
"Prims.list",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list"
] | [] | false | false | false | false | false | let vlarray_to_vldata
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
(x: vlarray t elem_count_min elem_count_max)
: Tot
(parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)) =
| [@@ inline_let ]let x':list t = x in
[@@ inline_let ]let _ =
vlarray_to_vldata_correct array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
x'
in
x' | false |
Vale.Interop.Base.fst | Vale.Interop.Base.all_live | val all_live : h: FStar.Monotonic.HyperStack.mem -> bs: Prims.list Vale.Interop.Base.arg -> Type0 | let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 33,
"end_line": 247,
"start_col": 0,
"start_line": 246
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h: FStar.Monotonic.HyperStack.mem -> bs: Prims.list Vale.Interop.Base.arg -> Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"Prims.list",
"Vale.Interop.Base.arg",
"FStar.BigOps.big_and'",
"Vale.Interop.Base.live_arg"
] | [] | false | false | false | true | true | let all_live (h: HS.mem) (bs: list arg) =
| BigOps.big_and' (live_arg h) bs | false |
|
Vale.Interop.Base.fst | Vale.Interop.Base.mem_roots_p | val mem_roots_p : h0: FStar.Monotonic.HyperStack.mem -> args: Prims.list Vale.Interop.Base.arg -> Prims.logical | let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 18,
"end_line": 252,
"start_col": 0,
"start_line": 250
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h0: FStar.Monotonic.HyperStack.mem -> args: Prims.list Vale.Interop.Base.arg -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"Prims.list",
"Vale.Interop.Base.arg",
"Prims.l_and",
"Vale.Interop.Base.disjoint_or_eq",
"Vale.Interop.Base.all_live",
"Prims.logical"
] | [] | false | false | false | true | true | let mem_roots_p (h0: HS.mem) (args: list arg) =
| disjoint_or_eq args /\ all_live h0 args | false |
|
LowParse.Spec.Array.fst | LowParse.Spec.Array.serialize_vlarray | val serialize_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
: Tot
(serializer (parse_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u)) | val serialize_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
: Tot
(serializer (parse_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u)) | let serialize_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Tot (serializer (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u))
= vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
vlarray_to_vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
serialize_synth
_
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
(serialize_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s))
(vlarray_to_vldata array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
() | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 6,
"end_line": 571,
"start_col": 0,
"start_line": 551
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low
let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max
let vldata_to_vlarray_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
(ensures (
vlarray_pred elem_count_min elem_count_max x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low
inline_for_extraction
let vlarray (t: Type) (min max: nat) =
(l: list t { min <= L.length l /\ L.length l <= max } )
inline_for_extraction
let vldata_to_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
: Tot (vlarray t elem_count_min elem_count_max)
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vldata_to_vlarray_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x'
let vldata_to_vlarray_inj
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Lemma
(forall (x1 x2: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)) .
{:pattern
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1);
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2)}
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1 ==
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_vlarray_kind
(array_byte_size_min: nat)
(array_byte_size_max: nat { array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\ array_byte_size_max < 4294967296 } )
: Tot parser_kind
= parse_bounded_vldata_strong_kind array_byte_size_min array_byte_size_max (log256' array_byte_size_max) parse_list_kind
let parse_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Tot (parser (parse_vlarray_kind array_byte_size_min array_byte_size_max) (vlarray t elem_count_min elem_count_max))
= vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s)
`parse_synth`
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u
let parse_vlarray_eq_some
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(input: bytes)
: Lemma
(requires (
Some? (parse (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) input)
))
(ensures (
let sz = log256' array_byte_size_max in
let pi = parse (parse_bounded_integer sz) input in
Some? pi /\ (
let Some (len, c_len) = pi in
c_len == sz /\
array_byte_size_min <= U32.v len /\ U32.v len <= array_byte_size_max /\ (
let input1 = Seq.slice input c_len (Seq.length input) in
U32.v len <= Seq.length input1 /\ (
let input2 = Seq.slice input1 0 (U32.v len) in
let pl = parse (parse_list p) input2 in
Some? pl /\ (
let Some (l, c_l) = pl in
c_l == U32.v len /\
vlarray_pred elem_count_min elem_count_max l /\
parse (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) input == Some (l, c_len + c_l)
))))))
=
parser_kind_prop_equiv k p;
vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max ();
parse_synth_eq (parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s)) (vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max ()) input;
parse_vldata_gen_eq (log256' array_byte_size_max) (in_bounds array_byte_size_min array_byte_size_max) (parse_list p) input;
parser_kind_prop_equiv parse_list_kind (parse_list p)
let vlarray_to_vldata_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
vlarray_pred elem_count_min elem_count_max x
))
(ensures (
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lemma_mult_le_right k.parser_kind_low elem_count_min l;
M.lemma_mult_le_right k.parser_kind_low l elem_count_max
inline_for_extraction
let vlarray_to_vldata
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: vlarray t elem_count_min elem_count_max)
: Tot (parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vlarray_to_vldata_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x'
let vlarray_to_vldata_to_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Lemma
(forall (x: vlarray t elem_count_min elem_count_max) .
{:pattern
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u
(vlarray_to_vldata array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x))
}
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u
(vlarray_to_vldata array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x)
== x)
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max: Prims.nat ->
s: LowParse.Spec.Base.serializer p ->
elem_count_min: Prims.nat ->
elem_count_max: Prims.nat ->
u535:
u536:
Prims.unit
{ LowParse.Spec.Array.vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true }
-> LowParse.Spec.Base.serializer (LowParse.Spec.Array.parse_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u535) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.vldata_vlarray_precond",
"LowParse.Spec.Combinators.serialize_synth",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"Prims.list",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list",
"LowParse.Spec.Array.vlarray",
"LowParse.Spec.VLData.parse_bounded_vldata_strong",
"LowParse.Spec.Array.vldata_to_vlarray",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong",
"LowParse.Spec.Array.vlarray_to_vldata",
"LowParse.Spec.Array.vlarray_to_vldata_to_vlarray",
"LowParse.Spec.Array.vldata_to_vlarray_inj",
"LowParse.Spec.Array.parse_vlarray_kind",
"LowParse.Spec.Array.parse_vlarray"
] | [] | false | false | false | false | false | let serialize_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
: Tot
(serializer (parse_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u)) =
| vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
vlarray_to_vldata_to_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u;
serialize_synth _
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
(serialize_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s))
(vlarray_to_vldata array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
() | false |
Vale.Interop.Base.fst | Vale.Interop.Base.elim_nil | val elim_nil (#dom: list td {Nil? dom}) (#r: Type) (f: n_arrow dom r) : r | val elim_nil (#dom: list td {Nil? dom}) (#r: Type) (f: n_arrow dom r) : r | let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 144,
"start_col": 0,
"start_line": 140
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: Vale.Interop.Base.n_arrow dom r -> r | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Vale.Interop.Base.td",
"Prims.b2t",
"Prims.uu___is_Nil",
"Vale.Interop.Base.n_arrow"
] | [] | false | false | false | false | false | let elim_nil (#dom: list td {Nil? dom}) (#r: Type) (f: n_arrow dom r) : r =
| f | false |
Vale.Interop.Base.fst | Vale.Interop.Base.mem_roots | val mem_roots : args: Prims.list Vale.Interop.Base.arg -> Type | let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args } | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 36,
"end_line": 256,
"start_col": 0,
"start_line": 255
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | args: Prims.list Vale.Interop.Base.arg -> Type | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Vale.Interop.Base.arg",
"FStar.Monotonic.HyperStack.mem",
"Vale.Interop.Base.mem_roots_p"
] | [] | false | false | false | true | true | let mem_roots (args: list arg) =
| h0: HS.mem{mem_roots_p h0 args} | false |
|
LowParse.Spec.Array.fst | LowParse.Spec.Array.vlarray_to_vldata_correct | val vlarray_to_vldata_correct
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(x: list t)
: Lemma
(requires
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true /\ vlarray_pred elem_count_min elem_count_max x))
(ensures
(parse_bounded_vldata_strong_pred array_byte_size_min
array_byte_size_max
(serialize_list _ s)
x)) | val vlarray_to_vldata_correct
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(x: list t)
: Lemma
(requires
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true /\ vlarray_pred elem_count_min elem_count_max x))
(ensures
(parse_bounded_vldata_strong_pred array_byte_size_min
array_byte_size_max
(serialize_list _ s)
x)) | let vlarray_to_vldata_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
vlarray_pred elem_count_min elem_count_max x
))
(ensures (
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lemma_mult_le_right k.parser_kind_low elem_count_min l;
M.lemma_mult_le_right k.parser_kind_low l elem_count_max | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 58,
"end_line": 505,
"start_col": 0,
"start_line": 482
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low
let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max
let vldata_to_vlarray_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
(ensures (
vlarray_pred elem_count_min elem_count_max x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low
inline_for_extraction
let vlarray (t: Type) (min max: nat) =
(l: list t { min <= L.length l /\ L.length l <= max } )
inline_for_extraction
let vldata_to_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
: Tot (vlarray t elem_count_min elem_count_max)
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vldata_to_vlarray_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x'
let vldata_to_vlarray_inj
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Lemma
(forall (x1 x2: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)) .
{:pattern
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1);
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2)}
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1 ==
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_vlarray_kind
(array_byte_size_min: nat)
(array_byte_size_max: nat { array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\ array_byte_size_max < 4294967296 } )
: Tot parser_kind
= parse_bounded_vldata_strong_kind array_byte_size_min array_byte_size_max (log256' array_byte_size_max) parse_list_kind
let parse_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Tot (parser (parse_vlarray_kind array_byte_size_min array_byte_size_max) (vlarray t elem_count_min elem_count_max))
= vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s)
`parse_synth`
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u
let parse_vlarray_eq_some
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(input: bytes)
: Lemma
(requires (
Some? (parse (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) input)
))
(ensures (
let sz = log256' array_byte_size_max in
let pi = parse (parse_bounded_integer sz) input in
Some? pi /\ (
let Some (len, c_len) = pi in
c_len == sz /\
array_byte_size_min <= U32.v len /\ U32.v len <= array_byte_size_max /\ (
let input1 = Seq.slice input c_len (Seq.length input) in
U32.v len <= Seq.length input1 /\ (
let input2 = Seq.slice input1 0 (U32.v len) in
let pl = parse (parse_list p) input2 in
Some? pl /\ (
let Some (l, c_l) = pl in
c_l == U32.v len /\
vlarray_pred elem_count_min elem_count_max l /\
parse (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) input == Some (l, c_len + c_l)
))))))
=
parser_kind_prop_equiv k p;
vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max ();
parse_synth_eq (parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s)) (vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max ()) input;
parse_vldata_gen_eq (log256' array_byte_size_max) (in_bounds array_byte_size_min array_byte_size_max) (parse_list p) input;
parser_kind_prop_equiv parse_list_kind (parse_list p) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max: Prims.nat ->
s: LowParse.Spec.Base.serializer p ->
elem_count_min: Prims.nat ->
elem_count_max: Prims.nat ->
x: Prims.list t
-> FStar.Pervasives.Lemma
(requires
LowParse.Spec.Array.vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true /\ LowParse.Spec.Array.vlarray_pred elem_count_min elem_count_max x)
(ensures
LowParse.Spec.VLData.parse_bounded_vldata_strong_pred array_byte_size_min
array_byte_size_max
(LowParse.Spec.List.serialize_list p s)
x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.list",
"FStar.Math.Lemmas.lemma_mult_le_right",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.unit",
"LowParse.Spec.List.list_length_constant_size_parser_correct",
"Prims._assert",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"LowParse.Spec.List.parse_list",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"FStar.List.Tot.Base.length",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.List.serialize_list",
"Prims.l_and",
"Prims.bool",
"LowParse.Spec.Array.vldata_vlarray_precond",
"LowParse.Spec.Array.vlarray_pred",
"Prims.squash",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_pred",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let vlarray_to_vldata_correct
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(x: list t)
: Lemma
(requires
(vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true /\ vlarray_pred elem_count_min elem_count_max x))
(ensures
(parse_bounded_vldata_strong_pred array_byte_size_min
array_byte_size_max
(serialize_list _ s)
x)) =
| let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lemma_mult_le_right k.parser_kind_low elem_count_min l;
M.lemma_mult_le_right k.parser_kind_low l elem_count_max | false |
Vale.Interop.Base.fst | Vale.Interop.Base.loc_modified_args | val loc_modified_args (args: list arg) : GTot B.loc | val loc_modified_args (args: list arg) : GTot B.loc | let loc_modified_args (args:list arg) : GTot B.loc =
let maybe_union_loc (x:arg) l =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_union (B.loc_buffer x) l
| _ -> l
in
List.Tot.fold_right_gtot args maybe_union_loc B.loc_none | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 60,
"end_line": 281,
"start_col": 0,
"start_line": 275
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args }
[@__reduce__]
let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer []
[@__reduce__]
let modified_arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_buffer x
| _ -> B.loc_none | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | args: Prims.list Vale.Interop.Base.arg -> Prims.GTot LowStar.Monotonic.Buffer.loc | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.list",
"Vale.Interop.Base.arg",
"FStar.List.Tot.Base.fold_right_gtot",
"LowStar.Monotonic.Buffer.loc",
"LowStar.Monotonic.Buffer.loc_none",
"Vale.Arch.HeapTypes_s.base_typ",
"Vale.Arch.HeapTypes_s.taint",
"Prims.bool",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.TD_Buffer",
"Vale.Interop.Base.Mkbuffer_qualifiers",
"LowStar.Monotonic.Buffer.loc_union",
"LowStar.Monotonic.Buffer.loc_buffer",
"Vale.Interop.Types.base_typ_as_type",
"LowStar.Buffer.trivial_preorder",
"Prims.dtuple2",
"Vale.Interop.Base.td"
] | [] | false | false | false | false | false | let loc_modified_args (args: list arg) : GTot B.loc =
| let maybe_union_loc (x: arg) l =
match x with
| (| TD_Buffer _ _ { modified = true } , x |) -> B.loc_union (B.loc_buffer x) l
| _ -> l
in
List.Tot.fold_right_gtot args maybe_union_loc B.loc_none | false |
Vale.Interop.Base.fst | Vale.Interop.Base.default_v_of_typ | val default_v_of_typ (t: base_typ) : (base_typ_as_type t) | val default_v_of_typ (t: base_typ) : (base_typ_as_type t) | let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0 | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 56,
"end_line": 36,
"start_col": 0,
"start_line": 31
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= () | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 0,
"max_fuel": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: Vale.Arch.HeapTypes_s.base_typ -> Vale.Interop.Types.base_typ_as_type t | Prims.Tot | [
"total"
] | [] | [
"Vale.Arch.HeapTypes_s.base_typ",
"FStar.UInt8.uint_to_t",
"FStar.UInt16.uint_to_t",
"FStar.UInt32.uint_to_t",
"FStar.UInt64.uint_to_t",
"Vale.Def.Words_s.Mkfour",
"Vale.Def.Words_s.nat32",
"Vale.Interop.Types.base_typ_as_type"
] | [] | false | false | false | false | false | let default_v_of_typ (t: base_typ) : (base_typ_as_type t) =
| match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0 | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.vldata_to_vlarray | val vldata_to_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
(x: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)
)
: Tot (vlarray t elem_count_min elem_count_max) | val vldata_to_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
(x: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)
)
: Tot (vlarray t elem_count_min elem_count_max) | let vldata_to_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
: Tot (vlarray t elem_count_min elem_count_max)
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vldata_to_vlarray_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x' | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 4,
"end_line": 392,
"start_col": 0,
"start_line": 374
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low
let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max
let vldata_to_vlarray_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
(ensures (
vlarray_pred elem_count_min elem_count_max x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low
inline_for_extraction
let vlarray (t: Type) (min max: nat) =
(l: list t { min <= L.length l /\ L.length l <= max } ) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max: Prims.nat ->
s: LowParse.Spec.Base.serializer p ->
elem_count_min: Prims.nat ->
elem_count_max: Prims.nat ->
u364:
u366:
Prims.unit
{ LowParse.Spec.Array.vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true } ->
x:
LowParse.Spec.VLData.parse_bounded_vldata_strong_t array_byte_size_min
array_byte_size_max
(LowParse.Spec.List.serialize_list p s)
-> LowParse.Spec.Array.vlarray t elem_count_min elem_count_max | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.vldata_vlarray_precond",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"LowParse.Spec.List.parse_list_kind",
"Prims.list",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list",
"LowParse.Spec.Array.vldata_to_vlarray_correct",
"LowParse.Spec.Array.vlarray"
] | [] | false | false | false | false | false | let vldata_to_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
(x:
parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)
)
: Tot (vlarray t elem_count_min elem_count_max) =
| [@@ inline_let ]let x':list t = x in
[@@ inline_let ]let _ =
vldata_to_vlarray_correct array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
x'
in
x' | false |
Vale.Interop.Base.fst | Vale.Interop.Base.modified_arg_loc | val modified_arg_loc (x: arg) : GTot B.loc | val modified_arg_loc (x: arg) : GTot B.loc | let modified_arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_buffer x
| _ -> B.loc_none | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 21,
"end_line": 272,
"start_col": 0,
"start_line": 269
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args }
[@__reduce__]
let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer [] | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Vale.Interop.Base.arg -> Prims.GTot LowStar.Monotonic.Buffer.loc | Prims.GTot | [
"sometrivial"
] | [] | [
"Vale.Interop.Base.arg",
"Vale.Arch.HeapTypes_s.base_typ",
"Vale.Arch.HeapTypes_s.taint",
"Prims.bool",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.TD_Buffer",
"Vale.Interop.Base.Mkbuffer_qualifiers",
"LowStar.Monotonic.Buffer.loc_buffer",
"Vale.Interop.Types.base_typ_as_type",
"LowStar.Buffer.trivial_preorder",
"Prims.dtuple2",
"Vale.Interop.Base.td",
"LowStar.Monotonic.Buffer.loc_none",
"LowStar.Monotonic.Buffer.loc"
] | [] | false | false | false | false | false | let modified_arg_loc (x: arg) : GTot B.loc =
| match x with
| (| TD_Buffer _ _ { modified = true } , x |) -> B.loc_buffer x
| _ -> B.loc_none | false |
Vale.Interop.Base.fst | Vale.Interop.Base.n_dep_arrow | val n_dep_arrow : dom: Prims.list Vale.Interop.Base.td -> codom: Vale.Interop.Base.n_arrow dom Type -> Type | let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x) | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 64,
"end_line": 165,
"start_col": 0,
"start_line": 162
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
//////////////////////////////////////////////////////////////////////////////// | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | dom: Prims.list Vale.Interop.Base.td -> codom: Vale.Interop.Base.n_arrow dom Type -> Type | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Vale.Interop.Base.td",
"Vale.Interop.Base.n_arrow",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.n_dep_arrow",
"Vale.Interop.Base.elim_1"
] | [
"recursion"
] | false | false | false | false | true | let rec n_dep_arrow (dom: list td) (codom: n_arrow dom Type) =
| match dom with
| [] -> codom
| hd :: tl -> x: td_as_type hd -> n_dep_arrow tl (elim_1 codom x) | false |
|
Vale.Interop.Base.fst | Vale.Interop.Base.loc_all_args | val loc_all_args (args: list arg) : GTot B.loc | val loc_all_args (args: list arg) : GTot B.loc | let loc_all_args (args:list arg) : GTot B.loc =
let l = List.Tot.map_gtot arg_loc args in
List.Tot.fold_right_gtot l B.loc_union B.loc_none | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 53,
"end_line": 293,
"start_col": 0,
"start_line": 291
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args }
[@__reduce__]
let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer []
[@__reduce__]
let modified_arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_buffer x
| _ -> B.loc_none
[@__reduce__]
let loc_modified_args (args:list arg) : GTot B.loc =
let maybe_union_loc (x:arg) l =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_union (B.loc_buffer x) l
| _ -> l
in
List.Tot.fold_right_gtot args maybe_union_loc B.loc_none
[@__reduce__]
let arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ _, x|) -> B.loc_buffer x
| (|TD_ImmBuffer _ _ _, x|) -> B.loc_buffer x
| (|TD_Base _, _|) -> B.loc_none | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | args: Prims.list Vale.Interop.Base.arg -> Prims.GTot LowStar.Monotonic.Buffer.loc | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.list",
"Vale.Interop.Base.arg",
"FStar.List.Tot.Base.fold_right_gtot",
"LowStar.Monotonic.Buffer.loc",
"LowStar.Monotonic.Buffer.loc_union",
"LowStar.Monotonic.Buffer.loc_none",
"FStar.List.Tot.Base.map_gtot",
"Vale.Interop.Base.arg_loc"
] | [] | false | false | false | false | false | let loc_all_args (args: list arg) : GTot B.loc =
| let l = List.Tot.map_gtot arg_loc args in
List.Tot.fold_right_gtot l B.loc_union B.loc_none | false |
Vale.Interop.Base.fst | Vale.Interop.Base.elim_1 | val elim_1: #dom: list td {Cons? dom} -> #r: Type -> f: n_arrow dom r -> td_as_type (Cons?.hd dom)
-> n_arrow (Cons?.tl dom) r | val elim_1: #dom: list td {Cons? dom} -> #r: Type -> f: n_arrow dom r -> td_as_type (Cons?.hd dom)
-> n_arrow (Cons?.tl dom) r | let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 137,
"start_col": 0,
"start_line": 133
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: Vale.Interop.Base.n_arrow dom r -> _: Vale.Interop.Base.td_as_type (Cons?.hd dom)
-> Vale.Interop.Base.n_arrow (Cons?.tl dom) r | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Vale.Interop.Base.td",
"Prims.b2t",
"Prims.uu___is_Cons",
"Vale.Interop.Base.n_arrow",
"Vale.Interop.Base.td_as_type",
"Prims.__proj__Cons__item__hd",
"Prims.__proj__Cons__item__tl"
] | [] | false | false | false | false | false | let elim_1 (#dom: list td {Cons? dom}) (#r: Type) (f: n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
| f | false |
Vale.Interop.Base.fst | Vale.Interop.Base.arg_of_sb | val arg_of_sb (#t: _) (x: buf_t TUInt64 t) : arg | val arg_of_sb (#t: _) (x: buf_t TUInt64 t) : arg | let arg_of_sb #t (x:buf_t TUInt64 t) :arg = (| TD_Buffer TUInt64 t stack_bq, x |) | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 81,
"end_line": 428,
"start_col": 0,
"start_line": 428
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args }
[@__reduce__]
let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer []
[@__reduce__]
let modified_arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_buffer x
| _ -> B.loc_none
[@__reduce__]
let loc_modified_args (args:list arg) : GTot B.loc =
let maybe_union_loc (x:arg) l =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_union (B.loc_buffer x) l
| _ -> l
in
List.Tot.fold_right_gtot args maybe_union_loc B.loc_none
[@__reduce__]
let arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ _, x|) -> B.loc_buffer x
| (|TD_ImmBuffer _ _ _, x|) -> B.loc_buffer x
| (|TD_Base _, _|) -> B.loc_none
[@__reduce__]
let loc_all_args (args:list arg) : GTot B.loc =
let l = List.Tot.map_gtot arg_loc args in
List.Tot.fold_right_gtot l B.loc_union B.loc_none
let all_live_cons (hd:arg) (tl:list arg) (h0:HS.mem)
: Lemma
(all_live h0 (hd :: tl) <==> (live_arg h0 hd /\ all_live h0 tl))
= ()
let disjoint_or_eq_def (l:list arg)
: Lemma (disjoint_or_eq l == BigOps.pairwise_and' disjoint_or_eq_1 l)
= ()
let disjoint_or_eq_cons (hd:arg) (tl:list arg)
: Lemma (disjoint_or_eq (hd::tl) <==> (BigOps.big_and' (disjoint_or_eq_1 hd) tl /\ disjoint_or_eq tl))
= BigOps.pairwise_and'_cons disjoint_or_eq_1 hd tl
#set-options "--initial_ifuel 2 --max_fuel 2"
let rec args_b8_mem (l:list arg) (y:b8)
: Lemma (L.memP y (args_b8 l) <==>
(exists (a:arg). {:pattern L.memP a l}
L.memP a l /\
(match a with
| (| TD_Base _, _|) -> False
| (| TD_Buffer src _ _, x|) -> mut_to_b8 src x == y
| (| TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x == y)))
= let goal (l:list arg) (a:arg) =
L.memP a l /\
(match a with
| (| TD_Base _, _|) -> False
| (| TD_Buffer src _ _, x|) -> mut_to_b8 src x == y
| (| TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x == y)
in
match l with
| [] -> ()
| hd::tl ->
match hd with
| (| TD_Base _, _ |) ->
args_b8_mem tl y;
assert ((exists a. goal tl a) ==> (exists a. goal l a))
| (| TD_Buffer bt _ q, x |) ->
let aux_1 ()
: Lemma (requires (y == mut_to_b8 bt x))
(ensures (exists a. goal l a)) =
FStar.Classical.exists_intro (goal l) hd
in
let aux_2 ()
: Lemma (requires (mut_to_b8 bt x =!= y))
(ensures (L.memP y (args_b8 l) <==> (exists a. goal l a))) =
args_b8_mem tl y
in
FStar.Classical.move_requires aux_1 ();
FStar.Classical.move_requires aux_2 ()
| (| TD_ImmBuffer bt _ q, x |) ->
let aux_1 ()
: Lemma (requires (y == imm_to_b8 bt x))
(ensures (exists a. goal l a)) =
FStar.Classical.exists_intro (goal l) hd
in
let aux_2 ()
: Lemma (requires (imm_to_b8 bt x =!= y))
(ensures (L.memP y (args_b8 l) <==> (exists a. goal l a))) =
args_b8_mem tl y
in
FStar.Classical.move_requires aux_1 ();
FStar.Classical.move_requires aux_2 ()
let rec args_b8_disjoint_or_eq (args:list arg)
: Lemma
(requires (disjoint_or_eq args))
(ensures (list_disjoint_or_eq (args_b8 args)))
=
list_disjoint_or_eq_reveal ();
match args with
| [] -> ()
| hd::tl ->
disjoint_or_eq_cons hd tl;
args_b8_disjoint_or_eq tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl;
FStar.Classical.forall_intro (args_b8_mem tl)
let rec args_b8_live (hs:HS.mem) (args:list arg{all_live hs args})
: Lemma (list_live hs (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
all_live_cons hd tl hs;
assert (live_arg hs hd);
assert (all_live hs tl);
args_b8_live hs tl;
match hd with
| (| TD_Base _ , _ |) ->
assert (args_b8 args == args_b8 tl)
| (| TD_Buffer t _ _, x |) ->
assert (B.live hs x);
assert (args_b8 args == Buffer true x :: args_b8 tl)
| (| TD_ImmBuffer t _ _, x |) ->
assert (B.live hs x);
assert (args_b8 args == imm_to_b8 t x :: args_b8 tl)
let liveness_disjointness (args:list arg) (h:mem_roots args)
: Lemma (list_disjoint_or_eq (args_b8 args) /\
list_live h (args_b8 args))
= args_b8_disjoint_or_eq args;
args_b8_live h args
let mk_mem (args:list arg) (h:mem_roots args) : GTot interop_heap =
liveness_disjointness args h;
mem_of_hs_roots (args_b8 args) h
let mk_mem_injective (args:list arg) (h:mem_roots args)
: Lemma (hs_of_mem (mk_mem args h) == h /\
ptrs_of_mem (mk_mem args h) == args_b8 args)
= ()
let rec mem_roots_p_modifies_none (args:list arg) (h0:HS.mem) (h1:HS.mem)
: Lemma
(requires
mem_roots_p h0 args /\
B.modifies B.loc_none h0 h1)
(ensures
mem_roots_p h1 args)
= match args with
| [] -> ()
| hd::tl ->
all_live_cons hd tl h0;
mem_roots_p_modifies_none tl h0 h1;
match hd with
| (| TD_Base _, _ |) -> ()
| (| TD_Buffer _ _ _, x |)
| (| TD_ImmBuffer _ _ _, x |) -> assert (B.live h1 x)
[@__reduce__]
let arg_of_lb #src #t (x:buf_t src t) : arg = (| TD_Buffer src t default_bq, x |) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 2,
"max_fuel": 2,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Vale.Interop.Base.buf_t Vale.Arch.HeapTypes_s.TUInt64 t -> Vale.Interop.Base.arg | Prims.Tot | [
"total"
] | [] | [
"Vale.Arch.HeapTypes_s.base_typ",
"Vale.Interop.Base.buf_t",
"Vale.Arch.HeapTypes_s.TUInt64",
"Prims.Mkdtuple2",
"Vale.Interop.Base.td",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.TD_Buffer",
"Vale.Interop.Base.stack_bq",
"Vale.Interop.Base.arg"
] | [] | false | false | false | false | false | let arg_of_sb #t (x: buf_t TUInt64 t) : arg =
| (| TD_Buffer TUInt64 t stack_bq, x |) | false |
Vale.Interop.Base.fst | Vale.Interop.Base.arg_of_lb | val arg_of_lb (#src #t: _) (x: buf_t src t) : arg | val arg_of_lb (#src #t: _) (x: buf_t src t) : arg | let arg_of_lb #src #t (x:buf_t src t) : arg = (| TD_Buffer src t default_bq, x |) | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 81,
"end_line": 425,
"start_col": 0,
"start_line": 425
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args }
[@__reduce__]
let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer []
[@__reduce__]
let modified_arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_buffer x
| _ -> B.loc_none
[@__reduce__]
let loc_modified_args (args:list arg) : GTot B.loc =
let maybe_union_loc (x:arg) l =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_union (B.loc_buffer x) l
| _ -> l
in
List.Tot.fold_right_gtot args maybe_union_loc B.loc_none
[@__reduce__]
let arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ _, x|) -> B.loc_buffer x
| (|TD_ImmBuffer _ _ _, x|) -> B.loc_buffer x
| (|TD_Base _, _|) -> B.loc_none
[@__reduce__]
let loc_all_args (args:list arg) : GTot B.loc =
let l = List.Tot.map_gtot arg_loc args in
List.Tot.fold_right_gtot l B.loc_union B.loc_none
let all_live_cons (hd:arg) (tl:list arg) (h0:HS.mem)
: Lemma
(all_live h0 (hd :: tl) <==> (live_arg h0 hd /\ all_live h0 tl))
= ()
let disjoint_or_eq_def (l:list arg)
: Lemma (disjoint_or_eq l == BigOps.pairwise_and' disjoint_or_eq_1 l)
= ()
let disjoint_or_eq_cons (hd:arg) (tl:list arg)
: Lemma (disjoint_or_eq (hd::tl) <==> (BigOps.big_and' (disjoint_or_eq_1 hd) tl /\ disjoint_or_eq tl))
= BigOps.pairwise_and'_cons disjoint_or_eq_1 hd tl
#set-options "--initial_ifuel 2 --max_fuel 2"
let rec args_b8_mem (l:list arg) (y:b8)
: Lemma (L.memP y (args_b8 l) <==>
(exists (a:arg). {:pattern L.memP a l}
L.memP a l /\
(match a with
| (| TD_Base _, _|) -> False
| (| TD_Buffer src _ _, x|) -> mut_to_b8 src x == y
| (| TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x == y)))
= let goal (l:list arg) (a:arg) =
L.memP a l /\
(match a with
| (| TD_Base _, _|) -> False
| (| TD_Buffer src _ _, x|) -> mut_to_b8 src x == y
| (| TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x == y)
in
match l with
| [] -> ()
| hd::tl ->
match hd with
| (| TD_Base _, _ |) ->
args_b8_mem tl y;
assert ((exists a. goal tl a) ==> (exists a. goal l a))
| (| TD_Buffer bt _ q, x |) ->
let aux_1 ()
: Lemma (requires (y == mut_to_b8 bt x))
(ensures (exists a. goal l a)) =
FStar.Classical.exists_intro (goal l) hd
in
let aux_2 ()
: Lemma (requires (mut_to_b8 bt x =!= y))
(ensures (L.memP y (args_b8 l) <==> (exists a. goal l a))) =
args_b8_mem tl y
in
FStar.Classical.move_requires aux_1 ();
FStar.Classical.move_requires aux_2 ()
| (| TD_ImmBuffer bt _ q, x |) ->
let aux_1 ()
: Lemma (requires (y == imm_to_b8 bt x))
(ensures (exists a. goal l a)) =
FStar.Classical.exists_intro (goal l) hd
in
let aux_2 ()
: Lemma (requires (imm_to_b8 bt x =!= y))
(ensures (L.memP y (args_b8 l) <==> (exists a. goal l a))) =
args_b8_mem tl y
in
FStar.Classical.move_requires aux_1 ();
FStar.Classical.move_requires aux_2 ()
let rec args_b8_disjoint_or_eq (args:list arg)
: Lemma
(requires (disjoint_or_eq args))
(ensures (list_disjoint_or_eq (args_b8 args)))
=
list_disjoint_or_eq_reveal ();
match args with
| [] -> ()
| hd::tl ->
disjoint_or_eq_cons hd tl;
args_b8_disjoint_or_eq tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl;
FStar.Classical.forall_intro (args_b8_mem tl)
let rec args_b8_live (hs:HS.mem) (args:list arg{all_live hs args})
: Lemma (list_live hs (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
all_live_cons hd tl hs;
assert (live_arg hs hd);
assert (all_live hs tl);
args_b8_live hs tl;
match hd with
| (| TD_Base _ , _ |) ->
assert (args_b8 args == args_b8 tl)
| (| TD_Buffer t _ _, x |) ->
assert (B.live hs x);
assert (args_b8 args == Buffer true x :: args_b8 tl)
| (| TD_ImmBuffer t _ _, x |) ->
assert (B.live hs x);
assert (args_b8 args == imm_to_b8 t x :: args_b8 tl)
let liveness_disjointness (args:list arg) (h:mem_roots args)
: Lemma (list_disjoint_or_eq (args_b8 args) /\
list_live h (args_b8 args))
= args_b8_disjoint_or_eq args;
args_b8_live h args
let mk_mem (args:list arg) (h:mem_roots args) : GTot interop_heap =
liveness_disjointness args h;
mem_of_hs_roots (args_b8 args) h
let mk_mem_injective (args:list arg) (h:mem_roots args)
: Lemma (hs_of_mem (mk_mem args h) == h /\
ptrs_of_mem (mk_mem args h) == args_b8 args)
= ()
let rec mem_roots_p_modifies_none (args:list arg) (h0:HS.mem) (h1:HS.mem)
: Lemma
(requires
mem_roots_p h0 args /\
B.modifies B.loc_none h0 h1)
(ensures
mem_roots_p h1 args)
= match args with
| [] -> ()
| hd::tl ->
all_live_cons hd tl h0;
mem_roots_p_modifies_none tl h0 h1;
match hd with
| (| TD_Base _, _ |) -> ()
| (| TD_Buffer _ _ _, x |)
| (| TD_ImmBuffer _ _ _, x |) -> assert (B.live h1 x) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 2,
"max_fuel": 2,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Vale.Interop.Base.buf_t src t -> Vale.Interop.Base.arg | Prims.Tot | [
"total"
] | [] | [
"Vale.Arch.HeapTypes_s.base_typ",
"Vale.Interop.Base.buf_t",
"Prims.Mkdtuple2",
"Vale.Interop.Base.td",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.TD_Buffer",
"Vale.Interop.Base.default_bq",
"Vale.Interop.Base.arg"
] | [] | false | false | false | false | false | let arg_of_lb #src #t (x: buf_t src t) : arg =
| (| TD_Buffer src t default_bq, x |) | false |
Vale.Interop.Base.fst | Vale.Interop.Base.disjoint_not_eq | val disjoint_not_eq : x: LowStar.Monotonic.Buffer.mbuffer (Vale.Interop.Types.base_typ_as_type src1) rel1 rrel1 ->
y: LowStar.Monotonic.Buffer.mbuffer (Vale.Interop.Types.base_typ_as_type src2) rel2 rrel2
-> Prims.logical | let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y) | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 64,
"end_line": 216,
"start_col": 0,
"start_line": 209
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
//////////////////////////////////////////////////////////////////////////////// | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: LowStar.Monotonic.Buffer.mbuffer (Vale.Interop.Types.base_typ_as_type src1) rel1 rrel1 ->
y: LowStar.Monotonic.Buffer.mbuffer (Vale.Interop.Types.base_typ_as_type src2) rel2 rrel2
-> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Vale.Arch.HeapTypes_s.base_typ",
"LowStar.Monotonic.Buffer.srel",
"Vale.Interop.Types.base_typ_as_type",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.l_and",
"LowStar.Monotonic.Buffer.loc_disjoint",
"LowStar.Monotonic.Buffer.loc_buffer",
"Prims.l_not",
"Prims.eq2",
"FStar.Preorder.relation",
"FStar.Seq.Base.seq",
"Prims.l_or",
"FStar.Preorder.preorder_rel",
"Prims.logical"
] | [] | false | false | false | false | true | let disjoint_not_eq
(#src1 #src2: base_typ)
(#rel1 #rrel1: MB.srel (base_typ_as_type src1))
(#rel2 #rrel2: MB.srel (base_typ_as_type src2))
(x: MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y: MB.mbuffer (base_typ_as_type src2) rel2 rrel2)
=
| B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~(src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y) | false |
|
Vale.Interop.Base.fst | Vale.Interop.Base.intro_dep_arrow_1 | val intro_dep_arrow_1 (a: td) (b: n_arrow [a] Type) (f: (x: td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b | val intro_dep_arrow_1 (a: td) (b: n_arrow [a] Type) (f: (x: td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b | let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 178,
"start_col": 0,
"start_line": 174
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Vale.Interop.Base.td ->
b: Vale.Interop.Base.n_arrow [a] Type ->
f: (x: Vale.Interop.Base.td_as_type a -> Vale.Interop.Base.elim_1 b x)
-> Vale.Interop.Base.n_dep_arrow [a] b | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.Base.td",
"Vale.Interop.Base.n_arrow",
"Prims.Cons",
"Prims.Nil",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.elim_1",
"Vale.Interop.Base.n_dep_arrow"
] | [] | false | false | false | false | false | let intro_dep_arrow_1 (a: td) (b: n_arrow [a] Type) (f: (x: td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b =
| f | false |
Vale.Interop.Base.fst | Vale.Interop.Base.elim_dep_arrow_nil | val elim_dep_arrow_nil (#codom: n_arrow [] Type) (f: n_dep_arrow [] codom) : elim_nil codom | val elim_dep_arrow_nil (#codom: n_arrow [] Type) (f: n_dep_arrow [] codom) : elim_nil codom | let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 6,
"end_line": 192,
"start_col": 0,
"start_line": 189
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: Vale.Interop.Base.n_dep_arrow [] codom -> Vale.Interop.Base.elim_nil codom | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.Base.n_arrow",
"Prims.Nil",
"Vale.Interop.Base.td",
"Vale.Interop.Base.n_dep_arrow",
"Vale.Interop.Base.elim_nil"
] | [] | false | false | false | false | false | let elim_dep_arrow_nil (#codom: n_arrow [] Type) (f: n_dep_arrow [] codom) : elim_nil codom =
| f | false |
Vale.Interop.Base.fst | Vale.Interop.Base.live_arg | val live_arg : h: FStar.Monotonic.HyperStack.mem -> x: Vale.Interop.Base.arg -> Type0 | let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 31,
"end_line": 243,
"start_col": 0,
"start_line": 239
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h: FStar.Monotonic.HyperStack.mem -> x: Vale.Interop.Base.arg -> Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"Vale.Interop.Base.arg",
"Vale.Arch.HeapTypes_s.base_typ",
"Vale.Interop.Base.buffer_qualifiers",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.TD_Buffer",
"LowStar.Monotonic.Buffer.live",
"Vale.Interop.Types.base_typ_as_type",
"LowStar.Buffer.trivial_preorder",
"Vale.Interop.Base.TD_ImmBuffer",
"LowStar.ImmutableBuffer.immutable_preorder",
"Vale.Interop.Base.valid_base_type",
"Vale.Interop.Base.TD_Base",
"Prims.l_True"
] | [] | false | false | false | true | true | let live_arg (h: HS.mem) (x: arg) =
| match x with
| (| TD_Buffer _ _ _ , x |) | (| TD_ImmBuffer _ _ _ , x |) -> B.live h x
| (| TD_Base _ , _ |) -> True | false |
|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.init_key_block | val init_key_block (a : alg) (kk : key_size a) (k : stateful_key_t a kk)
(buf_: B.buffer uint8 { B.length buf_ = Spec.size_block a }) :
ST.Stack unit
(requires fun h0 ->
let key = stateful_key a kk in
key.invariant h0 k /\
B.live h0 buf_ /\
B.(loc_disjoint (loc_buffer buf_) (key.footprint h0 k)))
(ensures fun h0 _ h1 ->
B.(modifies (loc_buffer buf_) h0 h1) /\
begin
let k = (stateful_key a kk).v () h0 k in
let input_length = if kk > 0 then Spec.size_block a else 0 in
let input = if kk > 0 then Spec.blake2_key_block a kk k else S.empty in
S.equal (S.slice (B.as_seq h1 buf_) 0 input_length) input
end) | val init_key_block (a : alg) (kk : key_size a) (k : stateful_key_t a kk)
(buf_: B.buffer uint8 { B.length buf_ = Spec.size_block a }) :
ST.Stack unit
(requires fun h0 ->
let key = stateful_key a kk in
key.invariant h0 k /\
B.live h0 buf_ /\
B.(loc_disjoint (loc_buffer buf_) (key.footprint h0 k)))
(ensures fun h0 _ h1 ->
B.(modifies (loc_buffer buf_) h0 h1) /\
begin
let k = (stateful_key a kk).v () h0 k in
let input_length = if kk > 0 then Spec.size_block a else 0 in
let input = if kk > 0 then Spec.blake2_key_block a kk k else S.empty in
S.equal (S.slice (B.as_seq h1 buf_) 0 input_length) input
end) | let init_key_block a kk k buf_ =
if kk = 0 then ()
else
begin
(**) let h0 = ST.get () in
(* Set the end of the buffer to 0 *)
[@inline_let] let sub_b_len = U32.(block_len a -^ U32.uint_to_t kk) in
let sub_b = B.sub buf_ (U32.uint_to_t kk) sub_b_len in
B.fill sub_b (Lib.IntTypes.u8 0) sub_b_len;
(**) let h1 = ST.get () in
(**) assert(S.slice (B.as_seq h1 buf_) kk (Spec.size_block a) `S.equal` B.as_seq h1 sub_b);
(* Copy the key at the beginning of the buffer *)
Lib.Buffer.update_sub #Lib.Buffer.MUT #uint8 #(U32.uint_to_t (Spec.size_block a))
buf_ 0ul (U32.uint_to_t kk) (stateful_key_to_buffer k);
(**) let h2 = ST.get () in
(**) begin
(**) let k : LS.lseq uint8 kk = (stateful_key a kk).v () h0 k in
(**) let buf_v1 : LS.lseq uint8 (Spec.size_block a) = B.as_seq h1 buf_ in
(**) let buf_v2 : LS.lseq uint8 (Spec.size_block a) = B.as_seq h2 buf_ in
(* Prove that [buf_] is equal to [key @ create ... 0] *)
(**) assert(buf_v2 `S.equal` LS.update_sub buf_v1 0 kk k);
(**) let zeroed : LS.lseq uint8 (Spec.size_block a - kk) = S.create (Spec.size_block a - kk) (Lib.IntTypes.u8 0) in
(**) assert(B.as_seq h1 sub_b `S.equal` zeroed);
(**) let key_and_zeroed : LS.lseq uint8 (Spec.size_block a) = Seq.append k zeroed in
(**) assert(S.equal (S.slice key_and_zeroed 0 kk) k);
(**) assert(S.equal (S.slice key_and_zeroed kk (Spec.size_block a)) zeroed);
(**) LS.lemma_update_sub #uint8 #(Spec.size_block a) buf_v1 0 kk k key_and_zeroed;
(**) assert(buf_v2 `S.equal` key_and_zeroed);
(* Prove that the initial input is equal to [key @ create ... 0] *)
(**) let input = Spec.blake2_key_block a kk k in
(**) let key_block0: LS.lseq uint8 (Spec.size_block a) = S.create (Spec.size_block a) (Lib.IntTypes.u8 0) in
(**) assert(input `S.equal` LS.update_sub key_block0 0 kk k);
(**) assert(Seq.equal (LS.sub key_and_zeroed 0 kk) k);
(**) assert(Seq.equal (LS.sub key_and_zeroed kk (Spec.size_block a - kk))
(LS.sub key_block0 kk (Spec.size_block a - kk)));
(**) LS.lemma_update_sub #uint8 #(Spec.size_block a) key_block0 0 kk k key_and_zeroed;
(**) assert(input `S.equal` key_and_zeroed)
(**) end
end | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 7,
"end_line": 544,
"start_col": 0,
"start_line": 502
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a
inline_for_extraction noextract
let output_size (a : alg) : nat = Spec.max_output a
inline_for_extraction noextract
let output_len (a : alg) = U32.uint_to_t (output_size a)
/// From the functor-provided previous length (uint64, public) to a suitable
/// type for Blake2 (secret uint64/uint128)
inline_for_extraction noextract
let blake2_prevlen (a : alg)
(prevlen : U64.t{ U64.v prevlen <= max_input_length a}) :
x:Spec.limb_t a {
Lib.IntTypes.uint_v x = U64.v prevlen } =
let open Lib.IntTypes in
match a with
| Spec.Blake2S -> to_u64 #U64 #PUB prevlen
| Spec.Blake2B ->
[@inline_let] let x : uint64 = to_u64 #U64 #PUB prevlen in
Lib.IntTypes.cast U128 SEC x
/// Specs
/// -----
noextract
let init_s (a : alg) (kk : size_nat{kk <= max_key a}) :
Tot (t a) =
Spec.blake2_init_hash a (Spec.blake2_default_params a) kk (output_size a)
noextract
let update_multi_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ prevlen + S.length input <= max_input_length a /\
S.length input % Spec.size_block a = 0 }) :
Tot (t a)
=
let nb = S.length input / U32.v (block_len a) in
Lib.LoopCombinators.repeati nb (Spec.blake2_update1 a prevlen input) acc
noextract
let update_last_s (#a : alg) (acc : t a)
(prevlen : nat{prevlen % Spec.size_block a = 0})
(input : Seq.seq uint8{ S.length input + prevlen <= max_input_length a /\
S.length input <= Spec.size_block a }) :
Tot (t a) =
Spec.blake2_update_last a prevlen (S.length input) input acc
noextract
let finish_s (#a : alg) (acc : t a) :
output : S.seq uint8 { S.length output = U32.v (output_len a) } =
Spec.blake2_finish a acc (U32.v (output_len a))
noextract
let spec_s (a : alg)
(kk : size_nat{kk <= max_key a})
(key : lbytes kk)
(input : S.seq uint8{if kk = 0 then S.length input <= max_input_length a else S.length input + Spec.size_block a <= max_input_length a}) =
Spec.blake2 a input (Spec.blake2_default_params a) kk key (output_size a)
/// Interlude for spec proofs
/// -------------------------
val update_multi_zero:
#a : alg ->
acc:t a ->
prevlen:nat{prevlen % Spec.size_block a = 0} ->
Lemma
(requires (prevlen <= max_input_length a))
(ensures (update_multi_s #a acc prevlen S.empty == acc))
let update_multi_zero #a acc prevlen =
Lib.LoopCombinators.eq_repeati0 (0 / U32.v (block_len a)) (Spec.blake2_update1 a prevlen S.empty) acc
#push-options "--z3cliopt smt.arith.nl=false"
val update_multi_associative:
#a : alg ->
acc: t a ->
prevlen1:nat ->
prevlen2:nat ->
input1:S.seq uint8 ->
input2:S.seq uint8 ->
Lemma
(requires (
(**) Math.Lemmas.pos_times_pos_is_pos Spec.size_block_w (Spec.size_word a);
prevlen1 % Spec.size_block a = 0 /\
S.length input1 % Spec.size_block a = 0 /\
S.length input2 % Spec.size_block a = 0 /\
prevlen1 + S.length input1 + S.length input2 <= max_input_length a /\
prevlen2 = prevlen1 + S.length input1))
(ensures (
let input = S.append input1 input2 in
S.length input % Spec.size_block a = 0 /\
prevlen2 % Spec.size_block a = 0 /\
update_multi_s (update_multi_s acc prevlen1 input1) prevlen2 input2 ==
update_multi_s acc prevlen1 input))
#pop-options
#push-options "--z3rlimit 400"
let update_multi_associative #a acc prevlen1 prevlen2 input1 input2 =
let input = S.append input1 input2 in
let nb = S.length input / U32.v (block_len a) in
let nb1 = S.length input1 / U32.v (block_len a) in
let nb2 = S.length input2 / U32.v (block_len a) in
let f = Spec.blake2_update1 a prevlen1 input in
let f1 = Spec.blake2_update1 a prevlen1 input1 in
let f2 = Spec.blake2_update1 a prevlen2 input2 in
let aux1 (i:nat{i < nb1}) (acc:t a) : Lemma (f i acc == f1 i acc)
= assert (Spec.get_blocki a input i `Seq.equal` Spec.get_blocki a input1 i)
in
let aux2 (i:nat{i < nb2}) (acc:t a) : Lemma (f2 i acc == f (i + nb1) acc)
= assert (Spec.get_blocki a input2 i `Seq.equal` Spec.get_blocki a input (i + nb1))
in
let open Lib.LoopCombinators in
let open Lib.Sequence.Lemmas in
calc (==) {
update_multi_s (update_multi_s acc prevlen1 input1) prevlen2 input2;
(==) { }
repeati nb2 f2 (repeati nb1 f1 acc);
(==) {
Classical.forall_intro_2 aux1;
repeati_extensionality nb1 f1 f acc
}
repeati nb2 f2 (repeati nb1 f acc);
(==) {
repeati_def nb1 f acc;
repeati_def nb2 f2 (repeat_right 0 nb1 (fixed_a (t a)) f acc)
}
repeat_right 0 nb2 (fixed_a (t a)) f2 (repeat_right 0 nb1 (fixed_a (t a)) f acc);
(==) {
Classical.forall_intro_2 aux2;
repeat_gen_right_extensionality nb2 nb1 (fixed_a (t a)) (fixed_a (t a)) f2 f (repeat_right 0 nb1 (fixed_a (t a)) f acc)
}
repeat_right nb1 (nb1 + nb2) (fixed_a (t a)) f (repeat_right 0 nb1 (fixed_a (t a)) f acc);
(==) { repeat_right_plus 0 nb1 nb (fixed_a (t a)) f acc; repeati_def nb f acc }
repeati nb f acc;
(==) { }
update_multi_s acc prevlen1 input;
}
#pop-options
/// A helper function: the hash incremental function defined with the functions
/// locally defined (with a signature adapted to the functor).
noextract
val blake2_hash_incremental_s :
a : alg ->
kk: size_nat{kk <= max_key a} ->
k: lbytes kk ->
input:S.seq uint8 { if kk = 0 then S.length input <= max_input_length a else S.length input + (Spec.size_block a) <= max_input_length a } ->
output:S.seq uint8 { S.length output = output_size a }
#push-options "--z3cliopt smt.arith.nl=false"
let blake2_hash_incremental_s a kk k input0 =
let key_block = if kk > 0 then Spec.blake2_key_block a kk k else S.empty in
let key_block_len = S.length key_block in
let input = Seq.append key_block input0 in
assert (key_block_len = (if kk = 0 then 0 else Spec.size_block a));
(**) Math.Lemmas.modulo_lemma 0 (U32.v (block_len a));
let bs, l = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
let acc1 = init_s a kk in
let acc2 = update_multi_s #a acc1 0 bs in
let acc3 = update_last_s #a acc2 (S.length bs) l in
let acc4 = finish_s #a acc3 in
acc4
#pop-options
#push-options "--z3cliopt smt.arith.nl=false"
val repeati_split_at_eq :
a : alg ->
s : t a ->
input:S.seq uint8 { S.length input <= max_input_length a } ->
Lemma(
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
n_blocks = Lib.Sequence.length blocks / Spec.size_block a /\ // This is necessary for type-checking
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 input) s ==
Lib.LoopCombinators.repeati n_blocks (Spec.blake2_update1 a 0 blocks) s)
#pop-options
#push-options "--z3cliopt smt.arith.nl=false"
let repeati_split_at_eq a s input =
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
let f = Spec.blake2_update1 a 0 input in
let g = Spec.blake2_update1 a 0 blocks in
let s1 = Lib.LoopCombinators.repeati n_blocks f s in
assert (Lib.Sequence.length blocks = n_blocks * Spec.size_block a);
Math.Lemmas.cancel_mul_div n_blocks (Spec.size_block a);
assert (n_blocks = Lib.Sequence.length blocks / Spec.size_block a);
assert (Lib.Sequence.length blocks <= max_input_length a);
let s2 = Lib.LoopCombinators.repeati n_blocks g s in
assert (input `Seq.equal` Seq.append blocks last);
assert (S.length input = S.length blocks + S.length last);
introduce forall (i:nat{i < n_blocks}). (Spec.get_blocki a input i) `S.equal` (Spec.get_blocki a blocks i)
with
begin
let b0 = Spec.get_blocki a input i in
let b1 = Spec.get_blocki a blocks i in
assert (S.length blocks = n_blocks * Spec.size_block a);
Math.Lemmas.lemma_mult_le_right (Spec.size_block a) (i + 1) n_blocks;
assert ((i + 1) * Spec.size_block a <= S.length blocks);
Math.Lemmas.lemma_mult_le_right (Spec.size_block a) i n_blocks;
assert (i * Spec.size_block a <= S.length blocks);
Math.Lemmas.distributivity_add_left i 1 (Spec.size_block a);
assert ((i + 1) * Spec.size_block a = i * Spec.size_block a + Spec.size_block a);
introduce forall (j: nat{j < Spec.size_block a}). S.index b0 j == S.index b1 j
with
begin
assert (i * Spec.size_block a + j < i * Spec.size_block a + Spec.size_block a);
Math.Lemmas.nat_times_nat_is_nat i (Spec.size_block a);
S.lemma_index_slice input (i * Spec.size_block a) ((i + 1) * Spec.size_block a) j;
assert (S.index b0 j == S.index input (j + (i * Spec.size_block a)))
end
end;
assert (forall (i:nat{i < n_blocks}) acc. f i acc == g i acc);
Lib.Sequence.Lemmas.repeati_extensionality n_blocks f g s
#pop-options
val spec_is_incremental :
a : alg ->
kk: size_nat{kk <= max_key a} ->
k: lbytes kk ->
input:S.seq uint8 { if kk = 0 then S.length input <= max_input_length a else S.length input + (Spec.size_block a) <= max_input_length a } ->
Lemma(
blake2_hash_incremental_s a kk k input ==
Spec.blake2 a input (Spec.blake2_default_params a) kk k (output_size a))
#restart-solver
#push-options "--z3cliopt smt.arith.nl=false"
let spec_is_incremental a kk k input0 =
let key_block = if kk > 0 then Spec.blake2_key_block a kk k else S.empty in
let key_block_len = S.length key_block in
let input = Seq.append key_block input0 in
let n_blocks, l_last = Spec.split a (S.length input) in
let blocks, last = Lib.UpdateMulti.split_at_last_lazy (U32.v (block_len a)) input in
let s = init_s a kk in
repeati_split_at_eq a s input;
let f = Spec.blake2_update1 a 0 input in
let g = Spec.blake2_update1 a 0 blocks in
let s1 = Lib.LoopCombinators.repeati n_blocks f s in
let s2 = Lib.LoopCombinators.repeati n_blocks g s in
assert (s1 == s2);
S.lemma_eq_intro (S.slice input (S.length input - l_last) (S.length input)) last;
S.lemma_eq_intro (S.slice last (S.length last - l_last) (S.length last)) last;
Spec.Blake2.Alternative.lemma_spec_equivalence_update a kk k input0 s;
assert (U32.v (output_len a) = output_size a)
#pop-options
inline_for_extraction noextract
val init_key_block (a : alg) (kk : key_size a) (k : stateful_key_t a kk)
(buf_: B.buffer uint8 { B.length buf_ = Spec.size_block a }) :
ST.Stack unit
(requires fun h0 ->
let key = stateful_key a kk in
key.invariant h0 k /\
B.live h0 buf_ /\
B.(loc_disjoint (loc_buffer buf_) (key.footprint h0 k)))
(ensures fun h0 _ h1 ->
B.(modifies (loc_buffer buf_) h0 h1) /\
begin
let k = (stateful_key a kk).v () h0 k in
let input_length = if kk > 0 then Spec.size_block a else 0 in
let input = if kk > 0 then Spec.blake2_key_block a kk k else S.empty in
S.equal (S.slice (B.as_seq h1 buf_) 0 input_length) input
end) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.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",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"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": 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.Streaming.Blake2.Common.alg ->
kk: Hacl.Streaming.Blake2.Common.key_size a ->
k: Hacl.Streaming.Blake2.Common.stateful_key_t a kk ->
buf_:
LowStar.Buffer.buffer Hacl.Streaming.Blake2.Common.uint8
{LowStar.Monotonic.Buffer.length buf_ = Spec.Blake2.Definitions.size_block a}
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.key_size",
"Hacl.Streaming.Blake2.Common.stateful_key_t",
"LowStar.Buffer.buffer",
"Hacl.Streaming.Blake2.Common.uint8",
"Prims.b2t",
"Prims.op_Equality",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"LowStar.Buffer.trivial_preorder",
"Spec.Blake2.Definitions.size_block",
"Prims.int",
"Prims.unit",
"Prims.bool",
"Prims._assert",
"FStar.Seq.Base.equal",
"Lib.IntTypes.uint8",
"Lib.Sequence.lemma_update_sub",
"Lib.Sequence.sub",
"Prims.op_Subtraction",
"Lib.Sequence.update_sub",
"Lib.Sequence.lseq",
"FStar.Seq.Base.create",
"Lib.IntTypes.u8",
"Spec.Blake2.Definitions.block_s",
"Spec.Blake2.blake2_key_block",
"FStar.Seq.Base.slice",
"FStar.Seq.Base.append",
"LowStar.Monotonic.Buffer.as_seq",
"Hacl.Streaming.Interface.__proj__Stateful__item__v",
"Hacl.Streaming.Blake2.Common.stateful_key",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Lib.Buffer.update_sub",
"Lib.Buffer.MUT",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.__uint_to_t",
"Hacl.Streaming.Blake2.Common.stateful_key_to_buffer",
"LowStar.Monotonic.Buffer.fill",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Buffer.sub",
"FStar.Ghost.hide",
"FStar.UInt32.t",
"FStar.UInt32.op_Subtraction_Hat",
"Hacl.Streaming.Blake2.Common.block_len"
] | [] | false | true | false | false | false | let init_key_block a kk k buf_ =
| if kk = 0
then ()
else
let h0 = ST.get () in
[@@ inline_let ]let sub_b_len = let open U32 in block_len a -^ U32.uint_to_t kk in
let sub_b = B.sub buf_ (U32.uint_to_t kk) sub_b_len in
B.fill sub_b (Lib.IntTypes.u8 0) sub_b_len;
let h1 = ST.get () in
assert ((S.slice (B.as_seq h1 buf_) kk (Spec.size_block a)) `S.equal` (B.as_seq h1 sub_b));
Lib.Buffer.update_sub #Lib.Buffer.MUT
#uint8
#(U32.uint_to_t (Spec.size_block a))
buf_
0ul
(U32.uint_to_t kk)
(stateful_key_to_buffer k);
let h2 = ST.get () in
let k:LS.lseq uint8 kk = (stateful_key a kk).v () h0 k in
let buf_v1:LS.lseq uint8 (Spec.size_block a) = B.as_seq h1 buf_ in
let buf_v2:LS.lseq uint8 (Spec.size_block a) = B.as_seq h2 buf_ in
assert (buf_v2 `S.equal` (LS.update_sub buf_v1 0 kk k));
let zeroed:LS.lseq uint8 (Spec.size_block a - kk) =
S.create (Spec.size_block a - kk) (Lib.IntTypes.u8 0)
in
assert ((B.as_seq h1 sub_b) `S.equal` zeroed);
let key_and_zeroed:LS.lseq uint8 (Spec.size_block a) = Seq.append k zeroed in
assert (S.equal (S.slice key_and_zeroed 0 kk) k);
assert (S.equal (S.slice key_and_zeroed kk (Spec.size_block a)) zeroed);
LS.lemma_update_sub #uint8 #(Spec.size_block a) buf_v1 0 kk k key_and_zeroed;
assert (buf_v2 `S.equal` key_and_zeroed);
let input = Spec.blake2_key_block a kk k in
let key_block0:LS.lseq uint8 (Spec.size_block a) =
S.create (Spec.size_block a) (Lib.IntTypes.u8 0)
in
assert (input `S.equal` (LS.update_sub key_block0 0 kk k));
assert (Seq.equal (LS.sub key_and_zeroed 0 kk) k);
assert (Seq.equal (LS.sub key_and_zeroed kk (Spec.size_block a - kk))
(LS.sub key_block0 kk (Spec.size_block a - kk)));
LS.lemma_update_sub #uint8 #(Spec.size_block a) key_block0 0 kk k key_and_zeroed;
assert (input `S.equal` key_and_zeroed) | false |
LowParse.Spec.Array.fst | LowParse.Spec.Array.length_serialize_vlarray | val length_serialize_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
(x: vlarray t elem_count_min elem_count_max)
: Lemma
(Seq.length (serialize (serialize_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u)
x) ==
log256' array_byte_size_max + ((L.length x) `FStar.Mul.op_Star` k.parser_kind_low)) | val length_serialize_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
(x: vlarray t elem_count_min elem_count_max)
: Lemma
(Seq.length (serialize (serialize_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u)
x) ==
log256' array_byte_size_max + ((L.length x) `FStar.Mul.op_Star` k.parser_kind_low)) | let length_serialize_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: vlarray t elem_count_min elem_count_max)
: Lemma
(Seq.length (serialize (serialize_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) x) == log256' array_byte_size_max + (L.length x `FStar.Mul.op_Star` k.parser_kind_low))
= vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
vlarray_to_vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
serialize_synth_eq
_
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
(serialize_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s))
(vlarray_to_vldata array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x) | {
"file_name": "src/lowparse/LowParse.Spec.Array.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 79,
"end_line": 598,
"start_col": 0,
"start_line": 573
} | module LowParse.Spec.Array
include LowParse.Spec.FLData
include LowParse.Spec.VLData
include LowParse.Spec.List
module Seq = FStar.Seq
module L = FStar.List.Tot
module M = LowParse.Math
module U32 = FStar.UInt32
open FStar.Mul // for Prims.op_Multiply
// arith lemmas must be called explicitly
#reset-options "--z3cliopt smt.arith.nl=false"
let array_pred (#t: Type) (n: nat) (s: list t) : GTot Type0 =
L.length s == n
inline_for_extraction
let fldata_array_precond
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot bool
= serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
elem_count * k.parser_kind_low = array_byte_size
let fldata_to_array_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
(ensures (
array_pred elem_count x
))
= let y = serialize (serialize_list _ s) x in
assert (parse (parse_list p) y == Some (x, array_byte_size));
assert (Seq.length y == array_byte_size);
list_length_constant_size_parser_correct p y;
M.mul_reg_r elem_count (L.length x) k.parser_kind_low
inline_for_extraction
let array (t: Type) (n: nat) = (l: list t { array_pred n l } )
inline_for_extraction
let fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: parse_fldata_strong_t (serialize_list _ s) array_byte_size)
: Tot (array t elem_count)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = fldata_to_array_correct s array_byte_size elem_count x' in
x'
let fldata_to_array_inj
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x1 x2: parse_fldata_strong_t (serialize_list _ s) array_byte_size) . {:pattern (fldata_to_array s array_byte_size elem_count u x1); (fldata_to_array s array_byte_size elem_count u x2)}
fldata_to_array s array_byte_size elem_count u x1 ==
fldata_to_array s array_byte_size elem_count u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_array_kind'
(array_byte_size: nat)
: Tot parser_kind
= parse_fldata_kind array_byte_size parse_list_kind
let parse_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind' array_byte_size) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_fldata_strong (serialize_list _ s) array_byte_size `parse_synth` (fldata_to_array s array_byte_size elem_count u)
let parse_array_total_constant_size
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: bytes)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= array_byte_size
))
(ensures (
Some? (parse (parse_array' s array_byte_size elem_count) x)
))
= let (u : unit { fldata_array_precond k array_byte_size elem_count == true } ) = () in
fldata_to_array_inj s array_byte_size elem_count u;
parse_synth_eq (parse_fldata_strong (serialize_list _ s) array_byte_size) (fldata_to_array s array_byte_size elem_count u) x;
let x' = Seq.slice x 0 array_byte_size in
parse_list_total_constant_size p elem_count x';
parser_kind_prop_equiv parse_list_kind (parse_list p)
inline_for_extraction
let parse_array_kind
(k: parser_kind)
(array_byte_size: nat)
(elem_count: nat)
: Tot parser_kind
= if
fldata_array_precond k array_byte_size elem_count &&
k.parser_kind_metadata = Some ParserKindMetadataTotal
then
total_constant_size_parser_kind array_byte_size
else
parse_array_kind' array_byte_size
let parse_array_kind_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (
parser_kind_prop (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
))
= if k.parser_kind_metadata = Some ParserKindMetadataTotal
then begin
parser_kind_prop_equiv (parse_array_kind' array_byte_size) (parse_array' s array_byte_size elem_count);
parser_kind_prop_equiv (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count);
Classical.forall_intro (Classical.move_requires (parse_array_total_constant_size s array_byte_size elem_count))
end
let parse_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
: Pure (parser (parse_array_kind k array_byte_size elem_count) (array t elem_count))
(requires (
fldata_array_precond k array_byte_size elem_count == true
))
(ensures (fun _ -> True))
= parse_array_kind_correct s array_byte_size elem_count;
strengthen (parse_array_kind k array_byte_size elem_count) (parse_array' s array_byte_size elem_count)
let array_to_fldata_correct
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(x: list t)
: Lemma
(requires (
fldata_array_precond k array_byte_size elem_count == true /\
array_pred elem_count x
))
(ensures (
parse_fldata_strong_pred (serialize_list _ s) array_byte_size x
))
= let y = serialize (serialize_list _ s) x in
list_length_constant_size_parser_correct p y
inline_for_extraction
let array_to_fldata
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Tot (parse_fldata_strong_t (serialize_list _ s) array_byte_size)
= [@inline_let]
let (x' : list t) = x in
[@inline_let]
let _ = array_to_fldata_correct s array_byte_size elem_count x' in
x'
let array_to_fldata_to_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u1 u2: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Lemma
(forall (x: array t elem_count) . {:pattern (fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x))}
fldata_to_array s array_byte_size elem_count u1 (array_to_fldata s array_byte_size elem_count u2 x) == x)
= ()
let serialize_array'
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array' s array_byte_size elem_count))
= fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
let serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
: Tot (serializer (parse_array s array_byte_size elem_count))
= fun x -> serialize (serialize_array' s array_byte_size elem_count u) x
let length_serialize_array
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(array_byte_size: nat)
(elem_count: nat)
(u: unit {
fldata_array_precond k array_byte_size elem_count == true
})
(x: array t elem_count)
: Lemma
(Seq.length (serialize (serialize_array s array_byte_size elem_count u) x) == L.length x `FStar.Mul.op_Star` k.parser_kind_low)
=
fldata_to_array_inj s array_byte_size elem_count u;
array_to_fldata_to_array s array_byte_size elem_count u u;
serialize_synth_eq
_
(fldata_to_array s array_byte_size elem_count u)
(serialize_fldata_strong (serialize_list _ s) array_byte_size)
(array_to_fldata s array_byte_size elem_count u)
()
x
;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x)
let vlarray_pred (#t: Type) (min max: nat) (s: list t) : GTot Type0 =
let l = L.length s in
min <= l /\ l <= max
let vldata_vlarray_precond
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: GTot bool
= (* constant-size serializable parser for elements *)
serialize_list_precond k &&
k.parser_kind_high = Some k.parser_kind_low &&
(* vldata *)
array_byte_size_min <= array_byte_size_max &&
array_byte_size_max > 0 &&
array_byte_size_max < 4294967296 &&
(* vlarray *)
elem_count_min <= elem_count_max &&
0 < elem_count_max &&
(* ceil (array_byte_size_min / k.parser_kind_low) = elem_count_min *)
elem_count_min * k.parser_kind_low < array_byte_size_min + k.parser_kind_low &&
array_byte_size_min <= elem_count_min * k.parser_kind_low &&
(* floor (array_byte_size_max / k.parser_kind_low) = elem_count_max *)
elem_count_max * k.parser_kind_low <= array_byte_size_max &&
array_byte_size_max < elem_count_max * k.parser_kind_low + k.parser_kind_low
let vldata_vlarray_precond_parser_kind_low
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(elem_count_min: nat)
(elem_count_max: nat)
: Lemma
(requires (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max))
(ensures (k.parser_kind_low < 4294967296))
[SMTPat (k.parser_kind_low); SMTPat (vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max)]
= M.lemma_mult_le_right k.parser_kind_low 1 elem_count_max
let vldata_to_vlarray_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
(ensures (
vlarray_pred elem_count_min elem_count_max x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lt_mul_add_reg_r elem_count_min l k.parser_kind_low;
M.lt_mul_add_reg_r l elem_count_max k.parser_kind_low
inline_for_extraction
let vlarray (t: Type) (min max: nat) =
(l: list t { min <= L.length l /\ L.length l <= max } )
inline_for_extraction
let vldata_to_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
: Tot (vlarray t elem_count_min elem_count_max)
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vldata_to_vlarray_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x'
let vldata_to_vlarray_inj
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Lemma
(forall (x1 x2: parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s)) .
{:pattern
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1);
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2)}
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x1 ==
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x2 ==>
x1 == x2)
= ()
inline_for_extraction
let parse_vlarray_kind
(array_byte_size_min: nat)
(array_byte_size_max: nat { array_byte_size_min <= array_byte_size_max /\ array_byte_size_max > 0 /\ array_byte_size_max < 4294967296 } )
: Tot parser_kind
= parse_bounded_vldata_strong_kind array_byte_size_min array_byte_size_max (log256' array_byte_size_max) parse_list_kind
let parse_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Tot (parser (parse_vlarray_kind array_byte_size_min array_byte_size_max) (vlarray t elem_count_min elem_count_max))
= vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s)
`parse_synth`
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u
let parse_vlarray_eq_some
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(input: bytes)
: Lemma
(requires (
Some? (parse (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) input)
))
(ensures (
let sz = log256' array_byte_size_max in
let pi = parse (parse_bounded_integer sz) input in
Some? pi /\ (
let Some (len, c_len) = pi in
c_len == sz /\
array_byte_size_min <= U32.v len /\ U32.v len <= array_byte_size_max /\ (
let input1 = Seq.slice input c_len (Seq.length input) in
U32.v len <= Seq.length input1 /\ (
let input2 = Seq.slice input1 0 (U32.v len) in
let pl = parse (parse_list p) input2 in
Some? pl /\ (
let Some (l, c_l) = pl in
c_l == U32.v len /\
vlarray_pred elem_count_min elem_count_max l /\
parse (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u) input == Some (l, c_len + c_l)
))))))
=
parser_kind_prop_equiv k p;
vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max ();
parse_synth_eq (parse_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s)) (vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max ()) input;
parse_vldata_gen_eq (log256' array_byte_size_max) (in_bounds array_byte_size_min array_byte_size_max) (parse_list p) input;
parser_kind_prop_equiv parse_list_kind (parse_list p)
let vlarray_to_vldata_correct
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(x: list t)
: Lemma
(requires (
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true /\
vlarray_pred elem_count_min elem_count_max x
))
(ensures (
parse_bounded_vldata_strong_pred array_byte_size_min array_byte_size_max (serialize_list _ s) x
))
= let y = serialize (serialize_list _ s) x in
let l = L.length x in
assert (parse (parse_list p) y == Some (x, Seq.length y));
list_length_constant_size_parser_correct p y;
M.lemma_mult_le_right k.parser_kind_low elem_count_min l;
M.lemma_mult_le_right k.parser_kind_low l elem_count_max
inline_for_extraction
let vlarray_to_vldata
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
(x: vlarray t elem_count_min elem_count_max)
: Tot (parse_bounded_vldata_strong_t array_byte_size_min array_byte_size_max (serialize_list _ s))
= [@inline_let]
let x' : list t = x in
[@inline_let]
let _ = vlarray_to_vldata_correct array_byte_size_min array_byte_size_max s elem_count_min elem_count_max x' in
x'
let vlarray_to_vldata_to_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Lemma
(forall (x: vlarray t elem_count_min elem_count_max) .
{:pattern
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u
(vlarray_to_vldata array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x))
}
vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u
(vlarray_to_vldata array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u x)
== x)
= ()
let serialize_vlarray
(array_byte_size_min: nat)
(array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min: nat)
(elem_count_max: nat)
(u: unit {
vldata_vlarray_precond array_byte_size_min array_byte_size_max p elem_count_min elem_count_max == true
})
: Tot (serializer (parse_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u))
= vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
vlarray_to_vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
serialize_synth
_
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
(serialize_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s))
(vlarray_to_vldata array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.VLData.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Array.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul // for Prims.op_Multiply",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.List",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.VLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"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": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
array_byte_size_min: Prims.nat ->
array_byte_size_max: Prims.nat ->
s: LowParse.Spec.Base.serializer p ->
elem_count_min: Prims.nat ->
elem_count_max: Prims.nat ->
u565:
u569:
Prims.unit
{ LowParse.Spec.Array.vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true } ->
x: LowParse.Spec.Array.vlarray t elem_count_min elem_count_max
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.length (LowParse.Spec.Base.serialize (LowParse.Spec.Array.serialize_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u565)
x) ==
LowParse.Spec.BoundedInt.log256' array_byte_size_max +
FStar.List.Tot.Base.length x * Mkparser_kind'?.parser_kind_low k) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.unit",
"Prims.eq2",
"Prims.bool",
"LowParse.Spec.Array.vldata_vlarray_precond",
"LowParse.Spec.Array.vlarray",
"LowParse.Spec.List.list_length_constant_size_parser_correct",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.List.parse_list_kind",
"Prims.list",
"LowParse.Spec.List.parse_list",
"LowParse.Spec.List.serialize_list",
"LowParse.Spec.Combinators.serialize_synth_eq",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"LowParse.Spec.VLData.parse_bounded_vldata_strong",
"LowParse.Spec.Array.vldata_to_vlarray",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong",
"LowParse.Spec.Array.vlarray_to_vldata",
"LowParse.Spec.Array.vlarray_to_vldata_to_vlarray",
"LowParse.Spec.Array.vldata_to_vlarray_inj",
"Prims.l_True",
"Prims.squash",
"Prims.int",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"LowParse.Spec.Array.parse_vlarray_kind",
"LowParse.Spec.Array.parse_vlarray",
"LowParse.Spec.Array.serialize_vlarray",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"FStar.List.Tot.Base.length",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let length_serialize_vlarray
(array_byte_size_min array_byte_size_max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(elem_count_min elem_count_max: nat)
(u:
unit
{ vldata_vlarray_precond array_byte_size_min
array_byte_size_max
p
elem_count_min
elem_count_max ==
true })
(x: vlarray t elem_count_min elem_count_max)
: Lemma
(Seq.length (serialize (serialize_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u)
x) ==
log256' array_byte_size_max + ((L.length x) `FStar.Mul.op_Star` k.parser_kind_low)) =
| vldata_to_vlarray_inj array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u;
vlarray_to_vldata_to_vlarray array_byte_size_min
array_byte_size_max
s
elem_count_min
elem_count_max
u;
serialize_synth_eq _
(vldata_to_vlarray array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
(serialize_bounded_vldata_strong array_byte_size_min array_byte_size_max (serialize_list _ s))
(vlarray_to_vldata array_byte_size_min array_byte_size_max s elem_count_min elem_count_max u)
()
x;
list_length_constant_size_parser_correct p (serialize (serialize_list _ s) x) | false |
Vale.Interop.Base.fst | Vale.Interop.Base.args_b8 | val args_b8 (args: list arg) : GTot (list b8) | val args_b8 (args: list arg) : GTot (list b8) | let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer [] | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 52,
"end_line": 266,
"start_col": 0,
"start_line": 259
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args } | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | args: Prims.list Vale.Interop.Base.arg -> Prims.GTot (Prims.list Vale.Interop.Types.b8) | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.list",
"Vale.Interop.Base.arg",
"FStar.List.Tot.Base.fold_right_gtot",
"Vale.Interop.Types.b8",
"Prims.Nil",
"Vale.Arch.HeapTypes_s.base_typ",
"Vale.Interop.Base.buffer_qualifiers",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.TD_Buffer",
"Prims.Cons",
"Vale.Interop.Base.mut_to_b8",
"Vale.Interop.Base.TD_ImmBuffer",
"Vale.Interop.Base.imm_to_b8",
"Vale.Interop.Base.valid_base_type",
"Vale.Interop.Base.TD_Base"
] | [] | false | false | false | false | false | let args_b8 (args: list arg) : GTot (list b8) =
| let maybe_cons_buffer (x: arg) (args: list b8) : GTot (list b8) =
match x with
| (| TD_Buffer src _ _ , x |) -> mut_to_b8 src x :: args
| (| TD_ImmBuffer src _ _ , x |) -> imm_to_b8 src x :: args
| (| TD_Base _ , _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer [] | false |
Vale.Interop.Base.fst | Vale.Interop.Base.disjoint_or_eq_cons | val disjoint_or_eq_cons (hd: arg) (tl: list arg)
: Lemma
(disjoint_or_eq (hd :: tl) <==> (BigOps.big_and' (disjoint_or_eq_1 hd) tl /\ disjoint_or_eq tl)) | val disjoint_or_eq_cons (hd: arg) (tl: list arg)
: Lemma
(disjoint_or_eq (hd :: tl) <==> (BigOps.big_and' (disjoint_or_eq_1 hd) tl /\ disjoint_or_eq tl)) | let disjoint_or_eq_cons (hd:arg) (tl:list arg)
: Lemma (disjoint_or_eq (hd::tl) <==> (BigOps.big_and' (disjoint_or_eq_1 hd) tl /\ disjoint_or_eq tl))
= BigOps.pairwise_and'_cons disjoint_or_eq_1 hd tl | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 52,
"end_line": 306,
"start_col": 0,
"start_line": 304
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args }
[@__reduce__]
let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer []
[@__reduce__]
let modified_arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_buffer x
| _ -> B.loc_none
[@__reduce__]
let loc_modified_args (args:list arg) : GTot B.loc =
let maybe_union_loc (x:arg) l =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_union (B.loc_buffer x) l
| _ -> l
in
List.Tot.fold_right_gtot args maybe_union_loc B.loc_none
[@__reduce__]
let arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ _, x|) -> B.loc_buffer x
| (|TD_ImmBuffer _ _ _, x|) -> B.loc_buffer x
| (|TD_Base _, _|) -> B.loc_none
[@__reduce__]
let loc_all_args (args:list arg) : GTot B.loc =
let l = List.Tot.map_gtot arg_loc args in
List.Tot.fold_right_gtot l B.loc_union B.loc_none
let all_live_cons (hd:arg) (tl:list arg) (h0:HS.mem)
: Lemma
(all_live h0 (hd :: tl) <==> (live_arg h0 hd /\ all_live h0 tl))
= ()
let disjoint_or_eq_def (l:list arg)
: Lemma (disjoint_or_eq l == BigOps.pairwise_and' disjoint_or_eq_1 l)
= () | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | hd: Vale.Interop.Base.arg -> tl: Prims.list Vale.Interop.Base.arg
-> FStar.Pervasives.Lemma
(ensures
Vale.Interop.Base.disjoint_or_eq (hd :: tl) <==>
FStar.BigOps.big_and' (Vale.Interop.Base.disjoint_or_eq_1 hd) tl /\
Vale.Interop.Base.disjoint_or_eq tl) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Vale.Interop.Base.arg",
"Prims.list",
"FStar.BigOps.pairwise_and'_cons",
"Vale.Interop.Base.disjoint_or_eq_1",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"Vale.Interop.Base.disjoint_or_eq",
"Prims.Cons",
"Prims.l_and",
"FStar.BigOps.big_and'",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let disjoint_or_eq_cons (hd: arg) (tl: list arg)
: Lemma
(disjoint_or_eq (hd :: tl) <==> (BigOps.big_and' (disjoint_or_eq_1 hd) tl /\ disjoint_or_eq tl)) =
| BigOps.pairwise_and'_cons disjoint_or_eq_1 hd tl | false |
Vale.Interop.Base.fst | Vale.Interop.Base.arg_loc | val arg_loc (x: arg) : GTot B.loc | val arg_loc (x: arg) : GTot B.loc | let arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ _, x|) -> B.loc_buffer x
| (|TD_ImmBuffer _ _ _, x|) -> B.loc_buffer x
| (|TD_Base _, _|) -> B.loc_none | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 36,
"end_line": 288,
"start_col": 0,
"start_line": 284
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args }
[@__reduce__]
let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer []
[@__reduce__]
let modified_arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_buffer x
| _ -> B.loc_none
[@__reduce__]
let loc_modified_args (args:list arg) : GTot B.loc =
let maybe_union_loc (x:arg) l =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_union (B.loc_buffer x) l
| _ -> l
in
List.Tot.fold_right_gtot args maybe_union_loc B.loc_none | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 1,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Vale.Interop.Base.arg -> Prims.GTot LowStar.Monotonic.Buffer.loc | Prims.GTot | [
"sometrivial"
] | [] | [
"Vale.Interop.Base.arg",
"Vale.Arch.HeapTypes_s.base_typ",
"Vale.Interop.Base.buffer_qualifiers",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.TD_Buffer",
"LowStar.Monotonic.Buffer.loc_buffer",
"Vale.Interop.Types.base_typ_as_type",
"LowStar.Buffer.trivial_preorder",
"Vale.Interop.Base.TD_ImmBuffer",
"LowStar.ImmutableBuffer.immutable_preorder",
"Vale.Interop.Base.valid_base_type",
"Vale.Interop.Base.TD_Base",
"LowStar.Monotonic.Buffer.loc_none",
"LowStar.Monotonic.Buffer.loc"
] | [] | false | false | false | false | false | let arg_loc (x: arg) : GTot B.loc =
| match x with
| (| TD_Buffer _ _ _ , x |) -> B.loc_buffer x
| (| TD_ImmBuffer _ _ _ , x |) -> B.loc_buffer x
| (| TD_Base _ , _ |) -> B.loc_none | false |
Vale.Interop.Base.fst | Vale.Interop.Base.liveness_disjointness | val liveness_disjointness (args: list arg) (h: mem_roots args)
: Lemma (list_disjoint_or_eq (args_b8 args) /\ list_live h (args_b8 args)) | val liveness_disjointness (args: list arg) (h: mem_roots args)
: Lemma (list_disjoint_or_eq (args_b8 args) /\ list_live h (args_b8 args)) | let liveness_disjointness (args:list arg) (h:mem_roots args)
: Lemma (list_disjoint_or_eq (args_b8 args) /\
list_live h (args_b8 args))
= args_b8_disjoint_or_eq args;
args_b8_live h args | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 23,
"end_line": 396,
"start_col": 0,
"start_line": 392
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args }
[@__reduce__]
let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer []
[@__reduce__]
let modified_arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_buffer x
| _ -> B.loc_none
[@__reduce__]
let loc_modified_args (args:list arg) : GTot B.loc =
let maybe_union_loc (x:arg) l =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_union (B.loc_buffer x) l
| _ -> l
in
List.Tot.fold_right_gtot args maybe_union_loc B.loc_none
[@__reduce__]
let arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ _, x|) -> B.loc_buffer x
| (|TD_ImmBuffer _ _ _, x|) -> B.loc_buffer x
| (|TD_Base _, _|) -> B.loc_none
[@__reduce__]
let loc_all_args (args:list arg) : GTot B.loc =
let l = List.Tot.map_gtot arg_loc args in
List.Tot.fold_right_gtot l B.loc_union B.loc_none
let all_live_cons (hd:arg) (tl:list arg) (h0:HS.mem)
: Lemma
(all_live h0 (hd :: tl) <==> (live_arg h0 hd /\ all_live h0 tl))
= ()
let disjoint_or_eq_def (l:list arg)
: Lemma (disjoint_or_eq l == BigOps.pairwise_and' disjoint_or_eq_1 l)
= ()
let disjoint_or_eq_cons (hd:arg) (tl:list arg)
: Lemma (disjoint_or_eq (hd::tl) <==> (BigOps.big_and' (disjoint_or_eq_1 hd) tl /\ disjoint_or_eq tl))
= BigOps.pairwise_and'_cons disjoint_or_eq_1 hd tl
#set-options "--initial_ifuel 2 --max_fuel 2"
let rec args_b8_mem (l:list arg) (y:b8)
: Lemma (L.memP y (args_b8 l) <==>
(exists (a:arg). {:pattern L.memP a l}
L.memP a l /\
(match a with
| (| TD_Base _, _|) -> False
| (| TD_Buffer src _ _, x|) -> mut_to_b8 src x == y
| (| TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x == y)))
= let goal (l:list arg) (a:arg) =
L.memP a l /\
(match a with
| (| TD_Base _, _|) -> False
| (| TD_Buffer src _ _, x|) -> mut_to_b8 src x == y
| (| TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x == y)
in
match l with
| [] -> ()
| hd::tl ->
match hd with
| (| TD_Base _, _ |) ->
args_b8_mem tl y;
assert ((exists a. goal tl a) ==> (exists a. goal l a))
| (| TD_Buffer bt _ q, x |) ->
let aux_1 ()
: Lemma (requires (y == mut_to_b8 bt x))
(ensures (exists a. goal l a)) =
FStar.Classical.exists_intro (goal l) hd
in
let aux_2 ()
: Lemma (requires (mut_to_b8 bt x =!= y))
(ensures (L.memP y (args_b8 l) <==> (exists a. goal l a))) =
args_b8_mem tl y
in
FStar.Classical.move_requires aux_1 ();
FStar.Classical.move_requires aux_2 ()
| (| TD_ImmBuffer bt _ q, x |) ->
let aux_1 ()
: Lemma (requires (y == imm_to_b8 bt x))
(ensures (exists a. goal l a)) =
FStar.Classical.exists_intro (goal l) hd
in
let aux_2 ()
: Lemma (requires (imm_to_b8 bt x =!= y))
(ensures (L.memP y (args_b8 l) <==> (exists a. goal l a))) =
args_b8_mem tl y
in
FStar.Classical.move_requires aux_1 ();
FStar.Classical.move_requires aux_2 ()
let rec args_b8_disjoint_or_eq (args:list arg)
: Lemma
(requires (disjoint_or_eq args))
(ensures (list_disjoint_or_eq (args_b8 args)))
=
list_disjoint_or_eq_reveal ();
match args with
| [] -> ()
| hd::tl ->
disjoint_or_eq_cons hd tl;
args_b8_disjoint_or_eq tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl;
FStar.Classical.forall_intro (args_b8_mem tl)
let rec args_b8_live (hs:HS.mem) (args:list arg{all_live hs args})
: Lemma (list_live hs (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
all_live_cons hd tl hs;
assert (live_arg hs hd);
assert (all_live hs tl);
args_b8_live hs tl;
match hd with
| (| TD_Base _ , _ |) ->
assert (args_b8 args == args_b8 tl)
| (| TD_Buffer t _ _, x |) ->
assert (B.live hs x);
assert (args_b8 args == Buffer true x :: args_b8 tl)
| (| TD_ImmBuffer t _ _, x |) ->
assert (B.live hs x);
assert (args_b8 args == imm_to_b8 t x :: args_b8 tl) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 2,
"max_fuel": 2,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | args: Prims.list Vale.Interop.Base.arg -> h: Vale.Interop.Base.mem_roots args
-> FStar.Pervasives.Lemma
(ensures
Vale.Interop.Heap_s.list_disjoint_or_eq (Vale.Interop.Base.args_b8 args) /\
Vale.Interop.Heap_s.list_live h (Vale.Interop.Base.args_b8 args)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Vale.Interop.Base.arg",
"Vale.Interop.Base.mem_roots",
"Vale.Interop.Base.args_b8_live",
"Prims.unit",
"Vale.Interop.Base.args_b8_disjoint_or_eq",
"Prims.l_True",
"Prims.squash",
"Prims.l_and",
"Vale.Interop.Heap_s.list_disjoint_or_eq",
"Vale.Interop.Base.args_b8",
"Vale.Interop.Heap_s.list_live",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let liveness_disjointness (args: list arg) (h: mem_roots args)
: Lemma (list_disjoint_or_eq (args_b8 args) /\ list_live h (args_b8 args)) =
| args_b8_disjoint_or_eq args;
args_b8_live h args | false |
Vale.Interop.Base.fst | Vale.Interop.Base.mem_roots_p_modifies_none | val mem_roots_p_modifies_none (args: list arg) (h0 h1: HS.mem)
: Lemma (requires mem_roots_p h0 args /\ B.modifies B.loc_none h0 h1)
(ensures mem_roots_p h1 args) | val mem_roots_p_modifies_none (args: list arg) (h0 h1: HS.mem)
: Lemma (requires mem_roots_p h0 args /\ B.modifies B.loc_none h0 h1)
(ensures mem_roots_p h1 args) | let rec mem_roots_p_modifies_none (args:list arg) (h0:HS.mem) (h1:HS.mem)
: Lemma
(requires
mem_roots_p h0 args /\
B.modifies B.loc_none h0 h1)
(ensures
mem_roots_p h1 args)
= match args with
| [] -> ()
| hd::tl ->
all_live_cons hd tl h0;
mem_roots_p_modifies_none tl h0 h1;
match hd with
| (| TD_Base _, _ |) -> ()
| (| TD_Buffer _ _ _, x |)
| (| TD_ImmBuffer _ _ _, x |) -> assert (B.live h1 x) | {
"file_name": "vale/specs/interop/Vale.Interop.Base.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 59,
"end_line": 422,
"start_col": 0,
"start_line": 407
} | module Vale.Interop.Base
open Vale.Arch.HeapTypes_s
include Vale.Interop.Types
include Vale.Interop.Heap_s
module MB = LowStar.Monotonic.Buffer
module DV = LowStar.BufferView.Down
module B = LowStar.Buffer
module IB = LowStar.ImmutableBuffer
include Vale.Arch.Heap
module BS = Vale.X64.Machine_Semantics_s
module BV = LowStar.BufferView
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module P = Vale.Def.Prop_s
module HS = FStar.HyperStack
module W = Vale.Def.Words_s
module L = FStar.List.Tot
open FStar.Mul
[@__reduce__]
let buf_t src t = b:B.buffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
[@__reduce__]
let ibuf_t src t = b:IB.ibuffer (base_typ_as_type src){(B.length b * view_n_unfold src) % view_n_unfold t = 0}
let lemma_seq_neq_intro (#a:Type) (s1:Seq.seq a) (s2:Seq.seq a)
: Lemma (requires (Seq.length s1 =!= Seq.length s2))
(ensures (~ (Seq.equal s1 s2)))
= ()
let default_v_of_typ (t:base_typ) : (base_typ_as_type t) = match t with
| TUInt8 -> UInt8.uint_to_t 0
| TUInt16 -> UInt16.uint_to_t 0
| TUInt32 -> UInt32.uint_to_t 0
| TUInt64 -> UInt64.uint_to_t 0
| TUInt128 -> Vale.Def.Words_s.Mkfour #W.nat32 0 0 0 0
let imm_to_b8 (src:base_typ) (b:IB.ibuffer (base_typ_as_type src)) : GTot b8 =
Buffer false b
let mut_to_b8 (src:base_typ) (b:B.buffer (base_typ_as_type src)) : GTot b8 =
Buffer true b
[@__reduce__]
let coerce (x:'a{'a == 'b}) : 'b = x
////////////////////////////////////////////////////////////////////////////////
type buffer_qualifiers = {
modified:bool;
taint:taint;
strict_disjointness:bool
}
[@__reduce__]
let default_bq = {
modified = true;
taint = Secret;
strict_disjointness = false
}
[@__reduce__]
let stack_bq = {
modified = true;
taint = Public;
strict_disjointness = true
}
let valid_base_type = x:base_typ{x <> TUInt128}
//type descriptors
type td =
| TD_Base of valid_base_type
// The initial type of the buffer, and the final type we want in Vale
| TD_Buffer : base_typ -> base_typ -> buffer_qualifiers -> td
| TD_ImmBuffer : base_typ -> base_typ -> buffer_qualifiers -> td
unfold
let normal (#a:Type) (x:a) : a =
FStar.Pervasives.norm
[iota;
zeta;
delta_attr [`%__reduce__; `%BigOps.__reduce__];
delta_only [`%TD_Buffer?;
`%BS.Mkmachine_state?.ms_ok;
`%BS.Mkmachine_state?.ms_regs;
`%BS.Mkmachine_state?.ms_flags;
`%BS.Mkmachine_state?.ms_heap;
`%BS.Mkmachine_state?.ms_stack;
`%BS.Mkmachine_state?.ms_stackTaint;
`%BS.Mkmachine_state?.ms_trace;
`%FStar.FunctionalExtensionality.on_dom;
`%FStar.FunctionalExtensionality.on;
`%List.Tot.fold_right_gtot;
`%List.Tot.map_gtot;
`%List.Tot.length;
`%fst;
`%snd;
`%Mktuple2?._1;
`%Mktuple2?._2
];
primops;
simplify]
x
////////////////////////////////////////////////////////////////////////////////
#set-options "--initial_ifuel 1"
[@__reduce__]
let td_as_type : td -> Type =
function
| TD_Base bt -> base_typ_as_type bt
| TD_Buffer src bt _ -> buf_t src bt
| TD_ImmBuffer src bt _ -> ibuf_t src bt
let arg = t:td & td_as_type t
////////////////////////////////////////////////////////////////////////////////
// n_arrow: Arrows with a generic number of vale types as the domain
// and a result type `codom` that does not depend on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_arrow (dom:list td) (codom:Type) =
match dom with
| [] -> codom
| hd::tl -> td_as_type hd -> n_arrow tl codom
[@(unifier_hint_injective) (__reduce__)]
let arr (dom:Type) (codom:Type) = dom -> codom
[@__reduce__]
let elim_1 (#dom:list td{Cons? dom})
(#r:Type)
(f:n_arrow dom r)
: td_as_type (Cons?.hd dom) -> n_arrow (Cons?.tl dom) r =
f
[@__reduce__]
let elim_nil (#dom:list td{Nil? dom})
(#r:Type)
(f:n_arrow dom r)
: r =
f
[@__reduce__]
let intro_n_arrow_nil (a:Type) (x:a)
: n_arrow [] a
= x
[@__reduce__]
let intro_n_arrow_cons (hd:td) (b:Type) (tl:list td)
(x:td_as_type hd -> n_arrow tl b)
: n_arrow (hd::tl) b
= x
////////////////////////////////////////////////////////////////////////////////
// n_dep_arrow: Arrows with a generic number of vale types as the domain
// and a result type codom that depends on the domain
////////////////////////////////////////////////////////////////////////////////
[@(unifier_hint_injective) (__reduce__)]
let rec n_dep_arrow (dom:list td) (codom: n_arrow dom Type) =
match dom with
| [] -> codom
| hd::tl -> x:td_as_type hd -> n_dep_arrow tl (elim_1 codom x)
[@__reduce__]
let intro_dep_arrow_nil (b:Type)
(f:b)
: n_dep_arrow [] b
= f
[@__reduce__]
let intro_dep_arrow_1 (a:td)
(b:n_arrow [a] Type)
(f:(x:td_as_type a -> elim_1 b x))
: n_dep_arrow [a] b
= f
[@__reduce__]
let intro_dep_arrow_cons (hd:td)
(tl:list td)
(b: n_arrow (hd::tl) Type)
(f: (x:td_as_type hd -> n_dep_arrow tl (elim_1 b x)))
: n_dep_arrow (hd::tl) b
= f
[@__reduce__]
let elim_dep_arrow_nil (#codom:n_arrow [] Type)
(f:n_dep_arrow [] codom)
: elim_nil codom
= f
[@__reduce__]
let elim_dep_arrow_cons (hd:td)
(tl:list td)
(#codom:n_arrow (hd::tl) Type)
(f:n_dep_arrow (hd::tl) codom)
: x:td_as_type hd ->
n_dep_arrow tl (elim_1 codom x)
= f
//Just a small test function to see how these coercions work
let __test : n_dep_arrow [TD_Base TUInt8] (fun (x:UInt8.t) -> y:UInt8.t{x == y}) =
fun (x:UInt8.t) -> intro_dep_arrow_nil (y:UInt8.t{x == y}) x
////////////////////////////////////////////////////////////////////////////////
[@__reduce__]
let disjoint_not_eq
(#src1 #src2:base_typ)
(#rel1 #rrel1:MB.srel (base_typ_as_type src1))
(#rel2 #rrel2:MB.srel (base_typ_as_type src2))
(x:MB.mbuffer (base_typ_as_type src1) rel1 rrel1)
(y:MB.mbuffer (base_typ_as_type src2) rel2 rrel2) =
B.loc_disjoint (B.loc_buffer x) (B.loc_buffer y) /\
~ (src1 == src2 /\ rel1 == rel2 /\ rrel1 == rrel2 /\ x == y)
[@__reduce__]
let disjoint_or_eq_1 (a:arg) (b:arg) =
match a, b with
| (| TD_Buffer _ _ {strict_disjointness=true}, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_ImmBuffer _ _ {strict_disjointness=true}, xb |), (| TD_ImmBuffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_Buffer _ _ {strict_disjointness=true}, yb |)
| (| TD_ImmBuffer _ _ _, xb |), (| TD_ImmBuffer _ _ {strict_disjointness=true}, yb |)
// An immutable buffer and a trivial buffer should not be equal
| (| TD_ImmBuffer _ _ _, xb |), (| TD_Buffer _ _ _, yb |)
| (| TD_Buffer _ _ _, xb |), (| TD_ImmBuffer _ _ _, yb |) ->
disjoint_not_eq xb yb
| (| TD_Buffer srcx tx {taint=tntx}, xb |), (| TD_Buffer srcy ty {taint=tnty}, yb |)
| (| TD_ImmBuffer srcx tx {taint=tntx}, xb |), (| TD_ImmBuffer srcy ty {taint=tnty}, yb |) ->
disjoint_not_eq xb yb \/ (xb === yb /\ tntx == tnty /\ tx == ty /\ srcx == srcy)
| _ -> True
[@__reduce__]
let disjoint_or_eq (l:list arg) =
BigOps.pairwise_and' disjoint_or_eq_1 l
[@__reduce__]
let live_arg (h:HS.mem) (x:arg) =
match x with
| (|TD_Buffer _ _ _, x|)
| (|TD_ImmBuffer _ _ _, x|) -> B.live h x
| (|TD_Base _, _ |) -> True
[@__reduce__]
let all_live (h:HS.mem) (bs:list arg) =
BigOps.big_and' (live_arg h) bs
[@__reduce__]
let mem_roots_p (h0:HS.mem) (args:list arg) =
disjoint_or_eq args /\
all_live h0 args
[@__reduce__]
let mem_roots (args:list arg) =
h0:HS.mem{ mem_roots_p h0 args }
[@__reduce__]
let args_b8 (args:list arg) : GTot (list b8) =
let maybe_cons_buffer (x:arg) (args:list b8) : GTot (list b8) =
match x with
| (|TD_Buffer src _ _, x|) -> mut_to_b8 src x :: args
| (|TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x :: args
| (|TD_Base _, _ |) -> args
in
List.Tot.fold_right_gtot args maybe_cons_buffer []
[@__reduce__]
let modified_arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_buffer x
| _ -> B.loc_none
[@__reduce__]
let loc_modified_args (args:list arg) : GTot B.loc =
let maybe_union_loc (x:arg) l =
match x with
| (|TD_Buffer _ _ {modified=true}, x|) -> B.loc_union (B.loc_buffer x) l
| _ -> l
in
List.Tot.fold_right_gtot args maybe_union_loc B.loc_none
[@__reduce__]
let arg_loc (x:arg) : GTot B.loc =
match x with
| (|TD_Buffer _ _ _, x|) -> B.loc_buffer x
| (|TD_ImmBuffer _ _ _, x|) -> B.loc_buffer x
| (|TD_Base _, _|) -> B.loc_none
[@__reduce__]
let loc_all_args (args:list arg) : GTot B.loc =
let l = List.Tot.map_gtot arg_loc args in
List.Tot.fold_right_gtot l B.loc_union B.loc_none
let all_live_cons (hd:arg) (tl:list arg) (h0:HS.mem)
: Lemma
(all_live h0 (hd :: tl) <==> (live_arg h0 hd /\ all_live h0 tl))
= ()
let disjoint_or_eq_def (l:list arg)
: Lemma (disjoint_or_eq l == BigOps.pairwise_and' disjoint_or_eq_1 l)
= ()
let disjoint_or_eq_cons (hd:arg) (tl:list arg)
: Lemma (disjoint_or_eq (hd::tl) <==> (BigOps.big_and' (disjoint_or_eq_1 hd) tl /\ disjoint_or_eq tl))
= BigOps.pairwise_and'_cons disjoint_or_eq_1 hd tl
#set-options "--initial_ifuel 2 --max_fuel 2"
let rec args_b8_mem (l:list arg) (y:b8)
: Lemma (L.memP y (args_b8 l) <==>
(exists (a:arg). {:pattern L.memP a l}
L.memP a l /\
(match a with
| (| TD_Base _, _|) -> False
| (| TD_Buffer src _ _, x|) -> mut_to_b8 src x == y
| (| TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x == y)))
= let goal (l:list arg) (a:arg) =
L.memP a l /\
(match a with
| (| TD_Base _, _|) -> False
| (| TD_Buffer src _ _, x|) -> mut_to_b8 src x == y
| (| TD_ImmBuffer src _ _, x|) -> imm_to_b8 src x == y)
in
match l with
| [] -> ()
| hd::tl ->
match hd with
| (| TD_Base _, _ |) ->
args_b8_mem tl y;
assert ((exists a. goal tl a) ==> (exists a. goal l a))
| (| TD_Buffer bt _ q, x |) ->
let aux_1 ()
: Lemma (requires (y == mut_to_b8 bt x))
(ensures (exists a. goal l a)) =
FStar.Classical.exists_intro (goal l) hd
in
let aux_2 ()
: Lemma (requires (mut_to_b8 bt x =!= y))
(ensures (L.memP y (args_b8 l) <==> (exists a. goal l a))) =
args_b8_mem tl y
in
FStar.Classical.move_requires aux_1 ();
FStar.Classical.move_requires aux_2 ()
| (| TD_ImmBuffer bt _ q, x |) ->
let aux_1 ()
: Lemma (requires (y == imm_to_b8 bt x))
(ensures (exists a. goal l a)) =
FStar.Classical.exists_intro (goal l) hd
in
let aux_2 ()
: Lemma (requires (imm_to_b8 bt x =!= y))
(ensures (L.memP y (args_b8 l) <==> (exists a. goal l a))) =
args_b8_mem tl y
in
FStar.Classical.move_requires aux_1 ();
FStar.Classical.move_requires aux_2 ()
let rec args_b8_disjoint_or_eq (args:list arg)
: Lemma
(requires (disjoint_or_eq args))
(ensures (list_disjoint_or_eq (args_b8 args)))
=
list_disjoint_or_eq_reveal ();
match args with
| [] -> ()
| hd::tl ->
disjoint_or_eq_cons hd tl;
args_b8_disjoint_or_eq tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl;
FStar.Classical.forall_intro (args_b8_mem tl)
let rec args_b8_live (hs:HS.mem) (args:list arg{all_live hs args})
: Lemma (list_live hs (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
all_live_cons hd tl hs;
assert (live_arg hs hd);
assert (all_live hs tl);
args_b8_live hs tl;
match hd with
| (| TD_Base _ , _ |) ->
assert (args_b8 args == args_b8 tl)
| (| TD_Buffer t _ _, x |) ->
assert (B.live hs x);
assert (args_b8 args == Buffer true x :: args_b8 tl)
| (| TD_ImmBuffer t _ _, x |) ->
assert (B.live hs x);
assert (args_b8 args == imm_to_b8 t x :: args_b8 tl)
let liveness_disjointness (args:list arg) (h:mem_roots args)
: Lemma (list_disjoint_or_eq (args_b8 args) /\
list_live h (args_b8 args))
= args_b8_disjoint_or_eq args;
args_b8_live h args
let mk_mem (args:list arg) (h:mem_roots args) : GTot interop_heap =
liveness_disjointness args h;
mem_of_hs_roots (args_b8 args) h
let mk_mem_injective (args:list arg) (h:mem_roots args)
: Lemma (hs_of_mem (mk_mem args h) == h /\
ptrs_of_mem (mk_mem args h) == args_b8 args)
= () | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Types.fst.checked",
"Vale.Interop.Heap_s.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowStar.ImmutableBuffer.fst.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.BufferView.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.Base.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Vale.Def.Words_s",
"short_module": "W"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.Def.Prop_s",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.ImmutableBuffer",
"short_module": "IB"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "MB"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Heap_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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": 2,
"max_fuel": 2,
"max_ifuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
args: Prims.list Vale.Interop.Base.arg ->
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
Vale.Interop.Base.mem_roots_p h0 args /\
LowStar.Monotonic.Buffer.modifies LowStar.Monotonic.Buffer.loc_none h0 h1)
(ensures Vale.Interop.Base.mem_roots_p h1 args) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Vale.Interop.Base.arg",
"FStar.Monotonic.HyperStack.mem",
"Vale.Interop.Base.valid_base_type",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.TD_Base",
"Vale.Arch.HeapTypes_s.base_typ",
"Vale.Interop.Base.buffer_qualifiers",
"Vale.Interop.Base.TD_Buffer",
"Prims._assert",
"LowStar.Monotonic.Buffer.live",
"Vale.Interop.Types.base_typ_as_type",
"LowStar.Buffer.trivial_preorder",
"Vale.Interop.Base.TD_ImmBuffer",
"LowStar.ImmutableBuffer.immutable_preorder",
"Prims.unit",
"Vale.Interop.Base.mem_roots_p_modifies_none",
"Vale.Interop.Base.all_live_cons",
"Prims.l_and",
"Vale.Interop.Base.mem_roots_p",
"LowStar.Monotonic.Buffer.modifies",
"LowStar.Monotonic.Buffer.loc_none",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec mem_roots_p_modifies_none (args: list arg) (h0 h1: HS.mem)
: Lemma (requires mem_roots_p h0 args /\ B.modifies B.loc_none h0 h1)
(ensures mem_roots_p h1 args) =
| match args with
| [] -> ()
| hd :: tl ->
all_live_cons hd tl h0;
mem_roots_p_modifies_none tl h0 h1;
match hd with
| (| TD_Base _ , _ |) -> ()
| (| TD_Buffer _ _ _ , x |) | (| TD_ImmBuffer _ _ _ , x |) -> assert (B.live h1 x) | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.