effect
stringclasses
48 values
original_source_type
stringlengths
0
23k
opens_and_abbrevs
listlengths
2
92
isa_cross_project_example
bool
1 class
source_definition
stringlengths
9
57.9k
partial_definition
stringlengths
7
23.3k
is_div
bool
2 classes
is_type
null
is_proof
bool
2 classes
completed_definiton
stringlengths
1
250k
dependencies
dict
effect_flags
sequencelengths
0
2
ideal_premises
sequencelengths
0
236
mutual_with
sequencelengths
0
11
file_context
stringlengths
0
407k
interleaved
bool
1 class
is_simply_typed
bool
2 classes
file_name
stringlengths
5
48
vconfig
dict
is_simple_lemma
null
source_type
stringlengths
10
23k
proof_features
sequencelengths
0
1
name
stringlengths
8
95
source
dict
verbose_type
stringlengths
1
7.42k
source_range
dict
Prims.GTot
val no_lookahead_on (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x'
val no_lookahead_on (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0 let no_lookahead_on (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0 =
false
null
false
no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x'
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.bare_parser", "LowParse.Bytes.bytes", "Prims.l_imp", "LowParse.Spec.Base.no_lookahead_on_precond", "LowParse.Spec.Base.no_lookahead_on_postcond" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val no_lookahead_on (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0
[]
LowParse.Spec.Base.no_lookahead_on
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
f: LowParse.Spec.Base.bare_parser t -> x: LowParse.Bytes.bytes -> x': LowParse.Bytes.bytes -> Prims.GTot Type0
{ "end_col": 68, "end_line": 139, "start_col": 2, "start_line": 139 }
Prims.GTot
val no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x'
val no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 =
false
null
false
forall (x: bytes) (x': bytes). {:pattern (no_lookahead_on_precond f x x')\/(no_lookahead_on_postcond f x x')} no_lookahead_on f x x'
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.bare_parser", "Prims.l_Forall", "LowParse.Bytes.bytes", "LowParse.Spec.Base.no_lookahead_on", "LowParse.Spec.Base.no_lookahead_on_postcond", "LowParse.Spec.Base.no_lookahead_on_precond" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.no_lookahead
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
f: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 129, "end_line": 159, "start_col": 2, "start_line": 159 }
Prims.GTot
val is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s))
val is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 =
false
null
false
forall (s: bytes). {:pattern (parse f s)} (Seq.length s < sz) == (None? (parse f s))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "Prims.nat", "LowParse.Spec.Base.bare_parser", "Prims.l_Forall", "LowParse.Bytes.bytes", "Prims.eq2", "Prims.bool", "Prims.op_LessThan", "FStar.Seq.Base.length", "LowParse.Bytes.byte", "FStar.Pervasives.Native.uu___is_None", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.is_total_constant_size_parser
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
sz: Prims.nat -> f: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 44, "end_line": 265, "start_col": 2, "start_line": 264 }
Prims.GTot
val injective (#t: Type) (p: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2
val injective (#t: Type) (p: bare_parser t) : GTot Type0 let injective (#t: Type) (p: bare_parser t) : GTot Type0 =
false
null
false
forall (b1: bytes) (b2: bytes). {:pattern (injective_precond p b1 b2)\/(injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.bare_parser", "Prims.l_Forall", "LowParse.Bytes.bytes", "Prims.l_imp", "LowParse.Spec.Base.injective_precond", "LowParse.Spec.Base.injective_postcond" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = ()
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val injective (#t: Type) (p: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.injective
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 28, "end_line": 93, "start_col": 2, "start_line": 91 }
Prims.GTot
val parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f
val parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 =
false
null
false
injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.bare_parser", "Prims.l_and", "LowParse.Spec.Base.injective", "Prims.l_imp", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "LowParse.Spec.Base.parser_subkind", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind", "LowParse.Spec.Base.parser_subkind_prop", "FStar.Pervasives.Native.__proj__Some__item__v", "LowParse.Spec.Base.parses_at_least", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low", "Prims.nat", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high", "LowParse.Spec.Base.parses_at_most", "LowParse.Spec.Base.parser_kind_metadata_prop" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.parser_kind_prop'
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
k: LowParse.Spec.Base.parser_kind -> f: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 31, "end_line": 327, "start_col": 2, "start_line": 323 }
Prims.Tot
val coerce_to_tot_bare_parser (t: Type) (k2: parser_kind) (p: tot_parser k2 t) : Tot (tot_bare_parser t)
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p
val coerce_to_tot_bare_parser (t: Type) (k2: parser_kind) (p: tot_parser k2 t) : Tot (tot_bare_parser t) let coerce_to_tot_bare_parser (t: Type) (k2: parser_kind) (p: tot_parser k2 t) : Tot (tot_bare_parser t) =
false
null
false
p
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.tot_parser", "LowParse.Spec.Base.tot_bare_parser" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val coerce_to_tot_bare_parser (t: Type) (k2: parser_kind) (p: tot_parser k2 t) : Tot (tot_bare_parser t)
[]
LowParse.Spec.Base.coerce_to_tot_bare_parser
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
t: Type -> k2: LowParse.Spec.Base.parser_kind -> p: LowParse.Spec.Base.tot_parser k2 t -> LowParse.Spec.Base.tot_bare_parser t
{ "end_col": 30, "end_line": 426, "start_col": 29, "start_line": 426 }
Prims.Tot
val get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k
val get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind =
false
null
false
k
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind
[]
LowParse.Spec.Base.get_parser_kind
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> LowParse.Spec.Base.parser_kind
{ "end_col": 3, "end_line": 368, "start_col": 2, "start_line": 368 }
Prims.Tot
val coerce_to_bare_parser (t: Type) (k2: parser_kind) (p: parser k2 t) : Tot (bare_parser t)
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p
val coerce_to_bare_parser (t: Type) (k2: parser_kind) (p: parser k2 t) : Tot (bare_parser t) let coerce_to_bare_parser (t: Type) (k2: parser_kind) (p: parser k2 t) : Tot (bare_parser t) =
false
null
false
p
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.bare_parser" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val coerce_to_bare_parser (t: Type) (k2: parser_kind) (p: parser k2 t) : Tot (bare_parser t)
[]
LowParse.Spec.Base.coerce_to_bare_parser
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
t: Type -> k2: LowParse.Spec.Base.parser_kind -> p: LowParse.Spec.Base.parser k2 t -> LowParse.Spec.Base.bare_parser t
{ "end_col": 26, "end_line": 416, "start_col": 25, "start_line": 416 }
Prims.Tot
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high })
let parser_kind =
false
null
false
(x: parser_kind'{Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high})
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind'", "Prims.l_imp", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "Prims.nat", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high", "Prims.op_LessThanOrEqual", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low", "FStar.Pervasives.Native.__proj__Some__item__v" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; }
false
true
LowParse.Spec.Base.fsti
{ "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" }
null
val parser_kind : Type0
[]
LowParse.Spec.Base.parser_kind
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
Type0
{ "end_col": 2, "end_line": 294, "start_col": 18, "start_line": 292 }
Prims.GTot
val parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None
val parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 =
false
null
false
forall input. {:pattern (parse f input)} parse f input == None
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.bare_parser", "Prims.l_Forall", "LowParse.Bytes.bytes", "Prims.eq2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "FStar.Pervasives.Native.None" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.parser_always_fails
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
f: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 65, "end_line": 314, "start_col": 2, "start_line": 314 }
Prims.GTot
val parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) )
val parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 =
false
null
false
forall (s: bytes). {:pattern (parse f s)} Some? (parse f s) ==> (let _, consumed = Some?.v (parse f s) in sz <= (consumed <: nat))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "Prims.nat", "LowParse.Spec.Base.bare_parser", "Prims.l_Forall", "LowParse.Bytes.bytes", "Prims.l_imp", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.op_LessThanOrEqual", "Prims.logical", "FStar.Pervasives.Native.__proj__Some__item__v" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.parses_at_least
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
sz: Prims.nat -> f: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 3, "end_line": 196, "start_col": 2, "start_line": 192 }
Prims.GTot
val no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' ))
val no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0 let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0 =
false
null
false
Some? (parse f x) ==> (let Some v = parse f x in let y, _ = v in Some? (parse f x') /\ (let Some v' = parse f x' in let y', _ = v' in y == y'))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.bare_parser", "LowParse.Bytes.bytes", "Prims.l_imp", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.l_and", "Prims.eq2", "Prims.logical", "FStar.Pervasives.Native.option" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0
[]
LowParse.Spec.Base.no_lookahead_on_postcond
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
f: LowParse.Spec.Base.bare_parser t -> x: LowParse.Bytes.bytes -> x': LowParse.Bytes.bytes -> Prims.GTot Type0
{ "end_col": 4, "end_line": 132, "start_col": 2, "start_line": 125 }
Prims.GTot
val no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off )
val no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0 let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0 =
false
null
false
Some? (parse f x) /\ (let Some v = parse f x in let _, off = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.bare_parser", "LowParse.Bytes.bytes", "Prims.l_and", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.op_LessThanOrEqual", "FStar.Seq.Base.length", "LowParse.Bytes.byte", "Prims.eq2", "FStar.Seq.Base.seq", "FStar.Seq.Base.slice", "Prims.logical", "FStar.Pervasives.Native.option" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x': bytes) : GTot Type0
[]
LowParse.Spec.Base.no_lookahead_on_precond
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
f: LowParse.Spec.Base.bare_parser t -> x: LowParse.Bytes.bytes -> x': LowParse.Bytes.bytes -> Prims.GTot Type0
{ "end_col": 3, "end_line": 118, "start_col": 2, "start_line": 113 }
Prims.GTot
val parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) )
val parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 =
false
null
false
forall (s: bytes). {:pattern (parse f s)} Some? (parse f s) ==> (let _, consumed = Some?.v (parse f s) in sz >= (consumed <: nat))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "Prims.nat", "LowParse.Spec.Base.bare_parser", "Prims.l_Forall", "LowParse.Bytes.bytes", "Prims.l_imp", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.op_GreaterThanOrEqual", "Prims.logical", "FStar.Pervasives.Native.__proj__Some__item__v" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.parses_at_most
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
sz: Prims.nat -> f: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 3, "end_line": 238, "start_col": 2, "start_line": 234 }
Prims.GTot
val is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) )
val is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 =
false
null
false
forall (s: bytes). {:pattern (parse f s)} Some? (parse f s) ==> (let _, consumed = Some?.v (parse f s) in sz == (consumed <: nat))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "Prims.nat", "LowParse.Spec.Base.bare_parser", "Prims.l_Forall", "LowParse.Bytes.bytes", "Prims.l_imp", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.eq2", "Prims.logical", "FStar.Pervasives.Native.__proj__Some__item__v" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.is_constant_size_parser
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
sz: Prims.nat -> f: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 3, "end_line": 249, "start_col": 2, "start_line": 245 }
Prims.GTot
val injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 )
val injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 =
false
null
false
Some? (parse p b1) /\ Some? (parse p b2) /\ (let Some (v1, len1) = parse p b1 in let Some (v2, len2) = parse p b2 in v1 == v2)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.bare_parser", "LowParse.Bytes.bytes", "Prims.l_and", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.eq2", "Prims.logical", "FStar.Pervasives.Native.option" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0
[]
LowParse.Spec.Base.injective_precond
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.bare_parser t -> b1: LowParse.Bytes.bytes -> b2: LowParse.Bytes.bytes -> Prims.GTot Type0
{ "end_col": 3, "end_line": 47, "start_col": 2, "start_line": 42 }
FStar.Pervasives.Lemma
val injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires (forall (b: bytes). parse p2 b == parse p1 b)) (ensures (injective p2 <==> injective p1))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1))
val injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires (forall (b: bytes). parse p2 b == parse p1 b)) (ensures (injective p2 <==> injective p1)) let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires (forall (b: bytes). parse p2 b == parse p1 b)) (ensures (injective p2 <==> injective p1)) =
false
null
true
Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "LowParse.Spec.Base.bare_parser", "FStar.Classical.forall_intro_2", "LowParse.Bytes.bytes", "Prims.l_imp", "Prims.l_and", "Prims.eq2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.l_iff", "LowParse.Spec.Base.injective_postcond", "FStar.Classical.move_requires", "LowParse.Spec.Base.injective_postcond_ext", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "LowParse.Spec.Base.injective_precond", "LowParse.Spec.Base.injective_precond_ext", "Prims.l_Forall", "LowParse.Spec.Base.injective" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires (forall (b: bytes). parse p2 b == parse p1 b)) (ensures (injective p2 <==> injective p1))
[]
LowParse.Spec.Base.injective_ext
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p1: LowParse.Spec.Base.bare_parser t -> p2: LowParse.Spec.Base.bare_parser t -> FStar.Pervasives.Lemma (requires forall (b: LowParse.Bytes.bytes). LowParse.Spec.Base.parse p2 b == LowParse.Spec.Base.parse p1 b) (ensures LowParse.Spec.Base.injective p2 <==> LowParse.Spec.Base.injective p1)
{ "end_col": 96, "end_line": 106, "start_col": 2, "start_line": 105 }
Prims.Pure
val strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; }
val strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) =
false
null
false
{ parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md }
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "Prims.nat", "LowParse.Spec.Base.parser_kind_metadata_t", "LowParse.Spec.Base.Mkparser_kind'", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.parser_subkind", "LowParse.Spec.Base.ParserStrong", "LowParse.Spec.Base.parser_kind", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.strong_parser_kind
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
lo: Prims.nat -> hi: Prims.nat -> md: LowParse.Spec.Base.parser_kind_metadata_t -> Prims.Pure LowParse.Spec.Base.parser_kind
{ "end_col": 30, "end_line": 304, "start_col": 4, "start_line": 301 }
Prims.Tot
val is_some (#t: Type) (x: option t) : Tot (y: bool{y == Some? x})
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false
val is_some (#t: Type) (x: option t) : Tot (y: bool{y == Some? x}) let is_some (#t: Type) (x: option t) : Tot (y: bool{y == Some? x}) =
false
null
false
match x with | Some _ -> true | _ -> false
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "FStar.Pervasives.Native.option", "Prims.bool", "Prims.eq2", "FStar.Pervasives.Native.uu___is_Some" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t)
false
false
LowParse.Spec.Base.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val is_some (#t: Type) (x: option t) : Tot (y: bool{y == Some? x})
[]
LowParse.Spec.Base.is_some
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
x: FStar.Pervasives.Native.option t -> y: Prims.bool{y == Some? x}
{ "end_col": 14, "end_line": 457, "start_col": 2, "start_line": 455 }
FStar.Pervasives.Lemma
val no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires (forall (b: bytes). parse p2 b == parse p1 b)) (ensures (no_lookahead p2 <==> no_lookahead p1))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1))
val no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires (forall (b: bytes). parse p2 b == parse p1 b)) (ensures (no_lookahead p2 <==> no_lookahead p1)) let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires (forall (b: bytes). parse p2 b == parse p1 b)) (ensures (no_lookahead p2 <==> no_lookahead p1)) =
false
null
true
Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "LowParse.Spec.Base.bare_parser", "FStar.Classical.forall_intro_2", "LowParse.Bytes.bytes", "Prims.l_imp", "Prims.l_and", "Prims.eq2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.l_iff", "LowParse.Spec.Base.no_lookahead_on", "FStar.Classical.move_requires", "LowParse.Spec.Base.no_lookahead_on_ext", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "Prims.l_Forall", "LowParse.Spec.Base.no_lookahead" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires (forall (b: bytes). parse p2 b == parse p1 b)) (ensures (no_lookahead p2 <==> no_lookahead p1))
[]
LowParse.Spec.Base.no_lookahead_ext
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p1: LowParse.Spec.Base.bare_parser t -> p2: LowParse.Spec.Base.bare_parser t -> FStar.Pervasives.Lemma (requires forall (b: LowParse.Bytes.bytes). LowParse.Spec.Base.parse p2 b == LowParse.Spec.Base.parse p1 b) (ensures LowParse.Spec.Base.no_lookahead p2 <==> LowParse.Spec.Base.no_lookahead p1)
{ "end_col": 93, "end_line": 171, "start_col": 2, "start_line": 171 }
Prims.Tot
val some_v (#t: Type) (x: option t {Some? x}) : Tot (y: t{y == Some?.v x})
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y
val some_v (#t: Type) (x: option t {Some? x}) : Tot (y: t{y == Some?.v x}) let some_v (#t: Type) (x: option t {Some? x}) : Tot (y: t{y == Some?.v x}) =
false
null
false
match x with | Some y -> y
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "FStar.Pervasives.Native.option", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "Prims.eq2", "FStar.Pervasives.Native.__proj__Some__item__v" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x })
false
false
LowParse.Spec.Base.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val some_v (#t: Type) (x: option t {Some? x}) : Tot (y: t{y == Some?.v x})
[]
LowParse.Spec.Base.some_v
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
x: FStar.Pervasives.Native.option t {Some? x} -> y: t{y == Some?.v x}
{ "end_col": 15, "end_line": 466, "start_col": 2, "start_line": 465 }
Prims.GTot
val is_weaker_than (k1 k2: parser_kind) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high ))))
val is_weaker_than (k1 k2: parser_kind) : GTot Type0 let is_weaker_than (k1 k2: parser_kind) : GTot Type0 =
false
null
false
(Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ((Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> (Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high))))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "Prims.l_and", "Prims.l_imp", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "LowParse.Spec.Base.parser_kind_metadata_some", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata", "Prims.eq2", "LowParse.Spec.Base.parser_kind_metadata_t", "Prims.op_disEquality", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.ParserKindMetadataFail", "LowParse.Spec.Base.parser_subkind", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind", "Prims.op_LessThanOrEqual", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low", "Prims.nat", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high", "FStar.Pervasives.Native.__proj__Some__item__v" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val is_weaker_than (k1 k2: parser_kind) : GTot Type0
[]
LowParse.Spec.Base.is_weaker_than
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
k1: LowParse.Spec.Base.parser_kind -> k2: LowParse.Spec.Base.parser_kind -> Prims.GTot Type0
{ "end_col": 6, "end_line": 403, "start_col": 2, "start_line": 396 }
Prims.Tot
val bool_or (b1 b2: bool) : Tot (y: bool{y == (b1 || b2)})
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2
val bool_or (b1 b2: bool) : Tot (y: bool{y == (b1 || b2)}) let bool_or (b1 b2: bool) : Tot (y: bool{y == (b1 || b2)}) =
false
null
false
if b1 then true else b2
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "Prims.bool", "Prims.eq2", "Prims.op_BarBar" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool)
false
false
LowParse.Spec.Base.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val bool_or (b1 b2: bool) : Tot (y: bool{y == (b1 || b2)})
[]
LowParse.Spec.Base.bool_or
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
b1: Prims.bool -> b2: Prims.bool -> y: Prims.bool{y == (b1 || b2)}
{ "end_col": 25, "end_line": 480, "start_col": 2, "start_line": 480 }
Prims.Pure
val glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl. {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl)))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l
val glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl. {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl))) let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl. {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl))) =
false
null
false
glb_list_of id l
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "Prims.list", "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.glb_list_of", "FStar.Pervasives.id", "Prims.l_True", "Prims.l_Forall", "Prims.l_imp", "Prims.b2t", "FStar.List.Tot.Base.mem", "LowParse.Spec.Base.is_weaker_than" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl. {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl)))
[]
LowParse.Spec.Base.glb_list
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
l: Prims.list LowParse.Spec.Base.parser_kind -> Prims.Pure LowParse.Spec.Base.parser_kind
{ "end_col": 18, "end_line": 573, "start_col": 2, "start_line": 573 }
Prims.Pure
val coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p
val coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) =
false
null
false
p
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "Prims.eq2", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.coerce_parser
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
t2: Type -> p: LowParse.Spec.Base.parser k t1 -> Prims.Pure (LowParse.Spec.Base.parser k t2)
{ "end_col": 3, "end_line": 606, "start_col": 2, "start_line": 606 }
Prims.Pure
val strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f
val strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) =
false
null
false
f
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.bare_parser", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.parser_kind_prop", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.strengthen
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
k: LowParse.Spec.Base.parser_kind -> f: LowParse.Spec.Base.bare_parser t -> Prims.Pure (LowParse.Spec.Base.parser k t)
{ "end_col": 3, "end_line": 445, "start_col": 2, "start_line": 445 }
Prims.Pure
val serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2
val serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) =
false
null
false
serialize_ext p1 s1 p2
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.Spec.Base.serialize_ext", "Prims.l_and", "Prims.eq2", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.serialize_ext'
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p1: LowParse.Spec.Base.parser k1 t1 -> s1: LowParse.Spec.Base.serializer p1 -> p2: LowParse.Spec.Base.parser k2 t2 -> Prims.Pure (LowParse.Spec.Base.serializer p2)
{ "end_col": 24, "end_line": 754, "start_col": 2, "start_line": 754 }
Prims.Tot
val mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: (x: t -> Lemma (parse p (f x) == Some (x, Seq.length (f x))))) : Tot (serializer p)
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f
val mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: (x: t -> Lemma (parse p (f x) == Some (x, Seq.length (f x))))) : Tot (serializer p) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: (x: t -> Lemma (parse p (f x) == Some (x, Seq.length (f x))))) : Tot (serializer p) =
false
null
false
Classical.forall_intro prf; f
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.bare_serializer", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "FStar.Seq.Base.length", "LowParse.Bytes.byte", "Prims.Nil", "FStar.Pervasives.pattern", "FStar.Classical.forall_intro", "LowParse.Spec.Base.serializer" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) ))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: (x: t -> Lemma (parse p (f x) == Some (x, Seq.length (f x))))) : Tot (serializer p)
[]
LowParse.Spec.Base.mk_serializer
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> f: LowParse.Spec.Base.bare_serializer t -> prf: (x: t -> FStar.Pervasives.Lemma (ensures LowParse.Spec.Base.parse p (f x) == FStar.Pervasives.Native.Some (x, FStar.Seq.Base.length (f x)))) -> LowParse.Spec.Base.serializer p
{ "end_col": 3, "end_line": 716, "start_col": 2, "start_line": 715 }
Prims.Pure
val seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Pure (s_: Seq.seq t {Seq.length s_ == Seq.length s}) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s)))
val seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Pure (s_: Seq.seq t {Seq.length s_ == Seq.length s}) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Pure (s_: Seq.seq t {Seq.length s_ == Seq.length s}) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) =
false
null
false
Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s)))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "FStar.Seq.Base.seq", "Prims.nat", "FStar.Seq.Base.append", "FStar.Seq.Base.slice", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.eq2", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Pure (s_: Seq.seq t {Seq.length s_ == Seq.length s}) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.seq_upd_seq
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> s': FStar.Seq.Base.seq t -> Prims.Pure (s_: FStar.Seq.Base.seq t {FStar.Seq.Base.length s_ == FStar.Seq.Base.length s})
{ "end_col": 68, "end_line": 932, "start_col": 2, "start_line": 930 }
FStar.Pervasives.Lemma
val seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s'))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s')
val seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) =
false
null
true
assert ((Seq.slice (seq_upd_seq s i s') i (i + Seq.length s')) `Seq.equal` s')
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.slice", "LowParse.Spec.Base.seq_upd_seq", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.unit", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.squash", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s'))
[]
LowParse.Spec.Base.seq_upd_seq_slice
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> s': FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires i + FStar.Seq.Base.length s' <= FStar.Seq.Base.length s) (ensures FStar.Seq.Base.slice (LowParse.Spec.Base.seq_upd_seq s i s') i (i + FStar.Seq.Base.length s') == s')
{ "end_col": 78, "end_line": 954, "start_col": 2, "start_line": 954 }
FStar.Pervasives.Lemma
val seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s'))
val seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) =
false
null
true
seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s'))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "FStar.Seq.Properties.slice_slice", "LowParse.Spec.Base.seq_upd_seq", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.op_Subtraction", "Prims.unit", "LowParse.Spec.Base.seq_upd_seq_slice_right", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.squash", "Prims.eq2", "FStar.Seq.Base.slice", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2))
[]
LowParse.Spec.Base.seq_upd_seq_slice_right'
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> s': FStar.Seq.Base.seq t -> j1: Prims.nat -> j2: Prims.nat -> FStar.Pervasives.Lemma (requires i + FStar.Seq.Base.length s' <= j1 /\ j1 <= j2 /\ j2 <= FStar.Seq.Base.length s) (ensures FStar.Seq.Base.slice (LowParse.Spec.Base.seq_upd_seq s i s') j1 j2 == FStar.Seq.Base.slice s j1 j2)
{ "end_col": 127, "end_line": 1010, "start_col": 2, "start_line": 1009 }
Prims.Tot
val coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit{t2 == t1}) : Tot (serializer (coerce_parser t2 p))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s
val coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit{t2 == t1}) : Tot (serializer (coerce_parser t2 p)) let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit{t2 == t1}) : Tot (serializer (coerce_parser t2 p)) =
false
null
false
s
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "Prims.unit", "Prims.eq2", "LowParse.Spec.Base.coerce_parser" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } )
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit{t2 == t1}) : Tot (serializer (coerce_parser t2 p))
[]
LowParse.Spec.Base.coerce_serializer
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
t2: Type -> s: LowParse.Spec.Base.serializer p -> u198: u199: Prims.unit{t2 == t1} -> LowParse.Spec.Base.serializer (LowParse.Spec.Base.coerce_parser t2 p)
{ "end_col": 3, "end_line": 727, "start_col": 2, "start_line": 727 }
FStar.Pervasives.Lemma
val parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures (let Some (y, consumed) = parse p x in ((serialize s y) `Seq.append` (Seq.slice x consumed (Seq.length x))) `Seq.equal` x))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x
val parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures (let Some (y, consumed) = parse p x in ((serialize s y) `Seq.append` (Seq.slice x consumed (Seq.length x))) `Seq.equal` x)) let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures (let Some (y, consumed) = parse p x in ((serialize s y) `Seq.append` (Seq.slice x consumed (Seq.length x))) `Seq.equal` x)) =
false
null
true
let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.Bytes.bytes", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse_injective", "LowParse.Spec.Base.serialize", "Prims.unit", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.parse", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "Prims.squash", "FStar.Seq.Base.equal", "LowParse.Bytes.byte", "FStar.Seq.Base.append", "FStar.Seq.Base.slice", "FStar.Seq.Base.length", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures (let Some (y, consumed) = parse p x in ((serialize s y) `Seq.append` (Seq.slice x consumed (Seq.length x))) `Seq.equal` x))
[]
LowParse.Spec.Base.parsed_data_is_serialize
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> x: LowParse.Bytes.bytes -> FStar.Pervasives.Lemma (requires Some? (LowParse.Spec.Base.parse p x)) (ensures (let _ = LowParse.Spec.Base.parse p x in (let FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ y consumed) = _ in FStar.Seq.Base.equal (FStar.Seq.Base.append (LowParse.Spec.Base.serialize s y) (FStar.Seq.Base.slice x consumed (FStar.Seq.Base.length x))) x) <: Type0))
{ "end_col": 37, "end_line": 788, "start_col": 1, "start_line": 787 }
FStar.Pervasives.Lemma
val seq_upd_seq_right (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s'))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = assert (seq_upd_seq s (Seq.length s - Seq.length s') s' `Seq.equal` Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')
val seq_upd_seq_right (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) let seq_upd_seq_right (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) =
false
null
true
assert ((seq_upd_seq s (Seq.length s - Seq.length s') s') `Seq.equal` (Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s'))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims._assert", "FStar.Seq.Base.equal", "LowParse.Spec.Base.seq_upd_seq", "Prims.op_Subtraction", "FStar.Seq.Base.length", "FStar.Seq.Base.append", "FStar.Seq.Base.slice", "Prims.unit", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.squash", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))) let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_right (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s'))
[]
LowParse.Spec.Base.seq_upd_seq_right
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> s': FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires FStar.Seq.Base.length s' <= FStar.Seq.Base.length s) (ensures LowParse.Spec.Base.seq_upd_seq s (FStar.Seq.Base.length s - FStar.Seq.Base.length s') s' == FStar.Seq.Base.append (FStar.Seq.Base.slice s 0 (FStar.Seq.Base.length s - FStar.Seq.Base.length s')) s')
{ "end_col": 131, "end_line": 1047, "start_col": 2, "start_line": 1047 }
FStar.Pervasives.Lemma
val seq_append_seq_upd_seq_l (#t: Type) (s: Seq.seq t) (i': nat) (s' sl: Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures (Seq.length sl + i' <= Seq.length (sl `Seq.append` s) /\ sl `Seq.append` (seq_upd_seq s i' s') == seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s'))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_append_seq_upd_seq_l (#t: Type) (s: Seq.seq t) (i': nat) (s' : Seq.seq t) (sl : Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures ( Seq.length sl + i' <= Seq.length (sl `Seq.append` s) /\ sl `Seq.append` seq_upd_seq s i' s' == seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s' )) = assert (sl `Seq.append` seq_upd_seq s i' s' `Seq.equal` seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s')
val seq_append_seq_upd_seq_l (#t: Type) (s: Seq.seq t) (i': nat) (s' sl: Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures (Seq.length sl + i' <= Seq.length (sl `Seq.append` s) /\ sl `Seq.append` (seq_upd_seq s i' s') == seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s')) let seq_append_seq_upd_seq_l (#t: Type) (s: Seq.seq t) (i': nat) (s' sl: Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures (Seq.length sl + i' <= Seq.length (sl `Seq.append` s) /\ sl `Seq.append` (seq_upd_seq s i' s') == seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s')) =
false
null
true
assert ((sl `Seq.append` (seq_upd_seq s i' s')) `Seq.equal` (seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s'))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.append", "LowParse.Spec.Base.seq_upd_seq", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.unit", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.squash", "Prims.l_and", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))) let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = assert (seq_upd_seq s (Seq.length s - Seq.length s') s' `Seq.equal` Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s') let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) `Seq.equal` seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3) let seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1: nat) (hi: nat) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) `Seq.equal` seq_upd_seq s1 (i1 + i2) s3) let seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires ( i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1) )) (ensures ( seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1 )) = assert (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 `Seq.equal` seq_upd_seq (seq_upd_seq s i2 s2) i1 s1) let seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s)) (ensures (Seq.upd s i x == seq_upd_seq s i (Seq.create 1 x))) = assert (Seq.upd s i x `Seq.equal` seq_upd_seq s i (Seq.create 1 x)) let seq_append_seq_upd_seq_l (#t: Type) (s: Seq.seq t) (i': nat) (s' : Seq.seq t) (sl : Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures ( Seq.length sl + i' <= Seq.length (sl `Seq.append` s) /\ sl `Seq.append` seq_upd_seq s i' s' == seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s'
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_append_seq_upd_seq_l (#t: Type) (s: Seq.seq t) (i': nat) (s' sl: Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures (Seq.length sl + i' <= Seq.length (sl `Seq.append` s) /\ sl `Seq.append` (seq_upd_seq s i' s') == seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s'))
[]
LowParse.Spec.Base.seq_append_seq_upd_seq_l
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i': Prims.nat -> s': FStar.Seq.Base.seq t -> sl: FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires i' + FStar.Seq.Base.length s' <= FStar.Seq.Base.length s) (ensures FStar.Seq.Base.length sl + i' <= FStar.Seq.Base.length (FStar.Seq.Base.append sl s) /\ FStar.Seq.Base.append sl (LowParse.Spec.Base.seq_upd_seq s i' s') == LowParse.Spec.Base.seq_upd_seq (FStar.Seq.Base.append sl s) (FStar.Seq.Base.length sl + i') s')
{ "end_col": 114, "end_line": 1117, "start_col": 2, "start_line": 1117 }
FStar.Pervasives.Lemma
val seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i)
val seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) =
false
null
true
assert ((Seq.slice (seq_upd_seq s i s') 0 i) `Seq.equal` (Seq.slice s 0 i))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.slice", "LowParse.Spec.Base.seq_upd_seq", "Prims.unit", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.squash", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i))
[]
LowParse.Spec.Base.seq_upd_seq_slice_left
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> s': FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires i + FStar.Seq.Base.length s' <= FStar.Seq.Base.length s) (ensures FStar.Seq.Base.slice (LowParse.Spec.Base.seq_upd_seq s i s') 0 i == FStar.Seq.Base.slice s 0 i)
{ "end_col": 73, "end_line": 976, "start_col": 2, "start_line": 976 }
FStar.Pervasives.Lemma
val seq_upd_bw_seq_right (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_bw_seq s 0 s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s'))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_bw_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_bw_seq s 0 s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = seq_upd_seq_right s s'
val seq_upd_bw_seq_right (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_bw_seq s 0 s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) let seq_upd_bw_seq_right (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_bw_seq s 0 s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) =
false
null
true
seq_upd_seq_right s s'
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "LowParse.Spec.Base.seq_upd_seq_right", "Prims.unit", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Seq.Base.length", "Prims.squash", "Prims.eq2", "LowParse.Spec.Base.seq_upd_bw_seq", "FStar.Seq.Base.append", "FStar.Seq.Base.slice", "Prims.op_Subtraction", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))) let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = assert (seq_upd_seq s (Seq.length s - Seq.length s') s' `Seq.equal` Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s') let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) `Seq.equal` seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3) let seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1: nat) (hi: nat) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) `Seq.equal` seq_upd_seq s1 (i1 + i2) s3) let seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires ( i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1) )) (ensures ( seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1 )) = assert (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 `Seq.equal` seq_upd_seq (seq_upd_seq s i2 s2) i1 s1) let seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s)) (ensures (Seq.upd s i x == seq_upd_seq s i (Seq.create 1 x))) = assert (Seq.upd s i x `Seq.equal` seq_upd_seq s i (Seq.create 1 x)) let seq_append_seq_upd_seq_l (#t: Type) (s: Seq.seq t) (i': nat) (s' : Seq.seq t) (sl : Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures ( Seq.length sl + i' <= Seq.length (sl `Seq.append` s) /\ sl `Seq.append` seq_upd_seq s i' s' == seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s' )) = assert (sl `Seq.append` seq_upd_seq s i' s' `Seq.equal` seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s') let seq_append_seq_upd_seq_r (#t: Type) (s: Seq.seq t) (i': nat) (s' : Seq.seq t) (sr : Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures ( i' <= Seq.length (s `Seq.append` sr) /\ seq_upd_seq s i' s' `Seq.append` sr == seq_upd_seq (s `Seq.append` sr) i' s' )) = assert ((seq_upd_seq s i' s' `Seq.append` sr) `Seq.equal` seq_upd_seq (s `Seq.append` sr) i' s') let seq_upd_bw_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = seq_upd_seq s (Seq.length s - i - Seq.length s') s' let seq_upd_bw_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_bw_seq_right (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_bw_seq s 0 s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s'))
[]
LowParse.Spec.Base.seq_upd_bw_seq_right
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> s': FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires FStar.Seq.Base.length s' <= FStar.Seq.Base.length s) (ensures LowParse.Spec.Base.seq_upd_bw_seq s 0 s' == FStar.Seq.Base.append (FStar.Seq.Base.slice s 0 (FStar.Seq.Base.length s - FStar.Seq.Base.length s')) s')
{ "end_col": 24, "end_line": 1150, "start_col": 2, "start_line": 1150 }
FStar.Pervasives.Lemma
val serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2))
val serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) =
false
null
true
assert (parse p (s x1) == parse p (s x2))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "Prims._assert", "Prims.eq2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.unit", "LowParse.Bytes.bytes", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2))
[]
LowParse.Spec.Base.serializer_injective
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> s: LowParse.Spec.Base.serializer p -> x1: t -> x2: t -> FStar.Pervasives.Lemma (requires s x1 == s x2) (ensures x1 == x2)
{ "end_col": 43, "end_line": 813, "start_col": 2, "start_line": 813 }
FStar.Pervasives.Lemma
val seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i)))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i)
val seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) =
false
null
true
seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "FStar.Seq.Properties.slice_slice", "LowParse.Spec.Base.seq_upd_seq", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.op_Subtraction", "Prims.unit", "LowParse.Spec.Base.seq_upd_seq_slice", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.squash", "Prims.eq2", "FStar.Seq.Base.slice", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s'))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i)))
[]
LowParse.Spec.Base.seq_upd_seq_slice'
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> s': FStar.Seq.Base.seq t -> j1: Prims.nat -> j2: Prims.nat -> FStar.Pervasives.Lemma (requires i + FStar.Seq.Base.length s' <= FStar.Seq.Base.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + FStar.Seq.Base.length s') (ensures FStar.Seq.Base.slice (LowParse.Spec.Base.seq_upd_seq s i s') j1 j2 == FStar.Seq.Base.slice s' (j1 - i) (j2 - i))
{ "end_col": 78, "end_line": 966, "start_col": 2, "start_line": 965 }
FStar.Pervasives.Lemma
val seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2
val seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) =
false
null
true
seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "FStar.Seq.Properties.slice_slice", "LowParse.Spec.Base.seq_upd_seq", "Prims.unit", "LowParse.Spec.Base.seq_upd_seq_slice_left", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.squash", "Prims.eq2", "FStar.Seq.Base.slice", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2))
[]
LowParse.Spec.Base.seq_upd_seq_slice_left'
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> s': FStar.Seq.Base.seq t -> j1: Prims.nat -> j2: Prims.nat -> FStar.Pervasives.Lemma (requires i + FStar.Seq.Base.length s' <= FStar.Seq.Base.length s /\ j1 <= j2 /\ j2 <= i) (ensures FStar.Seq.Base.slice (LowParse.Spec.Base.seq_upd_seq s i s') j1 j2 == FStar.Seq.Base.slice s j1 j2)
{ "end_col": 48, "end_line": 988, "start_col": 2, "start_line": 987 }
Prims.GTot
val parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f
val parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 =
false
null
false
match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_subkind", "LowParse.Spec.Base.bare_parser", "LowParse.Spec.Base.no_lookahead", "LowParse.Spec.Base.consumes_all" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.parser_subkind_prop
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
k: LowParse.Spec.Base.parser_subkind -> f: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 18, "end_line": 276, "start_col": 2, "start_line": 272 }
Prims.GTot
val consumes_all (#t: Type) (p: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len )
val consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 =
false
null
false
forall (b: bytes). {:pattern (parse p b)} Some? (parse p b) ==> (let Some (_, len) = parse p b in Seq.length b == len)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.bare_parser", "Prims.l_Forall", "LowParse.Bytes.bytes", "Prims.l_imp", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.eq2", "Prims.nat", "FStar.Seq.Base.length", "LowParse.Bytes.byte", "Prims.logical", "FStar.Pervasives.Native.option" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val consumes_all (#t: Type) (p: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.consumes_all
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 3, "end_line": 183, "start_col": 2, "start_line": 180 }
Prims.Tot
val total_constant_size_parser_kind (sz: nat) : Tot parser_kind
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal)
val total_constant_size_parser_kind (sz: nat) : Tot parser_kind let total_constant_size_parser_kind (sz: nat) : Tot parser_kind =
false
null
false
strong_parser_kind sz sz (Some ParserKindMetadataTotal)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "Prims.nat", "LowParse.Spec.Base.strong_parser_kind", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.parser_kind_metadata_some", "LowParse.Spec.Base.ParserKindMetadataTotal", "LowParse.Spec.Base.parser_kind" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat)
false
true
LowParse.Spec.Base.fsti
{ "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" }
null
val total_constant_size_parser_kind (sz: nat) : Tot parser_kind
[]
LowParse.Spec.Base.total_constant_size_parser_kind
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
sz: Prims.nat -> LowParse.Spec.Base.parser_kind
{ "end_col": 57, "end_line": 311, "start_col": 2, "start_line": 311 }
Prims.GTot
val parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f
val parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 =
false
null
false
match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.bare_parser", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata", "Prims.l_True", "Prims.l_imp", "Prims.eq2", "FStar.Pervasives.Native.option", "Prims.nat", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low", "LowParse.Spec.Base.is_total_constant_size_parser", "LowParse.Spec.Base.parser_always_fails" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0
[]
LowParse.Spec.Base.parser_kind_metadata_prop
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
k: LowParse.Spec.Base.parser_kind -> f: LowParse.Spec.Base.bare_parser t -> Prims.GTot Type0
{ "end_col": 56, "end_line": 320, "start_col": 2, "start_line": 317 }
Prims.GTot
val injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 )
val injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 =
false
null
false
Some? (parse p b1) /\ Some? (parse p b2) /\ (let Some (v1, len1) = parse p b1 in let Some (v2, len2) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.bare_parser", "LowParse.Bytes.bytes", "Prims.l_and", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.eq2", "Prims.nat", "FStar.Seq.Base.seq", "LowParse.Bytes.byte", "FStar.Seq.Base.slice", "Prims.logical", "FStar.Pervasives.Native.option" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0
[]
LowParse.Spec.Base.injective_postcond
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.bare_parser t -> b1: LowParse.Bytes.bytes -> b2: LowParse.Bytes.bytes -> Prims.GTot Type0
{ "end_col": 3, "end_line": 74, "start_col": 2, "start_line": 68 }
Prims.GTot
val serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x
val serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes =
false
null
false
s x
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.Bytes.bytes" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes
[]
LowParse.Spec.Base.serialize
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> x: t -> Prims.GTot LowParse.Bytes.bytes
{ "end_col": 5, "end_line": 763, "start_col": 2, "start_line": 763 }
Prims.Pure
val weaken (k1 #k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t
val weaken (k1 #k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) let weaken (k1 #k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) =
false
null
false
let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "Prims.unit", "LowParse.Spec.Base.is_weaker_than_correct", "LowParse.Spec.Base.bare_parser", "LowParse.Spec.Base.coerce_to_bare_parser", "LowParse.Spec.Base.is_weaker_than", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val weaken (k1 #k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.weaken
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
k1: LowParse.Spec.Base.parser_kind -> p2: LowParse.Spec.Base.parser k2 t -> Prims.Pure (LowParse.Spec.Base.parser k1 t)
{ "end_col": 18, "end_line": 423, "start_col": 1, "start_line": 421 }
Prims.Tot
val is_strong (#k: parser_kind) (#t: Type) (p: parser k t) : Tot (r: bool{r ==> k.parser_kind_subkind == Some (ParserStrong)})
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong)
val is_strong (#k: parser_kind) (#t: Type) (p: parser k t) : Tot (r: bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) let is_strong (#k: parser_kind) (#t: Type) (p: parser k t) : Tot (r: bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) =
false
null
false
k.parser_kind_subkind = Some (ParserStrong)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "Prims.op_Equality", "FStar.Pervasives.Native.option", "LowParse.Spec.Base.parser_subkind", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.ParserStrong", "Prims.bool", "Prims.l_imp", "Prims.b2t", "Prims.eq2" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val is_strong (#k: parser_kind) (#t: Type) (p: parser k t) : Tot (r: bool{r ==> k.parser_kind_subkind == Some (ParserStrong)})
[]
LowParse.Spec.Base.is_strong
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> r: Prims.bool { r ==> Mkparser_kind'?.parser_kind_subkind k == FStar.Pervasives.Native.Some LowParse.Spec.Base.ParserStrong }
{ "end_col": 45, "end_line": 391, "start_col": 2, "start_line": 391 }
Prims.Pure
val tot_weaken (k1 #k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t
val tot_weaken (k1 #k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) let tot_weaken (k1 #k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) =
false
null
false
let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.tot_parser", "Prims.unit", "LowParse.Spec.Base.is_weaker_than_correct", "LowParse.Spec.Base.tot_bare_parser", "LowParse.Spec.Base.coerce_to_tot_bare_parser", "LowParse.Spec.Base.is_weaker_than", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val tot_weaken (k1 #k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.tot_weaken
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
k1: LowParse.Spec.Base.parser_kind -> p2: LowParse.Spec.Base.tot_parser k2 t -> Prims.Pure (LowParse.Spec.Base.tot_parser k1 t)
{ "end_col": 22, "end_line": 433, "start_col": 1, "start_line": 431 }
Prims.Pure
val tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f
val tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) =
false
null
false
f
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.tot_bare_parser", "LowParse.Spec.Base.tot_parser", "LowParse.Spec.Base.parser_kind_prop", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.tot_strengthen
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
k: LowParse.Spec.Base.parser_kind -> f: LowParse.Spec.Base.tot_bare_parser t -> Prims.Pure (LowParse.Spec.Base.tot_parser k t)
{ "end_col": 3, "end_line": 439, "start_col": 2, "start_line": 439 }
Prims.Tot
val bool_and (b1 b2: bool) : Tot (y: bool{y == (b1 && b2)})
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false
val bool_and (b1 b2: bool) : Tot (y: bool{y == (b1 && b2)}) let bool_and (b1 b2: bool) : Tot (y: bool{y == (b1 && b2)}) =
false
null
false
if b1 then b2 else false
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "Prims.bool", "Prims.eq2", "Prims.op_AmpAmp" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool)
false
false
LowParse.Spec.Base.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val bool_and (b1 b2: bool) : Tot (y: bool{y == (b1 && b2)})
[]
LowParse.Spec.Base.bool_and
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
b1: Prims.bool -> b2: Prims.bool -> y: Prims.bool{y == (b1 && b2)}
{ "end_col": 26, "end_line": 473, "start_col": 2, "start_line": 473 }
Prims.Tot
val default_parser_kind:(x: parser_kind { forall (t: Type) (p: bare_parser t). {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p })
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x
val default_parser_kind:(x: parser_kind { forall (t: Type) (p: bare_parser t). {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) let default_parser_kind:(x: parser_kind { forall (t: Type) (p: bare_parser t). {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) =
false
null
false
let aux (t: Type) (k: parser_kind) (p: bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None } in x
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "total" ]
[ "LowParse.Spec.Base.parser_kind'", "LowParse.Spec.Base.Mkparser_kind'", "FStar.Pervasives.Native.None", "Prims.nat", "LowParse.Spec.Base.parser_subkind", "LowParse.Spec.Base.parser_kind_metadata_some", "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.bare_parser", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.l_iff", "LowParse.Spec.Base.parser_kind_prop", "LowParse.Spec.Base.parser_kind_prop'", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil", "LowParse.Spec.Base.parser_kind_prop_equiv" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val default_parser_kind:(x: parser_kind { forall (t: Type) (p: bare_parser t). {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p })
[]
LowParse.Spec.Base.default_parser_kind
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
x: LowParse.Spec.Base.parser_kind { forall (t: Type) (p: LowParse.Spec.Base.bare_parser t). {:pattern LowParse.Spec.Base.parser_kind_prop x p} LowParse.Spec.Base.injective p ==> LowParse.Spec.Base.parser_kind_prop x p }
{ "end_col": 3, "end_line": 539, "start_col": 1, "start_line": 528 }
Prims.GTot
val serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x))
val serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 =
false
null
false
forall (x: t). {:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.bare_serializer", "Prims.l_Forall", "Prims.eq2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "FStar.Seq.Base.length", "LowParse.Bytes.byte" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0
[]
LowParse.Spec.Base.serializer_correct
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> f: LowParse.Spec.Base.bare_serializer t -> Prims.GTot Type0
{ "end_col": 87, "end_line": 659, "start_col": 2, "start_line": 659 }
Prims.Pure
val coerce (t2 #t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2)
val coerce (t2 #t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) let coerce (t2 #t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) =
false
null
false
(x <: t2)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "Prims.eq2", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val coerce (t2 #t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.coerce
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
t2: Type -> x: t1 -> Prims.Pure t2
{ "end_col": 11, "end_line": 586, "start_col": 2, "start_line": 586 }
Prims.Pure
val coerce' (t2 #t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2)
val coerce' (t2 #t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) let coerce' (t2 #t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) =
false
null
false
(x <: t2)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "Prims.eq2", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val coerce' (t2 #t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.coerce'
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
t2: Type -> x: t1 -> Prims.Pure t2
{ "end_col": 11, "end_line": 595, "start_col": 2, "start_line": 595 }
Prims.GTot
val serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len )
val serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 =
false
null
false
forall (s: bytes). {:pattern (parse p s)} Some? (parse p s) ==> (let Some (x, len) = parse p s in f x == Seq.slice s 0 len)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "sometrivial" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.bare_serializer", "Prims.l_Forall", "LowParse.Bytes.bytes", "Prims.l_imp", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.eq2", "FStar.Seq.Base.seq", "LowParse.Bytes.byte", "FStar.Seq.Base.slice", "Prims.logical", "FStar.Pervasives.Native.option" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0
[]
LowParse.Spec.Base.serializer_complete
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> f: LowParse.Spec.Base.bare_serializer t -> Prims.GTot Type0
{ "end_col": 3, "end_line": 684, "start_col": 2, "start_line": 680 }
FStar.Pervasives.Lemma
val serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x)
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2
val serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) =
false
null
true
let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.Spec.Base.serializer_correct_implies_complete", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "LowParse.Bytes.bytes", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x)
[]
LowParse.Spec.Base.serializer_unique
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: LowParse.Spec.Base.parser k t -> s1: LowParse.Spec.Base.serializer p -> s2: LowParse.Spec.Base.serializer p -> x: t -> FStar.Pervasives.Lemma (ensures s1 x == s2 x)
{ "end_col": 42, "end_line": 801, "start_col": 1, "start_line": 798 }
Prims.Pure
val serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes). parse p1 input == parse p2 input))) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2)
val serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes). parse p1 input == parse p2 input))) (ensures (fun _ -> True)) let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes). parse p1 input == parse p2 input))) (ensures (fun _ -> True)) =
false
null
false
serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.Spec.Base.bare_serializer", "Prims.unit", "LowParse.Spec.Base.serializer_correct_ext", "Prims.l_and", "Prims.eq2", "Prims.l_Forall", "LowParse.Bytes.bytes", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input)))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes). parse p1 input == parse p2 input))) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.serialize_ext
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p1: LowParse.Spec.Base.parser k1 t1 -> s1: LowParse.Spec.Base.serializer p1 -> p2: LowParse.Spec.Base.parser k2 t2 -> Prims.Pure (LowParse.Spec.Base.serializer p2)
{ "end_col": 28, "end_line": 741, "start_col": 2, "start_line": 740 }
FStar.Pervasives.Lemma
val serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires (is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s) ) (ensures (p1 x == p2 x))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else ()
val serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires (is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s) ) (ensures (p1 x == p2 x)) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires (is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s) ) (ensures (p1 x == p2 x)) =
false
null
true
if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.bare_serializer", "LowParse.Bytes.bytes", "FStar.Pervasives.Native.uu___is_Some", "FStar.Pervasives.Native.tuple2", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.serializer_parser_unique'", "Prims.bool", "Prims.unit", "Prims.l_and", "Prims.b2t", "LowParse.Spec.Base.is_strong", "LowParse.Spec.Base.serializer_correct", "Prims.squash", "Prims.eq2", "FStar.Pervasives.Native.option", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires (is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s) ) (ensures (p1 x == p2 x))
[]
LowParse.Spec.Base.serializer_parser_unique
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p1: LowParse.Spec.Base.parser k1 t -> p2: LowParse.Spec.Base.parser k2 t -> s: LowParse.Spec.Base.bare_serializer t -> x: LowParse.Bytes.bytes -> FStar.Pervasives.Lemma (requires LowParse.Spec.Base.is_strong p1 /\ LowParse.Spec.Base.is_strong p2 /\ LowParse.Spec.Base.serializer_correct p1 s /\ LowParse.Spec.Base.serializer_correct p2 s) (ensures p1 x == p2 x)
{ "end_col": 9, "end_line": 857, "start_col": 2, "start_line": 853 }
FStar.Pervasives.Lemma
val seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s)
val seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) =
false
null
true
assert ((seq_upd_seq s i s') `Seq.equal` s)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "LowParse.Spec.Base.seq_upd_seq", "Prims.unit", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Seq.Base.length", "Prims.eq2", "Prims.int", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s))
[]
LowParse.Spec.Base.seq_upd_seq_empty
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> s': FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires i <= FStar.Seq.Base.length s /\ FStar.Seq.Base.length s' == 0) (ensures LowParse.Spec.Base.seq_upd_seq s i s' == s)
{ "end_col": 43, "end_line": 1020, "start_col": 2, "start_line": 1020 }
FStar.Pervasives.Lemma
val seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s)
val seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) =
false
null
true
assert ((seq_upd_seq s lo (Seq.slice s lo hi)) `Seq.equal` s)
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "LowParse.Spec.Base.seq_upd_seq", "FStar.Seq.Base.slice", "Prims.unit", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Seq.Base.length", "Prims.squash", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s))
[]
LowParse.Spec.Base.seq_upd_seq_slice_idem
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> lo: Prims.nat -> hi: Prims.nat -> FStar.Pervasives.Lemma (requires lo <= hi /\ hi <= FStar.Seq.Base.length s) (ensures LowParse.Spec.Base.seq_upd_seq s lo (FStar.Seq.Base.slice s lo hi) == s)
{ "end_col": 61, "end_line": 1029, "start_col": 2, "start_line": 1029 }
FStar.Pervasives.Lemma
val parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p {k.parser_kind_subkind == Some ParserStrong}) (x: t) (n: nat) : Lemma (requires (n < Seq.length (serialize s x))) (ensures (parse p (Seq.slice (serialize s x) 0 n) == None))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0
val parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p {k.parser_kind_subkind == Some ParserStrong}) (x: t) (n: nat) : Lemma (requires (n < Seq.length (serialize s x))) (ensures (parse p (Seq.slice (serialize s x) 0 n) == None)) let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p {k.parser_kind_subkind == Some ParserStrong}) (x: t) (n: nat) : Lemma (requires (n < Seq.length (serialize s x))) (ensures (parse p (Seq.slice (serialize s x) 0 n) == None)) =
false
null
true
let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "Prims.eq2", "FStar.Pervasives.Native.option", "LowParse.Spec.Base.parser_subkind", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.ParserStrong", "Prims.nat", "LowParse.Spec.Base.parse", "LowParse.Spec.Base.consumed_length", "LowParse.Spec.Base.parse_strong_prefix", "Prims.unit", "FStar.Seq.Base.seq", "LowParse.Bytes.byte", "FStar.Seq.Base.slice", "LowParse.Bytes.bytes", "LowParse.Spec.Base.serialize", "Prims.b2t", "Prims.op_LessThan", "FStar.Seq.Base.length", "Prims.squash", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.None", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p {k.parser_kind_subkind == Some ParserStrong}) (x: t) (n: nat) : Lemma (requires (n < Seq.length (serialize s x))) (ensures (parse p (Seq.slice (serialize s x) 0 n) == None))
[]
LowParse.Spec.Base.parse_truncate
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p { Mkparser_kind'?.parser_kind_subkind k == FStar.Pervasives.Native.Some LowParse.Spec.Base.ParserStrong } -> x: t -> n: Prims.nat -> FStar.Pervasives.Lemma (requires n < FStar.Seq.Base.length (LowParse.Spec.Base.serialize s x)) (ensures LowParse.Spec.Base.parse p (FStar.Seq.Base.slice (LowParse.Spec.Base.serialize s x) 0 n) == FStar.Pervasives.Native.None)
{ "end_col": 33, "end_line": 920, "start_col": 1, "start_line": 915 }
Prims.Pure
val seq_upd_bw_seq (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Pure (s_: Seq.seq t {Seq.length s_ == Seq.length s}) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_bw_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = seq_upd_seq s (Seq.length s - i - Seq.length s') s'
val seq_upd_bw_seq (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Pure (s_: Seq.seq t {Seq.length s_ == Seq.length s}) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) let seq_upd_bw_seq (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Pure (s_: Seq.seq t {Seq.length s_ == Seq.length s}) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) =
false
null
false
seq_upd_seq s (Seq.length s - i - Seq.length s') s'
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "FStar.Seq.Base.seq", "Prims.nat", "LowParse.Spec.Base.seq_upd_seq", "Prims.op_Subtraction", "FStar.Seq.Base.length", "Prims.eq2", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "Prims.l_True" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))) let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = assert (seq_upd_seq s (Seq.length s - Seq.length s') s' `Seq.equal` Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s') let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) `Seq.equal` seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3) let seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1: nat) (hi: nat) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) `Seq.equal` seq_upd_seq s1 (i1 + i2) s3) let seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires ( i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1) )) (ensures ( seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1 )) = assert (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 `Seq.equal` seq_upd_seq (seq_upd_seq s i2 s2) i1 s1) let seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s)) (ensures (Seq.upd s i x == seq_upd_seq s i (Seq.create 1 x))) = assert (Seq.upd s i x `Seq.equal` seq_upd_seq s i (Seq.create 1 x)) let seq_append_seq_upd_seq_l (#t: Type) (s: Seq.seq t) (i': nat) (s' : Seq.seq t) (sl : Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures ( Seq.length sl + i' <= Seq.length (sl `Seq.append` s) /\ sl `Seq.append` seq_upd_seq s i' s' == seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s' )) = assert (sl `Seq.append` seq_upd_seq s i' s' `Seq.equal` seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s') let seq_append_seq_upd_seq_r (#t: Type) (s: Seq.seq t) (i': nat) (s' : Seq.seq t) (sr : Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures ( i' <= Seq.length (s `Seq.append` sr) /\ seq_upd_seq s i' s' `Seq.append` sr == seq_upd_seq (s `Seq.append` sr) i' s' )) = assert ((seq_upd_seq s i' s' `Seq.append` sr) `Seq.equal` seq_upd_seq (s `Seq.append` sr) i' s') let seq_upd_bw_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_bw_seq (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Pure (s_: Seq.seq t {Seq.length s_ == Seq.length s}) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True))
[]
LowParse.Spec.Base.seq_upd_bw_seq
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> s': FStar.Seq.Base.seq t -> Prims.Pure (s_: FStar.Seq.Base.seq t {FStar.Seq.Base.length s_ == FStar.Seq.Base.length s})
{ "end_col": 53, "end_line": 1141, "start_col": 2, "start_line": 1141 }
FStar.Pervasives.Lemma
val seq_upd_seq_left (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))
val seq_upd_seq_left (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) let seq_upd_seq_left (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) =
false
null
true
assert ((seq_upd_seq s 0 s') `Seq.equal` (Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims._assert", "FStar.Seq.Base.equal", "LowParse.Spec.Base.seq_upd_seq", "FStar.Seq.Base.append", "FStar.Seq.Base.slice", "FStar.Seq.Base.length", "Prims.unit", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.squash", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_left (#t: Type) (s s': Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))))
[]
LowParse.Spec.Base.seq_upd_seq_left
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> s': FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires FStar.Seq.Base.length s' <= FStar.Seq.Base.length s) (ensures LowParse.Spec.Base.seq_upd_seq s 0 s' == FStar.Seq.Base.append s' (FStar.Seq.Base.slice s (FStar.Seq.Base.length s') (FStar.Seq.Base.length s)))
{ "end_col": 100, "end_line": 1038, "start_col": 2, "start_line": 1038 }
FStar.Pervasives.Lemma
val seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s)))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s))
val seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) =
false
null
true
assert ((Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s)) `Seq.equal` (Seq.slice s (i + Seq.length s') (Seq.length s)))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.slice", "LowParse.Spec.Base.seq_upd_seq", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.unit", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.squash", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s': Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s)))
[]
LowParse.Spec.Base.seq_upd_seq_slice_right
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> s': FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires i + FStar.Seq.Base.length s' <= FStar.Seq.Base.length s) (ensures FStar.Seq.Base.slice (LowParse.Spec.Base.seq_upd_seq s i s') (i + FStar.Seq.Base.length s') (FStar.Seq.Base.length s) == FStar.Seq.Base.slice s (i + FStar.Seq.Base.length s') (FStar.Seq.Base.length s))
{ "end_col": 135, "end_line": 998, "start_col": 2, "start_line": 998 }
FStar.Pervasives.Lemma
val serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires (k.parser_kind_subkind == Some ParserStrong /\ (serialize s x1) `Seq.append` q1 == (serialize s x2) `Seq.append` q2)) (ensures (x1 == x2 /\ q1 == q2))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2
val serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires (k.parser_kind_subkind == Some ParserStrong /\ (serialize s x1) `Seq.append` q1 == (serialize s x2) `Seq.append` q2)) (ensures (x1 == x2 /\ q1 == q2)) let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires (k.parser_kind_subkind == Some ParserStrong /\ (serialize s x1) `Seq.append` q1 == (serialize s x2) `Seq.append` q2)) (ensures (x1 == x2 /\ q1 == q2)) =
false
null
true
parse_strong_prefix p (serialize s x1) ((serialize s x1) `Seq.append` q1); parse_strong_prefix p (serialize s x2) ((serialize s x2) `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "LowParse.Spec.Base.parser_kind", "LowParse.Spec.Base.parser", "LowParse.Spec.Base.serializer", "LowParse.Bytes.bytes", "FStar.Seq.Properties.lemma_append_inj", "LowParse.Bytes.byte", "LowParse.Spec.Base.serialize", "Prims.unit", "LowParse.Spec.Base.parse_strong_prefix", "FStar.Seq.Base.append", "Prims.l_and", "Prims.eq2", "FStar.Pervasives.Native.option", "LowParse.Spec.Base.parser_subkind", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.ParserStrong", "FStar.Seq.Base.seq", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 ))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires (k.parser_kind_subkind == Some ParserStrong /\ (serialize s x1) `Seq.append` q1 == (serialize s x2) `Seq.append` q2)) (ensures (x1 == x2 /\ q1 == q2))
[]
LowParse.Spec.Base.serialize_strong_prefix
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: LowParse.Spec.Base.serializer p -> x1: t -> x2: t -> q1: LowParse.Bytes.bytes -> q2: LowParse.Bytes.bytes -> FStar.Pervasives.Lemma (requires Mkparser_kind'?.parser_kind_subkind k == FStar.Pervasives.Native.Some LowParse.Spec.Base.ParserStrong /\ FStar.Seq.Base.append (LowParse.Spec.Base.serialize s x1) q1 == FStar.Seq.Base.append (LowParse.Spec.Base.serialize s x2) q2) (ensures x1 == x2 /\ q1 == q2)
{ "end_col": 62, "end_line": 899, "start_col": 2, "start_line": 897 }
FStar.Pervasives.Lemma
val seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s)) (ensures (Seq.upd s i x == seq_upd_seq s i (Seq.create 1 x)))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s)) (ensures (Seq.upd s i x == seq_upd_seq s i (Seq.create 1 x))) = assert (Seq.upd s i x `Seq.equal` seq_upd_seq s i (Seq.create 1 x))
val seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s)) (ensures (Seq.upd s i x == seq_upd_seq s i (Seq.create 1 x))) let seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s)) (ensures (Seq.upd s i x == seq_upd_seq s i (Seq.create 1 x))) =
false
null
true
assert ((Seq.upd s i x) `Seq.equal` (seq_upd_seq s i (Seq.create 1 x)))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.upd", "LowParse.Spec.Base.seq_upd_seq", "FStar.Seq.Base.create", "Prims.unit", "Prims.b2t", "Prims.op_LessThan", "FStar.Seq.Base.length", "Prims.squash", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))) let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = assert (seq_upd_seq s (Seq.length s - Seq.length s') s' `Seq.equal` Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s') let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) `Seq.equal` seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3) let seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1: nat) (hi: nat) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) `Seq.equal` seq_upd_seq s1 (i1 + i2) s3) let seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires ( i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1) )) (ensures ( seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1 )) = assert (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 `Seq.equal` seq_upd_seq (seq_upd_seq s i2 s2) i1 s1) let seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s))
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s)) (ensures (Seq.upd s i x == seq_upd_seq s i (Seq.create 1 x)))
[]
LowParse.Spec.Base.seq_upd_seq_seq_upd
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i: Prims.nat -> x: t -> FStar.Pervasives.Lemma (requires i < FStar.Seq.Base.length s) (ensures FStar.Seq.Base.upd s i x == LowParse.Spec.Base.seq_upd_seq s i (FStar.Seq.Base.create 1 x))
{ "end_col": 69, "end_line": 1103, "start_col": 2, "start_line": 1103 }
Prims.Pure
val glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl. L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k'. (Cons? l /\ (forall kl. L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k)))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k'
val glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl. L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k'. (Cons? l /\ (forall kl. L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k))) let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl. L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k'. (Cons? l /\ (forall kl. L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k))) =
false
null
false
match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k'
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "Prims.eqtype", "LowParse.Spec.Base.parser_kind", "Prims.list", "LowParse.Spec.Base.default_parser_kind", "LowParse.Spec.Base.glb", "LowParse.Spec.Base.glb_list_of", "Prims.l_True", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp", "Prims.b2t", "FStar.List.Tot.Base.mem", "LowParse.Spec.Base.is_weaker_than", "Prims.uu___is_Cons" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k)
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl. L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k'. (Cons? l /\ (forall kl. L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k)))
[ "recursion" ]
LowParse.Spec.Base.glb_list_of
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
f: (_: t -> LowParse.Spec.Base.parser_kind) -> l: Prims.list t -> Prims.Pure LowParse.Spec.Base.parser_kind
{ "end_col": 17, "end_line": 561, "start_col": 2, "start_line": 556 }
FStar.Pervasives.Lemma
val seq_append_seq_upd_seq_r (#t: Type) (s: Seq.seq t) (i': nat) (s' sr: Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures (i' <= Seq.length (s `Seq.append` sr) /\ (seq_upd_seq s i' s') `Seq.append` sr == seq_upd_seq (s `Seq.append` sr) i' s'))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_append_seq_upd_seq_r (#t: Type) (s: Seq.seq t) (i': nat) (s' : Seq.seq t) (sr : Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures ( i' <= Seq.length (s `Seq.append` sr) /\ seq_upd_seq s i' s' `Seq.append` sr == seq_upd_seq (s `Seq.append` sr) i' s' )) = assert ((seq_upd_seq s i' s' `Seq.append` sr) `Seq.equal` seq_upd_seq (s `Seq.append` sr) i' s')
val seq_append_seq_upd_seq_r (#t: Type) (s: Seq.seq t) (i': nat) (s' sr: Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures (i' <= Seq.length (s `Seq.append` sr) /\ (seq_upd_seq s i' s') `Seq.append` sr == seq_upd_seq (s `Seq.append` sr) i' s')) let seq_append_seq_upd_seq_r (#t: Type) (s: Seq.seq t) (i': nat) (s' sr: Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures (i' <= Seq.length (s `Seq.append` sr) /\ (seq_upd_seq s i' s') `Seq.append` sr == seq_upd_seq (s `Seq.append` sr) i' s')) =
false
null
true
assert (((seq_upd_seq s i' s') `Seq.append` sr) `Seq.equal` (seq_upd_seq (s `Seq.append` sr) i' s'))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.append", "LowParse.Spec.Base.seq_upd_seq", "Prims.unit", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.squash", "Prims.l_and", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))) let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = assert (seq_upd_seq s (Seq.length s - Seq.length s') s' `Seq.equal` Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s') let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) `Seq.equal` seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3) let seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1: nat) (hi: nat) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) `Seq.equal` seq_upd_seq s1 (i1 + i2) s3) let seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires ( i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1) )) (ensures ( seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1 )) = assert (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 `Seq.equal` seq_upd_seq (seq_upd_seq s i2 s2) i1 s1) let seq_upd_seq_seq_upd (#t: Type) (s: Seq.seq t) (i: nat) (x: t) : Lemma (requires (i < Seq.length s)) (ensures (Seq.upd s i x == seq_upd_seq s i (Seq.create 1 x))) = assert (Seq.upd s i x `Seq.equal` seq_upd_seq s i (Seq.create 1 x)) let seq_append_seq_upd_seq_l (#t: Type) (s: Seq.seq t) (i': nat) (s' : Seq.seq t) (sl : Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures ( Seq.length sl + i' <= Seq.length (sl `Seq.append` s) /\ sl `Seq.append` seq_upd_seq s i' s' == seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s' )) = assert (sl `Seq.append` seq_upd_seq s i' s' `Seq.equal` seq_upd_seq (sl `Seq.append` s) (Seq.length sl + i') s') let seq_append_seq_upd_seq_r (#t: Type) (s: Seq.seq t) (i': nat) (s' : Seq.seq t) (sr : Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures ( i' <= Seq.length (s `Seq.append` sr) /\ seq_upd_seq s i' s' `Seq.append` sr == seq_upd_seq (s `Seq.append` sr) i' s'
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_append_seq_upd_seq_r (#t: Type) (s: Seq.seq t) (i': nat) (s' sr: Seq.seq t) : Lemma (requires (i' + Seq.length s' <= Seq.length s)) (ensures (i' <= Seq.length (s `Seq.append` sr) /\ (seq_upd_seq s i' s') `Seq.append` sr == seq_upd_seq (s `Seq.append` sr) i' s'))
[]
LowParse.Spec.Base.seq_append_seq_upd_seq_r
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i': Prims.nat -> s': FStar.Seq.Base.seq t -> sr: FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires i' + FStar.Seq.Base.length s' <= FStar.Seq.Base.length s) (ensures i' <= FStar.Seq.Base.length (FStar.Seq.Base.append s sr) /\ FStar.Seq.Base.append (LowParse.Spec.Base.seq_upd_seq s i' s') sr == LowParse.Spec.Base.seq_upd_seq (FStar.Seq.Base.append s sr) i' s')
{ "end_col": 98, "end_line": 1131, "start_col": 2, "start_line": 1131 }
FStar.Pervasives.Lemma
val seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1 hi i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1: nat) (hi: nat) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) `Seq.equal` seq_upd_seq s1 (i1 + i2) s3)
val seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1 hi i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3)) let seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1 hi i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3)) =
false
null
true
assert ((seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3)) `Seq.equal` (seq_upd_seq s1 (i1 + i2) s3))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "LowParse.Spec.Base.seq_upd_seq", "FStar.Seq.Base.slice", "Prims.op_Addition", "Prims.unit", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Seq.Base.length", "Prims.squash", "Prims.eq2", "Prims.l_or", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))) let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = assert (seq_upd_seq s (Seq.length s - Seq.length s') s' `Seq.equal` Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s') let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) `Seq.equal` seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3) let seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1: nat) (hi: nat) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1 hi i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3))
[]
LowParse.Spec.Base.seq_upd_seq_seq_upd_seq_slice
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s1: FStar.Seq.Base.seq t -> i1: Prims.nat -> hi: Prims.nat -> i2: Prims.nat -> s3: FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires i1 <= hi /\ hi <= FStar.Seq.Base.length s1 /\ i1 + i2 + FStar.Seq.Base.length s3 <= hi) (ensures LowParse.Spec.Base.seq_upd_seq s1 i1 (LowParse.Spec.Base.seq_upd_seq (FStar.Seq.Base.slice s1 i1 hi) i2 s3) == LowParse.Spec.Base.seq_upd_seq s1 (i1 + i2) s3)
{ "end_col": 109, "end_line": 1075, "start_col": 2, "start_line": 1075 }
FStar.Pervasives.Lemma
val seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 ))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) `Seq.equal` seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3)
val seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) =
false
null
true
assert ((seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3)) `Seq.equal` (seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "LowParse.Spec.Base.seq_upd_seq", "Prims.op_Addition", "Prims.unit", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Seq.Base.length", "Prims.squash", "Prims.eq2", "Prims.l_or", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))) let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = assert (seq_upd_seq s (Seq.length s - Seq.length s') s' `Seq.equal` Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s') let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 ))
[]
LowParse.Spec.Base.seq_upd_seq_right_to_left
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s1: FStar.Seq.Base.seq t -> i1: Prims.nat -> s2: FStar.Seq.Base.seq t -> i2: Prims.nat -> s3: FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires i1 + FStar.Seq.Base.length s2 <= FStar.Seq.Base.length s1 /\ i2 + FStar.Seq.Base.length s3 <= FStar.Seq.Base.length s2) (ensures LowParse.Spec.Base.seq_upd_seq s1 i1 (LowParse.Spec.Base.seq_upd_seq s2 i2 s3) == LowParse.Spec.Base.seq_upd_seq (LowParse.Spec.Base.seq_upd_seq s1 i1 s2) (i1 + i2) s3)
{ "end_col": 111, "end_line": 1061, "start_col": 2, "start_line": 1061 }
FStar.Pervasives.Lemma
val seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires (i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1))) (ensures (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1))
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires ( i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1) )) (ensures ( seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1 )) = assert (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 `Seq.equal` seq_upd_seq (seq_upd_seq s i2 s2) i1 s1)
val seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires (i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1))) (ensures (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1)) let seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires (i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1))) (ensures (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1)) =
false
null
true
assert ((seq_upd_seq (seq_upd_seq s i1 s1) i2 s2) `Seq.equal` (seq_upd_seq (seq_upd_seq s i2 s2) i1 s1))
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[ "lemma" ]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims._assert", "FStar.Seq.Base.equal", "LowParse.Spec.Base.seq_upd_seq", "Prims.unit", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Seq.Base.length", "Prims.l_or", "Prims.squash", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None } #pop-options #push-options "--warn_error -271" let default_parser_kind : (x: parser_kind { forall (t: Type) (p: bare_parser t) . {:pattern (parser_kind_prop x p)} injective p ==> parser_kind_prop x p }) = let aux (t:Type) (k:parser_kind) (p:bare_parser t) : Lemma (parser_kind_prop k p <==> parser_kind_prop' k p) [SMTPat ()] = parser_kind_prop_equiv k p in let x = { parser_kind_low = 0; parser_kind_high = None; parser_kind_metadata = None; parser_kind_subkind = None; } in x #pop-options // #set-options "--max_fuel 8 --max_ifuel 8" module L = FStar.List.Tot let rec glb_list_of (#t: eqtype) (f: (t -> Tot parser_kind)) (l: list t) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . L.mem kl l ==> k `is_weaker_than` (f kl)) /\ (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` (f kl))) ==> k' `is_weaker_than` k) )) = match l with | [] -> default_parser_kind | [k] -> f k | k1 :: q -> let k' = glb_list_of f q in glb (f k1) k' #reset-options let glb_list (l: list parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> (forall kl . {:pattern (L.mem kl l)} L.mem kl l ==> k `is_weaker_than` kl) // (forall k' . (Cons? l /\ (forall kl . L.mem kl l ==> k' `is_weaker_than` kl)) ==> k' `is_weaker_than` k) )) = glb_list_of id l (* Coercions *) unfold inline_for_extraction let coerce (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) let coerce' (t2: Type) (#t1: Type) (x: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun _ -> True)) = (x <: t2) unfold let coerce_parser (t2: Type) (#k: parser_kind) (#t1: Type) (p: parser k t1) : Pure (parser k t2) (requires (t2 == t1)) (ensures (fun _ -> True)) = p val parse_injective (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( injective_precond p input1 input2 )) (ensures ( injective_postcond p input1 input2 )) val parse_strong_prefix (#k: parser_kind) (#t: Type) (p: parser k t) (input1: bytes) (input2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ Seq.slice input1 0 consumed `Seq.equal` Seq.slice input2 0 consumed | _ -> False ))) (ensures ( match parse p input1 with | Some (x, consumed) -> consumed <= Seq.length input2 /\ parse p input2 == Some (x, consumed) | _ -> False )) (* Pure serializers *) inline_for_extraction let bare_serializer (t: Type) : Tot Type = t -> GTot bytes let serializer_correct (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (x: t) .{:pattern (parse p (f x))} parse p (f x) == Some (x, Seq.length (f x)) let serializer_correct_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (f: bare_serializer t1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Lemma (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (serializer_correct p1 f <==> serializer_correct p2 f)) = () let serializer_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : GTot Type0 = forall (s: bytes) . {:pattern (parse p s)} Some? (parse p s) ==> ( let (Some (x, len)) = parse p s in f x == Seq.slice s 0 len ) val serializer_correct_implies_complete (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) : Lemma (requires (serializer_correct p f)) (ensures (serializer_complete p f)) [@unifier_hint_injective] inline_for_extraction let serializer (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = (f: bare_serializer t { serializer_correct p f } ) let mk_serializer (#k: parser_kind) (#t: Type) (p: parser k t) (f: bare_serializer t) (prf: ( (x: t) -> Lemma (parse p (f x) == Some (x, Seq.length (f x))) )) : Tot (serializer p) = Classical.forall_intro prf; f unfold let coerce_serializer (t2: Type) (#k: parser_kind) (#t1: Type) (#p: parser k t1) (s: serializer p) (u: unit { t2 == t1 } ) : Tot (serializer (coerce_parser t2 p)) = s let serialize_ext (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ (forall (input: bytes) . parse p1 input == parse p2 input))) (ensures (fun _ -> True)) = serializer_correct_ext p1 s1 p2; (s1 <: bare_serializer t2) let serialize_ext' (#k1: parser_kind) (#t1: Type) (p1: parser k1 t1) (s1: serializer p1) (#k2: parser_kind) (#t2: Type) (p2: parser k2 t2) : Pure (serializer p2) (requires (t1 == t2 /\ k1 == k2 /\ p1 == p2)) (ensures (fun _ -> True)) = serialize_ext p1 s1 p2 let serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : GTot bytes = s x let parse_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (parse p (serialize s x) == Some (x, Seq.length (serialize s x))) = () let parsed_data_is_serialize (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: bytes) : Lemma (requires (Some? (parse p x))) (ensures ( let Some (y, consumed) = parse p x in (serialize s y `Seq.append` Seq.slice x consumed (Seq.length x)) `Seq.equal` x )) = let Some (y, consumed) = parse p x in parse_injective p (serialize s y) x let serializer_unique (#k: parser_kind) (#t: Type) (p: parser k t) (s1 s2: serializer p) (x: t) : Lemma (s1 x == s2 x) = (* need these because of patterns *) let _ = parse p (s1 x) in let _ = parse p (s2 x) in serializer_correct_implies_complete p s2 let serializer_injective (#k: parser_kind) (#t: Type) (p: parser k t) (s: serializer p) (x1 x2: t) : Lemma (requires (s x1 == s x2)) (ensures (x1 == x2)) = (* patterns, again *) assert (parse p (s x1) == parse p (s x2)) val serializer_parser_unique' (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s /\ Some? (parse p1 x) )) (ensures ( parse p1 x == parse p2 x )) let serializer_parser_unique (#k1: parser_kind) (#t: Type) (p1: parser k1 t) (#k2: parser_kind) (p2: parser k2 t) (s: bare_serializer t) (x: bytes) : Lemma (requires ( is_strong p1 /\ is_strong p2 /\ serializer_correct p1 s /\ serializer_correct p2 s )) (ensures ( p1 x == p2 x )) = if Some? (p1 x) then serializer_parser_unique' p1 p2 s x else if Some? (p2 x) then serializer_parser_unique' p2 p1 s x else () val serialize_length (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (let x = Seq.length (serialize s x) in k.parser_kind_low <= x /\ ( match k.parser_kind_high with | None -> True | Some y -> x <= y )) [SMTPat (Seq.length (serialize s x))] val serialize_not_fail (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x: t) : Lemma (k.parser_kind_metadata <> Some ParserKindMetadataFail) [SMTPat (serialize s x)] let serialize_strong_prefix (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p) (x1 x2: t) (q1 q2: bytes) : Lemma (requires ( k.parser_kind_subkind == Some ParserStrong /\ serialize s x1 `Seq.append` q1 == serialize s x2 `Seq.append` q2 )) (ensures (x1 == x2 /\ q1 == q2)) = parse_strong_prefix p (serialize s x1) (serialize s x1 `Seq.append` q1); parse_strong_prefix p (serialize s x2) (serialize s x2 `Seq.append` q2); Seq.lemma_append_inj (serialize s x1) q1 (serialize s x2) q2 let parse_truncate (#k: parser_kind) (#t: Type) (#p: parser k t) (s: serializer p { k.parser_kind_subkind == Some ParserStrong }) (x: t) (n: nat) : Lemma (requires ( n < Seq.length (serialize s x) )) (ensures ( parse p (Seq.slice (serialize s x) 0 n) == None )) = let sq0 = serialize s x in let sq1 = Seq.slice sq0 0 n in match parse p sq1 with | None -> () | Some (x', consumed) -> parse_strong_prefix p sq1 sq0 let seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Pure (s_ : Seq.seq t { Seq.length s_ == Seq.length s } ) (requires (i + Seq.length s' <= Seq.length s)) (ensures (fun _ -> True)) = Seq.append (Seq.slice s 0 i) (Seq.append s' (Seq.slice s (i + Seq.length s') (Seq.length s))) let index_seq_upd_seq (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j < Seq.length s)) (ensures ( Seq.index (seq_upd_seq s i s') j == (if i <= j && j < i + Seq.length s' then Seq.index s' (j - i) else Seq.index s j))) = () let seq_upd_seq_slice (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') == s')) = assert (Seq.slice (seq_upd_seq s i s') i (i + Seq.length s') `Seq.equal` s') let seq_upd_seq_slice' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ i <= j1 /\ j1 <= j2 /\ j2 <= i + Seq.length s')) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s' (j1 - i) (j2 - i))) = seq_upd_seq_slice s i s'; Seq.slice_slice (seq_upd_seq s i s') i (i + Seq.length s') (j1 - i) (j2 - i) let seq_upd_seq_slice_left (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') 0 i == Seq.slice s 0 i)) = assert (Seq.slice (seq_upd_seq s i s') 0 i `Seq.equal` Seq.slice s 0 i) let seq_upd_seq_slice_left' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= Seq.length s /\ j1 <= j2 /\ j2 <= i)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_left s i s'; Seq.slice_slice (seq_upd_seq s i s') 0 i j1 j2 let seq_upd_seq_slice_right (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i + Seq.length s' <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) == Seq.slice s (i + Seq.length s') (Seq.length s))) = assert (Seq.slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) `Seq.equal` Seq.slice s (i + Seq.length s') (Seq.length s)) let seq_upd_seq_slice_right' (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) (j1 j2: nat) : Lemma (requires (i + Seq.length s' <= j1 /\ j1 <= j2 /\ j2 <= Seq.length s)) (ensures (Seq.slice (seq_upd_seq s i s') j1 j2 == Seq.slice s j1 j2)) = seq_upd_seq_slice_right s i s'; Seq.slice_slice (seq_upd_seq s i s') (i + Seq.length s') (Seq.length s) (j1 - (i + Seq.length s')) (j2 - (i + Seq.length s')) let seq_upd_seq_empty (#t: Type) (s: Seq.seq t) (i: nat) (s' : Seq.seq t) : Lemma (requires (i <= Seq.length s /\ Seq.length s' == 0)) (ensures (seq_upd_seq s i s' == s)) = assert (seq_upd_seq s i s' `Seq.equal` s) let seq_upd_seq_slice_idem (#t: Type) (s: Seq.seq t) (lo hi: nat) : Lemma (requires (lo <= hi /\ hi <= Seq.length s)) (ensures (seq_upd_seq s lo (Seq.slice s lo hi) == s)) = assert (seq_upd_seq s lo (Seq.slice s lo hi) `Seq.equal` s) let seq_upd_seq_left (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s 0 s' == Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s)))) = assert (seq_upd_seq s 0 s' `Seq.equal` Seq.append s' (Seq.slice s (Seq.length s') (Seq.length s))) let seq_upd_seq_right (#t: Type) (s: Seq.seq t) (s' : Seq.seq t) : Lemma (requires (Seq.length s' <= Seq.length s)) (ensures (seq_upd_seq s (Seq.length s - Seq.length s') s' == Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s')) = assert (seq_upd_seq s (Seq.length s - Seq.length s') s' `Seq.equal` Seq.append (Seq.slice s 0 (Seq.length s - Seq.length s')) s') let seq_upd_seq_right_to_left (#t: Type) (s1: Seq.seq t) (i1: nat) (s2: Seq.seq t) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 + Seq.length s2 <= Seq.length s1 /\ i2 + Seq.length s3 <= Seq.length s2)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) == seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq s2 i2 s3) `Seq.equal` seq_upd_seq (seq_upd_seq s1 i1 s2) (i1 + i2) s3) let seq_upd_seq_seq_upd_seq_slice (#t: Type) (s1: Seq.seq t) (i1: nat) (hi: nat) (i2: nat) (s3: Seq.seq t) : Lemma (requires (i1 <= hi /\ hi <= Seq.length s1 /\ i1 + i2 + Seq.length s3 <= hi)) (ensures ( seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) == seq_upd_seq s1 (i1 + i2) s3 )) = assert (seq_upd_seq s1 i1 (seq_upd_seq (Seq.slice s1 i1 hi) i2 s3) `Seq.equal` seq_upd_seq s1 (i1 + i2) s3) let seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires ( i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1) )) (ensures ( seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1
false
false
LowParse.Spec.Base.fsti
{ "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" }
null
val seq_upd_seq_disj_comm (#t: Type) (s: Seq.seq t) (i1: nat) (s1: Seq.seq t) (i2: nat) (s2: Seq.seq t) : Lemma (requires (i1 + Seq.length s1 <= Seq.length s /\ i2 + Seq.length s2 <= Seq.length s /\ (i1 + Seq.length s1 <= i2 \/ i2 + Seq.length s2 <= i1))) (ensures (seq_upd_seq (seq_upd_seq s i1 s1) i2 s2 == seq_upd_seq (seq_upd_seq s i2 s2) i1 s1))
[]
LowParse.Spec.Base.seq_upd_seq_disj_comm
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
s: FStar.Seq.Base.seq t -> i1: Prims.nat -> s1: FStar.Seq.Base.seq t -> i2: Prims.nat -> s2: FStar.Seq.Base.seq t -> FStar.Pervasives.Lemma (requires i1 + FStar.Seq.Base.length s1 <= FStar.Seq.Base.length s /\ i2 + FStar.Seq.Base.length s2 <= FStar.Seq.Base.length s /\ (i1 + FStar.Seq.Base.length s1 <= i2 \/ i2 + FStar.Seq.Base.length s2 <= i1)) (ensures LowParse.Spec.Base.seq_upd_seq (LowParse.Spec.Base.seq_upd_seq s i1 s1) i2 s2 == LowParse.Spec.Base.seq_upd_seq (LowParse.Spec.Base.seq_upd_seq s i2 s2) i1 s1)
{ "end_col": 102, "end_line": 1093, "start_col": 2, "start_line": 1093 }
Prims.Pure
val glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2))
[ { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": false, "full_module": "LowParse.Norm", "short_module": null }, { "abbrev": false, "full_module": "LowParse.Bytes", "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 } ]
false
let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k) )) = match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None); } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None; } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = ( if is_some k1.parser_kind_high `bool_and` is_some k2.parser_kind_high then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None ); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None }
val glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2)) let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2)) =
false
null
false
match k1.parser_kind_metadata, k2.parser_kind_metadata with | _, Some ParserKindMetadataFail -> { parser_kind_low = k1.parser_kind_low; parser_kind_high = k1.parser_kind_high; parser_kind_subkind = k1.parser_kind_subkind; parser_kind_metadata = (match k1.parser_kind_metadata with | Some ParserKindMetadataFail -> Some ParserKindMetadataFail | _ -> None) } | Some ParserKindMetadataFail, _ -> { parser_kind_low = k2.parser_kind_low; parser_kind_high = k2.parser_kind_high; parser_kind_subkind = k2.parser_kind_subkind; parser_kind_metadata = None } | _ -> { parser_kind_low = (if k1.parser_kind_low < k2.parser_kind_low then k1.parser_kind_low else k2.parser_kind_low); parser_kind_high = (if (is_some k1.parser_kind_high) `bool_and` (is_some k2.parser_kind_high) then if some_v k2.parser_kind_high < some_v k1.parser_kind_high then k1.parser_kind_high else k2.parser_kind_high else None); parser_kind_metadata = if k1.parser_kind_metadata = k2.parser_kind_metadata then k1.parser_kind_metadata else None; parser_kind_subkind = if k1.parser_kind_subkind = k2.parser_kind_subkind then k1.parser_kind_subkind else None }
{ "checked_file": "LowParse.Spec.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "LowParse.Norm.fst.checked", "LowParse.Bytes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "LowParse.Spec.Base.fsti" }
[]
[ "LowParse.Spec.Base.parser_kind", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.option", "LowParse.Spec.Base.parser_kind_metadata_some", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata", "LowParse.Spec.Base.Mkparser_kind'", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high", "LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind", "FStar.Pervasives.Native.Some", "LowParse.Spec.Base.ParserKindMetadataFail", "FStar.Pervasives.Native.None", "LowParse.Spec.Base.parser_kind_metadata_t", "FStar.Pervasives.Native.tuple2", "Prims.op_LessThan", "Prims.bool", "Prims.nat", "LowParse.Spec.Base.bool_and", "LowParse.Spec.Base.is_some", "LowParse.Spec.Base.some_v", "Prims.op_Equality", "LowParse.Spec.Base.parser_subkind", "Prims.l_True", "Prims.l_and", "LowParse.Spec.Base.is_weaker_than" ]
[]
module LowParse.Spec.Base include LowParse.Bytes include LowParse.Norm module Seq = FStar.Seq module U8 = FStar.UInt8 module U32 = FStar.UInt32 /// parse a value of type t /// /// - the parser can fail (currently reporting an uninformative [None]) /// - it returns the parsed value as well as the number of bytes read /// (this is intended to be the number of bytes to advance the input pointer) /// /// note that the type now forbids lookahead; the parser cannot depend on /// values beyond the returned offset /// /// these parsers are used as specifications, and thus use unrepresentable types /// such as byte sequences and natural numbers and are always pure [@"substitute"] inline_for_extraction let consumed_length (b: bytes) : Tot Type = (n: nat { n <= Seq.length b } ) inline_for_extraction let bare_parser (t:Type) : Tot Type = (b: bytes) -> GTot (option (t * consumed_length b)) let parse (#t: Type) (p: bare_parser t) (input: bytes) : GTot (option (t * consumed_length input)) = p input (** Injectivity of parsing *) let injective_precond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in v1 == v2 ) let injective_precond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_precond p2 b1 b2 <==> injective_precond p1 b1 b2 )) = () let injective_postcond (#t: Type) (p: bare_parser t) (b1 b2: bytes) : GTot Type0 = Some? (parse p b1) /\ Some? (parse p b2) /\ ( let (Some (v1, len1)) = parse p b1 in let (Some (v2, len2)) = parse p b2 in (len1 <: nat) == (len2 <: nat) /\ Seq.slice b1 0 len1 == Seq.slice b2 0 len2 ) let injective_postcond_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( injective_postcond p2 b1 b2 <==> injective_postcond p1 b1 b2 )) = () let injective (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b1 b2: bytes) . {:pattern (injective_precond p b1 b2) \/ (injective_postcond p b1 b2)} injective_precond p b1 b2 ==> injective_postcond p b1 b2 let injective_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( injective p2 <==> injective p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_precond_ext p1 p2 b1)); Classical.forall_intro_2 (fun b1 -> Classical.move_requires (injective_postcond_ext p1 p2 b1)) let no_lookahead_on_precond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) /\ ( let (Some v) = parse f x in let (_, off) = v in off <= Seq.length x' /\ Seq.slice x' 0 off == Seq.slice x 0 off ) let no_lookahead_on_postcond (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = Some? (parse f x) ==> ( let (Some v) = parse f x in let (y, _) = v in Some? (parse f x') /\ ( let (Some v') = parse f x' in let (y', _) = v' in y == y' )) let no_lookahead_on (#t: Type) (f: bare_parser t) (x x' : bytes) : GTot Type0 = no_lookahead_on_precond f x x' ==> no_lookahead_on_postcond f x x' let no_lookahead_on_ext (#t: Type) (p1 p2: bare_parser t) (b1 b2: bytes) : Lemma (requires ( parse p2 b1 == parse p1 b1 /\ parse p2 b2 == parse p1 b2 )) (ensures ( no_lookahead_on p2 b1 b2 <==> no_lookahead_on p1 b1 b2 )) = () let no_lookahead (#t: Type) (f: bare_parser t) : GTot Type0 = forall (x x' : bytes) . {:pattern (no_lookahead_on_precond f x x') \/ (no_lookahead_on_postcond f x x')} no_lookahead_on f x x' let no_lookahead_ext (#t: Type) (p1 p2: bare_parser t) : Lemma (requires ( forall (b: bytes) . parse p2 b == parse p1 b )) (ensures ( no_lookahead p2 <==> no_lookahead p1 )) = Classical.forall_intro_2 (fun b1 -> Classical.move_requires (no_lookahead_on_ext p1 p2 b1)) (** A parser that always consumes all its input *) let consumes_all (#t: Type) (p: bare_parser t) : GTot Type0 = forall (b: bytes) . {:pattern (parse p b)} Some? (parse p b) ==> ( let (Some (_, len)) = parse p b in Seq.length b == len ) (** Parsing data of bounded size *) let parses_at_least (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz <= (consumed <: nat) ) let parses_at_least_0 (#t: Type) (f: bare_parser t) : Lemma (parses_at_least 0 f) = () let parses_at_least_le (sz sz': nat) (#t: Type) (f: bare_parser t) : Lemma (requires ( parses_at_least sz f /\ sz' <= sz )) (ensures ( parses_at_least sz' f )) = () (** A parser that always consumes at least one byte. A list can be serialized only if the parser for elements always consumes at least one byte. Anyway, since we require such a parser to have the prefix property, this is always true except for the parser for empty data. *) let parses_at_most (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz >= (consumed <: nat) ) let is_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s)} Some? (parse f s) ==> ( let (_, consumed) = Some?.v (parse f s) in sz == (consumed <: nat) ) let is_constant_size_parser_equiv (sz: nat) (#t: Type) (f: bare_parser t) : Lemma (is_constant_size_parser sz f <==> (parses_at_least sz f /\ parses_at_most sz f)) = () let is_total_constant_size_parser (sz: nat) (#t: Type) (f: bare_parser t) : GTot Type0 = forall (s: bytes) . {:pattern (parse f s) } (Seq.length s < sz) == (None? (parse f s)) type parser_subkind = | ParserStrong | ParserConsumesAll let parser_subkind_prop (k: parser_subkind) (#t: Type) (f: bare_parser t) : GTot Type0 = match k with | ParserStrong -> no_lookahead f | ParserConsumesAll -> consumes_all f type parser_kind_metadata_some = | ParserKindMetadataTotal | ParserKindMetadataFail type parser_kind_metadata_t = option parser_kind_metadata_some inline_for_extraction type parser_kind' = { parser_kind_low: nat; parser_kind_high: option nat; parser_kind_subkind: option parser_subkind; parser_kind_metadata: parser_kind_metadata_t; } let parser_kind = (x: parser_kind' { Some? x.parser_kind_high ==> x.parser_kind_low <= Some?.v x.parser_kind_high }) inline_for_extraction let strong_parser_kind (lo hi: nat) (md: parser_kind_metadata_t) : Pure parser_kind (requires (lo <= hi)) (ensures (fun _ -> True)) = { parser_kind_low = lo; parser_kind_high = Some hi; parser_kind_subkind = Some ParserStrong; parser_kind_metadata = md; } inline_for_extraction let total_constant_size_parser_kind (sz: nat) : Tot parser_kind = strong_parser_kind sz sz (Some ParserKindMetadataTotal) let parser_always_fails (#t: Type) (f: bare_parser t) : GTot Type0 = forall input . {:pattern (parse f input)} parse f input == None let parser_kind_metadata_prop (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = match k.parser_kind_metadata with | None -> True | Some ParserKindMetadataTotal -> k.parser_kind_high == Some k.parser_kind_low ==> is_total_constant_size_parser k.parser_kind_low f | Some ParserKindMetadataFail -> parser_always_fails f let parser_kind_prop' (#t: Type) (k: parser_kind) (f: bare_parser t) : GTot Type0 = injective f /\ (Some? k.parser_kind_subkind ==> parser_subkind_prop (Some?.v k.parser_kind_subkind) f) /\ parses_at_least k.parser_kind_low f /\ (Some? k.parser_kind_high ==> (parses_at_most (Some?.v k.parser_kind_high) f)) /\ parser_kind_metadata_prop k f val parser_kind_prop (#a:Type) (k:parser_kind) (f:bare_parser a) : GTot Type0 val parser_kind_prop_equiv (#t: Type) (k: parser_kind) (f: bare_parser t) : Lemma (parser_kind_prop k f <==> parser_kind_prop' k f) val parser_kind_prop_ext (#t: Type) (k: parser_kind) (f1 f2: bare_parser t) : Lemma (requires (forall (input: bytes) . parse f1 input == parse f2 input)) (ensures (parser_kind_prop k f1 <==> parser_kind_prop k f2)) [@unifier_hint_injective] inline_for_extraction let parser (k: parser_kind) (t: Type) : Tot Type = (f: bare_parser t { parser_kind_prop k f } ) inline_for_extraction let tot_bare_parser (t:Type) : Tot Type = (b: bytes) -> Tot (option (t * consumed_length b)) [@unifier_hint_injective] let tot_parser (k: parser_kind) (t: Type) : Tot Type = (f: tot_bare_parser t { parser_kind_prop k f } ) inline_for_extraction let get_parser_kind (#k: parser_kind) (#t: Type) (p: parser k t) : Tot parser_kind = k inline_for_extraction let get_parser_type (#k: parser_kind) (#t: Type) (p: parser k t) : Tot Type = t let parser_kind_prop_intro (k: parser_kind) (#t: Type) (f: parser k t) : Lemma (parser_kind_prop k f) = () let is_strong (#k:parser_kind) (#t:Type) (p:parser k t) : Tot (r:bool{r ==> k.parser_kind_subkind == Some (ParserStrong)}) = k.parser_kind_subkind = Some (ParserStrong) let is_weaker_than (k1 k2: parser_kind) : GTot Type0 = (Some? k1.parser_kind_metadata ==> k1.parser_kind_metadata == k2.parser_kind_metadata) /\ ((k1.parser_kind_metadata <> Some ParserKindMetadataFail /\ k2.parser_kind_metadata <> Some ParserKindMetadataFail) ==> ( (Some? k1.parser_kind_subkind ==> k1.parser_kind_subkind == k2.parser_kind_subkind) /\ k1.parser_kind_low <= k2.parser_kind_low /\ (Some? k1.parser_kind_high ==> ( Some? k2.parser_kind_high /\ Some?.v k2.parser_kind_high <= Some?.v k1.parser_kind_high )))) val is_weaker_than_correct (k1: parser_kind) (k2: parser_kind) (#t: Type) (f: bare_parser t) : Lemma (requires (parser_kind_prop k2 f /\ k1 `is_weaker_than` k2)) (ensures (parser_kind_prop k1 f)) (* AR: see bug#1349 *) unfold let coerce_to_bare_parser (t:Type) (k2:parser_kind) (p:parser k2 t) :Tot (bare_parser t) = p let weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: parser k2 t) : Pure (parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: parser k1 t unfold let coerce_to_tot_bare_parser (t:Type) (k2:parser_kind) (p:tot_parser k2 t) :Tot (tot_bare_parser t) = p let tot_weaken (k1: parser_kind) (#k2: parser_kind) (#t: Type) (p2: tot_parser k2 t) : Pure (tot_parser k1 t) (requires (k1 `is_weaker_than` k2)) (ensures (fun _ -> True)) = let p = coerce_to_tot_bare_parser t k2 p2 in is_weaker_than_correct k1 k2 p; p <: tot_parser k1 t // inline_for_extraction let tot_strengthen (k: parser_kind) (#t: Type) (f: tot_bare_parser t) : Pure (tot_parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f // inline_for_extraction let strengthen (k: parser_kind) (#t: Type) (f: bare_parser t) : Pure (parser k t) (requires (parser_kind_prop k f)) (ensures (fun _ -> True)) = f #push-options "--z3rlimit 16" [@"opaque_to_smt"] inline_for_extraction let is_some (#t: Type) (x: option t) : Tot (y: bool { y == Some? x }) = match x with | Some _ -> true | _ -> false [@"opaque_to_smt"] inline_for_extraction let some_v (#t: Type) (x: option t { Some? x }) : Tot (y: t { y == Some?.v x }) = match x with | Some y -> y [@"opaque_to_smt"] inline_for_extraction let bool_and (b1 b2: bool) : Tot (y: bool { y == (b1 && b2) }) = if b1 then b2 else false [@"opaque_to_smt"] inline_for_extraction let bool_or (b1 b2: bool) : Tot (y: bool { y == (b1 || b2) }) = if b1 then true else b2 inline_for_extraction let glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2 // (forall k' . (k' `is_weaker_than` k1 /\ k' `is_weaker_than` k2) ==> k' `is_weaker_than` k)
false
false
LowParse.Spec.Base.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val glb (k1 k2: parser_kind) : Pure parser_kind (requires True) (ensures (fun k -> k `is_weaker_than` k1 /\ k `is_weaker_than` k2))
[]
LowParse.Spec.Base.glb
{ "file_name": "src/lowparse/LowParse.Spec.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
k1: LowParse.Spec.Base.parser_kind -> k2: LowParse.Spec.Base.parser_kind -> Prims.Pure LowParse.Spec.Base.parser_kind
{ "end_col": 3, "end_line": 519, "start_col": 2, "start_line": 492 }
Prims.Tot
val invariant_s: #a:supported_alg -> state_s a -> HS.mem -> Type0
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let invariant_s #a st h = invariant (p st) h
val invariant_s: #a:supported_alg -> state_s a -> HS.mem -> Type0 let invariant_s #a st h =
false
null
false
invariant (p st) h
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "Hacl.HMAC_DRBG.supported_alg", "EverCrypt.DRBG.state_s", "FStar.Monotonic.HyperStack.mem", "Hacl.HMAC_DRBG.invariant", "EverCrypt.DRBG.p" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st)
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val invariant_s: #a:supported_alg -> state_s a -> HS.mem -> Type0
[]
EverCrypt.DRBG.invariant_s
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
st: EverCrypt.DRBG.state_s a -> h: FStar.Monotonic.HyperStack.mem -> Type0
{ "end_col": 44, "end_line": 53, "start_col": 26, "start_line": 53 }
Prims.Tot
val freeable_s: #a:supported_alg -> st:state_s a -> Type0
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let freeable_s #a st = freeable (p st)
val freeable_s: #a:supported_alg -> st:state_s a -> Type0 let freeable_s #a st =
false
null
false
freeable (p st)
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "Hacl.HMAC_DRBG.supported_alg", "EverCrypt.DRBG.state_s", "Hacl.HMAC_DRBG.freeable", "EverCrypt.DRBG.p" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val freeable_s: #a:supported_alg -> st:state_s a -> Type0
[]
EverCrypt.DRBG.freeable_s
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
st: EverCrypt.DRBG.state_s a -> Type0
{ "end_col": 38, "end_line": 49, "start_col": 23, "start_line": 49 }
FStar.Pervasives.Lemma
val invert_state_s (a: supported_alg) : Lemma (requires True) (ensures inversion (state_s a)) [SMTPat (state_s a)]
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a)
val invert_state_s (a: supported_alg) : Lemma (requires True) (ensures inversion (state_s a)) [SMTPat (state_s a)] let invert_state_s (a: supported_alg) : Lemma (requires True) (ensures inversion (state_s a)) [SMTPat (state_s a)] =
false
null
true
allow_inversion (state_s a)
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "lemma" ]
[ "Hacl.HMAC_DRBG.supported_alg", "FStar.Pervasives.allow_inversion", "EverCrypt.DRBG.state_s", "Prims.unit", "Prims.l_True", "Prims.squash", "FStar.Pervasives.inversion", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ]
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val invert_state_s (a: supported_alg) : Lemma (requires True) (ensures inversion (state_s a)) [SMTPat (state_s a)]
[]
EverCrypt.DRBG.invert_state_s
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
a: Hacl.HMAC_DRBG.supported_alg -> FStar.Pervasives.Lemma (ensures FStar.Pervasives.inversion (EverCrypt.DRBG.state_s a)) [SMTPat (EverCrypt.DRBG.state_s a)]
{ "end_col": 29, "end_line": 38, "start_col": 2, "start_line": 38 }
Prims.GTot
val footprint_s: #a:supported_alg -> state_s a -> GTot B.loc
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let footprint_s #a st = footprint (p st)
val footprint_s: #a:supported_alg -> state_s a -> GTot B.loc let footprint_s #a st =
false
null
false
footprint (p st)
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "sometrivial" ]
[ "Hacl.HMAC_DRBG.supported_alg", "EverCrypt.DRBG.state_s", "Hacl.HMAC_DRBG.footprint", "EverCrypt.DRBG.p", "LowStar.Monotonic.Buffer.loc" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st)
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val footprint_s: #a:supported_alg -> state_s a -> GTot B.loc
[]
EverCrypt.DRBG.footprint_s
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
st: EverCrypt.DRBG.state_s a -> Prims.GTot LowStar.Monotonic.Buffer.loc
{ "end_col": 40, "end_line": 51, "start_col": 24, "start_line": 51 }
FStar.Pervasives.Lemma
val loc_includes_union_l_footprint_s: #a:supported_alg -> l1:B.loc -> l2:B.loc -> st:state_s a -> Lemma (requires B.loc_includes l1 (footprint_s st) \/ B.loc_includes l2 (footprint_s st)) (ensures B.loc_includes (B.loc_union l1 l2) (footprint_s st)) [SMTPat (B.loc_includes (B.loc_union l1 l2) (footprint_s st))]
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st)
val loc_includes_union_l_footprint_s: #a:supported_alg -> l1:B.loc -> l2:B.loc -> st:state_s a -> Lemma (requires B.loc_includes l1 (footprint_s st) \/ B.loc_includes l2 (footprint_s st)) (ensures B.loc_includes (B.loc_union l1 l2) (footprint_s st)) [SMTPat (B.loc_includes (B.loc_union l1 l2) (footprint_s st))] let loc_includes_union_l_footprint_s #a l1 l2 st =
false
null
true
B.loc_includes_union_l l1 l2 (footprint_s st)
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "lemma" ]
[ "Hacl.HMAC_DRBG.supported_alg", "LowStar.Monotonic.Buffer.loc", "EverCrypt.DRBG.state_s", "LowStar.Monotonic.Buffer.loc_includes_union_l", "EverCrypt.DRBG.footprint_s", "Prims.unit" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val loc_includes_union_l_footprint_s: #a:supported_alg -> l1:B.loc -> l2:B.loc -> st:state_s a -> Lemma (requires B.loc_includes l1 (footprint_s st) \/ B.loc_includes l2 (footprint_s st)) (ensures B.loc_includes (B.loc_union l1 l2) (footprint_s st)) [SMTPat (B.loc_includes (B.loc_union l1 l2) (footprint_s st))]
[]
EverCrypt.DRBG.loc_includes_union_l_footprint_s
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
l1: LowStar.Monotonic.Buffer.loc -> l2: LowStar.Monotonic.Buffer.loc -> st: EverCrypt.DRBG.state_s a -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.loc_includes l1 (EverCrypt.DRBG.footprint_s st) \/ LowStar.Monotonic.Buffer.loc_includes l2 (EverCrypt.DRBG.footprint_s st)) (ensures LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_union l1 l2) (EverCrypt.DRBG.footprint_s st)) [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_union l1 l2) (EverCrypt.DRBG.footprint_s st)) ]
{ "end_col": 47, "end_line": 60, "start_col": 2, "start_line": 60 }
Prims.GTot
val repr: #a:supported_alg -> st:state a -> h:HS.mem -> GTot (S.state a)
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let repr #a st h = let st = B.get h st 0 in repr (p st) h
val repr: #a:supported_alg -> st:state a -> h:HS.mem -> GTot (S.state a) let repr #a st h =
false
null
false
let st = B.get h st 0 in repr (p st) h
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "sometrivial" ]
[ "Hacl.HMAC_DRBG.supported_alg", "EverCrypt.DRBG.state", "FStar.Monotonic.HyperStack.mem", "Hacl.HMAC_DRBG.repr", "EverCrypt.DRBG.p", "EverCrypt.DRBG.state_s", "LowStar.Monotonic.Buffer.get", "LowStar.Buffer.trivial_preorder", "Spec.HMAC_DRBG.state" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val repr: #a:supported_alg -> st:state a -> h:HS.mem -> GTot (S.state a)
[]
EverCrypt.DRBG.repr
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
st: EverCrypt.DRBG.state a -> h: FStar.Monotonic.HyperStack.mem -> Prims.GTot (Spec.HMAC_DRBG.state a)
{ "end_col": 15, "end_line": 57, "start_col": 18, "start_line": 55 }
Prims.Tot
val p (#a: _) (s: state_s a) : Hacl.HMAC_DRBG.state a
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p
val p (#a: _) (s: state_s a) : Hacl.HMAC_DRBG.state a let p #a (s: state_s a) : Hacl.HMAC_DRBG.state a =
false
null
false
match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "Spec.HMAC_DRBG.supported_alg", "EverCrypt.DRBG.state_s", "Hacl.HMAC_DRBG.state", "Spec.Hash.Definitions.SHA1", "Spec.Hash.Definitions.SHA2_256", "Spec.Hash.Definitions.SHA2_384", "Spec.Hash.Definitions.SHA2_512" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val p (#a: _) (s: state_s a) : Hacl.HMAC_DRBG.state a
[]
EverCrypt.DRBG.p
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
s: EverCrypt.DRBG.state_s a -> Hacl.HMAC_DRBG.state a
{ "end_col": 41, "end_line": 47, "start_col": 2, "start_line": 43 }
Prims.Tot
val uninstantiate: a:(Ghost.erased supported_alg) -> uninstantiate_st (Ghost.reveal a)
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let uninstantiate a st = match !* st with | SHA1_s _ -> uninstantiate_sha1 st | SHA2_256_s _ -> uninstantiate_sha2_256 st | SHA2_384_s _ -> uninstantiate_sha2_384 st | SHA2_512_s _ -> uninstantiate_sha2_512 st
val uninstantiate: a:(Ghost.erased supported_alg) -> uninstantiate_st (Ghost.reveal a) let uninstantiate a st =
false
null
false
match !*st with | SHA1_s _ -> uninstantiate_sha1 st | SHA2_256_s _ -> uninstantiate_sha2_256 st | SHA2_384_s _ -> uninstantiate_sha2_384 st | SHA2_512_s _ -> uninstantiate_sha2_512 st
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "FStar.Ghost.erased", "Hacl.HMAC_DRBG.supported_alg", "EverCrypt.DRBG.state", "FStar.Ghost.reveal", "Hacl.HMAC_DRBG.state", "Spec.Hash.Definitions.SHA1", "EverCrypt.DRBG.uninstantiate_sha1", "Prims.unit", "Spec.Hash.Definitions.SHA2_256", "EverCrypt.DRBG.uninstantiate_sha2_256", "Spec.Hash.Definitions.SHA2_384", "EverCrypt.DRBG.uninstantiate_sha2_384", "Spec.Hash.Definitions.SHA2_512", "EverCrypt.DRBG.uninstantiate_sha2_512", "EverCrypt.DRBG.state_s", "Spec.HMAC_DRBG.supported_alg", "LowStar.BufferOps.op_Bang_Star", "LowStar.Buffer.trivial_preorder" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512 let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1 let generate_sha2_256 = mk_generate EverCrypt.HMAC.compute_sha2_256 let generate_sha2_384 = mk_generate EverCrypt.HMAC.compute_sha2_384 let generate_sha2_512 = mk_generate EverCrypt.HMAC.compute_sha2_512 /// Uninstantiate function inline_for_extraction noextract val mk_uninstantiate: a:supported_alg -> uninstantiate_st a let mk_uninstantiate a st = let st_s:state_s a = !*st in let s:Hacl.HMAC_DRBG.state a = p st_s in let k:B.buffer uint8 = s.k in let v:B.buffer uint8 = s.v in let ctr:B.buffer size_t = s.reseed_counter in assert (B.disjoint k v /\ B.disjoint k ctr /\ B.disjoint v ctr); assert (B.loc_disjoint (B.loc_addr_of_buffer st) (footprint_s st_s)); Lib.Memzero0.memzero k (hash_len a); Lib.Memzero0.memzero v (hash_len a); ctr.(0ul) <- 0ul; B.free k; B.free v; B.free ctr; B.free st (** @type: true *) val uninstantiate_sha1 : uninstantiate_st SHA1 (** @type: true *) val uninstantiate_sha2_256: uninstantiate_st SHA2_256 (** @type: true *) val uninstantiate_sha2_384: uninstantiate_st SHA2_384 (** @type: true *) val uninstantiate_sha2_512: uninstantiate_st SHA2_512 let uninstantiate_sha1 = mk_uninstantiate SHA1 let uninstantiate_sha2_256 = mk_uninstantiate SHA2_256 let uninstantiate_sha2_384 = mk_uninstantiate SHA2_384 let uninstantiate_sha2_512 = mk_uninstantiate SHA2_512 /// Agile variants that dispatch dynamically to the appropriate monomorphic variants above let instantiate a st personalization_string personalization_string_len = match !*st with | SHA1_s _ -> instantiate_sha1 st personalization_string personalization_string_len | SHA2_256_s _ -> instantiate_sha2_256 st personalization_string personalization_string_len | SHA2_384_s _ -> instantiate_sha2_384 st personalization_string personalization_string_len | SHA2_512_s _ -> instantiate_sha2_512 st personalization_string personalization_string_len let reseed a st additional_input additional_input_len = match !*st with | SHA1_s _ -> reseed_sha1 st additional_input additional_input_len | SHA2_256_s _ -> reseed_sha2_256 st additional_input additional_input_len | SHA2_384_s _ -> reseed_sha2_384 st additional_input additional_input_len | SHA2_512_s _ -> reseed_sha2_512 st additional_input additional_input_len let generate a output st n additional_input additional_input_len = match !*st with | SHA1_s _ -> generate_sha1 output st n additional_input additional_input_len | SHA2_256_s _ -> generate_sha2_256 output st n additional_input additional_input_len | SHA2_384_s _ -> generate_sha2_384 output st n additional_input additional_input_len | SHA2_512_s _ -> generate_sha2_512 output st n additional_input additional_input_len
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val uninstantiate: a:(Ghost.erased supported_alg) -> uninstantiate_st (Ghost.reveal a)
[]
EverCrypt.DRBG.uninstantiate
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
a: FStar.Ghost.erased Hacl.HMAC_DRBG.supported_alg -> EverCrypt.DRBG.uninstantiate_st (FStar.Ghost.reveal a)
{ "end_col": 45, "end_line": 315, "start_col": 2, "start_line": 311 }
Prims.Tot
val generate_sha1 : generate_st SHA1
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1
val generate_sha1 : generate_st SHA1 let generate_sha1 =
false
null
false
mk_generate EverCrypt.HMAC.compute_sha1
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_generate", "Spec.Hash.Definitions.SHA1", "EverCrypt.HMAC.compute_sha1" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val generate_sha1 : generate_st SHA1
[]
EverCrypt.DRBG.generate_sha1
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.generate_st Spec.Hash.Definitions.SHA1
{ "end_col": 63, "end_line": 240, "start_col": 24, "start_line": 240 }
Prims.Tot
val instantiate: a:(Ghost.erased supported_alg) -> instantiate_st (Ghost.reveal a)
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let instantiate a st personalization_string personalization_string_len = match !*st with | SHA1_s _ -> instantiate_sha1 st personalization_string personalization_string_len | SHA2_256_s _ -> instantiate_sha2_256 st personalization_string personalization_string_len | SHA2_384_s _ -> instantiate_sha2_384 st personalization_string personalization_string_len | SHA2_512_s _ -> instantiate_sha2_512 st personalization_string personalization_string_len
val instantiate: a:(Ghost.erased supported_alg) -> instantiate_st (Ghost.reveal a) let instantiate a st personalization_string personalization_string_len =
false
null
false
match !*st with | SHA1_s _ -> instantiate_sha1 st personalization_string personalization_string_len | SHA2_256_s _ -> instantiate_sha2_256 st personalization_string personalization_string_len | SHA2_384_s _ -> instantiate_sha2_384 st personalization_string personalization_string_len | SHA2_512_s _ -> instantiate_sha2_512 st personalization_string personalization_string_len
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "FStar.Ghost.erased", "Hacl.HMAC_DRBG.supported_alg", "EverCrypt.DRBG.state", "FStar.Ghost.reveal", "LowStar.Buffer.buffer", "Lib.IntTypes.uint8", "Lib.IntTypes.size_t", "Hacl.HMAC_DRBG.state", "Spec.Hash.Definitions.SHA1", "EverCrypt.DRBG.instantiate_sha1", "Prims.bool", "Spec.Hash.Definitions.SHA2_256", "EverCrypt.DRBG.instantiate_sha2_256", "Spec.Hash.Definitions.SHA2_384", "EverCrypt.DRBG.instantiate_sha2_384", "Spec.Hash.Definitions.SHA2_512", "EverCrypt.DRBG.instantiate_sha2_512", "EverCrypt.DRBG.state_s", "Spec.HMAC_DRBG.supported_alg", "LowStar.BufferOps.op_Bang_Star", "LowStar.Buffer.trivial_preorder" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512 let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1 let generate_sha2_256 = mk_generate EverCrypt.HMAC.compute_sha2_256 let generate_sha2_384 = mk_generate EverCrypt.HMAC.compute_sha2_384 let generate_sha2_512 = mk_generate EverCrypt.HMAC.compute_sha2_512 /// Uninstantiate function inline_for_extraction noextract val mk_uninstantiate: a:supported_alg -> uninstantiate_st a let mk_uninstantiate a st = let st_s:state_s a = !*st in let s:Hacl.HMAC_DRBG.state a = p st_s in let k:B.buffer uint8 = s.k in let v:B.buffer uint8 = s.v in let ctr:B.buffer size_t = s.reseed_counter in assert (B.disjoint k v /\ B.disjoint k ctr /\ B.disjoint v ctr); assert (B.loc_disjoint (B.loc_addr_of_buffer st) (footprint_s st_s)); Lib.Memzero0.memzero k (hash_len a); Lib.Memzero0.memzero v (hash_len a); ctr.(0ul) <- 0ul; B.free k; B.free v; B.free ctr; B.free st (** @type: true *) val uninstantiate_sha1 : uninstantiate_st SHA1 (** @type: true *) val uninstantiate_sha2_256: uninstantiate_st SHA2_256 (** @type: true *) val uninstantiate_sha2_384: uninstantiate_st SHA2_384 (** @type: true *) val uninstantiate_sha2_512: uninstantiate_st SHA2_512 let uninstantiate_sha1 = mk_uninstantiate SHA1 let uninstantiate_sha2_256 = mk_uninstantiate SHA2_256 let uninstantiate_sha2_384 = mk_uninstantiate SHA2_384 let uninstantiate_sha2_512 = mk_uninstantiate SHA2_512 /// Agile variants that dispatch dynamically to the appropriate monomorphic variants above
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val instantiate: a:(Ghost.erased supported_alg) -> instantiate_st (Ghost.reveal a)
[]
EverCrypt.DRBG.instantiate
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
a: FStar.Ghost.erased Hacl.HMAC_DRBG.supported_alg -> EverCrypt.DRBG.instantiate_st (FStar.Ghost.reveal a)
{ "end_col": 93, "end_line": 294, "start_col": 2, "start_line": 290 }
Prims.Tot
val reseed: a:(Ghost.erased supported_alg) -> reseed_st (Ghost.reveal a)
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let reseed a st additional_input additional_input_len = match !*st with | SHA1_s _ -> reseed_sha1 st additional_input additional_input_len | SHA2_256_s _ -> reseed_sha2_256 st additional_input additional_input_len | SHA2_384_s _ -> reseed_sha2_384 st additional_input additional_input_len | SHA2_512_s _ -> reseed_sha2_512 st additional_input additional_input_len
val reseed: a:(Ghost.erased supported_alg) -> reseed_st (Ghost.reveal a) let reseed a st additional_input additional_input_len =
false
null
false
match !*st with | SHA1_s _ -> reseed_sha1 st additional_input additional_input_len | SHA2_256_s _ -> reseed_sha2_256 st additional_input additional_input_len | SHA2_384_s _ -> reseed_sha2_384 st additional_input additional_input_len | SHA2_512_s _ -> reseed_sha2_512 st additional_input additional_input_len
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "FStar.Ghost.erased", "Hacl.HMAC_DRBG.supported_alg", "EverCrypt.DRBG.state", "FStar.Ghost.reveal", "LowStar.Buffer.buffer", "Lib.IntTypes.uint8", "Lib.IntTypes.size_t", "Hacl.HMAC_DRBG.state", "Spec.Hash.Definitions.SHA1", "EverCrypt.DRBG.reseed_sha1", "Prims.bool", "Spec.Hash.Definitions.SHA2_256", "EverCrypt.DRBG.reseed_sha2_256", "Spec.Hash.Definitions.SHA2_384", "EverCrypt.DRBG.reseed_sha2_384", "Spec.Hash.Definitions.SHA2_512", "EverCrypt.DRBG.reseed_sha2_512", "EverCrypt.DRBG.state_s", "Spec.HMAC_DRBG.supported_alg", "LowStar.BufferOps.op_Bang_Star", "LowStar.Buffer.trivial_preorder" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512 let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1 let generate_sha2_256 = mk_generate EverCrypt.HMAC.compute_sha2_256 let generate_sha2_384 = mk_generate EverCrypt.HMAC.compute_sha2_384 let generate_sha2_512 = mk_generate EverCrypt.HMAC.compute_sha2_512 /// Uninstantiate function inline_for_extraction noextract val mk_uninstantiate: a:supported_alg -> uninstantiate_st a let mk_uninstantiate a st = let st_s:state_s a = !*st in let s:Hacl.HMAC_DRBG.state a = p st_s in let k:B.buffer uint8 = s.k in let v:B.buffer uint8 = s.v in let ctr:B.buffer size_t = s.reseed_counter in assert (B.disjoint k v /\ B.disjoint k ctr /\ B.disjoint v ctr); assert (B.loc_disjoint (B.loc_addr_of_buffer st) (footprint_s st_s)); Lib.Memzero0.memzero k (hash_len a); Lib.Memzero0.memzero v (hash_len a); ctr.(0ul) <- 0ul; B.free k; B.free v; B.free ctr; B.free st (** @type: true *) val uninstantiate_sha1 : uninstantiate_st SHA1 (** @type: true *) val uninstantiate_sha2_256: uninstantiate_st SHA2_256 (** @type: true *) val uninstantiate_sha2_384: uninstantiate_st SHA2_384 (** @type: true *) val uninstantiate_sha2_512: uninstantiate_st SHA2_512 let uninstantiate_sha1 = mk_uninstantiate SHA1 let uninstantiate_sha2_256 = mk_uninstantiate SHA2_256 let uninstantiate_sha2_384 = mk_uninstantiate SHA2_384 let uninstantiate_sha2_512 = mk_uninstantiate SHA2_512 /// Agile variants that dispatch dynamically to the appropriate monomorphic variants above let instantiate a st personalization_string personalization_string_len = match !*st with | SHA1_s _ -> instantiate_sha1 st personalization_string personalization_string_len | SHA2_256_s _ -> instantiate_sha2_256 st personalization_string personalization_string_len | SHA2_384_s _ -> instantiate_sha2_384 st personalization_string personalization_string_len | SHA2_512_s _ -> instantiate_sha2_512 st personalization_string personalization_string_len
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val reseed: a:(Ghost.erased supported_alg) -> reseed_st (Ghost.reveal a)
[]
EverCrypt.DRBG.reseed
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
a: FStar.Ghost.erased Hacl.HMAC_DRBG.supported_alg -> EverCrypt.DRBG.reseed_st (FStar.Ghost.reveal a)
{ "end_col": 76, "end_line": 301, "start_col": 2, "start_line": 297 }
Prims.Tot
val instantiate_sha1 : instantiate_st SHA1
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1
val instantiate_sha1 : instantiate_st SHA1 let instantiate_sha1 =
false
null
false
mk_instantiate EverCrypt.HMAC.compute_sha1
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_instantiate", "Spec.Hash.Definitions.SHA1", "EverCrypt.HMAC.compute_sha1" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val instantiate_sha1 : instantiate_st SHA1
[]
EverCrypt.DRBG.instantiate_sha1
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.instantiate_st Spec.Hash.Definitions.SHA1
{ "end_col": 69, "end_line": 142, "start_col": 27, "start_line": 142 }
Prims.Tot
val reseed_sha2_256: reseed_st SHA2_256
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256
val reseed_sha2_256: reseed_st SHA2_256 let reseed_sha2_256 =
false
null
false
mk_reseed EverCrypt.HMAC.compute_sha2_256
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_reseed", "Spec.Hash.Definitions.SHA2_256", "EverCrypt.HMAC.compute_sha2_256" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val reseed_sha2_256: reseed_st SHA2_256
[]
EverCrypt.DRBG.reseed_sha2_256
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.reseed_st Spec.Hash.Definitions.SHA2_256
{ "end_col": 63, "end_line": 193, "start_col": 22, "start_line": 193 }
Prims.Tot
val generate: a:(Ghost.erased supported_alg) -> generate_st (Ghost.reveal a)
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let generate a output st n additional_input additional_input_len = match !*st with | SHA1_s _ -> generate_sha1 output st n additional_input additional_input_len | SHA2_256_s _ -> generate_sha2_256 output st n additional_input additional_input_len | SHA2_384_s _ -> generate_sha2_384 output st n additional_input additional_input_len | SHA2_512_s _ -> generate_sha2_512 output st n additional_input additional_input_len
val generate: a:(Ghost.erased supported_alg) -> generate_st (Ghost.reveal a) let generate a output st n additional_input additional_input_len =
false
null
false
match !*st with | SHA1_s _ -> generate_sha1 output st n additional_input additional_input_len | SHA2_256_s _ -> generate_sha2_256 output st n additional_input additional_input_len | SHA2_384_s _ -> generate_sha2_384 output st n additional_input additional_input_len | SHA2_512_s _ -> generate_sha2_512 output st n additional_input additional_input_len
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "FStar.Ghost.erased", "Hacl.HMAC_DRBG.supported_alg", "LowStar.Buffer.buffer", "Lib.IntTypes.uint8", "EverCrypt.DRBG.state", "FStar.Ghost.reveal", "Lib.IntTypes.size_t", "Hacl.HMAC_DRBG.state", "Spec.Hash.Definitions.SHA1", "EverCrypt.DRBG.generate_sha1", "Prims.bool", "Spec.Hash.Definitions.SHA2_256", "EverCrypt.DRBG.generate_sha2_256", "Spec.Hash.Definitions.SHA2_384", "EverCrypt.DRBG.generate_sha2_384", "Spec.Hash.Definitions.SHA2_512", "EverCrypt.DRBG.generate_sha2_512", "EverCrypt.DRBG.state_s", "Spec.HMAC_DRBG.supported_alg", "LowStar.BufferOps.op_Bang_Star", "LowStar.Buffer.trivial_preorder" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512 let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1 let generate_sha2_256 = mk_generate EverCrypt.HMAC.compute_sha2_256 let generate_sha2_384 = mk_generate EverCrypt.HMAC.compute_sha2_384 let generate_sha2_512 = mk_generate EverCrypt.HMAC.compute_sha2_512 /// Uninstantiate function inline_for_extraction noextract val mk_uninstantiate: a:supported_alg -> uninstantiate_st a let mk_uninstantiate a st = let st_s:state_s a = !*st in let s:Hacl.HMAC_DRBG.state a = p st_s in let k:B.buffer uint8 = s.k in let v:B.buffer uint8 = s.v in let ctr:B.buffer size_t = s.reseed_counter in assert (B.disjoint k v /\ B.disjoint k ctr /\ B.disjoint v ctr); assert (B.loc_disjoint (B.loc_addr_of_buffer st) (footprint_s st_s)); Lib.Memzero0.memzero k (hash_len a); Lib.Memzero0.memzero v (hash_len a); ctr.(0ul) <- 0ul; B.free k; B.free v; B.free ctr; B.free st (** @type: true *) val uninstantiate_sha1 : uninstantiate_st SHA1 (** @type: true *) val uninstantiate_sha2_256: uninstantiate_st SHA2_256 (** @type: true *) val uninstantiate_sha2_384: uninstantiate_st SHA2_384 (** @type: true *) val uninstantiate_sha2_512: uninstantiate_st SHA2_512 let uninstantiate_sha1 = mk_uninstantiate SHA1 let uninstantiate_sha2_256 = mk_uninstantiate SHA2_256 let uninstantiate_sha2_384 = mk_uninstantiate SHA2_384 let uninstantiate_sha2_512 = mk_uninstantiate SHA2_512 /// Agile variants that dispatch dynamically to the appropriate monomorphic variants above let instantiate a st personalization_string personalization_string_len = match !*st with | SHA1_s _ -> instantiate_sha1 st personalization_string personalization_string_len | SHA2_256_s _ -> instantiate_sha2_256 st personalization_string personalization_string_len | SHA2_384_s _ -> instantiate_sha2_384 st personalization_string personalization_string_len | SHA2_512_s _ -> instantiate_sha2_512 st personalization_string personalization_string_len let reseed a st additional_input additional_input_len = match !*st with | SHA1_s _ -> reseed_sha1 st additional_input additional_input_len | SHA2_256_s _ -> reseed_sha2_256 st additional_input additional_input_len | SHA2_384_s _ -> reseed_sha2_384 st additional_input additional_input_len | SHA2_512_s _ -> reseed_sha2_512 st additional_input additional_input_len
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val generate: a:(Ghost.erased supported_alg) -> generate_st (Ghost.reveal a)
[]
EverCrypt.DRBG.generate
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
a: FStar.Ghost.erased Hacl.HMAC_DRBG.supported_alg -> EverCrypt.DRBG.generate_st (FStar.Ghost.reveal a)
{ "end_col": 87, "end_line": 308, "start_col": 2, "start_line": 304 }
Prims.Tot
val uninstantiate_sha1 : uninstantiate_st SHA1
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let uninstantiate_sha1 = mk_uninstantiate SHA1
val uninstantiate_sha1 : uninstantiate_st SHA1 let uninstantiate_sha1 =
false
null
false
mk_uninstantiate SHA1
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_uninstantiate", "Spec.Hash.Definitions.SHA1" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512 let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1 let generate_sha2_256 = mk_generate EverCrypt.HMAC.compute_sha2_256 let generate_sha2_384 = mk_generate EverCrypt.HMAC.compute_sha2_384 let generate_sha2_512 = mk_generate EverCrypt.HMAC.compute_sha2_512 /// Uninstantiate function inline_for_extraction noextract val mk_uninstantiate: a:supported_alg -> uninstantiate_st a let mk_uninstantiate a st = let st_s:state_s a = !*st in let s:Hacl.HMAC_DRBG.state a = p st_s in let k:B.buffer uint8 = s.k in let v:B.buffer uint8 = s.v in let ctr:B.buffer size_t = s.reseed_counter in assert (B.disjoint k v /\ B.disjoint k ctr /\ B.disjoint v ctr); assert (B.loc_disjoint (B.loc_addr_of_buffer st) (footprint_s st_s)); Lib.Memzero0.memzero k (hash_len a); Lib.Memzero0.memzero v (hash_len a); ctr.(0ul) <- 0ul; B.free k; B.free v; B.free ctr; B.free st (** @type: true *) val uninstantiate_sha1 : uninstantiate_st SHA1 (** @type: true *) val uninstantiate_sha2_256: uninstantiate_st SHA2_256 (** @type: true *) val uninstantiate_sha2_384: uninstantiate_st SHA2_384 (** @type: true *) val uninstantiate_sha2_512: uninstantiate_st SHA2_512
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val uninstantiate_sha1 : uninstantiate_st SHA1
[]
EverCrypt.DRBG.uninstantiate_sha1
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.uninstantiate_st Spec.Hash.Definitions.SHA1
{ "end_col": 50, "end_line": 281, "start_col": 29, "start_line": 281 }
Prims.Tot
val instantiate_sha2_384: instantiate_st SHA2_384
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384
val instantiate_sha2_384: instantiate_st SHA2_384 let instantiate_sha2_384 =
false
null
false
mk_instantiate EverCrypt.HMAC.compute_sha2_384
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_instantiate", "Spec.Hash.Definitions.SHA2_384", "EverCrypt.HMAC.compute_sha2_384" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val instantiate_sha2_384: instantiate_st SHA2_384
[]
EverCrypt.DRBG.instantiate_sha2_384
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.instantiate_st Spec.Hash.Definitions.SHA2_384
{ "end_col": 73, "end_line": 144, "start_col": 27, "start_line": 144 }
Prims.Tot
val reseed_sha1 : reseed_st SHA1
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1
val reseed_sha1 : reseed_st SHA1 let reseed_sha1 =
false
null
false
mk_reseed EverCrypt.HMAC.compute_sha1
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_reseed", "Spec.Hash.Definitions.SHA1", "EverCrypt.HMAC.compute_sha1" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val reseed_sha1 : reseed_st SHA1
[]
EverCrypt.DRBG.reseed_sha1
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.reseed_st Spec.Hash.Definitions.SHA1
{ "end_col": 59, "end_line": 192, "start_col": 22, "start_line": 192 }
Prims.Tot
val instantiate_sha2_256: instantiate_st SHA2_256
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256
val instantiate_sha2_256: instantiate_st SHA2_256 let instantiate_sha2_256 =
false
null
false
mk_instantiate EverCrypt.HMAC.compute_sha2_256
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_instantiate", "Spec.Hash.Definitions.SHA2_256", "EverCrypt.HMAC.compute_sha2_256" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val instantiate_sha2_256: instantiate_st SHA2_256
[]
EverCrypt.DRBG.instantiate_sha2_256
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.instantiate_st Spec.Hash.Definitions.SHA2_256
{ "end_col": 73, "end_line": 143, "start_col": 27, "start_line": 143 }
FStar.HyperStack.ST.ST
val create: a:supported_alg -> ST (state a) (requires fun _ -> True) (ensures fun h0 st h1 -> B.modifies B.loc_none h0 h1 /\ B.fresh_loc (footprint st h1) h0 h1 /\ invariant st h1 /\ freeable st h1)
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let create a = create_in a HS.root
val create: a:supported_alg -> ST (state a) (requires fun _ -> True) (ensures fun h0 st h1 -> B.modifies B.loc_none h0 h1 /\ B.fresh_loc (footprint st h1) h0 h1 /\ invariant st h1 /\ freeable st h1) let create a =
true
null
false
create_in a HS.root
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[]
[ "Hacl.HMAC_DRBG.supported_alg", "EverCrypt.DRBG.create_in", "FStar.Monotonic.HyperHeap.root", "EverCrypt.DRBG.state" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul
false
false
EverCrypt.DRBG.fst
{ "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" }
null
val create: a:supported_alg -> ST (state a) (requires fun _ -> True) (ensures fun h0 st h1 -> B.modifies B.loc_none h0 h1 /\ B.fresh_loc (footprint st h1) h0 h1 /\ invariant st h1 /\ freeable st h1)
[]
EverCrypt.DRBG.create
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
a: Hacl.HMAC_DRBG.supported_alg -> FStar.HyperStack.ST.ST (EverCrypt.DRBG.state a)
{ "end_col": 34, "end_line": 90, "start_col": 15, "start_line": 90 }
Prims.Tot
val uninstantiate_sha2_256: uninstantiate_st SHA2_256
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let uninstantiate_sha2_256 = mk_uninstantiate SHA2_256
val uninstantiate_sha2_256: uninstantiate_st SHA2_256 let uninstantiate_sha2_256 =
false
null
false
mk_uninstantiate SHA2_256
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_uninstantiate", "Spec.Hash.Definitions.SHA2_256" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512 let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1 let generate_sha2_256 = mk_generate EverCrypt.HMAC.compute_sha2_256 let generate_sha2_384 = mk_generate EverCrypt.HMAC.compute_sha2_384 let generate_sha2_512 = mk_generate EverCrypt.HMAC.compute_sha2_512 /// Uninstantiate function inline_for_extraction noextract val mk_uninstantiate: a:supported_alg -> uninstantiate_st a let mk_uninstantiate a st = let st_s:state_s a = !*st in let s:Hacl.HMAC_DRBG.state a = p st_s in let k:B.buffer uint8 = s.k in let v:B.buffer uint8 = s.v in let ctr:B.buffer size_t = s.reseed_counter in assert (B.disjoint k v /\ B.disjoint k ctr /\ B.disjoint v ctr); assert (B.loc_disjoint (B.loc_addr_of_buffer st) (footprint_s st_s)); Lib.Memzero0.memzero k (hash_len a); Lib.Memzero0.memzero v (hash_len a); ctr.(0ul) <- 0ul; B.free k; B.free v; B.free ctr; B.free st (** @type: true *) val uninstantiate_sha1 : uninstantiate_st SHA1 (** @type: true *) val uninstantiate_sha2_256: uninstantiate_st SHA2_256 (** @type: true *) val uninstantiate_sha2_384: uninstantiate_st SHA2_384 (** @type: true *) val uninstantiate_sha2_512: uninstantiate_st SHA2_512
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val uninstantiate_sha2_256: uninstantiate_st SHA2_256
[]
EverCrypt.DRBG.uninstantiate_sha2_256
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.uninstantiate_st Spec.Hash.Definitions.SHA2_256
{ "end_col": 54, "end_line": 282, "start_col": 29, "start_line": 282 }
Prims.Tot
val generate_sha2_256: generate_st SHA2_256
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let generate_sha2_256 = mk_generate EverCrypt.HMAC.compute_sha2_256
val generate_sha2_256: generate_st SHA2_256 let generate_sha2_256 =
false
null
false
mk_generate EverCrypt.HMAC.compute_sha2_256
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_generate", "Spec.Hash.Definitions.SHA2_256", "EverCrypt.HMAC.compute_sha2_256" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val generate_sha2_256: generate_st SHA2_256
[]
EverCrypt.DRBG.generate_sha2_256
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.generate_st Spec.Hash.Definitions.SHA2_256
{ "end_col": 67, "end_line": 241, "start_col": 24, "start_line": 241 }
Prims.Tot
val reseed_sha2_384: reseed_st SHA2_384
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384
val reseed_sha2_384: reseed_st SHA2_384 let reseed_sha2_384 =
false
null
false
mk_reseed EverCrypt.HMAC.compute_sha2_384
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_reseed", "Spec.Hash.Definitions.SHA2_384", "EverCrypt.HMAC.compute_sha2_384" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val reseed_sha2_384: reseed_st SHA2_384
[]
EverCrypt.DRBG.reseed_sha2_384
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.reseed_st Spec.Hash.Definitions.SHA2_384
{ "end_col": 63, "end_line": 194, "start_col": 22, "start_line": 194 }
Prims.Tot
val uninstantiate_sha2_384: uninstantiate_st SHA2_384
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let uninstantiate_sha2_384 = mk_uninstantiate SHA2_384
val uninstantiate_sha2_384: uninstantiate_st SHA2_384 let uninstantiate_sha2_384 =
false
null
false
mk_uninstantiate SHA2_384
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_uninstantiate", "Spec.Hash.Definitions.SHA2_384" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512 let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1 let generate_sha2_256 = mk_generate EverCrypt.HMAC.compute_sha2_256 let generate_sha2_384 = mk_generate EverCrypt.HMAC.compute_sha2_384 let generate_sha2_512 = mk_generate EverCrypt.HMAC.compute_sha2_512 /// Uninstantiate function inline_for_extraction noextract val mk_uninstantiate: a:supported_alg -> uninstantiate_st a let mk_uninstantiate a st = let st_s:state_s a = !*st in let s:Hacl.HMAC_DRBG.state a = p st_s in let k:B.buffer uint8 = s.k in let v:B.buffer uint8 = s.v in let ctr:B.buffer size_t = s.reseed_counter in assert (B.disjoint k v /\ B.disjoint k ctr /\ B.disjoint v ctr); assert (B.loc_disjoint (B.loc_addr_of_buffer st) (footprint_s st_s)); Lib.Memzero0.memzero k (hash_len a); Lib.Memzero0.memzero v (hash_len a); ctr.(0ul) <- 0ul; B.free k; B.free v; B.free ctr; B.free st (** @type: true *) val uninstantiate_sha1 : uninstantiate_st SHA1 (** @type: true *) val uninstantiate_sha2_256: uninstantiate_st SHA2_256 (** @type: true *) val uninstantiate_sha2_384: uninstantiate_st SHA2_384 (** @type: true *) val uninstantiate_sha2_512: uninstantiate_st SHA2_512 let uninstantiate_sha1 = mk_uninstantiate SHA1
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val uninstantiate_sha2_384: uninstantiate_st SHA2_384
[]
EverCrypt.DRBG.uninstantiate_sha2_384
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.uninstantiate_st Spec.Hash.Definitions.SHA2_384
{ "end_col": 54, "end_line": 283, "start_col": 29, "start_line": 283 }
Prims.Tot
val generate_sha2_384: generate_st SHA2_384
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let generate_sha2_384 = mk_generate EverCrypt.HMAC.compute_sha2_384
val generate_sha2_384: generate_st SHA2_384 let generate_sha2_384 =
false
null
false
mk_generate EverCrypt.HMAC.compute_sha2_384
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_generate", "Spec.Hash.Definitions.SHA2_384", "EverCrypt.HMAC.compute_sha2_384" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512 let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val generate_sha2_384: generate_st SHA2_384
[]
EverCrypt.DRBG.generate_sha2_384
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.generate_st Spec.Hash.Definitions.SHA2_384
{ "end_col": 67, "end_line": 242, "start_col": 24, "start_line": 242 }
Prims.Tot
val instantiate_sha2_512: instantiate_st SHA2_512
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512
val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha2_512 =
false
null
false
mk_instantiate EverCrypt.HMAC.compute_sha2_512
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_instantiate", "Spec.Hash.Definitions.SHA2_512", "EverCrypt.HMAC.compute_sha2_512" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val instantiate_sha2_512: instantiate_st SHA2_512
[]
EverCrypt.DRBG.instantiate_sha2_512
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.instantiate_st Spec.Hash.Definitions.SHA2_512
{ "end_col": 73, "end_line": 145, "start_col": 27, "start_line": 145 }
Prims.Tot
val reseed_sha2_512: reseed_st SHA2_512
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512
val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha2_512 =
false
null
false
mk_reseed EverCrypt.HMAC.compute_sha2_512
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_reseed", "Spec.Hash.Definitions.SHA2_512", "EverCrypt.HMAC.compute_sha2_512" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val reseed_sha2_512: reseed_st SHA2_512
[]
EverCrypt.DRBG.reseed_sha2_512
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.reseed_st Spec.Hash.Definitions.SHA2_512
{ "end_col": 63, "end_line": 195, "start_col": 22, "start_line": 195 }
Prims.Tot
val uninstantiate_sha2_512: uninstantiate_st SHA2_512
[ { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "Lib.RandomBuffer.System", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HMAC_DRBG", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": true, "full_module": "Spec.HMAC_DRBG", "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": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "short_module": null }, { "abbrev": false, "full_module": "EverCrypt", "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 } ]
false
let uninstantiate_sha2_512 = mk_uninstantiate SHA2_512
val uninstantiate_sha2_512: uninstantiate_st SHA2_512 let uninstantiate_sha2_512 =
false
null
false
mk_uninstantiate SHA2_512
{ "checked_file": "EverCrypt.DRBG.fst.checked", "dependencies": [ "Spec.HMAC_DRBG.fsti.checked", "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.RandomBuffer.System.fsti.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.HMAC_DRBG.fst.checked", "Hacl.HMAC_DRBG.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "EverCrypt.HMAC.fst.checked" ], "interface_file": true, "source_file": "EverCrypt.DRBG.fst" }
[ "total" ]
[ "EverCrypt.DRBG.mk_uninstantiate", "Spec.Hash.Definitions.SHA2_512" ]
[]
module EverCrypt.DRBG open FStar.HyperStack.ST open Lib.IntTypes open Spec.Hash.Definitions module HS = FStar.HyperStack module B = LowStar.Buffer module S = Spec.HMAC_DRBG open Hacl.HMAC_DRBG open Lib.RandomBuffer.System open LowStar.BufferOps friend Hacl.HMAC_DRBG friend EverCrypt.HMAC #set-options "--max_ifuel 0 --max_fuel 0 --z3rlimit 50" /// Some duplication from Hacl.HMAC_DRBG because we don't want clients to depend on it /// /// Respects EverCrypt convention and reverses order of buf_len, buf arguments [@CAbstractStruct] noeq type state_s: supported_alg -> Type0 = | SHA1_s : state SHA1 -> state_s SHA1 | SHA2_256_s: state SHA2_256 -> state_s SHA2_256 | SHA2_384_s: state SHA2_384 -> state_s SHA2_384 | SHA2_512_s: state SHA2_512 -> state_s SHA2_512 let invert_state_s (a:supported_alg): Lemma (requires True) (ensures inversion (state_s a)) [ SMTPat (state_s a) ] = allow_inversion (state_s a) /// Only call this function in extracted code with a known `a` inline_for_extraction noextract let p #a (s:state_s a) : Hacl.HMAC_DRBG.state a = match a with | SHA1 -> let SHA1_s p = s in p | SHA2_256 -> let SHA2_256_s p = s in p | SHA2_384 -> let SHA2_384_s p = s in p | SHA2_512 -> let SHA2_512_s p = s in p let freeable_s #a st = freeable (p st) let footprint_s #a st = footprint (p st) let invariant_s #a st h = invariant (p st) h let repr #a st h = let st = B.get h st 0 in repr (p st) h let loc_includes_union_l_footprint_s #a l1 l2 st = B.loc_includes_union_l l1 l2 (footprint_s st) let invariant_loc_in_footprint #a st m = () let frame_invariant #a l st h0 h1 = () /// State allocation // Would like to specialize alloca in each branch, but two calls to StackInline // functions in the same block lead to variable redefinitions at extraction. let alloca a = let st = match a with | SHA1 -> SHA1_s (alloca a) | SHA2_256 -> SHA2_256_s (alloca a) | SHA2_384 -> SHA2_384_s (alloca a) | SHA2_512 -> SHA2_512_s (alloca a) in B.alloca st 1ul let create_in a r = let st = match a with | SHA1 -> SHA1_s (create_in SHA1 r) | SHA2_256 -> SHA2_256_s (create_in SHA2_256 r) | SHA2_384 -> SHA2_384_s (create_in SHA2_384 r) | SHA2_512 -> SHA2_512_s (create_in SHA2_512 r) in B.malloc r st 1ul let create a = create_in a HS.root /// Instantiate function inline_for_extraction noextract val mk_instantiate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> instantiate_st a let mk_instantiate #a hmac st personalization_string personalization_string_len = if personalization_string_len >. max_personalization_string_length then false else let entropy_input_len = min_length a in let nonce_len = min_length a /. 2ul in let min_entropy = entropy_input_len +! nonce_len in push_frame(); assert_norm (range (v min_entropy) U32); let entropy = B.alloca (u8 0) min_entropy in let ok = randombytes entropy min_entropy in let result = if not ok then false else begin let entropy_input = B.sub entropy 0ul entropy_input_len in let nonce = B.sub entropy entropy_input_len nonce_len in S.hmac_input_bound a; let st_s = !*st in mk_instantiate hmac (p st_s) entropy_input_len entropy_input nonce_len nonce personalization_string_len personalization_string; true end in pop_frame(); result (** @type: true *) val instantiate_sha1 : instantiate_st SHA1 (** @type: true *) val instantiate_sha2_256: instantiate_st SHA2_256 (** @type: true *) val instantiate_sha2_384: instantiate_st SHA2_384 (** @type: true *) val instantiate_sha2_512: instantiate_st SHA2_512 let instantiate_sha1 = mk_instantiate EverCrypt.HMAC.compute_sha1 let instantiate_sha2_256 = mk_instantiate EverCrypt.HMAC.compute_sha2_256 let instantiate_sha2_384 = mk_instantiate EverCrypt.HMAC.compute_sha2_384 let instantiate_sha2_512 = mk_instantiate EverCrypt.HMAC.compute_sha2_512 /// Reseed function inline_for_extraction noextract val mk_reseed: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> reseed_st a let mk_reseed #a hmac st additional_input additional_input_len = if additional_input_len >. max_additional_input_length then false else let entropy_input_len = min_length a in push_frame(); let entropy_input = B.alloca (u8 0) entropy_input_len in let ok = randombytes entropy_input entropy_input_len in let result = if not ok then false else begin S.hmac_input_bound a; let st_s = !*st in mk_reseed hmac (p st_s) entropy_input_len entropy_input additional_input_len additional_input; true end in pop_frame(); result (** @type: true *) val reseed_sha1 : reseed_st SHA1 (** @type: true *) val reseed_sha2_256: reseed_st SHA2_256 (** @type: true *) val reseed_sha2_384: reseed_st SHA2_384 (** @type: true *) val reseed_sha2_512: reseed_st SHA2_512 let reseed_sha1 = mk_reseed EverCrypt.HMAC.compute_sha1 let reseed_sha2_256 = mk_reseed EverCrypt.HMAC.compute_sha2_256 let reseed_sha2_384 = mk_reseed EverCrypt.HMAC.compute_sha2_384 let reseed_sha2_512 = mk_reseed EverCrypt.HMAC.compute_sha2_512 /// Generate function inline_for_extraction noextract val mk_generate: #a:supported_alg -> EverCrypt.HMAC.compute_st a -> generate_st a let mk_generate #a hmac output st n additional_input additional_input_len = if additional_input_len >. max_additional_input_length || n >. max_output_length then false else ( push_frame(); let ok = mk_reseed hmac st additional_input additional_input_len in let result = if not ok then false else begin let st_s = !*st in let b = mk_generate hmac output (p st_s) n additional_input_len additional_input in b (* This used to be true, which is fishy *) end in let h1 = get () in pop_frame(); let h2 = get () in frame_invariant (B.loc_all_regions_from false (HS.get_tip h1)) st h1 h2; result ) (** @type: true *) val generate_sha1 : generate_st SHA1 (** @type: true *) val generate_sha2_256: generate_st SHA2_256 (** @type: true *) val generate_sha2_384: generate_st SHA2_384 (** @type: true *) val generate_sha2_512: generate_st SHA2_512 let generate_sha1 = mk_generate EverCrypt.HMAC.compute_sha1 let generate_sha2_256 = mk_generate EverCrypt.HMAC.compute_sha2_256 let generate_sha2_384 = mk_generate EverCrypt.HMAC.compute_sha2_384 let generate_sha2_512 = mk_generate EverCrypt.HMAC.compute_sha2_512 /// Uninstantiate function inline_for_extraction noextract val mk_uninstantiate: a:supported_alg -> uninstantiate_st a let mk_uninstantiate a st = let st_s:state_s a = !*st in let s:Hacl.HMAC_DRBG.state a = p st_s in let k:B.buffer uint8 = s.k in let v:B.buffer uint8 = s.v in let ctr:B.buffer size_t = s.reseed_counter in assert (B.disjoint k v /\ B.disjoint k ctr /\ B.disjoint v ctr); assert (B.loc_disjoint (B.loc_addr_of_buffer st) (footprint_s st_s)); Lib.Memzero0.memzero k (hash_len a); Lib.Memzero0.memzero v (hash_len a); ctr.(0ul) <- 0ul; B.free k; B.free v; B.free ctr; B.free st (** @type: true *) val uninstantiate_sha1 : uninstantiate_st SHA1 (** @type: true *) val uninstantiate_sha2_256: uninstantiate_st SHA2_256 (** @type: true *) val uninstantiate_sha2_384: uninstantiate_st SHA2_384 (** @type: true *) val uninstantiate_sha2_512: uninstantiate_st SHA2_512 let uninstantiate_sha1 = mk_uninstantiate SHA1 let uninstantiate_sha2_256 = mk_uninstantiate SHA2_256
false
true
EverCrypt.DRBG.fst
{ "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" }
null
val uninstantiate_sha2_512: uninstantiate_st SHA2_512
[]
EverCrypt.DRBG.uninstantiate_sha2_512
{ "file_name": "providers/evercrypt/fst/EverCrypt.DRBG.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
EverCrypt.DRBG.uninstantiate_st Spec.Hash.Definitions.SHA2_512
{ "end_col": 54, "end_line": 284, "start_col": 29, "start_line": 284 }